我有一个类似的问题: Commands color in spring shell 2但我对如何覆盖用户输入的颜色更感兴趣。
当我打开 Spring Shell 2 应用程序并开始输入命令(在执行命令之前)时,文本输入为红色,直到我输入有效的命令,然后文本变为粗体白色。有没有办法让我将红色文本输入覆盖为另一种颜色?
我有一个类似的问题: Commands color in 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 块将文本设置为红色。