【问题标题】:How do I set color of user input in Spring Shell 2?如何在 Spring Shell 2 中设置用户输入的颜色?
【发布时间】:2018-11-07 17:30:33
【问题描述】:

我有一个类似的问题: Commands color in spring shell 2 但我更感兴趣的是如何覆盖用户输入的颜色。

当我打开我的 Spring Shell 2 应用程序并开始输入命令(在执行命令之前)时,文本输入为红色,直到我输入有效的命令,然后文本变为粗体白色。有没有办法让我将红色文本输入覆盖为另一种颜色?

【问题讨论】:

  • 你检查过色域的位置吗?是公开的吗?如果是这样,您可以扩展它并覆盖它吗?
  • 我什至无法找到该字段的位置。如果我知道这一点,我可能会想办法改变它。我想我遗漏了一些明显的东西,或者它被埋在了底层库中。我已经能够弄清楚如何更改除此之外我想更改的所有其他内容的颜色。
  • 一个好的步骤通常是检查文档。它有关于颜色的任何信息吗?此外,您可以尝试从 IDE 中查看类(如果它支持查看类),方法是右键单击类名,转到声明。
  • 文档中有一些很好的例子来说明如何设置输出颜色,我在我的项目中利用了这些例子。不幸的是,除非我遗漏了一些东西,否则文档不包括更改输入的行为。我尝试在我的 IDE 中四处跳动,看到一些看起来正在处理用户输入的类,但我找不到与文本颜色或样式相关的任何内容
  • 哦,我想我找到了它的设置位置。昨晚我正在查看相同的代码,但一定错过了。该代码使用数字-> 颜色映射,因此没有明确设置红色,也许这就是我没有看到它的原因。仍然需要弄清楚如何覆盖它,但我认为我应该能够处理它。如果我找到答案会更新。

标签: java command-line-interface spring-shell


【解决方案1】:

我能够通过覆盖 org.springframework.shell.jline.JLineShellAutoConfiguration 中的 LineReader bean 来获得我想要的行为

@Bean
public LineReader lineReader() {
    LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder().terminal(this.terminal()).appName("Spring Shell").completer(this.completer()).history(this.history).highlighter(new Highlighter() {
        public AttributedString highlight(LineReader reader, String buffer) {
            int l = 0;
            String best = null;
            Iterator var5 = JLineShellAutoConfiguration.this.shell.listCommands().keySet().iterator();

            while(var5.hasNext()) {
                String command = (String)var5.next();
                if (buffer.startsWith(command) && command.length() > l) {
                    l = command.length();
                    best = command;
                }
            }

            if (best != null) {
                return (new AttributedStringBuilder(buffer.length())).append(best, AttributedStyle.BOLD).append(buffer.substring(l)).toAttributedString();
            } else {
                return new AttributedString(buffer, AttributedStyle.DEFAULT.foreground(1));
            }
        }
    }).parser(this.parser());
    LineReader lineReader = lineReaderBuilder.build();
    lineReader.unsetOpt(Option.INSERT_TAB);
    return lineReader;
}

在 'if (best != null)' 块中,if 在匹配候选命令时将文本设置为粗体,而 else 块将文本设置为红色。

【讨论】:

    猜你喜欢
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2018-09-05
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多