mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-01-22 23:34:25 +03:00
[FEATURE] Автокомплит
This commit is contained in:
parent
179733a987
commit
31ac1895af
2 changed files with 35 additions and 3 deletions
|
@ -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;
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue