Class StyleExpression


  • public class StyleExpression
    extends java.lang.Object
    Provides evaluation of style expressions in the format @{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:
    StyleResolver, InterpolationHelper
    • Constructor Summary

      Constructors 
      Constructor Description
      StyleExpression()
      Constructs a new StyleExpression with a default StyleResolver.
      StyleExpression​(StyleResolver resolver)
      Constructs a new StyleExpression 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.
      void evaluate​(org.jline.utils.AttributedStringBuilder buff, java.lang.String expression)
      Evaluates a style expression and appends the result to the specified buffer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • 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

        public StyleExpression​(StyleResolver resolver)
        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:
        java.lang.NullPointerException - if resolver is null
    • Method Detail

      • evaluate

        public void evaluate​(org.jline.utils.AttributedStringBuilder buff,
                             java.lang.String expression)
        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:
        java.lang.NullPointerException - if buff or expression is null
      • 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.

        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:
        java.lang.NullPointerException - if expression is null