Class StyledText

java.lang.Object
org.strassburger.tui4j.formatting.StyledText

public final class StyledText extends Object

Represents styled text composed of multiple spans, each with its own style.

Example usage:


 StyledText styled = StyledText.text("Hello, ")
     .fg(Color.fromHex("00ff00"))
     .append(StyledText.text("World!").bold())
     .underline();
 
  • Method Details

    • getSpans

      public List<Span> getSpans()
    • text

      public static StyledText text(String text)
      Create a StyledText with a single span of plain text
      Parameters:
      text - the text content
      Returns:
      the StyledText instance
    • text

      public static StyledText text(String text, Style style)
      Create a StyledText with a single span of text and the given style
      Parameters:
      text - the text content
      style - the style to apply
      Returns:
      the StyledText instance
    • append

      public StyledText append(StyledText other)
      Append another StyledText to this one
      Parameters:
      other - the other StyledText to append
      Returns:
      a new StyledText instance representing the combined text
    • prepend

      public StyledText prepend(StyledText other)
      Prepend another StyledText to this one
      Parameters:
      other - the other StyledText to prepend
      Returns:
      a new StyledText instance representing the combined text
    • append

      public StyledText append(String text, Style style)
      Append a string with the given style to this StyledText
      Parameters:
      text - the text content
      style - the style to apply
      Returns:
      a new StyledText instance representing the combined text
    • append

      public StyledText append(String text)
      Append a plain string to this StyledText
      Parameters:
      text - the text content
      Returns:
      a new StyledText instance representing the combined text
    • prepend

      public StyledText prepend(String text, Style style)
      Prepend a string with the given style to this StyledText
      Parameters:
      text - the text content
      style - the style to apply
      Returns:
      a new StyledText instance representing the combined text
    • prepend

      public StyledText prepend(String text)
      Prepend a plain string to this StyledText
      Parameters:
      text - the text content
      Returns:
      a new StyledText instance representing the combined text
    • replace

      public StyledText replace(String target, String replacement)
      Replace occurrences of a target string with a replacement string in all spans
      Parameters:
      target - the string to be replaced
      replacement - the string to replace with
      Returns:
      a new StyledText instance with the replacements made
    • repeat

      public StyledText repeat(int count)
      Repeat the entire StyledText a specified number of times
      Parameters:
      count - the number of times to repeat
      Returns:
      a new StyledText instance with the repeated content
    • fg

      public StyledText fg(org.strassburger.colorlab4j.color.Color color)
      Set the foreground color of the last span
      Parameters:
      color - the foreground color
      Returns:
      the modified StyledText
    • bg

      public StyledText bg(org.strassburger.colorlab4j.color.Color color)
      Set the background color of the last span
      Parameters:
      color - the background color
      Returns:
      the modified StyledText
    • bold

      public StyledText bold()
      Make the last span bold
      Returns:
      the modified StyledText
    • underline

      public StyledText underline()
      Make the last span italic
      Returns:
      the modified StyledText
    • reversed

      public StyledText reversed()
      Inverse the colors of the last span
      Returns:
      the modified StyledText
    • toString

      public String toString()
      Overrides:
      toString in class Object