Skip to content

Highlighting and parsing

mattirn edited this page Jan 25, 2022 · 11 revisions

The LineReader can be given an implementation of the Highlighter and Parser interfaces which are responsible for highlighting and parsing of the command line.

Builtin highlighters

DefaultHighlighter

DefaultHighlighter is JLine provided implementation of Highlighter interface.

SystemHighlighter

SystemHighlighter extends DefaultHighlighter. SystemHighlighter highlights command and language syntax using nano SyntaxHighlighter.

Nano SyntaxHighlighter

SyntaxHighlighter is a JLine implementation of nanorc syntax highlighter that works with standard nanorc syntaxfiles.

JLine supported keywords: the syntax, color, and icolor are used to define syntax highlighting rules for different text patterns. Other nanorc keywords are quietly ignored.

JLine extension

JLine has extended the commands color and icolor first parameter syntax to support 255 colors, true color and additional ansi styles:

# nanorc syntax
color [bold,][italic,]fgcolor,bgcolor ...

# jline syntax 
color style1[,style2]...[,stylen]  ...

where style<i> define either foreground color, background color or additional text styles.

Valid text styles names are blink, bold, conceal, crossed-out, faint, hidden, inverse, inverse-neg, italic and underline.

Color styles can be defined using:

  1. standard color names white, black, blue, green, red, cyan, yellow, magenta
  2. standard color name with prefix: bright, light or !
  3. 255 color name: ~<name>
  4. 255 color number
  5. 24-bit color: #<hexcode>
  6. default color: normal, default
  7. empty string: does not redefine color

The first color style in a list will define the foreground color and subsequent color styles the background color. More than two colors in a list can be treated as configuration error even though it is silently accepted.

Note

SyntaxHighlighter does not implement Highlighter interface but it can be useful for implementing one or it can be just used to highlight command output and/or descriptions.

Usage

ConfigurationPath configPath = new ConfigurationPath(Paths.get("/pub/myApp"),   // application-wide settings
                       Paths.get(System.getProperty("user.home"), ".myApp"));   // user-specific settings
SyntaxHighlighter javaSyntax = SyntaxHighlighter.build(configPath.getConfig("jnanorc"), "Java");
javaSyntax.highlight("public AttributedString highlight(String string)").println(terminal);