java.lang.Object
io.github.darvil.terminal.textformatter.TextFormatter

public class TextFormatter extends Object
Allows easily formatting of text for it to be displayed in a terminal.

Multiple formatters can be concatenated together. This is useful for when you want to format a string that has multiple parts that need to be formatted differently.

  • Field Details

    • enableSequences

      public static boolean enableSequences
      When set to false, no formatting will be applied to text. Raw text will be generated without any color or formatting.

      This will be set to false if the environment variable NO_COLOR is set.

      See Also:
    • defaultColor

      @NotNull public static @NotNull Color defaultColor
      The default color that should be used when no foreground color is specified (if startWithDefaultColorIfNotDefined is set to true), or when the foreground color is reset.
    • startWithDefaultColorIfNotDefined

      public static boolean startWithDefaultColorIfNotDefined
      When set to true, the default color will be used when no foreground color is specified.
    • debug

      public static boolean debug
      When set to true, the toString() method will not add any terminal sequences, but rather return the sequences that would be added by marking them as ESC[<sequence here>]
  • Constructor Details

    • TextFormatter

      protected TextFormatter(@NotNull @NotNull String contents)
      Creates a new TextFormatter with the specified contents.
      Parameters:
      contents - The contents of the formatter.
  • Method Details

    • of

      public static TextFormatter of(@NotNull @NotNull String contents)
      Creates a new TextFormatter with the specified contents.
      Parameters:
      contents - The contents of the formatter.
    • of

      public static TextFormatter of(@NotNull @NotNull String contents, @NotNull @NotNull Color foreground)
      Creates a new TextFormatter with the specified contents and foreground color.
      Parameters:
      contents - The contents of the formatter.
      foreground - The foreground color of the formatter.
    • of

      public static TextFormatter of(@NotNull @NotNull String contents, @NotNull @NotNull Color foreground, @NotNull @NotNull Color background)
      Creates a new TextFormatter with the specified contents and colors.
      Parameters:
      contents - The contents of the formatter.
      foreground - The foreground color of the formatter.
      background - The background color of the formatter.
    • create

      public static TextFormatter create()
      Creates a new TextFormatter with empty contents.
      Returns:
      a new TextFormatter with empty contents
    • error

      @NotNull public static @NotNull TextFormatter error(@NotNull @NotNull String msg)
      Returns a new TextFormatter with the specified contents and the error formatting.

      The error formatting is a bold, black text with a bright red background.

      Parameters:
      msg - The contents of the formatter.
      Returns:
      a new TextFormatter with the specified contents and the error formatting
    • addFormat

      public TextFormatter addFormat(@NotNull @NotNull FormatOption... options)
      Adds the specified formatting options to the formatter.
      Parameters:
      options - The formatting options to add.
    • removeFormat

      public TextFormatter removeFormat(@NotNull @NotNull FormatOption... options)
      Removes the specified formatting options from the formatter.
      Parameters:
      options - The formatting options to remove.
    • withForegroundColor

      public TextFormatter withForegroundColor(@Nullable @Nullable Color foreground)
      Sets the foreground color of the formatter.
      Parameters:
      foreground - The foreground color of the formatter.
    • withBackgroundColor

      public TextFormatter withBackgroundColor(@Nullable @Nullable Color background)
      Sets the background color of the formatter.
      Parameters:
      background - The background color of the formatter.
    • withColors

      public TextFormatter withColors(@Nullable @Nullable Color foreground, @Nullable @Nullable Color background)
      Sets the foreground and background color of the formatter.
      Parameters:
      foreground - The foreground color of the formatter.
      background - The background color of the formatter.
    • withContents

      public TextFormatter withContents(@NotNull @NotNull String contents)
      Sets the contents of the formatter.
      Parameters:
      contents - The contents of the formatter.
    • withConcatGap

      public TextFormatter withConcatGap(@Nullable @Nullable String gap)
      Sets the gap between concatenated formatters. By default, this is null, which means that no gap will be added.
      Parameters:
      gap - The gap between concatenated formatters.
    • concat

      public TextFormatter concat(@NotNull @NotNull Object... objects)
      Concatenates the specified values to the formatter. Another TextFormatter may also be concatenated.
      Parameters:
      objects - The values to concatenate.
    • isSimple

      public boolean isSimple()
      Returns whether the formatter is simple. A formatter is simple if it has no formatting options, no foreground color, no background color, and no concatenated formatters.
      Returns:
      true if the formatter is simple
    • isFormattingNotDefined

      public boolean isFormattingNotDefined()
      Returns whether the formatter has no formatting options, no foreground color, and no background color.
      Returns:
      true if the formatter has no formatting options, no foreground color, and no background color
    • toString

      @NotNull public @NotNull String toString()
      Creates a new String with the contents and all the formatting applied.
      Overrides:
      toString in class Object
    • getSequence

      @NotNull public static @NotNull String getSequence(@NotNull @NotNull Object... values)
      Returns a string with a terminal sequence with the specified values, separated by a semicolon. (e.g. "ESC[<values here>m")

      If debug is set to true, then the text "ESC" will be used instead of the actual escape character.

      If enableSequences is set to false, then an empty string will be returned.
      Parameters:
      values - The values to add to the terminal sequence.
      Returns:
      a string with a terminal sequence with the specified values
    • isColorDisabledEnv

      public static boolean isColorDisabledEnv()
      Returns whether there is an environment variable that specifies that the terminal does not support color. NO_COLOR.org
      Returns:
      true if the terminal supports color