Archived
0

команда по выводу значений System.properties

This commit is contained in:
2020-05-05 15:04:34 +03:00
parent c000b82374
commit b1efb13b05
2 changed files with 85 additions and 6 deletions

View File

@@ -1,19 +1,48 @@
package mc.server; package mc.server;
import org.jline.reader.LineReader; import mc.server.shell.CommandStore;
import org.jline.reader.LineReaderBuilder; import org.jline.builtins.Builtins;
import org.jline.reader.UserInterruptException; import org.jline.builtins.CommandRegistry;
import org.jline.reader.*;
import org.jline.reader.impl.completer.NullCompleter;
import java.io.PrintWriter;
import java.util.Collections;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
LineReader reader = LineReaderBuilder.builder().build(); CommandStore commandStore = new CommandStore();
commandStore.register("system-properties", new Builtins.CommandMethods(
commandInput -> {
final PrintWriter writer = commandInput.terminal().writer();
System.getProperties().forEach((key, value) ->
writer.println(String.format("%s = %s", key, value.toString())));
},
commandName -> Collections.singletonList(NullCompleter.INSTANCE)
));
final Completer completer = CommandRegistry.compileCompleters(commandStore);
final LineReader reader = LineReaderBuilder.builder()
.completer(completer)
.build();
try { try {
final CommandRegistry.CommandSession session = new CommandRegistry.CommandSession(reader.getTerminal());
final PrintWriter terminalWriter = reader.getTerminal().writer();
//noinspection InfiniteLoopStatement //noinspection InfiniteLoopStatement
while (true) { while (true) {
String line = reader.readLine(); final String line = reader.readLine().trim();
System.out.println("line = '" + line + "'");
final ParsedLine parsedLine = reader.getParser().parse(line, 0);
final String commandName = parsedLine.word();
if (commandStore.hasCommand(commandName)) {
commandStore.execute(session, commandName);
}
terminalWriter.println("line = " + String.join(", ", parsedLine.words()));
} }
} catch (UserInterruptException ignore) { } catch (UserInterruptException ignore) {
// ignore // ignore

View File

@@ -0,0 +1,50 @@
package mc.server.shell;
import org.jline.builtins.Builtins.CommandInput;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.CommandRegistry;
import org.jline.builtins.Completers.SystemCompleter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CommandStore implements CommandRegistry {
private final Map<String, CommandMethods> commandExecutors = new HashMap<>();
public void register(String name, CommandMethods commandMethods) {
commandExecutors.put(name, commandMethods);
}
public void execute(CommandSession session, String command) {
commandExecutors.get(command).execute().accept(new CommandInput(command, new Object[0], session));
}
@Override
public Set<String> commandNames() {
return commandExecutors.keySet();
}
@Override
public Map<String, String> commandAliases() {
return Collections.emptyMap();
}
@Override
public boolean hasCommand(String command) {
return commandExecutors.containsKey(command);
}
@Override
public SystemCompleter compileCompleters() {
SystemCompleter systemCompleter = new SystemCompleter();
commandExecutors.forEach((name, commandMethods) ->
systemCompleter.add(name, commandMethods.compileCompleter().apply(name)));
systemCompleter.addAliases(commandAliases());
return systemCompleter;
}
}