Class AttributedStyle
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final AttributedStyle
Style with bold attribute enabled.static final AttributedStyle
Style with bold attribute explicitly disabled.static final int
static final int
static final AttributedStyle
Default style with no attributes or colors set.static final int
static final AttributedStyle
Style with hidden attribute enabled.static final AttributedStyle
Style with hidden attribute explicitly disabled.static final AttributedStyle
Style with inverse (reverse video) attribute enabled.static final AttributedStyle
Style with inverse (reverse video) attribute explicitly disabled.static final int
static final int
static final int
static final int
-
Constructor Summary
ConstructorsConstructorDescriptionCreates 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.Creates a new AttributedStyle by copying another style. -
Method Summary
Modifier and TypeMethodDescriptionbackground
(int color) Returns a new style with the specified background color.background
(int r, int g, int b) Returns a new style with the specified RGB background color.Returns a new style with the background color set to its default state.Returns a new style with the background color explicitly disabled.backgroundRgb
(int color) Returns a new style with the specified RGB background color.blink()
Returns a new style with the blink attribute enabled.Returns a new style with the blink attribute set to its default state.blinkOff()
Returns a new style with the blink attribute explicitly disabled.bold()
Returns a new style with the bold attribute enabled.Returns a new style with the bold attribute set to its default state.boldOff()
Returns a new style with the bold attribute explicitly disabled.conceal()
Returns a new style with the conceal attribute enabled.Returns a new style with the conceal attribute set to its default state.Returns a new style with the conceal attribute explicitly disabled.Returns a new style with the crossed-out attribute enabled.Returns a new style with the crossed-out attribute set to its default state.Returns a new style with the crossed-out attribute explicitly disabled.boolean
Compares this AttributedStyle with another object for equality.faint()
Returns a new style with the faint attribute enabled.Returns a new style with the faint attribute set to its default state.faintOff()
Returns a new style with the faint attribute explicitly disabled.foreground
(int color) Returns a new style with the specified foreground color.foreground
(int r, int g, int b) Returns a new style with the specified RGB foreground color.Returns a new style with the foreground color set to its default state.Returns a new style with the foreground color explicitly disabled.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.hidden()
The hidden flag can be used to embed custom escape sequences.Returns a new style with the hidden attribute set to its default state.Returns a new style with the hidden attribute explicitly disabled.inverse()
Returns a new style with the inverse attribute enabled.Returns a new style with the inverse attribute set to its default state.Returns a new style with the inverse attribute toggled.Returns a new style with the inverse attribute explicitly disabled.italic()
Returns a new style with the italic attribute enabled.Returns a new style with the italic attribute set to its default state.Returns a new style with the italic attribute explicitly disabled.toAnsi()
Returns an ANSI escape sequence string that represents this style.toString()
Returns a string representation of this AttributedStyle.Returns a new style with the underline attribute enabled.Returns a new style with the underline attribute set to its default state.Returns a new style with the underline attribute explicitly disabled.
-
Field Details
-
BLACK
public static final int BLACK- See Also:
-
RED
public static final int RED- See Also:
-
GREEN
public static final int GREEN- See Also:
-
YELLOW
public static final int YELLOW- See Also:
-
BLUE
public static final int BLUE- See Also:
-
MAGENTA
public static final int MAGENTA- See Also:
-
CYAN
public static final int CYAN- See Also:
-
WHITE
public static final int WHITE- See Also:
-
BRIGHT
public static final int BRIGHT- See Also:
-
DEFAULT
Default style with no attributes or colors set. -
BOLD
Style with bold attribute enabled. -
BOLD_OFF
Style with bold attribute explicitly disabled. -
INVERSE
Style with inverse (reverse video) attribute enabled. -
INVERSE_OFF
Style with inverse (reverse video) attribute explicitly disabled. -
HIDDEN
Style with hidden attribute enabled. -
HIDDEN_OFF
Style with hidden attribute explicitly disabled.
-
-
Constructor Details
-
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
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 Details
-
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
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:
-
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.
- Returns:
- a new style with the bold attribute set to its default state
- See Also:
-
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
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:
-
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:
-
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
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:
-
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:
-
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
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:
-
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:
-
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
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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
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:
-
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:
-
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
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:
-
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:
-
foreground
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:
-
foreground
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
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:
-
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:
-
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:
-
background
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:
-
background
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
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:
-
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:
-
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:
-
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
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:
-
equals
Compares this AttributedStyle with another object for equality.Two AttributedStyle objects are considered equal if they have the same style and mask values.
-
hashCode
public int hashCode()Returns a hash code for this AttributedStyle.The hash code is computed based on the style and mask values.
-
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
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.
-