Ansi Modifier



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


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

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

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

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

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

  // Create custom Ansi modifier.
  final customModifier = Ansi.combine({Ansi.yellow, Ansi.bold, Ansi.underline});

  // Replace first modifier:
  final yellowGreen =, method: Replace.first);

  // Replace all modifiers.
  final magenta =, method: Replace.clearPrevious);

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

Runnig the program above:

$ dart example/bin/color_example.dart

produces the following output:

Console Output

Tips and Tricks

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

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

  • Ansi output can be globally disabled by setting Ansi.status = AnsiOutput.disabled or by using the option:

    $ dart --define=isMonochrome=true example/bin/color_example.dart

Features and bugs

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