Skip to content

Latest commit

 

History

History
75 lines (64 loc) · 2.31 KB

Re_CommandConsole.md

File metadata and controls

75 lines (64 loc) · 2.31 KB

带指令注册的终端模拟器

  • tab补全
  • 指令历史记录
  • Ctrl+C / Ctrl+V
  • 指令太长自动切换
  • 简易语法高亮 (可以自己换 roj.ui.terminal.DefaultConsole去看)
  • 快捷键监听(F1 - F12)
  • 活动的命令行始终显示在最下方 (效果图中第一个是println的)

效果

CommandConsole example image

代码

import roj.ui.CLIUtil;
import roj.ui.terminal.Argument;
import roj.ui.terminal.CommandConsole;
import roj.ui.terminal.CommandImpl;

import static roj.ui.terminal.CommandNode.argument;
import static roj.ui.terminal.CommandNode.literal;

/**
 * 代码来源roj.mod.FMDMain
 */
public final class Example {
	public static CommandConsole console = new CommandConsole("");

	@SuppressWarnings("fallthrough")
	public static void main(String[] args) throws Exception {
		CommandConsole c = console;

		// 简单的指令
		c.register(literal("reflect").executes(ctx -> ReflectTool.start(!isCLI)));
		c.register(literal("auto").then(argument("auto", Argument.bool()).executes(
			ctx -> AutoCompile.setEnabled(ctx.argument("auto", Boolean.class))
                )));

		// 复杂一点的Argument
		CommandImpl cDeobf = ctx -> {};
		c.register(literal("deobf").executes(cDeobf)
			.then(argument("reverse", Argument.string("mcp2srg", "srg2mcp")).executes(cDeobf)));

		// 变长参数
		c.register(literal("build").then(
			argument("flags", Argument.stringFlags("zl", "showErrorCode", "noupdate")).executes(ctx -> {
				List<String> flags = Helpers.cast(ctx.argument("flags", List.class));
				Map<String, Object> map = new MyHashMap<>();
				for (String flag : flags) map.put(flag, "");
				build(map);
			})));

		// 甚至是自定义参数
		Argument.ArgSetOf<File> dynamicProject = new Argument.ArgSetOf<>(1, new MyHashMap<>()) {
			@Override
			protected void updateChoices() {
				choice.clear();
				IOUtil.findAllFiles(CONFIG_DIR, (f) -> {
					String name = f.getName().toLowerCase();
					if (name.endsWith(".json")) choice.put(name.substring(0, name.length() - 5), f);
					return false;
				});
			}
		};

		// prompt (new的参数也是)
		c.setPrompt("114514 > ");
		// 别忘了
		CLIUtil.setConsole(c);

		// Console线程也是daemon的
		LockSupport.park();
	}
}