Class Styler
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 Summary
Modifier and TypeMethodDescriptionstatic <T extends StyleBundle>
TCreates aStyleBundleproxy for the specified interface.static <T extends StyleBundle>
TCreates aStyleBundleproxy for the specified interface with an explicit style group.static StyleFactoryCreates aStyleFactoryfor the given style group.static StyleSourceReturns the globalStyleSourceused by the Styler.static StyleResolverCreates aStyleResolverfor the given style group.static voidsetSource(StyleSource source) Installs a new globalStyleSource.
-
Method Details
-
getSource
Returns the globalStyleSourceused by the Styler.The global style source is used by all style operations that don't explicitly specify a different source. By default, a
NopStyleSourceis used, which doesn't provide any styles.- Returns:
- the global style source
- See Also:
-
setSource
Installs a new globalStyleSource.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
Creates aStyleResolverfor the given style group.The style resolver is used to resolve style specifications into
AttributedStyleobjects. 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
Creates aStyleFactoryfor the given style group.The style factory provides methods for creating styled strings using style specifications or expressions. It uses a
StyleResolverfor 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
Creates aStyleBundleproxy 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
AttributedStringand take a single parameter that will be styled according to the method'sStyleBundle.DefaultStyleannotation or a style definition from the global style source.The target interface must be annotated with
StyleBundle.StyleGroupto 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 withStyleBundle.StyleGroup)- Returns:
- a proxy implementing the specified interface
- Throws:
NullPointerException- if type is nullorg.jline.style.StyleBundleInvocationHandler.InvalidStyleGroupException- if the interface is not annotated withStyleBundle.StyleGroup
-
bundle
Creates aStyleBundleproxy 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 withStyleBundle.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
-