terminal_color_parser 0.8.0 copy "terminal_color_parser: ^0.8.0" to clipboard
terminal_color_parser: ^0.8.0 copied to clipboard

Parse terminal colors for displaying in other formats. Supports ANSI and xterm256.

example/example.dart

import 'package:terminal_color_parser/src/color.dart';
import 'package:terminal_color_parser/terminal_color_parser.dart';

void main(List<String> args) async {
  final coloredText = ColorParser('Hello, \x1B[32mworld\x1B[0m!').parse();

  print(coloredText);
  // ==> ColorToken("Hello, ", 0:0, , ()), ColorToken("world", 32:0, , ()), ColorToken("!", 0:0, , ())]

  var i = 0;
  for (final token in coloredText) {
    dbg(i, token);
    i++;
  }

  print('');
  print('');

  // Construct your own colored text
  final tokens = [
    ColorToken(text: 'Hello, '),
    ColorToken(
      text: 'world',
      fgColor: Color.fg(32),
      styles: {TermStyle.underline,TermStyle.reset},
    ),
    ColorToken(text: '!', styles: {TermStyle.bold}),
  ];

  i = 0;
  for (final token in tokens) {
    dbg(i, token);
    i++;
  }

  print('');
  print(tokens.map((t) => t.formatted).join(''));
}

void dbg(int i, ColorToken token) {
  print('Token #$i: ${token.formatted}\x1B[0m');
  print('  - Text: ${token.text}');
  print('  - Foreground: ${token.fgColor}');
  print('  - Background: ${token.bgColor}');
  print('  - Bold: ${token.bold}');
  print('  - Italic: ${token.italic}');
  print('  - Underline: ${token.underline}');
  print('  - Styles: ${token.styles}');
}
copied to clipboard
2
likes
150
points
516
downloads

Publisher

verified publishercasraf.dev

Weekly Downloads

2024.09.15 - 2025.03.30

Parse terminal colors for displaying in other formats. Supports ANSI and xterm256.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

More

Packages that depend on terminal_color_parser