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

Commit

Permalink
color foreground and background
Browse files Browse the repository at this point in the history
  • Loading branch information
aprosail committed May 4, 2024
2 parents 474c31c + 43b8f09 commit 117f439
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 78 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.0

- Support foreground and background decorations.

## 0.0.0

- Support basic font style decorations.
Expand Down
25 changes: 0 additions & 25 deletions example/basic_example.dart

This file was deleted.

5 changes: 5 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'standard_example.dart';

void main() {
standardExample();
}
65 changes: 65 additions & 0 deletions example/standard_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:terminal_decorate/terminal_decorate.dart';

void standardExample() {
for (final item in fontStyleDecorations) print(item);
for (final item in foregroundDecorations) print(item);
for (final item in backgroundDecorations) print(item);
}

final fontStyleDecorations = [
'\nfont style decorations:',
'(${Escape.bold.code}:${Escape.cancelBoldOrFaint.code}) ${'bold'.bold} style',
'(${Escape.faint.code}:${Escape.cancelBoldOrFaint.code}) '
'${'faint/dim'.faint} style',
'(${Escape.italic.code}:${Escape.cancelItalic.code}) '
'${'italic'.italic} style',
'(${Escape.underline.code}:${Escape.cancelUnderline.code}) '
'${'underline'.underline} style',
'(${Escape.blink.code}:${Escape.cancelBlink.code}) '
'${'blink'.blink} style '
'(usually unsupported)',
'(${Escape.rapidBlink.code}:${Escape.cancelBlink.code}) '
'${'rapid blink'.rapidBlink} style '
'(usually unsupported)',
'(${Escape.negative.code}:${Escape.cancelNegative.code}) '
'${'negative'.negative} style',
'(${Escape.conceal.code}:${Escape.cancelConceal.code}) '
'${'conceal'.conceal} (conceal/hide) style',
'(${Escape.crossLine.code}:${Escape.cancelCrossLine.code}) '
'${'cross line'.crossLine} style',
'(${Escape.doubleUnderline.code}:${Escape.cancelUnderline.code}) '
'${'double underline'.doubleUnderline} style '
'(usually unsupported)',
];

final foregroundDecorations = [
'(${Escape.black.code}:${Escape.cancelFg.code}) '
'${'black'.black} (black) style (sometimes deep gray)',
'(${Escape.red.code}:${Escape.cancelFg.code}) ${'red'.red} style',
'(${Escape.yellow.code}:${Escape.cancelFg.code}) ${'yellow'.yellow} style',
'(${Escape.green.code}:${Escape.cancelFg.code}) ${'green'.green} style',
'(${Escape.cyan.code}:${Escape.cancelFg.code}) ${'cyan'.cyan} style',
'(${Escape.blue.code}:${Escape.cancelFg.code}) ${'blue'.blue} style',
'(${Escape.magenta.code}:${Escape.cancelFg.code}) ${'magenta'.magenta} style',
'(${Escape.white.code}:${Escape.cancelFg.code}) '
'${'white'.white} (white) style',
];

final backgroundDecorations = [
'(${Escape.bgBlack.code}:${Escape.cancelBg.code}) '
'${'black background'.bgBlack} (black) style',
'(${Escape.bgRed.code}:${Escape.cancelBg.code}) '
'${'red background'.bgRed} style',
'(${Escape.bgYellow.code}:${Escape.cancelBg.code}) '
'${'yellow background'.bgYellow} style',
'(${Escape.bgGreen.code}:${Escape.cancelBg.code}) '
'${'green background'.bgGreen} style',
'(${Escape.bgCyan.code}:${Escape.cancelBg.code}) '
'${'cyan background'.bgCyan} style',
'(${Escape.bgBlue.code}:${Escape.cancelBg.code}) '
'${'blue background'.bgBlue} style',
'(${Escape.bgMagenta.code}:${Escape.cancelBg.code}) '
'${'magenta background'.bgMagenta} style',
'(${Escape.bgWhite.code}:${Escape.cancelBg.code}) '
'${'white background'.bgWhite} (white) style',
];
108 changes: 108 additions & 0 deletions lib/src/standard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
enum Escape {
cancelAll(0),
bold(1),

/// Also called "dim".
faint(2),
italic(3),
underline(4),

/// Usually unsupported.
blink(5),

/// Usually unsupported.
rapidBlink(6),
negative(7),

/// Also called "hide".
conceal(8),

/// Usually unsupported.
crossLine(9),

/// Usually unsupported.
doubleUnderline(21),

// Cancel font decorations.
cancelBoldOrFaint(22),
cancelItalic(23),
cancelUnderline(24),
cancelBlink(25),
cancelNegative(27),
cancelConceal(28),
cancelCrossLine(29),

// Foreground decorations.
black(30),
red(31),
green(32),
yellow(33),
blue(34),
magenta(35),
cyan(36),
white(37),
customizeFg(38),
cancelFg(39),

// Bg decoration.
bgBlack(40),
bgRed(41),
bgGreen(42),
bgYellow(43),
bgBlue(44),
bgMagenta(45),
bgCyan(46),
bgWhite(47),
customizeBg(48),
cancelBg(49);

const Escape(this.code)
: assert(code >= 0 && code <= 0xff, 'invalid code: $code (0~255)');

final int code;

String get format => '\x1b[${code}m';

@override
String toString() => format;
}

extension TerminalFontStyleDecorations on String {
String get bold => '${Escape.bold}$this${Escape.cancelBoldOrFaint}';
String get faint => '${Escape.faint}$this${Escape.cancelBoldOrFaint}';
String get italic => '${Escape.italic}$this${Escape.cancelItalic}';
String get underline => '${Escape.underline}$this${Escape.cancelUnderline}';
String get blink => '${Escape.blink}$this${Escape.cancelBlink}';
String get rapidBlink => '${Escape.rapidBlink}$this${Escape.cancelBlink}';
String get negative => '${Escape.negative}$this${Escape.cancelNegative}';
String get conceal => '${Escape.conceal}$this${Escape.cancelConceal}';
String get crossLine => '${Escape.crossLine}$this${Escape.cancelCrossLine}';
String get doubleUnderline =>
'${Escape.doubleUnderline}$this${Escape.cancelUnderline}';

// Syntax sugars.
String get dim => faint;
String get hide => conceal;
}

extension TerminalColorDecorations on String {
// Foreground.
String get black => '${Escape.black}$this${Escape.cancelFg}';
String get red => '${Escape.red}$this${Escape.cancelFg}';
String get green => '${Escape.green}$this${Escape.cancelFg}';
String get yellow => '${Escape.yellow}$this${Escape.cancelFg}';
String get blue => '${Escape.blue}$this${Escape.cancelFg}';
String get magenta => '${Escape.magenta}$this${Escape.cancelFg}';
String get cyan => '${Escape.cyan}$this${Escape.cancelFg}';
String get white => '${Escape.white}$this${Escape.cancelFg}';

// Background.
String get bgBlack => '${Escape.bgBlack}$this${Escape.cancelBg}';
String get bgRed => '${Escape.bgRed}$this${Escape.cancelBg}';
String get bgGreen => '${Escape.bgGreen}$this${Escape.cancelBg}';
String get bgYellow => '${Escape.bgYellow}$this${Escape.cancelBg}';
String get bgBlue => '${Escape.bgBlue}$this${Escape.cancelBg}';
String get bgMagenta => '${Escape.bgMagenta}$this${Escape.cancelBg}';
String get bgCyan => '${Escape.bgCyan}$this${Escape.cancelBg}';
String get bgWhite => '${Escape.bgWhite}$this${Escape.cancelBg}';
}
49 changes: 0 additions & 49 deletions lib/src/standart.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/terminal_decorate.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export 'src/standart.dart';
export 'src/standard.dart';
15 changes: 12 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
name: terminal_decorate
description: >
Terminal colorization and text style decoration
based on the escape code defined in ANSI X3.64, ECMA-48, or ISO 6429.
version: 0.0.0
Terminal colorization (colorful command line) and text style decoration.
version: 0.1.0
repository: https://github.com/aprosail/terminal-decorate
topics:
- cli
- cli-color
- color
- command-line
- command-line-color
- font-decoration
- terminal
- terminal-colorization
- terminal-decoration

environment: {sdk: ^3.3.4}

Expand Down

0 comments on commit 117f439

Please sign in to comment.