[FEATURE] Автокомплит

This commit is contained in:
Gravit 2019-05-11 16:50:17 +07:00
parent 179733a987
commit 31ac1895af
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 35 additions and 3 deletions

View file

@ -7,6 +7,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
public abstract class CommandHandler implements Runnable { public abstract class CommandHandler implements Runnable {
private final List<Category> categories = new ArrayList<>(); private final List<Category> categories = new ArrayList<>();
@ -130,6 +131,21 @@ public void run() {
LogHelper.error(e); LogHelper.error(e);
} }
} }
@FunctionalInterface
public interface CommandWalk
{
void walk(Category category, String name, Command command);
}
public void walk(CommandWalk callback)
{
for(CommandHandler.Category category : getCategories())
{
for (Map.Entry<String, Command> entry : category.category.commandsMap().entrySet())
callback.walk(category, entry.getKey(), entry.getValue());
}
for (Map.Entry<String, Command> entry : getBaseCategory().commandsMap().entrySet())
callback.walk(null, entry.getKey(), entry.getValue());
}
public CommandCategory getBaseCategory() { public CommandCategory getBaseCategory() {
return baseCategory; return baseCategory;

View file

@ -1,7 +1,6 @@
package ru.gravit.utils.command; package ru.gravit.utils.command;
import org.jline.reader.LineReader; import org.jline.reader.*;
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.Terminal; import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder; import org.jline.terminal.TerminalBuilder;
import org.jline.utils.InfoCmp; import org.jline.utils.InfoCmp;
@ -9,6 +8,7 @@
import ru.gravit.utils.helper.LogHelper.Output; import ru.gravit.utils.helper.LogHelper.Output;
import java.io.IOException; import java.io.IOException;
import java.util.List;
public class JLineCommandHandler extends CommandHandler { public class JLineCommandHandler extends CommandHandler {
/*private final class JLineOutput implements Output { /*private final class JLineOutput implements Output {
@ -26,14 +26,30 @@ public void println(String message) {
private final Terminal terminal; private final Terminal terminal;
private final TerminalBuilder terminalBuilder; private final TerminalBuilder terminalBuilder;
private final Completer completer;
private final LineReader reader; private final LineReader reader;
public class JLineConsoleCompleter implements Completer {
@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
String completeWord = line.word();
if(line.wordIndex() != 0) return;
walk((category, name, command) -> {
if(name.startsWith(completeWord))
{
candidates.add(new Candidate(name));
}
});
}
}
public JLineCommandHandler() throws IOException { public JLineCommandHandler() throws IOException {
super(); super();
terminalBuilder = TerminalBuilder.builder(); terminalBuilder = TerminalBuilder.builder();
terminal = terminalBuilder.build(); terminal = terminalBuilder.build();
completer = new JLineConsoleCompleter();
reader = LineReaderBuilder.builder() reader = LineReaderBuilder.builder()
.terminal(terminal) .terminal(terminal)
.completer(completer)
.build(); .build();
// Set reader // Set reader
@ -47,7 +63,7 @@ public JLineCommandHandler() throws IOException {
@Override @Override
public void bell() throws IOException { public void bell() throws IOException {
terminal.puts(InfoCmp.Capability.bell);
//reader.beep(); //reader.beep();
} }