Compare commits
5 Commits
g2/master
...
g2/feature
| Author | SHA1 | Date | |
|---|---|---|---|
|
0f4e8c0643
|
|||
|
cf16e9373f
|
|||
|
fba59bb009
|
|||
|
b1efb13b05
|
|||
|
c000b82374
|
16
build.gradle
16
build.gradle
@@ -13,3 +13,19 @@ repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
ext {
|
||||
libs_dir = 'libs'
|
||||
jline_version = '3.14.1'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile group: 'org.jline', name: 'jline', version: jline_version
|
||||
compile group: 'org.jline', name: 'jline-terminal-jansi', version: jline_version
|
||||
}
|
||||
|
||||
task copyDeps(type: Copy) {
|
||||
into project.buildDir.toPath().resolve('libs').resolve(libs_dir)
|
||||
from configurations.runtime
|
||||
}
|
||||
jar.dependsOn(copyDeps)
|
||||
|
||||
@@ -1,8 +1,48 @@
|
||||
package mc.server;
|
||||
|
||||
import mc.server.shell.CommandService;
|
||||
import mc.server.shell.CommandServiceImpl;
|
||||
import mc.server.shell.SystemPropertiesCommand;
|
||||
import org.jline.builtins.CommandRegistry;
|
||||
import org.jline.reader.*;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("hello?");
|
||||
System.out.println("mc-server");
|
||||
|
||||
CommandService commandService = new CommandServiceImpl();
|
||||
commandService.register("system-properties", new SystemPropertiesCommand());
|
||||
|
||||
final Completer completer = CommandRegistry.compileCompleters(commandService);
|
||||
|
||||
final LineReader reader = LineReaderBuilder.builder()
|
||||
.completer(completer)
|
||||
.build();
|
||||
|
||||
try {
|
||||
final CommandRegistry.CommandSession session = new CommandRegistry.CommandSession(reader.getTerminal());
|
||||
final PrintWriter terminalWriter = reader.getTerminal().writer();
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) {
|
||||
final String line = reader.readLine().trim();
|
||||
|
||||
final ParsedLine parsedLine = reader.getParser().parse(line, 0);
|
||||
final String commandName = parsedLine.word();
|
||||
final String[] argv = parsedLine.words().subList(1, parsedLine.words().size()).toArray(new String[0]);
|
||||
|
||||
if (commandService.hasCommand(commandName)) {
|
||||
commandService.execute(session, commandName, argv);
|
||||
}
|
||||
|
||||
terminalWriter.println("line = " + String.join(", ", parsedLine.words()));
|
||||
}
|
||||
} catch (UserInterruptException ignore) {
|
||||
// ignore
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
src/main/java/mc/server/shell/Command.java
Normal file
8
src/main/java/mc/server/shell/Command.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package mc.server.shell;
|
||||
|
||||
import org.jline.builtins.Builtins;
|
||||
|
||||
public interface Command {
|
||||
|
||||
void execute(Builtins.CommandInput commandInput);
|
||||
}
|
||||
8
src/main/java/mc/server/shell/CommandService.java
Normal file
8
src/main/java/mc/server/shell/CommandService.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package mc.server.shell;
|
||||
|
||||
import org.jline.builtins.CommandRegistry;
|
||||
|
||||
public interface CommandService extends CommandRegistry {
|
||||
|
||||
void register(String commandName, Command command);
|
||||
}
|
||||
57
src/main/java/mc/server/shell/CommandServiceImpl.java
Normal file
57
src/main/java/mc/server/shell/CommandServiceImpl.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package mc.server.shell;
|
||||
|
||||
import org.jline.builtins.Builtins;
|
||||
import org.jline.builtins.Builtins.CommandMethods;
|
||||
import org.jline.builtins.Completers.SystemCompleter;
|
||||
import org.jline.reader.impl.completer.NullCompleter;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandServiceImpl implements CommandService {
|
||||
|
||||
private final Map<String, CommandMethods> commands = new HashMap<>();
|
||||
|
||||
public void register(String commandName, Command command) {
|
||||
commands.put(commandName, new CommandMethods(
|
||||
command::execute,
|
||||
cmdName -> Collections.singletonList(NullCompleter.INSTANCE)
|
||||
));
|
||||
}
|
||||
|
||||
public Object execute(CommandSession session, String command) {
|
||||
return execute(session, command, new String[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(CommandSession session, String command, String[] args) {
|
||||
commands.get(command).execute().accept(new Builtins.CommandInput(command, args, session));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> commandNames() {
|
||||
return commands.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> commandAliases() {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCommand(String commandName) {
|
||||
return commands.containsKey(commandName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SystemCompleter compileCompleters() {
|
||||
final SystemCompleter systemCompleter = new SystemCompleter();
|
||||
systemCompleter.addAliases(commandAliases());
|
||||
commands.forEach((commandName, command) -> systemCompleter.add(commandName, NullCompleter.INSTANCE));
|
||||
|
||||
return systemCompleter;
|
||||
}
|
||||
}
|
||||
28
src/main/java/mc/server/shell/SystemPropertiesCommand.java
Normal file
28
src/main/java/mc/server/shell/SystemPropertiesCommand.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package mc.server.shell;
|
||||
|
||||
import org.jline.builtins.Builtins.CommandInput;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SystemPropertiesCommand implements Command {
|
||||
|
||||
@Override
|
||||
public void execute(CommandInput input) {
|
||||
final PrintWriter writer = input.terminal().writer();
|
||||
Stream<Map.Entry<Object, Object>> stream = System.getProperties().entrySet().stream();
|
||||
|
||||
if (input.args().length > 0) {
|
||||
final List<String> argv = Arrays.asList(input.args());
|
||||
stream = stream.filter(entry -> {
|
||||
//noinspection SuspiciousMethodCalls
|
||||
return argv.contains(entry.getKey());
|
||||
});
|
||||
}
|
||||
|
||||
stream.forEach(entry -> writer.printf("%s = %s\n", entry.getKey(), entry.getValue().toString()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user