Class StyleExpression
@{style value}
.
This class allows embedding styled text within regular strings using a special
syntax. Style expressions are enclosed in @{...}
delimiters, where the
first part specifies the style and the rest is the text to be styled.
Style expressions can be nested and combined with regular text. The style
specification can be a direct style specification or a reference to a named
style in a StyleSource
.
Examples of style expressions:
"Normal text with @{bold,fg:red important} parts" "@{bold Header}: @{fg:blue Value}" "@{.error Error message}" (references a named style "error")
- Since:
- 3.4
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new StyleExpression with a default StyleResolver.StyleExpression
(StyleResolver resolver) Constructs a new StyleExpression with the specified StyleResolver. -
Method Summary
Modifier and TypeMethodDescriptionorg.jline.utils.AttributedString
Evaluates a style expression and returns the result as an AttributedString.void
Evaluates a style expression and appends the result to the specified buffer.
-
Constructor Details
-
StyleExpression
public StyleExpression()Constructs a new StyleExpression with a default StyleResolver.This constructor creates a StyleExpression with a StyleResolver that uses a
NopStyleSource
and an empty group name. This means that only direct style specifications will work; named style references will always resolve to null. -
StyleExpression
Constructs a new StyleExpression with the specified StyleResolver.This constructor creates a StyleExpression that will use the specified StyleResolver to resolve style specifications within expressions.
- Parameters:
resolver
- the style resolver to use (must not be null)- Throws:
NullPointerException
- if resolver is null
-
-
Method Details
-
evaluate
Evaluates a style expression and appends the result to the specified buffer.This method processes the given expression, resolving any style expressions in the format
@{style value}
, and appends the resulting styled text to the provided buffer.Example:
AttributedStringBuilder buffer = new AttributedStringBuilder(); StyleExpression expr = new StyleExpression(resolver); expr.evaluate(buffer, "Normal text with @{bold,fg:red important} parts");
- Parameters:
buff
- the buffer to append the evaluated expression to (must not be null)expression
- the expression to evaluate (must not be null)- Throws:
NullPointerException
- if buff or expression is null
-
evaluate
Evaluates a style expression and returns the result as an AttributedString.This method processes the given expression, resolving any style expressions in the format
@{style value}
, and returns the resulting styled text as an AttributedString.Example:
StyleExpression expr = new StyleExpression(resolver); AttributedString result = expr.evaluate("Normal text with @{bold,fg:red important} parts");
- Parameters:
expression
- the expression to evaluate (must not be null)- Returns:
- the resulting AttributedString
- Throws:
NullPointerException
- if expression is null
-