Class Styler
- java.lang.Object
-
- org.jline.style.Styler
-
public class Styler extends java.lang.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 creatingStyleResolver
s,StyleFactory
s, andStyleBundle
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:
StyleBundle
,StyleFactory
,StyleSource
,StyleResolver
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends StyleBundle>
Tbundle(java.lang.Class<T> type)
Creates aStyleBundle
proxy for the specified interface.static <T extends StyleBundle>
Tbundle(java.lang.String group, java.lang.Class<T> type)
Creates aStyleBundle
proxy for the specified interface with an explicit style group.static StyleFactory
factory(java.lang.String group)
Creates aStyleFactory
for the given style group.static StyleSource
getSource()
Returns the globalStyleSource
used by the Styler.static StyleResolver
resolver(java.lang.String group)
Creates aStyleResolver
for the given style group.static void
setSource(StyleSource source)
Installs a new globalStyleSource
.
-
-
-
Method Detail
-
getSource
public static StyleSource getSource()
Returns the globalStyleSource
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(StyleSource)
-
setSource
public static void setSource(StyleSource source)
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:
java.lang.NullPointerException
- if source is null- See Also:
getSource()
-
resolver
public static StyleResolver resolver(java.lang.String group)
Creates aStyleResolver
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:
java.lang.NullPointerException
- if group is null
-
factory
public static StyleFactory factory(java.lang.String group)
Creates aStyleFactory
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:
java.lang.NullPointerException
- if group is null
-
bundle
public static <T extends StyleBundle> T bundle(java.lang.Class<T> type)
Creates aStyleBundle
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'sStyleBundle.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 withStyleBundle.StyleGroup
)- Returns:
- a proxy implementing the specified interface
- Throws:
java.lang.NullPointerException
- if type is nullorg.jline.style.StyleBundleInvocationHandler.InvalidStyleGroupException
- if the interface is not annotated withStyleBundle.StyleGroup
-
bundle
public static <T extends StyleBundle> T bundle(java.lang.String group, java.lang.Class<T> type)
Creates aStyleBundle
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 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:
java.lang.NullPointerException
- if group or type is null
-
-