Class AttributedStyle

java.lang.Object
org.jline.utils.AttributedStyle

public class AttributedStyle extends 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:
  • Field Details

  • 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

      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 colors
      mask - the mask value indicating which attributes and colors are set
  • Method Details

    • 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

      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:
    • 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.

      Returns:
      a new style with the bold attribute set to its default state
      See Also:
    • 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

      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:
    • 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:
    • 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

      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:
    • 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:
    • 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

      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:
    • 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:
    • 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

      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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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

      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:
    • 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:
    • 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

      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:
    • 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:
    • 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:
    • 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

      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:
    • 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:
    • 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:
    • 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:
    • 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

      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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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

      public boolean equals(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 class 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 class Object
      Returns:
      a hash code value for this object
    • toAnsi

      public 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 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 class Object
      Returns:
      a string representation of this AttributedStyle