자동 완성 기능이있는 간단한 쉘을 작성하려고합니다. 나는 JLine
라이브러리를 사용합니다. 여기 내 코드가있다.JLine을 사용한 Java 콘솔 자동 완성
public class ConsoleDemo {
public static void main(String[] args) {
try {
ConsoleReader console = new ConsoleReader();
console.setPrompt(">>> ");
console.addCompleter(new MyStringsCompleter("a", "aaa", "b", "bbb"));
String line;
while ((line = console.readLine()) != null) {
console.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
문제는 tab
을 누르면 내 앱이 아무 것도 완료하지 못하는 것입니다.
>>> a [press tab]
어떻게 바로 자동 완성 내 입력 얻을하는 데 사용할 수 있습니다?
UPD StringsCompleter
에서
public class MyStringsCompleter implements Completer {
private final SortedSet<String> strings = new TreeSet<>();
public MyStringsCompleter(Collection<String> strings) {
this.strings.addAll(strings);
}
public MyStringsCompleter(String... strings) {
this(asList(strings));
}
@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
if (buffer == null) {
candidates.addAll(strings);
} else {
for (String match : strings.tailSet(buffer)) {
if (!match.startsWith(buffer)) {
break;
}
candidates.add(match);
}
}
if (candidates.size() == 1) {
candidates.set(0, candidates.get(0) + " ");
}
return candidates.isEmpty() ? -1 : 0;
}
}
도움이되지 않습니다. 나는 내 자신의 Completer를 구현했다. – Alex
질문을 업데이트 할 수 있습니까? – HavelTheGreat
예. 나는 그 질문을 갱신했다. – Alex