An open API service indexing awesome lists of open source software.

https://github.com/chenasraf/terminal_color_parser_dart

Parse terminal colors for displaying in other formats. Supports ANSI and xterm256.
https://github.com/chenasraf/terminal_color_parser_dart

Last synced: 4 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# 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.

```dart
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.

```dart
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.