Skip to content

Commit

Permalink
Extend StringsCompleter to accept a lambda for the strings, fixes #464
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 10, 2019
1 parent 4e97478 commit a890660
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
38 changes: 37 additions & 1 deletion builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.jline.builtins.Commands;
import org.jline.builtins.Completers;
Expand All @@ -37,6 +38,7 @@
import org.jline.reader.impl.DefaultParser;
import org.jline.reader.impl.DefaultParser.Bracket;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.reader.impl.completer.AggregateCompleter;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.terminal.Cursor;
Expand All @@ -63,7 +65,8 @@ public static void usage() {
, " -system terminalBuilder.system(false)"
, " +system terminalBuilder.system(true)"
, " Completors:"
, " argumet an argument completor & autosuggestion"
, " aggregate an aggregate completor with strings Supplier"
, " argument an argument completor & autosuggestion"
, " files a completor that completes file names"
, " none no completors"
, " param a paramenter completer using Java functional interface"
Expand Down Expand Up @@ -120,6 +123,27 @@ public static void help() {

}

private static class ReaderOptions {
LineReader reader;

public ReaderOptions() {
}

public void setReader(LineReader reader) {
this.reader = reader;
}

List<String> unsetted(boolean set) {
List<String> out = new ArrayList<>();
for (Option option : Option.values()) {
if (set == (reader.isSet(option) == option.isDef())) {
out.add((option.isDef() ? "no-" : "") + option.toString().toLowerCase().replace('_', '-'));
}
}
return out;
}
}

public static void main(String[] args) throws IOException {
try {
String prompt = "prompt> ";
Expand All @@ -141,6 +165,7 @@ public static void main(String[] args) throws IOException {
Completer completer = null;
Parser parser = null;
List<Consumer<LineReader>> callbacks = new ArrayList<>();
ReaderOptions readerOptions = new ReaderOptions();

for (int index=0; index < args.length; index++) {
switch (args[index]) {
Expand Down Expand Up @@ -205,6 +230,16 @@ public static void main(String[] args) throws IOException {
}).start();
});
break;
case "aggregate":
List<Completer> ccc = new ArrayList<>();
ccc.add(new ArgumentCompleter(
new StringsCompleter("setopt"),
new StringsCompleter(() -> readerOptions.unsetted(true))));
ccc.add(new ArgumentCompleter(
new StringsCompleter("unsetopt"),
new StringsCompleter(() -> readerOptions.unsetted(false))));
completer = new AggregateCompleter(ccc);
break;
case "argument":
completer = new ArgumentCompleter(
new Completer() {
Expand Down Expand Up @@ -319,6 +354,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
.variable(LineReader.INDENTATION, 2)
.option(Option.INSERT_BRACKET, true)
.build();
readerOptions.setReader(reader);
AutopairWidgets autopairWidgets = new AutopairWidgets(reader);
AutosuggestionWidgets autosuggestionWidgets = new AutosuggestionWidgets(reader);
Map<String, CmdDesc> tailTips = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2019, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;

import org.jline.reader.Candidate;
import org.jline.reader.Completer;
Expand All @@ -27,11 +28,18 @@
*/
public class StringsCompleter implements Completer
{
protected final Collection<Candidate> candidates = new ArrayList<>();
protected Collection<Candidate> candidates = new ArrayList<>();
protected Supplier<List<String>> stringsSupplier;

public StringsCompleter() {
}

public StringsCompleter(Supplier<List<String>> stringsSupplier) {
assert stringsSupplier != null;
candidates = null;
this.stringsSupplier = stringsSupplier;
}

public StringsCompleter(String... strings) {
this(Arrays.asList(strings));
}
Expand All @@ -44,8 +52,7 @@ public StringsCompleter(Iterable<String> strings) {
}

public StringsCompleter(Candidate ... candidates) {
assert candidates != null;
this.candidates.addAll(Arrays.asList(candidates));
this(Arrays.asList(candidates));
}

public StringsCompleter(Collection<Candidate> candidates) {
Expand All @@ -56,7 +63,13 @@ public StringsCompleter(Collection<Candidate> candidates) {
public void complete(LineReader reader, final ParsedLine commandLine, final List<Candidate> candidates) {
assert commandLine != null;
assert candidates != null;
candidates.addAll(this.candidates);
if (this.candidates != null) {
candidates.addAll(this.candidates);
} else {
for (String string : stringsSupplier.get()) {
candidates.add(new Candidate(AttributedString.stripAnsi(string), string, null, null, null, null, true));
}
}
}

}

0 comments on commit a890660

Please sign in to comment.