Class TextFormatter
java.lang.Object
io.github.darvil.terminal.textformatter.TextFormatter
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 Summary
FieldsModifier and TypeFieldDescriptionstatic booleanWhen set totrue, thetoString()method will not add any terminal sequences, but rather return the sequences that would be added by marking them asESC[<sequence here>]static @NotNull ColorThe default color that should be used when no foreground color is specified (ifstartWithDefaultColorIfNotDefinedis set totrue), or when the foreground color is reset.static booleanWhen set tofalse, no formatting will be applied to text.static booleanWhen set totrue, the default color will be used when no foreground color is specified. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTextFormatter(@NotNull String contents) Creates a newTextFormatterwith the specified contents. -
Method Summary
Modifier and TypeMethodDescriptionaddFormat(@NotNull FormatOption... options) Adds the specified formatting options to the formatter.Concatenates the specified values to the formatter.static TextFormattercreate()Creates a newTextFormatterwith empty contents.static @NotNull TextFormatterReturns a newTextFormatterwith the specified contents and the error formatting.static @NotNull StringgetSequence(@NotNull Object... values) Returns a string with a terminal sequence with the specified values, separated by a semicolon.static booleanReturns whether there is an environment variable that specifies that the terminal does not support color.booleanReturns whether the formatter has no formatting options, no foreground color, and no background color.booleanisSimple()Returns whether the formatter is simple.static TextFormatterCreates a newTextFormatterwith the specified contents.static TextFormatterCreates a newTextFormatterwith the specified contents and foreground color.static TextFormatterCreates a newTextFormatterwith the specified contents and colors.removeFormat(@NotNull FormatOption... options) Removes the specified formatting options from the formatter.@NotNull StringtoString()Creates a newStringwith the contents and all the formatting applied.withBackgroundColor(@Nullable Color background) Sets the background color of the formatter.withColors(@Nullable Color foreground, @Nullable Color background) Sets the foreground and background color of the formatter.withConcatGap(@Nullable String gap) Sets the gap between concatenated formatters.withContents(@NotNull String contents) Sets the contents of the formatter.withForegroundColor(@Nullable Color foreground) Sets the foreground color of the formatter.
-
Field Details
-
enableSequences
public static boolean enableSequencesWhen set tofalse, no formatting will be applied to text. Raw text will be generated without any color or formatting.This will be set to
falseif the environment variableNO_COLORis set.- See Also:
-
defaultColor
The default color that should be used when no foreground color is specified (ifstartWithDefaultColorIfNotDefinedis set totrue), or when the foreground color is reset. -
startWithDefaultColorIfNotDefined
public static boolean startWithDefaultColorIfNotDefinedWhen set totrue, the default color will be used when no foreground color is specified. -
debug
public static boolean debugWhen set totrue, thetoString()method will not add any terminal sequences, but rather return the sequences that would be added by marking them asESC[<sequence here>]
-
-
Constructor Details
-
TextFormatter
Creates a newTextFormatterwith the specified contents.- Parameters:
contents- The contents of the formatter.
-
-
Method Details
-
of
Creates a newTextFormatterwith 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 newTextFormatterwith 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 newTextFormatterwith 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
Creates a newTextFormatterwith empty contents.- Returns:
- a new
TextFormatterwith empty contents
-
error
Returns a newTextFormatterwith 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
TextFormatterwith the specified contents and the error formatting
-
addFormat
Adds the specified formatting options to the formatter.- Parameters:
options- The formatting options to add.
-
removeFormat
Removes the specified formatting options from the formatter.- Parameters:
options- The formatting options to remove.
-
withForegroundColor
Sets the foreground color of the formatter.- Parameters:
foreground- The foreground color of the formatter.
-
withBackgroundColor
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
Sets the contents of the formatter.- Parameters:
contents- The contents of the formatter.
-
withConcatGap
Sets the gap between concatenated formatters. By default, this isnull, which means that no gap will be added.- Parameters:
gap- The gap between concatenated formatters.
-
concat
Concatenates the specified values to the formatter. AnotherTextFormattermay 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:
trueif the formatter is simple
-
isFormattingNotDefined
public boolean isFormattingNotDefined()Returns whether the formatter has no formatting options, no foreground color, and no background color.- Returns:
trueif the formatter has no formatting options, no foreground color, and no background color
-
toString
Creates a newStringwith the contents and all the formatting applied. -
getSequence
Returns a string with a terminal sequence with the specified values, separated by a semicolon. (e.g."ESC[<values here>m")If
Ifdebugis set totrue, then the text "ESC" will be used instead of the actual escape character.enableSequencesis set tofalse, 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:
trueif the terminal supports color
-