Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
simphotonics committed Sep 8, 2023
1 parent e1fd78b commit 1163d58
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 86 deletions.
60 changes: 27 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,51 @@

## Introduction

The package provides an enhanced enum holding ANSI modifier codes and
The package provides the class [`Ansi`][Ansi] holding ANSI modifier codes and
String extension methods for adding, replacing, and removing ANSI modifiers.


## Usage

Include [`ansi_modifier`][ansi_modifier] as a dependency
in your `pubspec.yaml` file.

Modify strings by adding, replacing, or clearing existing ANSI modifiers using
the String
extension functions [`modify()`][modify()] and [`clearAnsi()`][clearAnsi()].
Use the String extension functions [`modify`][modify] to add new modifiers or
to replace existing ones. The replacement method can be adjusted by setting the
optional argument `method`.

Use the function [`stripAnsi`][stripAnsi] to remove
all Ansi modifier from a string.

```Dart
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);
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:'.modify(Ansi.bold));
print('\nModify previously colorized strings:');
/// 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()}');
}
```

Expand Down Expand Up @@ -103,16 +101,12 @@ more difficult to read (grouped) benchmark results.

Please file feature requests and bugs at the [issue tracker][tracker].

[tracker]: https://github.com/simphotonics/benchmark_runner/issues

[benchmark_harness]: https://pub.dev/packages/benchmark_harness

[benchmark_runner]: https://pub.dev/packages/benchmark_runner
[tracker]: https://github.com/simphotonics/ansi_modifier/issues

[asyncBenchmark]: https://pub.dev/documentation/benchmark_runner/doc/api/benchmark_runner/asyncBenchmark.html
[ansi_modifier]: https://pub.dev/packages/ansi_modifier

[asyncGroup]: https://pub.dev/documentation/benchmark_runner/doc/api/benchmark_runner/asyncGroup.html
[Ansi]: https://pub.dev/packages/ansi_modifier/doc/api/ansi_modifier/Ansi-class.html

[benchmark]: https://pub.dev/documentation/benchmark_runner/doc/api/benchmark_runner/benchmark.html
[modify]: https://pub.dev/documentation/ansi_modifier/doc/api/ansi_modifier/AnsiModifier/modify.html

[group]: https://pub.dev/documentation/benchmark_runner/doc/api/benchmark_runner/group.html
[clearAnsi]: https://pub.dev/documentation/ansi_modifier/doc/api/ansi_modifier/AnsiModifier/asyncGroup.html
8 changes: 4 additions & 4 deletions example/bin/color_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:ansi_modifier/src/ansi.dart';

void main(List<String> args) {
/// Create colorized strings.
print('Create colorized strings:'.modify(Ansi.bold));
print('Create colorized strings:');
final blue = 'blueberry'.modify(Ansi.blue + Ansi.italic);
final green = 'green apple'.modify(Ansi.green);
final blueGreen = blue +
Expand All @@ -14,11 +14,11 @@ void main(List<String> args) {
print('$blue, $green, $blueGreen');

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

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

/// Replace all modifiers.
final magenta =
Expand Down
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.
97 changes: 58 additions & 39 deletions lib/src/ansi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,105 +27,114 @@ enum Replace {
clearPrevious,
}

/// Enumeration representing Ansi modifiers.
/// Class representing Ansi modifiers.
///
/// Used to print custom fonts and colorized output to Ansi compliant terminals.
class Ansi {
final class Ansi {
/// Ansi modifier: Reset to default.
static final reset = Ansi('0');
static final reset = Ansi._('0');

/// Ansi modifier bold forground text.
static final bold = Ansi('1');
static final bold = Ansi._('1');

/// Ansi modifier italic foreground text.
static final italic = Ansi('3');
static final italic = Ansi._('3');

/// Ansi mofifiers underlined foreground text.
static final underline = Ansi('4');
static final underline = Ansi._('4');

/// Ansi modifier crossed out foreground text.
static final crossedOut = Ansi('9');
static final crossedOut = Ansi._('9');

/// Ansi color modifier: black foreground.
static final black = Ansi('30');
static final black = Ansi._('30');

/// Ansi color modifier: red foreground.
static final red = Ansi('31');
static final red = Ansi._('31');

/// Ansi color modifier: green foreground.
static final green = Ansi('32');
static final green = Ansi._('32');

/// Ansi color modifier: yellow foreground.
static final yellow = Ansi('33');
static final yellow = Ansi._('33');

/// Ansi color modifier: blue foreground.
static final blue = Ansi('34');
static final blue = Ansi._('34');

/// Ansi color modifier: magenta foreground.
static final magenta = Ansi('35');
static final magenta = Ansi._('35');

/// Ansi color modifier: magenta bold foreground.
static final magentaBold = Ansi('1;35');
static final magentaBold = Ansi._('1;35');

/// Ansi color modifier: cyan foreground.
static final cyan = Ansi('36');
static final cyan = Ansi._('36');

/// Ansi color modifier: cyan bold text.
static final cyanBold = Ansi('1;36');
static final cyanBold = Ansi._('1;36');

/// Ansi color modifier: grey foreground
static final grey = Ansi('2;37');
static final grey = Ansi._('2;37');

/// Ansi color modifier: black background
static final blackBg = Ansi('40');
static final blackBg = Ansi._('40');

/// Ansi color modifier: red backgroound
static final redBg = Ansi('41');
static final redBg = Ansi._('41');

/// Ansi color modifier: green background
static final greenBg = Ansi('42');
static final greenBg = Ansi._('42');

/// Ansi color modifier: yellow background
static final yellowBg = Ansi('43');
static final yellowBg = Ansi._('43');

/// Ansi color modifier: blue background
static final blueBg = Ansi('44');
static final blueBg = Ansi._('44');

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

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

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

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

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

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

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

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

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

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

const Ansi(this.bareCode) : code = escLeft + bareCode + escRight;
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(';'));
// Extract modifiers:
final bareCodes = (modifiers
.map<String>((element) => element.bareCode)
.join(';')
.split(';')
..sort())
.toSet()
.join(';');

return Ansi._(bareCodes);
}

/// Returns the escaped Ansi code.
Expand All @@ -142,11 +151,21 @@ class Ansi {

/// 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,
);
Ansi operator +(Ansi other) =>
this == other ? this : Ansi.combine({this, other});

/// Returns `true` if [other] is of type [Ansi] and
/// `bareCode == other.bareCode`. Returns `false` otherwise.
bool operator ==(Object other) {
if (other is Ansi) {
return bareCode == other.bareCode;
} else {
return false;
}
}

@override
int get hashCode => bareCode.hashCode;

@override
String toString() {
Expand Down
51 changes: 51 additions & 0 deletions test/ansi_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:ansi_modifier/ansi_modifier.dart';
import 'package:test/test.dart';

void main() {
group('Constructors:', () {
test('factory Ansi.combine', () {
final ansi = Ansi.combine({Ansi.red, Ansi.italic});
expect(
ansi.bareCode,
Ansi.italic.bareCode + ';' + Ansi.red.bareCode,
reason: 'Bare code are sorted!',
);
});
});
group('Accessors', () {
test('fields', () {
expect(
Ansi.red,
isA<Ansi>()
.having(
(ansi) => ansi.code,
'escaped code',
escLeft + '31' + escRight,
)
.having((ansi) => ansi.bareCode, 'bare code', '31'),
);
});

test('status', () {
expect(Ansi.status, AnsiOutput.enabled);
});
});
group('Operator:', () {
test('+', () {
expect(
Ansi.combine({Ansi.cyan, Ansi.bold}),
(Ansi.cyan + Ansi.bold),
);
});
test('Equals', () {
expect(Ansi.red, Ansi.red);
expect(Ansi.red + Ansi.bold, Ansi.bold + Ansi.red);
});
test('bareCode', () {
expect(
(Ansi.bold + Ansi.italic).bareCode,
(Ansi.italic + Ansi.bold).bareCode,
);
});
});
}
11 changes: 1 addition & 10 deletions tool/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ echo
echo -e "${BLUE}=== Running Example $PWD...${RESET}"
echo

dart example/bin/directed_graph_example.dart
dart example/bin/color_example.dart

echo

# =================
# Running benchmark
# =================
echo
echo -e "${GREEN}=== Running Benchmark $PWD...${RESET}"
echo

dart run benchmark bin/

0 comments on commit 1163d58

Please sign in to comment.