-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace AnyOsFileValueProvider by ExtendedFileValueProvider and add s…
…upport to not add space while proposing directory
- Loading branch information
Showing
9 changed files
with
170 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
starter/src/main/java/com/github/fonimus/ssh/shell/ExtendedCompleterAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2020 François Onimus | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.fonimus.ssh.shell; | ||
|
||
import org.jline.reader.Candidate; | ||
import org.jline.reader.LineReader; | ||
import org.jline.reader.ParsedLine; | ||
import org.springframework.shell.CompletingParsedLine; | ||
import org.springframework.shell.CompletionContext; | ||
import org.springframework.shell.CompletionProposal; | ||
import org.springframework.shell.Shell; | ||
import org.springframework.shell.jline.JLineShellAutoConfiguration; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Extended completer adapter to be able to set complete attribute of proposal | ||
* <br> | ||
* Based on @{@link JLineShellAutoConfiguration.CompleterAdapter} | ||
*/ | ||
public class ExtendedCompleterAdapter extends JLineShellAutoConfiguration.CompleterAdapter { | ||
|
||
private final Shell shell; | ||
|
||
public ExtendedCompleterAdapter(Shell shell) { | ||
this.shell = shell; | ||
} | ||
|
||
@Override | ||
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) { | ||
CompletingParsedLine cpl = (line instanceof CompletingParsedLine) ? ((CompletingParsedLine) line) : t -> t; | ||
CompletionContext context = new CompletionContext(sanitizeInput(line.words()), line.wordIndex(), | ||
line.wordCursor()); | ||
|
||
shell.complete(context).stream().map(p -> new Candidate( | ||
p.dontQuote() ? p.value() : cpl.emit(p.value()).toString(), | ||
p.displayText(), | ||
p.category(), | ||
p.description(), | ||
null, | ||
null, | ||
isComplete(p) | ||
)).forEach(candidates::add); | ||
} | ||
|
||
private static boolean isComplete(CompletionProposal p) { | ||
if (p instanceof ExtendedCompletionProposal) { | ||
return ((ExtendedCompletionProposal) p).isComplete(); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Sanitize the buffer input given the customizations applied to the JLine parser (<em>e.g.</em> support for | ||
* line continuations, <em>etc.</em>) | ||
* See @{@link org.springframework.shell.jline.JLineShellAutoConfiguration} | ||
*/ | ||
static List<String> sanitizeInput(List<String> words) { | ||
words = words.stream() | ||
// CR at beginning/end of line introduced by backslash | ||
.map(s -> s.replaceAll("^\\n+|\\n+$", "")) | ||
// CR in middle of word introduced by return inside a quoted string | ||
.map(s -> s.replaceAll("\\n+", " ")) | ||
.collect(Collectors.toList()); | ||
return words; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
starter/src/main/java/com/github/fonimus/ssh/shell/ExtendedCompletionProposal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2020 François Onimus | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.fonimus.ssh.shell; | ||
|
||
import lombok.Data; | ||
import org.springframework.shell.CompletionProposal; | ||
|
||
/** | ||
* Extended completion proposal to be able to set complete attribute of proposal | ||
*/ | ||
@Data | ||
public class ExtendedCompletionProposal extends CompletionProposal { | ||
|
||
/** | ||
* If should add space after proposed proposal | ||
*/ | ||
private boolean complete; | ||
|
||
/** | ||
* Default constructor | ||
* | ||
* @param value string value | ||
* @param complete true if should add space after proposed proposal (true is default value when not using | ||
* extended completion proposal) | ||
*/ | ||
public ExtendedCompletionProposal(String value, boolean complete) { | ||
super(value); | ||
this.complete = complete; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters