Skip to content
This repository has been archived by the owner on Feb 16, 2025. It is now read-only.

Commit

Permalink
complex nested decoratoins
Browse files Browse the repository at this point in the history
  • Loading branch information
aprosail committed May 10, 2024
2 parents 117f439 + 787227f commit e436b13
Show file tree
Hide file tree
Showing 15 changed files with 676 additions and 181 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 0.1.0

- Support foreground and background decorations.
- Support combined complex foreground and background decorations.
- Support combined complex font style decorations.
- Tests to validate nested decorations.

## 0.0.0

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
Terminal colorization and text style decoration for Dart:
This package is an encapsulation of the terminal escape code,
refer to the standard: ANSI X3.64, ECMA-48, or ISO 6429.

Thanks to Dart's extension mechanism, you can use it like this:

```dart
// Font style decoration.
print('normal ${'italic ${'bold'.bold} italic'.italic} normal');
// Font color decoration.
print('normal ${'red ${'yellow'.yellow} red'.red} normal');
```
163 changes: 163 additions & 0 deletions example/color_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import 'package:terminal_decorate/terminal_decorate.dart';

void main() => colorExample();

void colorExample() {
for (final line in singleForegroundLines) print(line);
for (final line in singleBackgroundLines) print(line);
print(complexForegroundLine());
print(complexBackgroundLine());
}

final singleForegroundLines = [
singleForegroundLine(
code: Escape.foregroundBlack,
content: 'black'.black,
comment: 'black, sometimes dark gray',
),
singleForegroundLine(code: Escape.foregroundRed, content: 'red'.red),
singleForegroundLine(code: Escape.foregroundYellow, content: 'yellow'.yellow),
singleForegroundLine(code: Escape.foregroundGreen, content: 'green'.green),
singleForegroundLine(code: Escape.foregroundCyan, content: 'cyan'.cyan),
singleForegroundLine(code: Escape.foregroundBlue, content: 'blue'.blue),
singleForegroundLine(
code: Escape.foregroundMagenta,
content: 'magenta'.magenta,
),
singleForegroundLine(
code: Escape.foregroundWhite,
content: 'white'.white,
comment: 'white, sometimes light gray',
),
singleForegroundLine(
code: Escape.foregroundHiBlack,
content: 'hi black'.hiBlack,
comment: 'hi black',
),
singleForegroundLine(code: Escape.foregroundHiRed, content: 'hi red'.hiRed),
singleForegroundLine(
code: Escape.foregroundHiYellow,
content: 'hi yellow'.hiYellow,
),
singleForegroundLine(
code: Escape.foregroundHiGreen,
content: 'hi green'.hiGreen,
),
singleForegroundLine(
code: Escape.foregroundHiCyan,
content: 'hi cyan'.hiCyan,
),
singleForegroundLine(
code: Escape.foregroundHiBlue,
content: 'hi blue'.hiBlue,
),
singleForegroundLine(
code: Escape.foregroundHiMagenta,
content: 'hi magenta'.hiMagenta,
),
singleForegroundLine(
code: Escape.foregroundHiWhite,
content: 'hi white'.hiWhite,
comment: 'hi white',
),
];

final singleBackgroundLines = [
singleBackgroundLine(
code: Escape.backgroundBlack,
content: 'black'.bgBlack,
comment: 'black',
),
singleBackgroundLine(code: Escape.backgroundRed, content: 'red'.bgRed),
singleBackgroundLine(
code: Escape.backgroundYellow,
content: 'yellow'.bgYellow,
),
singleBackgroundLine(code: Escape.backgroundGreen, content: 'green'.bgGreen),
singleBackgroundLine(code: Escape.backgroundCyan, content: 'cyan'.bgCyan),
singleBackgroundLine(code: Escape.backgroundBlue, content: 'blue'.bgBlue),
singleBackgroundLine(
code: Escape.backgroundMagenta,
content: 'magenta'.bgMagenta,
),
singleBackgroundLine(
code: Escape.backgroundWhite,
content: 'white'.bgWhite,
comment: 'white',
),
singleBackgroundLine(
code: Escape.backgroundHiBlack,
content: 'hi black'.bgHiBlack,
comment: 'hi black',
),
singleBackgroundLine(code: Escape.backgroundHiRed, content: 'hi red'.bgHiRed),
singleBackgroundLine(
code: Escape.backgroundHiYellow,
content: 'hi yellow'.bgHiYellow,
),
singleBackgroundLine(
code: Escape.backgroundHiGreen,
content: 'hi green'.bgHiGreen,
),
singleBackgroundLine(
code: Escape.backgroundHiCyan,
content: 'hi cyan'.bgHiCyan,
),
singleBackgroundLine(
code: Escape.backgroundHiBlue,
content: 'hi blue'.bgHiBlue,
),
singleBackgroundLine(
code: Escape.backgroundHiMagenta,
content: 'hi magenta'.bgHiMagenta,
),
singleBackgroundLine(
code: Escape.backgroundHiWhite,
content: 'hi white'.bgHiWhite,
comment: 'hi white',
),
];

String singleForegroundLine({
required Escape code,
required String content,
String? comment,
}) {
return [
'(${code.code}:${Escape.cancelForeground.code})',
'$content color',
if (comment != null) '($comment)'
].join(' ');
}

String singleBackgroundLine({
required Escape code,
required String content,
String? comment,
}) {
return [
'(${code.code}:${Escape.cancelBackground.code})',
'$content background',
if (comment != null) '($comment)'
].join(' ');
}

String complexForegroundLine() {
final red = 'red'.foregroundRed;
final yellow = 'yellow $red yellow'.foregroundYellow;
final green = 'green $yellow green'.foregroundGreen;
final cyan = 'cyan $green cyan'.foregroundCyan;
final blue = 'blue $cyan blue'.foregroundBlue;
final magenta = 'magenta $blue magenta'.foregroundMagenta;
return 'complex $magenta line';
}

String complexBackgroundLine() {
final red = 'red'.backgroundRed;
final yellow = 'yellow $red yellow'.backgroundYellow;
final green = 'green $yellow green'.backgroundGreen;
final cyan = 'cyan $green cyan'.backgroundCyan;
final blue = 'blue $cyan blue'.backgroundBlue;
final magenta = 'magenta $blue magenta'.backgroundMagenta;
return 'complex $magenta background line';
}
95 changes: 95 additions & 0 deletions example/font_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import 'package:terminal_decorate/terminal_decorate.dart';

void main() => fontExample();

void fontExample() {
for (final line in basicFontDecorations) print(line);
print(mixBoldAndFaint());
print(mixBlinks());
print(mixUnderlines());
}

final basicFontDecorations = [
singleFontLine(
prefix: Escape.bold,
suffix: Escape.cancelBoldOrFaint,
content: 'bold'.bold,
),
singleFontLine(
prefix: Escape.faint,
suffix: Escape.cancelBoldOrFaint,
content: 'faint/dim'.faint,
),
singleFontLine(
prefix: Escape.italic,
suffix: Escape.cancelItalic,
content: 'italic'.italic,
),
singleFontLine(
prefix: Escape.underline,
suffix: Escape.cancelUnderline,
content: 'underline'.underline,
),
singleFontLine(
prefix: Escape.blink,
suffix: Escape.cancelBlink,
content: 'blink'.blink,
comment: 'usually unsupported',
),
singleFontLine(
prefix: Escape.rapidBlink,
suffix: Escape.cancelBlink,
content: 'rapid blink'.rapidBlink,
comment: 'usually unsupported',
),
singleFontLine(
prefix: Escape.negative,
suffix: Escape.cancelNegative,
content: 'negative'.negative,
),
singleFontLine(
prefix: Escape.conceal,
suffix: Escape.cancelConceal,
content: 'conceal/hide'.conceal,
comment: 'conceal/hide',
),
singleFontLine(
prefix: Escape.crossLine,
suffix: Escape.cancelCrossLine,
content: 'cross line'.crossLine,
),
singleFontLine(
prefix: Escape.doubleUnderline,
suffix: Escape.cancelUnderline,
content: 'double underline'.doubleUnderline,
comment: 'usually unsupported',
),
];

String singleFontLine({
required Escape prefix,
required Escape suffix,
required String content,
String? comment,
}) {
return '(${prefix.code}:${suffix.code}) $content style '
'${comment != null ? '($comment)' : ''}';
}

String mixBoldAndFaint() {
final bold = 'bold'.bold;
final faint = 'faint $bold faint'.faint;
return 'normal $faint normal';
}

String mixBlinks() {
final rapidBlink = 'rapid blink'.blink;
final blink = 'blink $rapidBlink blink'.blink;
return 'normal $blink normal (usually unsupported)';
}

String mixUnderlines() {
final doubleUnderline = 'double underline'.doubleUnderline;
final underline = 'underline $doubleUnderline underline'.underline;
return 'normal $underline normal (usually unsupported)';
}
6 changes: 4 additions & 2 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'standard_example.dart';
import 'color_example.dart';
import 'font_example.dart';

void main() {
standardExample();
fontExample();
colorExample();
}
65 changes: 0 additions & 65 deletions example/standard_example.dart

This file was deleted.

Loading

0 comments on commit e436b13

Please sign in to comment.