Class StyleFactory
- java.lang.Object
-
- org.jline.style.StyleFactory
-
public class StyleFactory extends java.lang.Object
Factory for creating styled strings using a specific style group.This class provides methods for creating
AttributedString
s with styles applied to them. It uses aStyleResolver
to resolve style specifications intoAttributedStyle
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:
StyleResolver
,StyleExpression
,Styler.factory(String)
- Direct styling with the
-
-
Constructor Summary
Constructors Constructor Description StyleFactory(StyleResolver resolver)
Constructs a new StyleFactory with the specified StyleResolver.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AttributedString
evaluate(java.lang.String expression)
Evaluates a style expression and returns the result as an AttributedString.AttributedString
evaluate(java.lang.String format, java.lang.Object... params)
Evaluates a style expression with formatting and returns the result as an AttributedString.AttributedString
style(java.lang.String style, java.lang.String value)
Creates a styled string by applying the specified style to the given value.AttributedString
style(java.lang.String style, java.lang.String format, java.lang.Object... params)
Creates a styled string by applying the specified style to a formatted value.
-
-
-
Constructor Detail
-
StyleFactory
public StyleFactory(StyleResolver resolver)
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:
java.lang.NullPointerException
- if resolver is null- See Also:
Styler.factory(String)
-
-
Method Detail
-
style
public AttributedString style(java.lang.String style, java.lang.String value)
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:
java.lang.NullPointerException
- if value is null
-
style
public AttributedString style(java.lang.String style, java.lang.String format, java.lang.Object... params)
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:
java.lang.NullPointerException
- if format or params is null- See Also:
style(String, String)
,String.format(String, Object...)
-
evaluate
public AttributedString evaluate(java.lang.String expression)
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:
java.lang.NullPointerException
- if expression is null- See Also:
StyleExpression.evaluate(String)
-
evaluate
public AttributedString evaluate(java.lang.String format, java.lang.Object... params)
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:
java.lang.NullPointerException
- if format or params is null- See Also:
evaluate(String)
,String.format(String, Object...)
-
-