Parse terminal colors for displaying in other formats. Supports ANSI and xterm256.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-01-11 01:09:47 +02:00
.github Create dart.yml 2024-07-28 00:06:31 +03:00
example chore(release): version 0.8.0 2025-01-11 01:09:47 +02:00
lib refactor: color methods, parser clean ups 2025-01-11 01:09:47 +02:00
test feat!: add color classes 2025-01-11 00:05:27 +02:00
.editorconfig feat: initial commit 2024-04-09 14:30:51 +03:00
.eslintignore feat: initial commit 2024-04-09 14:30:51 +03:00
.gitignore feat: initial commit 2024-04-09 14:30:51 +03:00
.prettierignore feat: initial commit 2024-04-09 14:30:51 +03:00
.prettierrc feat: initial commit 2024-04-09 14:30:51 +03:00
analysis_options.yaml feat: initial commit 2024-04-09 14:30:51 +03:00
CHANGELOG.md chore(release): version 0.8.0 2025-01-11 01:09:47 +02:00
LICENSE feat: initial commit 2024-04-09 14:30:51 +03:00
pubspec.yaml chore(release): version 0.8.0 2025-01-11 01:09:47 +02:00
README.md chore(release): version 0.8.0 2025-01-11 01:09:47 +02:00

Terminal Color Parser

This package is an ANSI/xterm256 color parser. You can get the list of colored segments by creating a ColorParser instance and calling parse() method.

import 'package:terminal_color_parser/terminal_color_parser.dart';

final coloredText = ColorParser('Hello, \x1B[32mworld\x1B[0m!').parse();

var i = 0;
for (final token in coloredText) {
  print('Token #$i: ${token.formatted}');
  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}');
  i++;
}

You can also re-format the ANSI codes by using the formatted property on each token.

final tokens = [
  ColorToken(text: 'Hello, '),
  ColorToken(
    text: 'world',
    fgColor: ANSIColor.fg(32), // Can also use RGBColor.fg(r, g, b)
    bgColor: Color.none,
    styles: {StyleByte.underline},
  ),
  ColorToken(text: '!'),
];

var i = 0;
for (final token in coloredText) {
  print('Token #$i: ${token.formatted}');
  print('  - Text: ${token.text}');
  print('  - Foreground: ${token.fgColor.formatted}');
  print('  - Background: ${token.bgColor.formatted}');
  print('  - Bold: ${token.bold}');
  print('  - Italic: ${token.italic}');
  print('  - Underline: ${token.underline}');
  print('  - Styles: ${token.styles}');
  i++;
}

Contributing

I am developing this package on my free time, so any support, whether code, issues, or just stars is very helpful to sustaining its life. If you are feeling incredibly generous and would like to donate just a small amount to help sustain this project, I would be very very thankful!

Buy Me a Coffee at ko-fi.com

I welcome any issues or pull requests on GitHub. If you find a bug, or would like a new feature, don't hesitate to open an appropriate issue and I will do my best to reply promptly.