public static class JlineCompletionHandler implements CompletionHandler {
// TODO: handle quotes and escaped quotes && enable automatic escaping of whitespace
public boolean complete(final ConsoleReader reader, final List<CharSequence> candidatesJson, final int pos) throws IOException {
CursorBuffer buf = reader.getCursorBuffer();
CompletionResult completionResult = fromJson(sequence(candidatesJson).head().toString());
Sequence<String> candidatesToPrint = Sequences.empty(String.class);
// if there is only one completion, then fill in the buffer
if (completionResult.candidates().size() == 1) {
CharSequence value = completionResult.candidates().head().value();
// fail if the only candidate is the same as the current buffer
if (value.equals(buf.toString())) {
return false;
}
setBuffer(reader, value, pos);
candidatesToPrint = completionResult.candidates().flatMap(candidateForms());
} else if (completionResult.candidates().size() > 1) {
String value = getUnambiguousCompletions(completionResult.candidates());
setBuffer(reader, value, pos);
candidatesToPrint = completionResult.candidates().map(candidateValue());
}
printCandidates(reader, candidatesToPrint.safeCast(CharSequence.class).toList());
// redraw the current console buffer