Skip to content

Commit

Permalink
Converted Ansi to const class.
Browse files Browse the repository at this point in the history
  • Loading branch information
simphotonics committed Sep 8, 2023
1 parent 13e907b commit e1fd78b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 63 deletions.
30 changes: 12 additions & 18 deletions example/bin/color_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@ import 'package:ansi_modifier/src/ansi.dart';
void main(List<String> args) {
/// Create colorized strings.
print('Create colorized strings:'.modify(Ansi.bold));
final blue = 'A string.'.modify(Ansi.blue);
print('A blue string:');
print(blue);

print('\nA green string:');
final green = 'Another string'.modify(Ansi.green);
print(green);

print('\nA blue green string:');
final blueGreen = blue + ' ' + green;
print(blueGreen);
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:'.modify(Ansi.bold));

/// Replace first modifier:
final yellowGreen = blueGreen.modify(Ansi.yellow, method: Replace.first);
print('A yellow green string:');
print(yellowGreen);
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);
print('\nA magenta string:');
print(magenta);

/// Strip all Ansi modifiers.
print('\nA standard string:');
print(magenta.stripAnsi());
print('$yellowGreen, $magenta, ${magenta.stripAnsi()}');
}
Binary file modified images/console_output.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 92 additions & 45 deletions lib/src/ansi.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// Left Ansi escape sequence
const _escLeft = '\u001B[';
const escLeft = '\u001B[';

const escRight = 'm';

/// Ansi reset sequence
const resetSeq = '\u001B[0m';
Expand Down Expand Up @@ -28,78 +30,123 @@ enum Replace {
/// Enumeration representing Ansi modifiers.
///
/// Used to print custom fonts and colorized output to Ansi compliant terminals.
enum Ansi {
class Ansi {
/// Ansi modifier: Reset to default.
reset(resetSeq),
static final reset = Ansi('0');

/// Ansi color modifier: red foreground.
red('\u001B[31m'),
/// Ansi modifier bold forground text.
static final bold = Ansi('1');

/// Ansi color modifier: bright red foreground.
redBright('\u001B[91m'),
/// Ansi modifier italic foreground text.
static final italic = Ansi('3');

/// Ansi color modifier: green foreground.
green('\u001B[32m'),
/// Ansi mofifiers underlined foreground text.
static final underline = Ansi('4');

/// Ansi color modifier: bright green foreground.
greenBright('\u001B[92m'),
/// Ansi modifier crossed out foreground text.
static final crossedOut = Ansi('9');

/// Ansi color modifier: yellow foreground.
yellow('\u001B[33m'),
/// Ansi color modifier: black foreground.
static final black = Ansi('30');

/// Ansi color modifier: bright yellow foreground.
yellowBright('\u001B[93m'),
/// Ansi color modifier: red foreground.
static final red = Ansi('31');

/// Ansi color modifier: blue foreground.
blue('\u001B[34m'),
/// Ansi color modifier: green foreground.
static final green = Ansi('32');

/// Ansi color modifier: bright blue foreground.
blueBright('\u001B[94m'),
/// Ansi color modifier: yellow foreground.
static final yellow = Ansi('33');

/// Ansi color modifier: magenta foreground.
magenta('\u001B[35m'),
/// Ansi color modifier: blue foreground.
static final blue = Ansi('34');

/// Ansi color modifier: magenta foreground.
magentaBright('\u001B[95m'),
static final magenta = Ansi('35');

/// Ansi color modifier: magenta bold foreground.
magentaBold('\u001B[1;35m'),
static final magentaBold = Ansi('1;35');

/// Ansi color modifier: cyan foreground.
cyan('\u001B[36m'),
static final cyan = Ansi('36');

/// Ansi color modifier: cyan bold text.
cyanBold('\u001B[1;36m'),
static final cyanBold = Ansi('1;36');

/// Ansi color modifier: grey foreground
grey('\u001B[2;37m'),
static final grey = Ansi('2;37');

/// Ansi color modifier: grey bold foreground
greyBold('\u001B[1;90m'),
/// Ansi color modifier: black background
static final blackBg = Ansi('40');

/// Ansi color modifier: white bold foreground
whiteBold('\u001B[1;97m'),
/// Ansi color modifier: red backgroound
static final redBg = Ansi('41');

/// Ansi modifier bold forground text.
bold('\u001B[1m'),
/// Ansi color modifier: green background
static final greenBg = Ansi('42');

/// Ansi modifier italic foreground text.
italic('\u001B[3m'),
/// Ansi color modifier: yellow background
static final yellowBg = Ansi('43');

/// Ansi modifier crossed out foreground text.
crossedOut('\u001B[9m');
/// Ansi color modifier: blue background
static final blueBg = Ansi('44');

/// Ansi color modifier: magenta background
static final magentaBg = Ansi('45');

const Ansi(this.code);
/// Ansi color modifier: cyan background
static final cyanBg = Ansi('46');

/// Returns the Ansi code.
/// Ansi color modifier: white background
static final whiteBg = Ansi('47');

/// Ansi color modifier: bright red foreground.
static final redBright = Ansi('91');

/// Ansi color modifier: bright green foreground.
static final greenBright = Ansi('92');

/// Ansi color modifier: bright yellow foreground.
static final yellowBright = Ansi('93');

/// Ansi color modifier: bright blue foreground.
static final blueBright = Ansi('94');

/// Ansi color modifier: bright magenta foreground.
static final magentaBright = Ansi('95');

/// Ansi color modifier: grey bold foreground
static final greyBold = Ansi('1;90');

/// Ansi color modifier: white bold foreground
static final whiteBold = Ansi('1;97');

const Ansi(this.bareCode) : code = escLeft + bareCode + escRight;

/// Factory constructor combining several Ansi modifiers.
factory Ansi.combine(Set<Ansi> modifiers) {
return Ansi(modifiers.map<String>((element) => element.bareCode).join(';'));
}

/// Returns the escaped Ansi code.
/// ```
/// print(Ansi.red.code); // Prints \u001B[31m
/// ```
final String code;

/// Returns a set of all registered Ansi modifiers.
static final Set<String> modifiers = Ansi.values
.map(
(e) => e.code,
)
.toSet();
/// Returns the bare Ansi code.
/// ```
/// print(Ansi.red.bareCode); // Prints 31
/// ```
final String bareCode;

/// Creates a new Ansi object by joining the bare Ansi codes separated by a
/// semicolon character.
Ansi operator +(Ansi other) => this == other
? this
: Ansi(
bareCode + ';' + other.bareCode,
);

@override
String toString() {
Expand Down Expand Up @@ -136,7 +183,7 @@ extension AnsiModifier on String {
(AnsiOutput.disabled, _) => this,
(AnsiOutput.enabled, Replace.first) =>
replaceFirst(matchAnsi, modifier.code),
(AnsiOutput.enabled, Replace.starting) => startsWith(_escLeft)
(AnsiOutput.enabled, Replace.starting) => startsWith(escLeft)
? replaceFirst(
matchAnsi,
modifier.code,
Expand Down

0 comments on commit e1fd78b

Please sign in to comment.