AnsiColor class final
A utility class to apply ANSI terminal color codes to strings.
This is useful for enhancing log output or command-line interfaces with colored messages for better readability and severity-level distinction.
It supports both foreground and background color settings using ANSI 256-color codes.
Example usage:
final color = AnsiColor.fg(34); // Bright blue text
print(color('This text is blue'));
Constructors
- AnsiColor({int? bg, int? fg, bool color = false})
-
A utility class to apply ANSI terminal color codes to strings.
const
- AnsiColor.bg(int? bg)
-
A utility class to apply ANSI terminal color codes to strings.
const
- AnsiColor.fg(int? fg)
-
A utility class to apply ANSI terminal color codes to strings.
const
- AnsiColor.none()
-
A utility class to apply ANSI terminal color codes to strings.
const
Properties
- bg → int?
-
Background color, represented by a 256-color ANSI code.
final
- color → bool
-
Whether colorization is enabled.
final
- fg → int?
-
Foreground color (text color), represented by a 256-color ANSI code.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- resetBackground → String
-
ANSI escape sequence to reset the background color to default.
no setter
- resetForeground → String
-
ANSI escape sequence to reset the foreground color to default.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
String msg) → String -
Applies the ANSI color codes to the provided
msg
and resets formatting after. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toBg(
) → AnsiColor - Converts this instance to a background-only AnsiColor using the foreground color.
-
toFg(
) → AnsiColor - Converts this instance to a foreground-only AnsiColor using the background color.
-
toString(
) → String -
Returns the ANSI escape code string that applies this color configuration.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
Constants
- ansiDefault → const String
- ANSI sequence to reset all styles, colors, and attributes to default.
- ansiEsc → const String
- The ANSI escape sequence introducer. Signals the terminal that a control sequence follows.
- BG_BLACK → const AnsiColor
- Background: Standard black (ANSI code 0).
- BG_BLUE → const AnsiColor
- Background: Standard blue (ANSI code 4).
- BG_BRIGHT_BLACK → const AnsiColor
- Background: Bright black (dark gray, ANSI code 8).
- BG_BRIGHT_BLUE → const AnsiColor
- Background: Bright blue (ANSI code 12).
- BG_BRIGHT_CYAN → const AnsiColor
- Background: Bright cyan (ANSI code 14).
- BG_BRIGHT_GREEN → const AnsiColor
- Background: Bright green (ANSI code 10).
- BG_BRIGHT_MAGENTA → const AnsiColor
- Background: Bright magenta (ANSI code 13).
- BG_BRIGHT_RED → const AnsiColor
- Background: Bright red (ANSI code 9).
- BG_BRIGHT_WHITE → const AnsiColor
- Background: Bright white (ANSI code 15).
- BG_BRIGHT_YELLOW → const AnsiColor
- Background: Bright yellow (ANSI code 11).
- BG_CYAN → const AnsiColor
- Background: Standard cyan (ANSI code 6).
- BG_GREEN → const AnsiColor
- Background: Standard green (ANSI code 2).
- BG_MAGENTA → const AnsiColor
- Background: Standard magenta (ANSI code 5).
- BG_RED → const AnsiColor
- Background: Standard red (ANSI code 1).
- BG_WHITE → const AnsiColor
- Background: Standard white (ANSI code 7).
- BG_YELLOW → const AnsiColor
- Background: Standard yellow (ANSI code 3).
- BLACK → const AnsiColor
- Foreground: Standard black (ANSI code 0).
- BLUE → const AnsiColor
- Foreground: Bright blue (ANSI code 12).
- CYAN → const AnsiColor
- Foreground: Bright cyan (ANSI code 14).
- DARK_BLUE → const AnsiColor
- Foreground: Standard blue (ANSI code 4).
- DARK_CYAN → const AnsiColor
- Foreground: Standard cyan (ANSI code 6).
- DARK_GRAY → const AnsiColor
- Foreground: Bright black (dark gray, ANSI code 8).
- DARK_GREEN → const AnsiColor
- Foreground: Standard green (ANSI code 2).
- DARK_MAGENTA → const AnsiColor
- Foreground: Standard magenta (ANSI code 5).
- DARK_RED → const AnsiColor
- Foreground: Standard red (ANSI code 1).
- DARK_YELLOW → const AnsiColor
- Foreground: Standard yellow (ANSI code 3).
- GRAY → const AnsiColor
- Foreground: Standard white (ANSI code 7).
- GREEN → const AnsiColor
- Foreground: Bright green (ANSI code 10).
- MAGENTA → const AnsiColor
- Foreground: Bright magenta (ANSI code 13).
- RED → const AnsiColor
- Foreground: Bright red (ANSI code 9).
- WHITE → const AnsiColor
- Foreground: Bright white (ANSI code 15).
- YELLOW → const AnsiColor
- Foreground: Bright yellow (ANSI code 11).