Class CommandHighlighter
- All Implemented Interfaces:
Highlighter
Highlights the first word of the command line based on whether it is a known command in the dispatcher:
- Known commands are highlighted in bold
- Unknown commands are highlighted in red
- Pipeline operators (
|,&&,||,;, etc.) are highlighted in cyan
An optional delegate Highlighter can be provided for composability.
If provided, the delegate is called first, then command highlighting is applied
on top.
- Since:
- 4.0
-
Constructor Summary
ConstructorsConstructorDescriptionCommandHighlighter(CommandDispatcher dispatcher) Creates a command highlighter with no delegate.CommandHighlighter(CommandDispatcher dispatcher, Highlighter delegate) Creates a command highlighter with an optional delegate. -
Method Summary
Modifier and TypeMethodDescriptionhighlight(LineReader reader, String buffer) Highlights the provided text buffer with appropriate styling.voidsetErrorIndex(int errorIndex) Sets a specific character position in the buffer to be highlighted as an error.voidsetErrorPattern(Pattern errorPattern) Sets a regular expression pattern that identifies errors to be highlighted.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jline.reader.Highlighter
refresh
-
Constructor Details
-
CommandHighlighter
Creates a command highlighter with no delegate.- Parameters:
dispatcher- the command dispatcher for command lookup
-
CommandHighlighter
Creates a command highlighter with an optional delegate.When a delegate is provided, it is used to highlight the buffer first, and then command-specific highlighting is applied on top.
- Parameters:
dispatcher- the command dispatcher for command lookupdelegate- the delegate highlighter, or null
-
-
Method Details
-
highlight
Description copied from interface:HighlighterHighlights the provided text buffer with appropriate styling.This method is called by the LineReader to apply syntax highlighting to the current input line. It should analyze the buffer content and return an AttributedString with appropriate styling applied based on the content's syntax, structure, or other relevant characteristics.
- Specified by:
highlightin interfaceHighlighter- Parameters:
reader- The LineReader instance requesting highlightingbuffer- The text buffer to be highlighted- Returns:
- An AttributedString containing the highlighted buffer with styling applied
-
setErrorPattern
Description copied from interface:HighlighterSets a regular expression pattern that identifies errors to be highlighted.Text matching this pattern will typically be highlighted with error styling (often red or with a distinctive background color) to indicate problematic input.
- Specified by:
setErrorPatternin interfaceHighlighter- Parameters:
errorPattern- A regular expression pattern that matches text to be highlighted as errors
-
setErrorIndex
public void setErrorIndex(int errorIndex) Description copied from interface:HighlighterSets a specific character position in the buffer to be highlighted as an error.This is typically used to indicate the exact position of a syntax error or other issue in the input line. The highlighter will apply error styling at this position.
- Specified by:
setErrorIndexin interfaceHighlighter- Parameters:
errorIndex- The character index in the buffer to be highlighted as an error
-