Add limited support for completions, add .editorconfig and reformat.
This commit is contained in:
parent
10aba61110
commit
762bdb7ff4
22 changed files with 676 additions and 203 deletions
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>Commands</artifactId>
|
||||
|
|
|
|||
|
|
@ -27,12 +27,17 @@ import com.intellectualsites.commands.Command;
|
|||
import com.intellectualsites.commands.CommandManager;
|
||||
import com.intellectualsites.commands.CommandTree;
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.intellectualsites.commands.components.StaticComponent;
|
||||
import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
|
||||
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
||||
import com.intellectualsites.commands.parser.ComponentParseResult;
|
||||
import org.jline.reader.*;
|
||||
import org.jline.reader.Candidate;
|
||||
import org.jline.reader.Completer;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
import org.jline.reader.ParsedLine;
|
||||
import org.jline.terminal.Terminal;
|
||||
import org.jline.terminal.TerminalBuilder;
|
||||
|
||||
|
|
@ -45,34 +50,63 @@ import java.util.function.Function;
|
|||
*/
|
||||
public class JLineCommandManager extends CommandManager<JLineCommandSender> implements Completer {
|
||||
|
||||
public JLineCommandManager(@Nonnull
|
||||
final Function<CommandTree<JLineCommandSender>, CommandExecutionCoordinator<JLineCommandSender>> executionCoordinatorFunction) {
|
||||
super(executionCoordinatorFunction, CommandRegistrationHandler.NULL_COMMAND_REGISTRATION_HANDLER);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// TODO: REMOVE THIS!!!!
|
||||
final JLineCommandManager jLineCommandManager = new JLineCommandManager(CommandExecutionCoordinator.simpleCoordinator());
|
||||
final Terminal terminal = TerminalBuilder.builder().dumb(true).build();
|
||||
final Terminal terminal = TerminalBuilder.builder().build();
|
||||
LineReader lineReader = LineReaderBuilder.builder()
|
||||
.completer(jLineCommandManager).terminal(terminal).appName("Test").build();
|
||||
boolean[] shouldStop = new boolean[] { false };
|
||||
.completer(jLineCommandManager)
|
||||
.option(LineReader.Option.INSERT_TAB, false)
|
||||
.terminal(terminal)
|
||||
.appName("Test")
|
||||
.build();
|
||||
boolean[] shouldStop = new boolean[]{false};
|
||||
jLineCommandManager.registerCommand(Command.newBuilder("stop").withHandler(commandContext ->
|
||||
shouldStop[0] = true).build()).registerCommand(Command.newBuilder("echo")
|
||||
.withComponent(CommandComponent.ofType(String.class).named("string").asRequired()
|
||||
.withParser(((commandContext, inputQueue) -> {
|
||||
final StringBuilder stringBuilder = new StringBuilder();
|
||||
while (!inputQueue.isEmpty()) {
|
||||
stringBuilder.append(inputQueue.remove());
|
||||
if (!inputQueue.isEmpty()) {
|
||||
stringBuilder.append(" ");
|
||||
}
|
||||
}
|
||||
return ComponentParseResult.success(stringBuilder.toString());
|
||||
})).build())
|
||||
.withHandler(commandContext -> commandContext.get("string").ifPresent(System.out::println)).build());
|
||||
shouldStop[0] = true).build())
|
||||
.registerCommand(Command.newBuilder("echo")
|
||||
.withComponent(
|
||||
CommandComponent.ofType(String.class).named("string").asRequired()
|
||||
.withParser(((commandContext, inputQueue) -> {
|
||||
final StringBuilder stringBuilder = new StringBuilder();
|
||||
while (!inputQueue.isEmpty()) {
|
||||
stringBuilder.append(inputQueue.remove());
|
||||
if (!inputQueue.isEmpty()) {
|
||||
stringBuilder.append(" ");
|
||||
}
|
||||
}
|
||||
return ComponentParseResult.success(
|
||||
stringBuilder.toString());
|
||||
})).build())
|
||||
.withHandler(commandContext -> commandContext.get("string")
|
||||
.ifPresent(System.out::println))
|
||||
.build())
|
||||
.registerCommand(Command.newBuilder("test")
|
||||
.withComponent(StaticComponent.required("one"))
|
||||
.withHandler(commandContext -> System.out.println("Test (1)"))
|
||||
.build())
|
||||
.registerCommand(Command.newBuilder("test")
|
||||
.withComponent(StaticComponent.required("two"))
|
||||
.withHandler(commandContext -> System.out.println("Test (2)"))
|
||||
.build());
|
||||
System.out.println("Ready...");
|
||||
while (!shouldStop[0]) {
|
||||
final String line = lineReader.readLine();
|
||||
if (line == null || line.isEmpty() || !line.startsWith("/")) {
|
||||
System.out.println("Empty line");
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
jLineCommandManager.executeCommand(new JLineCommandSender(), line.substring(1)).join();
|
||||
final List<String> suggestions = jLineCommandManager.suggest(new JLineCommandSender(), line.substring(1));
|
||||
for (final String suggestion : suggestions) {
|
||||
System.out.printf("> %s\n", suggestion);
|
||||
}
|
||||
// jLineCommandManager.executeCommand(new JLineCommandSender(), line.substring(1)).join();
|
||||
// System.out.println("Successfully executed " + line);
|
||||
} catch (RuntimeException runtimeException) {
|
||||
if (runtimeException.getCause() instanceof NoSuchCommandException) {
|
||||
System.out.println("No such command");
|
||||
|
|
@ -89,13 +123,16 @@ public class JLineCommandManager extends CommandManager<JLineCommandSender> impl
|
|||
}
|
||||
}
|
||||
|
||||
public JLineCommandManager(@Nonnull final Function<CommandTree<JLineCommandSender>, CommandExecutionCoordinator<JLineCommandSender>> executionCoordinatorFunction) {
|
||||
super(executionCoordinatorFunction, CommandRegistrationHandler.NULL_COMMAND_REGISTRATION_HANDLER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete(@Nonnull final LineReader lineReader, @Nonnull final ParsedLine parsedLine, @Nonnull final List<Candidate> list) {
|
||||
// TODO: Implement
|
||||
public void complete(@Nonnull final LineReader lineReader,
|
||||
@Nonnull final ParsedLine parsedLine,
|
||||
@Nonnull final List<Candidate> list) {
|
||||
final String line = parsedLine.line();
|
||||
if (line == null || line.isEmpty() || !line.startsWith("/")) {
|
||||
System.out.println("Cannot suggest: empty line");
|
||||
return;
|
||||
}
|
||||
System.out.printf("Trying to complete '%s'\n", line);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue