Class StyleFactory
This class provides methods for creating AttributedString
s with styles
applied to them. It uses a StyleResolver
to resolve style specifications
into AttributedStyle
objects.
The factory supports two main ways of creating styled strings:
- Direct styling with the
style(String, String)
methods - Style expression evaluation with the
evaluate(String)
methods
Example usage:
StyleFactory factory = Styler.factory("mygroup"); // Direct styling AttributedString text1 = factory.style("bold,fg:red", "Important message"); AttributedString text2 = factory.style(".error", "Error message"); // Named style // Style expression evaluation AttributedString text3 = factory.evaluate("Normal text with @{bold,fg:red important} parts");
- Since:
- 3.4
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionStyleFactory
(StyleResolver resolver) Constructs a new StyleFactory with the specified StyleResolver. -
Method Summary
Modifier and TypeMethodDescriptionorg.jline.utils.AttributedString
Evaluates a style expression and returns the result as an AttributedString.org.jline.utils.AttributedString
Evaluates a style expression with formatting and returns the result as an AttributedString.org.jline.utils.AttributedString
Creates a styled string by applying the specified style to the given value.org.jline.utils.AttributedString
Creates a styled string by applying the specified style to a formatted value.
-
Constructor Details
-
StyleFactory
Constructs a new StyleFactory with the specified StyleResolver.This constructor creates a StyleFactory that will use the specified StyleResolver to resolve style specifications when creating styled strings.
Typically, you would use
Styler.factory(String)
to create a StyleFactory rather than constructing one directly.- Parameters:
resolver
- the style resolver to use (must not be null)- Throws:
NullPointerException
- if resolver is null- See Also:
-
-
Method Details
-
style
Creates a styled string by applying the specified style to the given value.This method resolves the style specification using the factory's StyleResolver and applies the resulting AttributedStyle to the value.
The style specification can be in any format supported by
StyleResolver
, including direct style specifications and named style references.Examples:
// Direct style specification AttributedString text1 = factory.style("bold,fg:red", "Important message"); // Named style reference AttributedString text2 = factory.style(".error", "Error message"); // Named style reference with default AttributedString text3 = factory.style(".missing:-bold,fg:blue", "Fallback message");
- Parameters:
style
- the style specification to apply (must not be null)value
- the text value to style (must not be null)- Returns:
- the resulting AttributedString
- Throws:
NullPointerException
- if value is null
-
style
Creates a styled string by applying the specified style to a formatted value.This method is similar to
style(String, String)
, but it allows formatting the value usingString.format(String, Object...)
before applying the style.Example:
AttributedString text = factory.style("bold,fg:red", "Error: %s", "File not found");
- Parameters:
style
- the style specification to apply (must not be null)format
- the format string (must not be null)params
- the parameters to use for formatting (must not be null, but may be empty)- Returns:
- the resulting AttributedString
- Throws:
NullPointerException
- if format or params is null- See Also:
-
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. It uses aStyleExpression
with the factory's StyleResolver to evaluate the expression.Example:
AttributedString text = factory.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- See Also:
-
evaluate
Evaluates a style expression with formatting and returns the result as an AttributedString.This method is similar to
evaluate(String)
, but it allows formatting the expression usingString.format(String, Object...)
before evaluating it.Example:
AttributedString text = factory.evaluate("File: @{bold,fg:blue %s}", "example.txt");
- Parameters:
format
- the format string (must not be null)params
- the parameters to use for formatting (must not be null, but may be empty)- Returns:
- the resulting AttributedString
- Throws:
NullPointerException
- if format or params is null- See Also:
-