Class AttributedStyle
- java.lang.Object
-
- org.jline.utils.AttributedStyle
-
public class AttributedStyle extends java.lang.Object
Text styling for terminal output with support for colors, fonts, and other attributes.The AttributedStyle class represents the styling attributes that can be applied to text in a terminal. It supports various text attributes such as bold, italic, underline, as well as foreground and background colors. The class uses a bit-packed long value to efficiently store multiple attributes.
This class provides a fluent API for building styles by chaining method calls. Styles are immutable, so each method returns a new instance with the requested modifications.
Color support includes:
- 8 standard ANSI colors (black, red, green, yellow, blue, magenta, cyan, white)
- 8 bright variants of the standard colors
- 256-color indexed mode
- 24-bit true color (RGB) mode
Text attributes include bold, faint, italic, underline, blink, inverse, conceal, and crossed-out.
Example usage:
// Create a style with red foreground, bold, and underline AttributedStyle style = AttributedStyle.DEFAULT .foreground(AttributedStyle.RED) .bold() .underline(); // Create a string with this style AttributedString str = new AttributedString("Error message", style);
- See Also:
AttributedString
,AttributedStringBuilder
-
-
Field Summary
Fields Modifier and Type Field Description static int
BLACK
static int
BLUE
static AttributedStyle
BOLD
Style with bold attribute enabled.static AttributedStyle
BOLD_OFF
Style with bold attribute explicitly disabled.static int
BRIGHT
static int
CYAN
static AttributedStyle
DEFAULT
Default style with no attributes or colors set.static int
GREEN
static AttributedStyle
HIDDEN
Style with hidden attribute enabled.static AttributedStyle
HIDDEN_OFF
Style with hidden attribute explicitly disabled.static AttributedStyle
INVERSE
Style with inverse (reverse video) attribute enabled.static AttributedStyle
INVERSE_OFF
Style with inverse (reverse video) attribute explicitly disabled.static int
MAGENTA
static int
RED
static int
WHITE
static int
YELLOW
-
Constructor Summary
Constructors Constructor Description AttributedStyle()
Creates a new AttributedStyle with no attributes or colors set.AttributedStyle(long style, long mask)
Creates a new AttributedStyle with the specified style and mask values.AttributedStyle(AttributedStyle s)
Creates a new AttributedStyle by copying another style.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AttributedStyle
background(int color)
Returns a new style with the specified background color.AttributedStyle
background(int r, int g, int b)
Returns a new style with the specified RGB background color.AttributedStyle
backgroundDefault()
Returns a new style with the background color set to its default state.AttributedStyle
backgroundOff()
Returns a new style with the background color explicitly disabled.AttributedStyle
backgroundRgb(int color)
Returns a new style with the specified RGB background color.AttributedStyle
blink()
Returns a new style with the blink attribute enabled.AttributedStyle
blinkDefault()
Returns a new style with the blink attribute set to its default state.AttributedStyle
blinkOff()
Returns a new style with the blink attribute explicitly disabled.AttributedStyle
bold()
Returns a new style with the bold attribute enabled.AttributedStyle
boldDefault()
Returns a new style with the bold attribute set to its default state.AttributedStyle
boldOff()
Returns a new style with the bold attribute explicitly disabled.AttributedStyle
conceal()
Returns a new style with the conceal attribute enabled.AttributedStyle
concealDefault()
Returns a new style with the conceal attribute set to its default state.AttributedStyle
concealOff()
Returns a new style with the conceal attribute explicitly disabled.AttributedStyle
crossedOut()
Returns a new style with the crossed-out attribute enabled.AttributedStyle
crossedOutDefault()
Returns a new style with the crossed-out attribute set to its default state.AttributedStyle
crossedOutOff()
Returns a new style with the crossed-out attribute explicitly disabled.boolean
equals(java.lang.Object o)
Compares this AttributedStyle with another object for equality.AttributedStyle
faint()
Returns a new style with the faint attribute enabled.AttributedStyle
faintDefault()
Returns a new style with the faint attribute set to its default state.AttributedStyle
faintOff()
Returns a new style with the faint attribute explicitly disabled.AttributedStyle
foreground(int color)
Returns a new style with the specified foreground color.AttributedStyle
foreground(int r, int g, int b)
Returns a new style with the specified RGB foreground color.AttributedStyle
foregroundDefault()
Returns a new style with the foreground color set to its default state.AttributedStyle
foregroundOff()
Returns a new style with the foreground color explicitly disabled.AttributedStyle
foregroundRgb(int color)
Returns a new style with the specified RGB foreground color.long
getMask()
Returns the raw mask value of this style.long
getStyle()
Returns the raw style value of this style.int
hashCode()
Returns a hash code for this AttributedStyle.AttributedStyle
hidden()
The hidden flag can be used to embed custom escape sequences.AttributedStyle
hiddenDefault()
Returns a new style with the hidden attribute set to its default state.AttributedStyle
hiddenOff()
Returns a new style with the hidden attribute explicitly disabled.AttributedStyle
inverse()
Returns a new style with the inverse attribute enabled.AttributedStyle
inverseDefault()
Returns a new style with the inverse attribute set to its default state.AttributedStyle
inverseNeg()
Returns a new style with the inverse attribute toggled.AttributedStyle
inverseOff()
Returns a new style with the inverse attribute explicitly disabled.AttributedStyle
italic()
Returns a new style with the italic attribute enabled.AttributedStyle
italicDefault()
Returns a new style with the italic attribute set to its default state.AttributedStyle
italicOff()
Returns a new style with the italic attribute explicitly disabled.java.lang.String
toAnsi()
Returns an ANSI escape sequence string that represents this style.java.lang.String
toString()
Returns a string representation of this AttributedStyle.AttributedStyle
underline()
Returns a new style with the underline attribute enabled.AttributedStyle
underlineDefault()
Returns a new style with the underline attribute set to its default state.AttributedStyle
underlineOff()
Returns a new style with the underline attribute explicitly disabled.
-
-
-
Field Detail
-
BLACK
public static final int BLACK
- See Also:
- Constant Field Values
-
RED
public static final int RED
- See Also:
- Constant Field Values
-
GREEN
public static final int GREEN
- See Also:
- Constant Field Values
-
YELLOW
public static final int YELLOW
- See Also:
- Constant Field Values
-
BLUE
public static final int BLUE
- See Also:
- Constant Field Values
-
MAGENTA
public static final int MAGENTA
- See Also:
- Constant Field Values
-
CYAN
public static final int CYAN
- See Also:
- Constant Field Values
-
WHITE
public static final int WHITE
- See Also:
- Constant Field Values
-
BRIGHT
public static final int BRIGHT
- See Also:
- Constant Field Values
-
DEFAULT
public static final AttributedStyle DEFAULT
Default style with no attributes or colors set.
-
BOLD
public static final AttributedStyle BOLD
Style with bold attribute enabled.
-
BOLD_OFF
public static final AttributedStyle BOLD_OFF
Style with bold attribute explicitly disabled.
-
INVERSE
public static final AttributedStyle INVERSE
Style with inverse (reverse video) attribute enabled.
-
INVERSE_OFF
public static final AttributedStyle INVERSE_OFF
Style with inverse (reverse video) attribute explicitly disabled.
-
HIDDEN
public static final AttributedStyle HIDDEN
Style with hidden attribute enabled.
-
HIDDEN_OFF
public static final AttributedStyle HIDDEN_OFF
Style with hidden attribute explicitly disabled.
-
-
Constructor Detail
-
AttributedStyle
public AttributedStyle()
Creates a new AttributedStyle with no attributes or colors set.This constructor creates a default style with no attributes or colors set. It is equivalent to
DEFAULT
.
-
AttributedStyle
public AttributedStyle(AttributedStyle s)
Creates a new AttributedStyle by copying another style.This constructor creates a new style with the same attributes and colors as the specified style.
- Parameters:
s
- the style to copy
-
AttributedStyle
public AttributedStyle(long style, long mask)
Creates a new AttributedStyle with the specified style and mask values.This constructor creates a new style with the specified style and mask values. The style value contains the actual attributes and colors, while the mask value indicates which attributes and colors are explicitly set (as opposed to being inherited or default).
This constructor is primarily for internal use and advanced scenarios.
- Parameters:
style
- the style value containing attributes and colorsmask
- the mask value indicating which attributes and colors are set
-
-
Method Detail
-
bold
public AttributedStyle bold()
Returns a new style with the bold attribute enabled.This method returns a new style with the bold attribute enabled. The bold attribute typically makes text appear with a heavier or thicker font.
- Returns:
- a new style with the bold attribute enabled
- See Also:
boldOff()
,boldDefault()
-
boldOff
public AttributedStyle boldOff()
Returns a new style with the bold attribute explicitly disabled.This method returns a new style with the bold attribute explicitly disabled. This is different from
boldDefault()
, which removes any explicit setting for the bold attribute.- Returns:
- a new style with the bold attribute explicitly disabled
- See Also:
bold()
,boldDefault()
-
boldDefault
public AttributedStyle boldDefault()
Returns a new style with the bold attribute set to its default state.This method returns a new style with no explicit setting for the bold attribute. When this style is applied, the bold attribute will be inherited from the parent style or use the terminal's default.
-
faint
public AttributedStyle faint()
Returns a new style with the faint attribute enabled.This method returns a new style with the faint attribute enabled. The faint attribute typically makes text appear with a lighter or thinner font, or with reduced intensity.
- Returns:
- a new style with the faint attribute enabled
- See Also:
faintOff()
,faintDefault()
-
faintOff
public AttributedStyle faintOff()
Returns a new style with the faint attribute explicitly disabled.This method returns a new style with the faint attribute explicitly disabled. This is different from
faintDefault()
, which removes any explicit setting for the faint attribute.- Returns:
- a new style with the faint attribute explicitly disabled
- See Also:
faint()
,faintDefault()
-
faintDefault
public AttributedStyle faintDefault()
Returns a new style with the faint attribute set to its default state.This method returns a new style with no explicit setting for the faint attribute. When this style is applied, the faint attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the faint attribute set to its default state
- See Also:
faint()
,faintOff()
-
italic
public AttributedStyle italic()
Returns a new style with the italic attribute enabled.This method returns a new style with the italic attribute enabled. The italic attribute typically makes text appear slanted or cursive.
- Returns:
- a new style with the italic attribute enabled
- See Also:
italicOff()
,italicDefault()
-
italicOff
public AttributedStyle italicOff()
Returns a new style with the italic attribute explicitly disabled.This method returns a new style with the italic attribute explicitly disabled. This is different from
italicDefault()
, which removes any explicit setting for the italic attribute.- Returns:
- a new style with the italic attribute explicitly disabled
- See Also:
italic()
,italicDefault()
-
italicDefault
public AttributedStyle italicDefault()
Returns a new style with the italic attribute set to its default state.This method returns a new style with no explicit setting for the italic attribute. When this style is applied, the italic attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the italic attribute set to its default state
- See Also:
italic()
,italicOff()
-
underline
public AttributedStyle underline()
Returns a new style with the underline attribute enabled.This method returns a new style with the underline attribute enabled. The underline attribute typically draws a line under the text.
- Returns:
- a new style with the underline attribute enabled
- See Also:
underlineOff()
,underlineDefault()
-
underlineOff
public AttributedStyle underlineOff()
Returns a new style with the underline attribute explicitly disabled.This method returns a new style with the underline attribute explicitly disabled. This is different from
underlineDefault()
, which removes any explicit setting for the underline attribute.- Returns:
- a new style with the underline attribute explicitly disabled
- See Also:
underline()
,underlineDefault()
-
underlineDefault
public AttributedStyle underlineDefault()
Returns a new style with the underline attribute set to its default state.This method returns a new style with no explicit setting for the underline attribute. When this style is applied, the underline attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the underline attribute set to its default state
- See Also:
underline()
,underlineOff()
-
blink
public AttributedStyle blink()
Returns a new style with the blink attribute enabled.This method returns a new style with the blink attribute enabled. The blink attribute typically makes text flash on and off, though support varies across terminals.
- Returns:
- a new style with the blink attribute enabled
- See Also:
blinkOff()
,blinkDefault()
-
blinkOff
public AttributedStyle blinkOff()
Returns a new style with the blink attribute explicitly disabled.This method returns a new style with the blink attribute explicitly disabled. This is different from
blinkDefault()
, which removes any explicit setting for the blink attribute.- Returns:
- a new style with the blink attribute explicitly disabled
- See Also:
blink()
,blinkDefault()
-
blinkDefault
public AttributedStyle blinkDefault()
Returns a new style with the blink attribute set to its default state.This method returns a new style with no explicit setting for the blink attribute. When this style is applied, the blink attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the blink attribute set to its default state
- See Also:
blink()
,blinkOff()
-
inverse
public AttributedStyle inverse()
Returns a new style with the inverse attribute enabled.This method returns a new style with the inverse attribute enabled. The inverse attribute (also known as reverse video) typically swaps the foreground and background colors of the text.
- Returns:
- a new style with the inverse attribute enabled
- See Also:
inverseOff()
,inverseDefault()
,inverseNeg()
-
inverseNeg
public AttributedStyle inverseNeg()
Returns a new style with the inverse attribute toggled.This method returns a new style with the inverse attribute toggled from its current state. If the inverse attribute is currently enabled, it will be disabled, and if it is currently disabled, it will be enabled.
- Returns:
- a new style with the inverse attribute toggled
- See Also:
inverse()
,inverseOff()
,inverseDefault()
-
inverseOff
public AttributedStyle inverseOff()
Returns a new style with the inverse attribute explicitly disabled.This method returns a new style with the inverse attribute explicitly disabled. This is different from
inverseDefault()
, which removes any explicit setting for the inverse attribute.- Returns:
- a new style with the inverse attribute explicitly disabled
- See Also:
inverse()
,inverseDefault()
,inverseNeg()
-
inverseDefault
public AttributedStyle inverseDefault()
Returns a new style with the inverse attribute set to its default state.This method returns a new style with no explicit setting for the inverse attribute. When this style is applied, the inverse attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the inverse attribute set to its default state
- See Also:
inverse()
,inverseOff()
,inverseNeg()
-
conceal
public AttributedStyle conceal()
Returns a new style with the conceal attribute enabled.This method returns a new style with the conceal attribute enabled. The conceal attribute typically hides text from display, though support varies across terminals.
- Returns:
- a new style with the conceal attribute enabled
- See Also:
concealOff()
,concealDefault()
-
concealOff
public AttributedStyle concealOff()
Returns a new style with the conceal attribute explicitly disabled.This method returns a new style with the conceal attribute explicitly disabled. This is different from
concealDefault()
, which removes any explicit setting for the conceal attribute.- Returns:
- a new style with the conceal attribute explicitly disabled
- See Also:
conceal()
,concealDefault()
-
concealDefault
public AttributedStyle concealDefault()
Returns a new style with the conceal attribute set to its default state.This method returns a new style with no explicit setting for the conceal attribute. When this style is applied, the conceal attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the conceal attribute set to its default state
- See Also:
conceal()
,concealOff()
-
crossedOut
public AttributedStyle crossedOut()
Returns a new style with the crossed-out attribute enabled.This method returns a new style with the crossed-out attribute enabled. The crossed-out attribute typically draws a line through the text, though support varies across terminals.
- Returns:
- a new style with the crossed-out attribute enabled
- See Also:
crossedOutOff()
,crossedOutDefault()
-
crossedOutOff
public AttributedStyle crossedOutOff()
Returns a new style with the crossed-out attribute explicitly disabled.This method returns a new style with the crossed-out attribute explicitly disabled. This is different from
crossedOutDefault()
, which removes any explicit setting for the crossed-out attribute.- Returns:
- a new style with the crossed-out attribute explicitly disabled
- See Also:
crossedOut()
,crossedOutDefault()
-
crossedOutDefault
public AttributedStyle crossedOutDefault()
Returns a new style with the crossed-out attribute set to its default state.This method returns a new style with no explicit setting for the crossed-out attribute. When this style is applied, the crossed-out attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the crossed-out attribute set to its default state
- See Also:
crossedOut()
,crossedOutOff()
-
foreground
public AttributedStyle foreground(int color)
Returns a new style with the specified foreground color.This method returns a new style with the specified foreground color. The color is specified as an index into the terminal's color palette. Standard ANSI colors are defined as constants in this class (BLACK, RED, etc.).
For 256-color support, use values from 0 to 255. For standard ANSI colors, use values from 0 to 7, or add BRIGHT (8) for bright variants.
- Parameters:
color
- the foreground color index- Returns:
- a new style with the specified foreground color
- See Also:
foregroundOff()
,foregroundDefault()
,foreground(int, int, int)
,foregroundRgb(int)
-
foreground
public AttributedStyle foreground(int r, int g, int b)
Returns a new style with the specified RGB foreground color.This method returns a new style with the specified RGB foreground color. The color is specified as separate red, green, and blue components, each with a value from 0 to 255.
Note that true color support (24-bit RGB) may not be available in all terminals. In terminals without true color support, the color will be approximated using the closest available color in the terminal's palette.
- Parameters:
r
- the red component (0-255)g
- the green component (0-255)b
- the blue component (0-255)- Returns:
- a new style with the specified RGB foreground color
- See Also:
foregroundRgb(int)
,foregroundOff()
,foregroundDefault()
-
foregroundRgb
public AttributedStyle foregroundRgb(int color)
Returns a new style with the specified RGB foreground color.This method returns a new style with the specified RGB foreground color. The color is specified as a single integer in the format 0xRRGGBB, where RR, GG, and BB are the red, green, and blue components in hexadecimal.
Note that true color support (24-bit RGB) may not be available in all terminals. In terminals without true color support, the color will be approximated using the closest available color in the terminal's palette.
- Parameters:
color
- the RGB color value (0xRRGGBB)- Returns:
- a new style with the specified RGB foreground color
- See Also:
foreground(int, int, int)
,foregroundOff()
,foregroundDefault()
-
foregroundOff
public AttributedStyle foregroundOff()
Returns a new style with the foreground color explicitly disabled.This method returns a new style with the foreground color explicitly disabled. This is different from
foregroundDefault()
, which removes any explicit setting for the foreground color.When a style with the foreground color disabled is applied, the text will typically be displayed using the terminal's default foreground color.
- Returns:
- a new style with the foreground color explicitly disabled
- See Also:
foreground(int)
,foregroundDefault()
-
foregroundDefault
public AttributedStyle foregroundDefault()
Returns a new style with the foreground color set to its default state.This method returns a new style with no explicit setting for the foreground color. When this style is applied, the foreground color will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the foreground color set to its default state
- See Also:
foreground(int)
,foregroundOff()
-
background
public AttributedStyle background(int color)
Returns a new style with the specified background color.This method returns a new style with the specified background color. The color is specified as an index into the terminal's color palette. Standard ANSI colors are defined as constants in this class (BLACK, RED, etc.).
For 256-color support, use values from 0 to 255. For standard ANSI colors, use values from 0 to 7, or add BRIGHT (8) for bright variants.
- Parameters:
color
- the background color index- Returns:
- a new style with the specified background color
- See Also:
backgroundOff()
,backgroundDefault()
,background(int, int, int)
,backgroundRgb(int)
-
background
public AttributedStyle background(int r, int g, int b)
Returns a new style with the specified RGB background color.This method returns a new style with the specified RGB background color. The color is specified as separate red, green, and blue components, each with a value from 0 to 255.
Note that true color support (24-bit RGB) may not be available in all terminals. In terminals without true color support, the color will be approximated using the closest available color in the terminal's palette.
- Parameters:
r
- the red component (0-255)g
- the green component (0-255)b
- the blue component (0-255)- Returns:
- a new style with the specified RGB background color
- See Also:
backgroundRgb(int)
,backgroundOff()
,backgroundDefault()
-
backgroundRgb
public AttributedStyle backgroundRgb(int color)
Returns a new style with the specified RGB background color.This method returns a new style with the specified RGB background color. The color is specified as a single integer in the format 0xRRGGBB, where RR, GG, and BB are the red, green, and blue components in hexadecimal.
Note that true color support (24-bit RGB) may not be available in all terminals. In terminals without true color support, the color will be approximated using the closest available color in the terminal's palette.
- Parameters:
color
- the RGB color value (0xRRGGBB)- Returns:
- a new style with the specified RGB background color
- See Also:
background(int, int, int)
,backgroundOff()
,backgroundDefault()
-
backgroundOff
public AttributedStyle backgroundOff()
Returns a new style with the background color explicitly disabled.This method returns a new style with the background color explicitly disabled. This is different from
backgroundDefault()
, which removes any explicit setting for the background color.When a style with the background color disabled is applied, the text will typically be displayed using the terminal's default background color.
- Returns:
- a new style with the background color explicitly disabled
- See Also:
background(int)
,backgroundDefault()
-
backgroundDefault
public AttributedStyle backgroundDefault()
Returns a new style with the background color set to its default state.This method returns a new style with no explicit setting for the background color. When this style is applied, the background color will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the background color set to its default state
- See Also:
background(int)
,backgroundOff()
-
hidden
public AttributedStyle hidden()
The hidden flag can be used to embed custom escape sequences. The characters are considered being 0-column long and will be printed as-is. The user is responsible for ensuring that those sequences do not move the cursor.- Returns:
- the new style
-
hiddenOff
public AttributedStyle hiddenOff()
Returns a new style with the hidden attribute explicitly disabled.This method returns a new style with the hidden attribute explicitly disabled. This is different from
hiddenDefault()
, which removes any explicit setting for the hidden attribute.- Returns:
- a new style with the hidden attribute explicitly disabled
- See Also:
hidden()
,hiddenDefault()
-
hiddenDefault
public AttributedStyle hiddenDefault()
Returns a new style with the hidden attribute set to its default state.This method returns a new style with no explicit setting for the hidden attribute. When this style is applied, the hidden attribute will be inherited from the parent style or use the terminal's default.
- Returns:
- a new style with the hidden attribute set to its default state
- See Also:
hidden()
,hiddenOff()
-
getStyle
public long getStyle()
Returns the raw style value of this style.This method returns the raw style value, which contains all the attributes and colors encoded as bit flags in a long value. This is primarily for internal use and advanced scenarios.
- Returns:
- the raw style value
- See Also:
getMask()
-
getMask
public long getMask()
Returns the raw mask value of this style.This method returns the raw mask value, which indicates which attributes and colors are explicitly set (as opposed to being inherited or default). This is primarily for internal use and advanced scenarios.
- Returns:
- the raw mask value
- See Also:
getStyle()
-
equals
public boolean equals(java.lang.Object o)
Compares this AttributedStyle with another object for equality.Two AttributedStyle objects are considered equal if they have the same style and mask values.
- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- the object to compare with- Returns:
true
if the objects are equal,false
otherwise
-
hashCode
public int hashCode()
Returns a hash code for this AttributedStyle.The hash code is computed based on the style and mask values.
- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a hash code value for this object
-
toAnsi
public java.lang.String toAnsi()
Returns an ANSI escape sequence string that represents this style.This method generates an ANSI escape sequence string that, when printed to a terminal, would apply this style. This is useful for debugging or for generating ANSI-colored output for terminals that support it.
The method works by creating a temporary AttributedStringBuilder, applying this style to a space character, and then extracting the ANSI escape sequences from the resulting string.
- Returns:
- an ANSI escape sequence string representing this style
-
toString
public java.lang.String toString()
Returns a string representation of this AttributedStyle.This method returns a string representation of this AttributedStyle, including the style value, mask value, and ANSI escape sequence. This is primarily useful for debugging.
- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of this AttributedStyle
-
-