Class StyleFactory


  • public class StyleFactory
    extends java.lang.Object
    Factory for creating styled strings using a specific style group.

    This class provides methods for creating AttributedStrings 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:

    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)
    • 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
      org.jline.utils.AttributedString evaluate​(java.lang.String expression)
      Evaluates a style expression and returns the result as an AttributedString.
      org.jline.utils.AttributedString evaluate​(java.lang.String format, java.lang.Object... params)
      Evaluates a style expression with formatting and returns the result as an AttributedString.
      org.jline.utils.AttributedString style​(java.lang.String style, java.lang.String value)
      Creates a styled string by applying the specified style to the given value.
      org.jline.utils.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 org.jline.utils.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 org.jline.utils.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 using String.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 org.jline.utils.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 a StyleExpression 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 org.jline.utils.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 using String.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...)