ansi_modifier 0.0.1 copy "ansi_modifier: ^0.0.1" to clipboard
ansi_modifier: ^0.0.1 copied to clipboard

Modify strings by adding, clearing, and replacing ANSI modifiers.

Ansi Modifier #

Dart

Introduction #

The package provides the class Ansi holding ANSI modifier codes and the String extension methods modify and removeAnsi for adding, replacing, and removing ANSI modifiers.

Usage #

Include ansi_modifier as a dependency in your pubspec.yaml file.

Use the String extension function modify to add new modifiers or to replace existing ones. Use the function removeAnsi to remove all Ansi modifier from a string.

import 'package:ansi_modifier/src/ansi.dart';

void main(List<String> args) {
  /// Create colorized strings.
  print('Create colorized strings:');
  final blue = 'blueberry'.modify(Ansi.blue + Ansi.italic);
  final green = 'green apple'.modify(Ansi.green);
  final blueGreen = blue +
      ' and ' +
      green.modify(
        Ansi.bold,
        method: Replace.none,
      );
  print('$blue, $green, $blueGreen');

  /// Modify a previously colorized string.
  print('\nModify previously colorized strings:');

  /// Replace first modifier:
  final yellowGreen = blueGreen.modify(Ansi.yellow + Ansi.bold + Ansi.underline,
      method: Replace.first);

  /// Replace all modifiers.
  final magenta =
      yellowGreen.modify(Ansi.magenta, method: Replace.clearPrevious);

  /// Strip all Ansi modifiers.
  print('$yellowGreen, $magenta, ${magenta.removeAnsi()}');
}

Runnig the program above:

$ dart example/bin/color_example.dart

produces the following output on a Visual Studio Code terminal:

Console Output

Tips and Tricks #

  • The String extension method modify supports different replacement modes that can be adjusted using the optional argument method.

  • Ansi codes can be combined using the addition operator Anis.red + Ansi.bold, or by using the factory constructor Ansi.combine.

Features and bugs #

If some Ansi modifiers are missing please file an enhancement request at the issue tracker.

1
likes
0
pub points
43%
popularity

Publisher

verified publishersimphotonics.com

Modify strings by adding, clearing, and replacing ANSI modifiers.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on ansi_modifier