0

I have a similar question to: Commands color in spring shell 2 but I am more interested in how I can override the color of user input.

When I open my Spring Shell 2 application and start typing a command (before a command is executed), the text input is in red until I type a command that is valid, then the text turns a bold white color. Is there a way for me to override the red text input to be another color?

  • Have you checked where the color field is located? Is it public? If so, can you extend it and override? – Frontear Nov 07 '18 at 17:33
  • I'm having trouble even finding where the field is located. If I knew that, I may be able to figure out a way to change it. I think I am missing something obvious or it is buried in an underlying library. I've been able to figure out how to change the color of everything else that I wanted to change except for this. – Robviously Nov 07 '18 at 17:49
  • a good step is usually to check the documentation. Does it say anything about colors? Additionally, you can try viewing the class from your IDE (if it supports class viewing) by right-click on class name, go to declaration. – Frontear Nov 07 '18 at 17:52
  • The documentation has some good examples of how to set the color for output, which I have taken advantage of in my project. Unfortunately, the docs don't cover changing the behavior of the input, unless I'm missing something. I've tried jumping around in my IDE and see a few classes where it looks to be handling user input, but I can't find anything related to text coloring or styles – Robviously Nov 07 '18 at 18:03
  • Oh, I think I found where it is setting it. I was looking at the same code last night, but must have missed it. The code uses a number -> color map so red isn't explicitly set, maybe that's why I wasn't seeing it. Still need to figure out how to override it, but I think I should be able to handle that. Will update if I find an answer. – Robviously Nov 07 '18 at 18:09

1 Answers1

0

I was able to get the behavior I wanted by overriding the LineReader bean in org.springframework.shell.jline.JLineShellAutoConfiguration

@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;
}

In the 'if (best != null)' block, the if sets the text to bold when a candidate command is matched, where the else block sets the text to red.