Skip to content

Commit

Permalink
Added Ansi code for pos. the cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
simphotonics committed Apr 12, 2024
1 parent 2496e59 commit 1f83385
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

# Note: This workflow uses the latest stable version of the Dart SDK.
# You can specify other versions if desired, see documentation here:
Expand Down
14 changes: 8 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

## 0.1.1
## 0.1.2
* Added named constructor for generating ansi sequences that move the
current cursor position. (`Use with stdout.write()`).
* Updated `dart.yml` actions.

Added Ansi modifiers.
## 0.1.1
* Added Ansi modifiers.

## 0.1.0

Breaking change: renamed method `modify()` -> `style()` and `removeAnsi()` -> `clearStyle()`.
* Breaking change: renamed method `modify()` -> `style()` and `removeAnsi()` -> `clearStyle()`.

## 0.0.1

Initial version
* Initial version
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Use the String extension function [`style`][style] to add new modifiers or
to replace existing ones. Use the function [`clearStyle`][clearStyle] to remove
all Ansi modifier from a string.

Ansi codes for moving the current cursor position can be constructed using the
constructors `.cursorUp`, `.cursorDown`, etc.

```Dart
import 'package:ansi_modifier/src/ansi.dart';
Expand Down Expand Up @@ -47,6 +50,14 @@ void main(List<String> args) {
// Strip all Ansi modifiers.
print('$yellowGreen, $magenta, ${magenta.clearStyle()}\n');
// Moving the cursor position:
// stdout.write('Hello world!');
// stdout.write(Ansi.cursorToColumn(1));
// stdout.write('Say hi to the ' + 'moon'.style(Ansi.yellow) + '!');
// stdout.write(Ansi.cursorForward(10));
// stdout.write('Hi there.');
// print('');
}
```

Expand Down Expand Up @@ -83,8 +94,8 @@ at the [issue tracker][tracker].

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

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

[style]: https://pub.dev/documentation/ansi_modifier/doc/api/ansi_modifier/AnsiModifier/style.html
[style]: https://pub.dev/documentation/ansi_modifier/latest/ansi_modifier/AnsiModifier/style.html

[clearStyle]: https://pub.dev/documentation/ansi_modifier/doc/api/ansi_modifier/AnsiModifier/clearStyle.html
[clearStyle]: https://pub.dev/documentation/ansi_modifier/latest/ansi_modifier/AnsiModifier/clearStyle.html
6 changes: 3 additions & 3 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ at the [issue tracker][tracker].

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

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

[style]: https://pub.dev/documentation/ansi_modifier/doc/api/ansi_modifier/AnsiModifier/style.html
[style]: https://pub.dev/documentation/ansi_modifier/latest/ansi_modifier/AnsiModifier/style.html

[clearAnsi]: https://pub.dev/documentation/ansi_modifier/doc/api/ansi_modifier/AnsiModifier/asyncGroup.html
[clearAnsi]: https://pub.dev/documentation/ansi_modifier/latest/ansi_modifier/AnsiModifier/asyncGroup.html
10 changes: 10 additions & 0 deletions example/bin/color_example.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

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

void main(List<String> args) {
Expand Down Expand Up @@ -28,4 +30,12 @@ void main(List<String> args) {

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

print('Moving the cursor:');
stdout.write('Hello world!');
stdout.write(Ansi.cursorToColumn(1));
stdout.write('Say hi to the ' + 'moon'.style(Ansi.yellow) + '!');
stdout.write(Ansi.cursorForward(10));
stdout.write('Hi there.');
print('');
}
124 changes: 89 additions & 35 deletions lib/src/ansi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,112 +32,166 @@ enum Replace {
/// Used to print custom fonts and colorized output to Ansi compliant terminals.
final class Ansi {
/// Ansi modifier: Reset to default.
static final reset = Ansi._('0');
static const reset = Ansi._('0');

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

/// Ansi modifier faint foreground text.
static final faint = Ansi._('2');
static const faint = Ansi._('2');

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

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

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

/// Ansi modifier default font.
static final defaultFont = Ansi._('10');
static const defaultFont = Ansi._('10');

/// Ansi modifier default intensity.
static final defaultIntensity = Ansi._('22');
static const defaultIntensity = Ansi._('22');

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

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

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

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

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

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

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

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

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

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

/// Ansi color modifier: default foreground colour.
static final defaultFg = Ansi._('39');
static const defaultFg = Ansi._('39');

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

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

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

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

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

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

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

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

/// Ansi color modifier: default background colour.
static final defaultBg = Ansi._('39');
static const defaultBg = Ansi._('39');

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

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

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

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

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

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

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

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

/// Write Ansi.cursorUp to stdout to move the
/// cursor up.
///
/// To move several characters up provide the input parameter `n`.
const Ansi.cursorUp([int n = 1])
: code = escLeft + '${n}A',
bareCode = 'A';

/// Write Ansi.cursorDown to stdout to move the
/// cursor down.
///
/// To move several characters down provide the input parameter `n`.
const Ansi.cursorDown([int n = 1])
: code = escLeft + '${n}B',
bareCode = 'B';

/// Write Ansi.cursorForward to stdout to move the
/// cursor forward.
///
/// To move several characters forward provide the input parameter `n`.
const Ansi.cursorForward([int n = 1])
: code = escLeft + '${n}C',
bareCode = 'C';

/// Write Ansi.cursorBack to stdout to move the
/// cursor back.
///
/// To move several character back provide the input parameter `n`.
const Ansi.cursorBack([int n = 1])
: code = escLeft + '${n}D',
bareCode = 'D';

/// Write Ansi.cursorNextLine to stdout to move the
/// cursor to the next line.
///
/// To move several lines provide the input parameter `n`.
const Ansi.cursorNextLine([int n = 1])
: code = escLeft + '${n}E',
bareCode = 'E';

/// Write `Ansi.cursorPreviousLine()` to stdout to move the
/// cursor to the beginning of the previous line.
///
/// To move several lines provide the input parameter `n`.
const Ansi.cursorPreviousLine([int n = 1])
: code = escLeft + '${n}F',
bareCode = 'F';

/// Write Ansi.cursorToColumn to stdout to move the
/// cursor to the column [n].
const Ansi.cursorToColumn(int n)
: code = escLeft + '${n}G',
bareCode = 'G';

/// Factory constructor combining several Ansi modifiers.
factory Ansi.combine(Set<Ansi> modifiers) {
// Extract modifiers:
Expand Down
14 changes: 7 additions & 7 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
name: ansi_modifier

description: Style strings for terminal output by
adding, replacing, or removing ANSI modifiers.
description: Style strings for terminal output by adding, replacing, or removing
ANSI modifiers.

version: 0.1.1
version: 0.1.2

homepage: https://github.com/simphotonics/ansi_modifier

topics:
- ansi
- terminal
- console
- color
- cursor-position

environment:
sdk: '^3.0.0'

dependencies:

dev_dependencies:
lints: ^2.1.1
test: ^1.24.6
lints: ^3.0.0
test: ^1.25.3
7 changes: 6 additions & 1 deletion test/ansi_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ void main() {
expect(
ansi.bareCode,
Ansi.italic.bareCode + ';' + Ansi.red.bareCode,
reason: 'Bare code are sorted!',
reason: 'Bare codes are sorted!',
);
});
test('Ansi.cursorUp', () {
final ansi = Ansi.cursorUp(29);
expect(ansi.bareCode, 'A');
expect(ansi.code, escLeft + '29' + ansi.bareCode);
});
});
group('Accessors', () {
test('fields', () {
Expand Down
2 changes: 1 addition & 1 deletion tool/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dart analyze \
echo
echo -e "${CYAN}=== Testing $PWD...${RESET}"
echo
dart run test -r expanded --test-randomize-ordering-seed=random
dart test -r expanded --test-randomize-ordering-seed=random


# ================
Expand Down

0 comments on commit 1f83385

Please sign in to comment.