Class Styler

java.lang.Object
org.jline.style.Styler

public class Styler extends Object
Style facade that provides static utility methods for working with styles.

The Styler class is the main entry point for the JLine style system. It provides access to the global StyleSource and factory methods for creating StyleResolvers, StyleFactorys, and StyleBundle proxies.

Typical usage patterns include:

 // Configure a global style source
 Styler.setSource(new MemoryStyleSource());

 // Create a style resolver for a specific group
 StyleResolver resolver = Styler.resolver("mygroup");

 // Create a style factory for a specific group
 StyleFactory factory = Styler.factory("mygroup");

 // Create a style bundle proxy
 MyStyles styles = Styler.bundle(MyStyles.class);
 
Since:
3.4
See Also:
  • Method Details

    • getSource

      public static StyleSource getSource()
      Returns the global StyleSource used by the Styler.

      The global style source is used by all style operations that don't explicitly specify a different source. By default, a NopStyleSource is used, which doesn't provide any styles.

      Returns:
      the global style source
      See Also:
    • setSource

      public static void setSource(StyleSource source)
      Installs a new global StyleSource.

      This method sets the global style source used by all style operations that don't explicitly specify a different source. A common implementation to use is MemoryStyleSource.

      Example:

       // Create and configure a style source
       MemoryStyleSource source = new MemoryStyleSource();
       source.set("mygroup", "error", "bold,fg:red");
       source.set("mygroup", "warning", "bold,fg:yellow");
      
       // Set it as the global source
       Styler.setSource(source);
       
      Parameters:
      source - the new global style source (must not be null)
      Throws:
      NullPointerException - if source is null
      See Also:
    • resolver

      public static StyleResolver resolver(String group)
      Creates a StyleResolver for the given style group.

      The style resolver is used to resolve style specifications into AttributedStyle objects. It uses the global style source to look up named styles within the specified group.

      Example:

       StyleResolver resolver = Styler.resolver("mygroup");
       AttributedStyle style = resolver.resolve("bold,fg:red");
       AttributedStyle namedStyle = resolver.resolve(".error"); // Looks up "error" in "mygroup"
       
      Parameters:
      group - the style group name (must not be null)
      Returns:
      a new style resolver for the specified group
      Throws:
      NullPointerException - if group is null
    • factory

      public static StyleFactory factory(String group)
      Creates a StyleFactory for the given style group.

      The style factory provides methods for creating styled strings using style specifications or expressions. It uses a StyleResolver for the specified group to resolve styles.

      Example:

       StyleFactory factory = Styler.factory("mygroup");
       AttributedString text = factory.style("bold,fg:red", "Important message");
       AttributedString namedText = factory.style(".error", "Error message");
       AttributedString expr = factory.evaluate("Normal text with @{bold,fg:red important} parts");
       
      Parameters:
      group - the style group name (must not be null)
      Returns:
      a new style factory for the specified group
      Throws:
      NullPointerException - if group is null
    • bundle

      public static <T extends StyleBundle> T bundle(Class<T> type)
      Creates a StyleBundle proxy for the specified interface.

      This method creates a dynamic proxy that implements the specified interface. Each method in the interface is expected to return an AttributedString and take a single parameter that will be styled according to the method's StyleBundle.DefaultStyle annotation or a style definition from the global style source.

      The target interface must be annotated with StyleBundle.StyleGroup to specify the style group to use for style lookups.

      Example:

       @StyleBundle.StyleGroup("mygroup")
       interface MyStyles extends StyleBundle {
           @StyleBundle.DefaultStyle("bold,fg:red")
           AttributedString error(String message);
      
           @StyleBundle.DefaultStyle("bold,fg:yellow")
           AttributedString warning(String message);
       }
      
       MyStyles styles = Styler.bundle(MyStyles.class);
       AttributedString errorText = styles.error("Error message");
       
      Type Parameters:
      T - the interface type to proxy
      Parameters:
      type - the interface class to proxy (must not be null and must be annotated with StyleBundle.StyleGroup)
      Returns:
      a proxy implementing the specified interface
      Throws:
      NullPointerException - if type is null
      org.jline.style.StyleBundleInvocationHandler.InvalidStyleGroupException - if the interface is not annotated with StyleBundle.StyleGroup
    • bundle

      public static <T extends StyleBundle> T bundle(String group, Class<T> type)
      Creates a StyleBundle proxy for the specified interface with an explicit style group.

      This method is similar to bundle(Class), but it allows specifying the style group explicitly instead of requiring the interface to be annotated with StyleBundle.StyleGroup.

      Example:

       interface MyStyles extends StyleBundle {
           @StyleBundle.DefaultStyle("bold,fg:red")
           AttributedString error(String message);
       }
      
       MyStyles styles = Styler.bundle("mygroup", MyStyles.class);
       AttributedString errorText = styles.error("Error message");
       
      Type Parameters:
      T - the interface type to proxy
      Parameters:
      group - the style group name to use (must not be null)
      type - the interface class to proxy (must not be null)
      Returns:
      a proxy implementing the specified interface
      Throws:
      NullPointerException - if group or type is null