繁体   English   中英

如何在Spring Shell 2中设置用户输入的颜色?

[英]How do I set color of user input in Spring Shell 2?

我有一个类似的问题: Spring Shell 2中的命令颜色,但是我对如何覆盖用户输入的颜色更感兴趣。

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

通过覆盖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块将文本设置为红色。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM