Skip to content

Instantly share code, notes, and snippets.

@matanlurey
Created September 4, 2022 02:13
Show Gist options
  • Save matanlurey/7266c16b0d8f01d70691abea35c8c202 to your computer and use it in GitHub Desktop.
Save matanlurey/7266c16b0d8f01d70691abea35c8c202 to your computer and use it in GitHub Desktop.
A simple example of using Dart's "enhanced" enums for ANSI escape sequences
/// ANSI escape sequence constants.
///
/// See also:
/// - <https://vt100.net/docs/vt100-ug/chapter3.html>.
/// - <https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html>
library ansi;
/// Provides constants for 16-bit colors and styles using ANSI-escape sequences.
enum AnsiEscapes16Bit {
/// Resets all styled output after this escape sequence.
reset(0),
bold(1),
underline(4),
/// Reverses the set foreground and background colors.
reversed(7),
foregroundBlack(30),
foregroundRed(31),
foregroundGreen(32),
foregroundYellow(33),
foregroundBlue(34),
foregroundMagenta(35),
foregroundCyan(36),
foregroundWhite(37),
foregroundBrightBlack.bright(30),
foregroundBrightRed.bright(31),
foregroundBrightGreen.bright(32),
foregroundBrightYellow.bright(33),
foregroundBrightBlue.bright(34),
foregroundBrightMagenta.bright(35),
foregroundBrightCyan.bright(36),
foregroundBrightWhite.bright(37),
backgroundBlack(40),
backgroundRed(41),
backgroundGreen(42),
backgroundYellow(43),
backgroundBlue(44),
backgroundMagenta(45),
backgroundCyan(46),
backgroundWhite(47),
backgroundBrightBlack.bright(40),
backgroundBrightRed.bright(41),
backgroundBrightGreen.bright(42),
backgroundBrightYellow.bright(43),
backgroundBrightBlue.bright(44),
backgroundBrightMagenta.bright(45),
backgroundBrightCyan.bright(46),
backgroundBrightWhite.bright(47);
/// Escape sequence to be added as part of the output to apply this style.
final String value;
const AnsiEscapes16Bit(int value) : value = '$_ansiPrefix${value}m';
const AnsiEscapes16Bit.bright(int value) : value = '$_ansiPrefix$value;1m';
static const _ansiPrefix = '\u001b[';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment