Top Description Fields Constructors Methods
io.github.darvil.terminal.textformatter

public Class TextFormatter

extends Object
Class Inheritance
Imports
org.jetbrains.annotations.NotNull, .Nullable, io.github.darvil.terminal.textformatter.color.Color, .SimpleColor, io.github.darvil.utils.UtlString, java.util.ArrayList, .Arrays, .List

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

Modifier and TypeField and Description
private @Nullable Color
private @Nullable String
private final @NotNull List<Object>
concatList

A list of all the values that should be concatenated to this formatter.

private @NotNull String
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>]

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.

public static boolean
enableSequences

When set to false, no formatting will be applied to text.

private @Nullable Color
private final @NotNull ArrayList<FormatOption>
formatOptions

A list of all the formatting options that should be applied to the text.

private @Nullable TextFormatter
parent

The parent formatter.

public static boolean
startWithDefaultColorIfNotDefined

When set to true, the default color will be used when no foreground color is specified.

Constructor Summary

AccessConstructor and Description
protected
TextFormatter(@NotNull String
The contents of the formatter.
contents
)

Creates a new TextFormatter with the specified contents.

Method Summary

Modifier and TypeMethod and Description
public TextFormatter
addFormat(@NotNull FormatOption...
The formatting options to add.
options
)

Adds the specified formatting options to the formatter.

public TextFormatter
concat(@NotNull Object...
The values to concatenate.
objects
)

Concatenates the specified values to the formatter.

private void
concatFormatter(@NotNull TextFormatter
The formatter to concatenate.
formatter
)

Properly concatenates the specified formatter to this formatter by also linking both formatters together.

public static TextFormatter

Returns:

a new TextFormatter with empty contents
create
()

Creates a new TextFormatter with empty contents.

public static @NotNull TextFormatter

Returns:

a new TextFormatter with the specified contents and the error formatting
error
(@NotNull String
The contents of the formatter.
msg
)

Returns a new TextFormatter with the specified contents and the error formatting.

private @NotNull String

Returns:

the end sequences to add to the contents of the formatter
getEndSequences
()

Returns the end sequences to add to the contents of the formatter.

private @Nullable Color

Returns:

the Color that should properly reset the background color
getResetBgColor
()

Returns the Color that should properly reset the background color.

private @Nullable Color

Returns:

the Color that should properly reset the foreground color
getResetFgColor
()

Returns the Color that should properly reset the foreground color.

public static @NotNull String

Returns:

a string with a terminal sequence with the specified values
getSequence
(@NotNull Object...
The values to add to the terminal sequence.
values
)

Returns a string with a terminal sequence with the specified values, separated by a semicolon.

private @NotNull String

Returns:

the start sequences to add to the contents of the formatter
getStartSequences
()

Returns the start sequences to add to the contents of the formatter.

public static boolean

Returns:

true if the terminal supports color
isColorDisabledEnv
()

Returns whether there is an environment variable that specifies that the terminal does not support color.

public boolean

Returns:

true if the formatter has no formatting options, no foreground color, and no background color
isFormattingNotDefined
()

Returns whether the formatter has no formatting options, no foreground color, and no background color.

public boolean

Returns:

true if the formatter is simple
isSimple
()

Returns whether the formatter is simple.

public static TextFormatter
of(@NotNull String
The contents of the formatter.
contents
)

Creates a new TextFormatter with the specified contents.

public static TextFormatter
of(@NotNull String
The contents of the formatter.
contents
,
@NotNull Color
The foreground color of the formatter.
foreground
)

Creates a new TextFormatter with the specified contents and foreground color.

public static TextFormatter
of(@NotNull String
The contents of the formatter.
contents
,
@NotNull Color
The foreground color of the formatter.
foreground
,
@NotNull Color
The background color of the formatter.
background
)

Creates a new TextFormatter with the specified contents and colors.

public TextFormatter
removeFormat(@NotNull FormatOption...
The formatting options to remove.
options
)

Removes the specified formatting options from the formatter.

public @NotNull String
toString()

Overrides java.lang.Object.toString.

Creates a new String with the contents and all the formatting applied.

public TextFormatter
withBackgroundColor(@Nullable Color
The background color of the formatter.
background
)

Sets the background color of the formatter.

public TextFormatter
withColors(@Nullable Color
The foreground color of the formatter.
foreground
,
@Nullable Color
The background color of the formatter.
background
)

Sets the foreground and background color of the formatter.

public TextFormatter
withConcatGap(@Nullable String
The gap between concatenated formatters.
gap
)

Sets the gap between concatenated formatters.

public TextFormatter
withContents(@NotNull String
The contents of the formatter.
contents
)

Sets the contents of the formatter.

public TextFormatter
withForegroundColor(@Nullable Color
The foreground color of the formatter.
foreground
)

Sets the foreground color of the formatter.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

backgroundColorback to summary
private @Nullable Color backgroundColor
Annotations
@Nullable
concatGapback to summary
private @Nullable String concatGap
Annotations
@Nullable
concatListback to summary
private final @NotNull List<Object> concatList

A list of all the values that should be concatenated to this formatter.

Annotations
@NotNull
contentsback to summary
private @NotNull String contents
Annotations
@NotNull
debugback to summary
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>]

defaultColorback to summary
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.

Annotations
@NotNull
enableSequencesback to summary
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
isColorDisabledEnv()
foregroundColorback to summary
private @Nullable Color foregroundColor
Annotations
@Nullable
formatOptionsback to summary
private final @NotNull ArrayList<FormatOption> formatOptions

A list of all the formatting options that should be applied to the text.

Annotations
@NotNull
parentback to summary
private @Nullable TextFormatter parent

The parent formatter. Used when being concatenated to another formatter.

Annotations
@Nullable
startWithDefaultColorIfNotDefinedback to summary
public static boolean startWithDefaultColorIfNotDefined

When set to true, the default color will be used when no foreground color is specified.

Constructor Detail

TextFormatterback to summary
protected TextFormatter(@NotNull String contents)

Creates a new TextFormatter with the specified contents.

Parameters
contents:@NotNull String

The contents of the formatter.

Method Detail

addFormatback to summary
public TextFormatter addFormat(@NotNull FormatOption... options)

Adds the specified formatting options to the formatter.

Parameters
options:@NotNull FormatOption[]

The formatting options to add.

concatback to summary
public TextFormatter concat(@NotNull Object... objects)

Concatenates the specified values to the formatter. Another TextFormatter may also be concatenated.

Parameters
objects:@NotNull Object[]

The values to concatenate.

concatFormatterback to summary
private void concatFormatter(@NotNull TextFormatter formatter)

Properly concatenates the specified formatter to this formatter by also linking both formatters together.

Parameters
formatter:@NotNull TextFormatter

The formatter to concatenate.

createback to summary
public static TextFormatter create()

Creates a new TextFormatter with empty contents.

Returns:TextFormatter

a new TextFormatter with empty contents

errorback to summary
public static @NotNull TextFormatter error(@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:@NotNull String

The contents of the formatter.

Returns:@NotNull TextFormatter

a new TextFormatter with the specified contents and the error formatting

Annotations
@NotNull
getEndSequencesback to summary
private @NotNull String getEndSequences()

Returns the end sequences to add to the contents of the formatter. This should properly reset the foreground color, the background color, and all the formatting options.

Returns:@NotNull String

the end sequences to add to the contents of the formatter

Annotations
@NotNull
getResetBgColorback to summary
private @Nullable Color getResetBgColor()

Returns the Color that should properly reset the background color. This is determined by looking at the parent formatters. If no parent formatter has a background color, then null is returned.

Returns:@Nullable Color

the Color that should properly reset the background color

Annotations
@Nullable
getResetFgColorback to summary
private @Nullable Color getResetFgColor()

Returns the Color that should properly reset the foreground color. This is determined by looking at the parent formatters. If no parent formatter has a foreground color, then SimpleColor#BRIGHT_WHITE is returned.

Returns:@Nullable Color

the Color that should properly reset the foreground color

Annotations
@Nullable
getSequenceback to summary
public static @NotNull String getSequence(@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:@NotNull Object[]

The values to add to the terminal sequence.

Returns:@NotNull String

a string with a terminal sequence with the specified values

Annotations
@NotNull
getStartSequencesback to summary
private @NotNull String getStartSequences()

Returns the start sequences to add to the contents of the formatter. This includes the foreground color, the background color, and all the formatting options.

Returns:@NotNull String

the start sequences to add to the contents of the formatter

Annotations
@NotNull
isColorDisabledEnvback to summary
public static boolean isColorDisabledEnv()

Returns whether there is an environment variable that specifies that the terminal does not support color. NO_COLOR.org

Returns:boolean

true if the terminal supports color

isFormattingNotDefinedback to summary
public boolean isFormattingNotDefined()

Returns whether the formatter has no formatting options, no foreground color, and no background color.

Returns:boolean

true if the formatter has no formatting options, no foreground color, and no background color

isSimpleback to summary
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:boolean

true if the formatter is simple

ofback to summary
public static TextFormatter of(@NotNull String contents)

Creates a new TextFormatter with the specified contents.

Parameters
contents:@NotNull String

The contents of the formatter.

ofback to summary
public static TextFormatter of(@NotNull String contents, @NotNull Color foreground)

Creates a new TextFormatter with the specified contents and foreground color.

Parameters
contents:@NotNull String

The contents of the formatter.

foreground:@NotNull Color

The foreground color of the formatter.

ofback to summary
public static TextFormatter of(@NotNull String contents, @NotNull Color foreground, @NotNull Color background)

Creates a new TextFormatter with the specified contents and colors.

Parameters
contents:@NotNull String

The contents of the formatter.

foreground:@NotNull Color

The foreground color of the formatter.

background:@NotNull Color

The background color of the formatter.

removeFormatback to summary
public TextFormatter removeFormat(@NotNull FormatOption... options)

Removes the specified formatting options from the formatter.

Parameters
options:@NotNull FormatOption[]

The formatting options to remove.

toStringback to summary
public @NotNull String toString()

Overrides java.lang.Object.toString.

Creates a new String with the contents and all the formatting applied.

Annotations
@Override
@NotNull
withBackgroundColorback to summary
public TextFormatter withBackgroundColor(@Nullable Color background)

Sets the background color of the formatter.

Parameters
background:@Nullable Color

The background color of the formatter.

withColorsback to summary
public TextFormatter withColors(@Nullable Color foreground, @Nullable Color background)

Sets the foreground and background color of the formatter.

Parameters
foreground:@Nullable Color

The foreground color of the formatter.

background:@Nullable Color

The background color of the formatter.

withConcatGapback to summary
public TextFormatter withConcatGap(@Nullable String gap)

Sets the gap between concatenated formatters. By default, this is null, which means that no gap will be added.

Parameters
gap:@Nullable String

The gap between concatenated formatters.

withContentsback to summary
public TextFormatter withContents(@NotNull String contents)

Sets the contents of the formatter.

Parameters
contents:@NotNull String

The contents of the formatter.

withForegroundColorback to summary
public TextFormatter withForegroundColor(@Nullable Color foreground)

Sets the foreground color of the formatter.

Parameters
foreground:@Nullable Color

The foreground color of the formatter.