ansi_text 1.2.0 copy "ansi_text: ^1.2.0" to clipboard
ansi_text: ^1.2.0 copied to clipboard

A library for styling and formatting terminal text using the ANSI escape codes.

example/example.dart

import 'dart:io';

import 'package:ansi_text/ansi_text.dart';

void main() {
  // disable markup and colors if the terminal does not support it
  AnsiText.enabled = stdout.hasTerminal && stdout.supportsAnsiEscapes;

  stdout
    ..writeln(AnsiText('AnsiText example page')
      ..boldUnderline()
      ..hyperlink(Uri.parse('https://github.com/drillster/ansi_text')))
    ..writeln();
  _printMarkupExamples();
  _printStandardColorExamples();
  _printBrightColorExamples();
  _printEightBitColorExamples();
  _printEightBitGrayscaleExamples();
  _printTrueColorExamples();
}

void _printMarkupExamples() {
  stdout
    ..writeln('Text markup:')
    ..write(' - ')
    ..writeln(AnsiText('Bold')..bold())
    ..write(' - ')
    ..writeln(AnsiText('Faint')..faint())
    ..write(' - ')
    ..writeln(AnsiText('Italic')..italic())
    ..write(' - ')
    ..writeln(AnsiText('Bold italic')..boldItalic())
    ..write(' - ')
    ..writeln(AnsiText('Underline')..underline())
    ..write(' - ')
    ..writeln(AnsiText('Bold underline')..boldUnderline())
    ..write(' - ')
    ..writeln(AnsiText('Blinking')..blink())
    ..write(' - ')
    ..writeln(AnsiText('Inverted')..inverted())
    ..write(' - ')
    ..write(AnsiText('Concealed')..concealed())
    ..writeln(' (concealed)')
    ..write(' - ')
    ..writeln(AnsiText('Strikethrough')..strikethrough())
    ..writeln();
}

void _printStandardColorExamples() {
  stdout
    ..writeln('Standard colors:')
    ..write(AnsiText(' Black   ')
      ..bold()
      ..bgBlack()
      ..brightWhite())
    ..write('  ')
    ..write(AnsiText(' Red     ')
      ..bold()
      ..bgRed()
      ..brightWhite())
    ..write('  ')
    ..write(AnsiText(' Green   ')
      ..bold()
      ..bgGreen()
      ..black())
    ..write('  ')
    ..write(AnsiText(' Yellow  ')
      ..bold()
      ..bgYellow()
      ..black())
    ..write('  ')
    ..write(AnsiText(' Blue    ')
      ..bold()
      ..bgBlue()
      ..black())
    ..write('  ')
    ..write(AnsiText(' Magenta ')
      ..bold()
      ..bgMagenta()
      ..white())
    ..write('  ')
    ..write(AnsiText(' Cyan    ')
      ..bold()
      ..bgCyan()
      ..black())
    ..write('  ')
    ..write(AnsiText(' White   ')
      ..bold()
      ..bgWhite()
      ..black())
    ..writeln()
    ..writeln();
}

void _printBrightColorExamples() {
  stdout
    ..writeln('Bright colors:')
    ..write(AnsiText(' Black   ')
      ..bold()
      ..bgBrightBlack()
      ..brightWhite())
    ..write('  ')
    ..write(AnsiText(' Red     ')
      ..bold()
      ..bgBrightRed()
      ..brightWhite())
    ..write('  ')
    ..write(AnsiText(' Green   ')
      ..bold()
      ..bgBrightGreen()
      ..black())
    ..write('  ')
    ..write(AnsiText(' Yellow  ')
      ..bold()
      ..bgBrightYellow()
      ..black())
    ..write('  ')
    ..write(AnsiText(' Blue    ')
      ..bold()
      ..bgBrightBlue()
      ..black())
    ..write('  ')
    ..write(AnsiText(' Magenta ')
      ..bold()
      ..bgBrightMagenta()
      ..white())
    ..write('  ')
    ..write(AnsiText(' Cyan    ')
      ..bold()
      ..bgBrightCyan()
      ..black())
    ..write('  ')
    ..write(AnsiText(' White   ')
      ..bold()
      ..bgBrightWhite()
      ..black())
    ..writeln()
    ..writeln();
}

void _printEightBitColorExamples() {
  stdout.writeln('216 RGB colors:');
  for (var r = 0; r <= 5; r++) {
    for (var g = 0; g <= 5; g++) {
      for (var b = 0; b <= 5; b++) {
        stdout
          ..write(AnsiText(' Color $r $g $b ')
            ..apply(Styles.color.background.rgb(r, g, b))
            ..apply(Styles.color.text.rgb(5 - r, 5 - g, 5 - b))
            ..bold())
          ..write('  ');
      }
      stdout.writeln();
    }
  }
  stdout.writeln();
}

void _printEightBitGrayscaleExamples() {
  stdout.writeln('Grayscale values:');
  for (var i = 0; i <= 23; i++) {
    stdout
      ..write(AnsiText(' Gray $i ${i < 10 ? ' ' : ''}')
        ..apply(Styles.color.background.gray(i))
        ..apply(Styles.color.text.gray(23 - i))
        ..bold())
      ..write('  ');
    if ((i + 1) % 6 == 0) {
      stdout.writeln();
    }
  }
  stdout.writeln();
}

void _printTrueColorExamples() {
  stdout.writeln('24-bit true color:');
  for (var mask = 1; mask < 8; mask++) {
    for (var i = 0; i < 256; i += 3) {
      stdout.write(
        AnsiText(' ')
          ..apply(
            Styles.color.background.trueColor(
              (mask & 4) != 0 ? i : 0,
              (mask & 2) != 0 ? i : 0,
              (mask & 1) != 0 ? i : 0,
            ),
          ),
      );
    }
    stdout.writeln();
  }
  stdout.writeln();
}
3
likes
160
points
286
downloads

Publisher

verified publisherdrillster.dev

Weekly Downloads

A library for styling and formatting terminal text using the ANSI escape codes.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on ansi_text