Skip to content

Commit 8492b48

Browse files
committed
remove the dep on package:ansicolor
1 parent 4945226 commit 8492b48

File tree

6 files changed

+111
-39
lines changed

6 files changed

+111
-39
lines changed

packages/devtools_app/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ resolution: workspace
1616

1717
dependencies:
1818
ansi_up: ^1.0.0
19-
ansicolor: ^2.0.0
2019
async: ^2.0.0 # okay
2120
clock: ^1.1.1 # Okay
2221
collection: ^1.15.0

packages/devtools_app/test/screens/debugger/debugger_console_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:ansicolor/ansicolor.dart';
65
import 'package:devtools_app/devtools_app.dart';
76
import 'package:devtools_app/src/shared/console/widgets/console_pane.dart';
87
import 'package:devtools_app_shared/ui.dart';
@@ -14,6 +13,7 @@ import 'package:flutter/services.dart';
1413
import 'package:flutter_test/flutter_test.dart';
1514
import 'package:mockito/mockito.dart';
1615

16+
import '../../test_infra/utils/ansi.dart';
1717
import '../../test_infra/utils/test_utils.dart';
1818

1919
void main() {
@@ -129,7 +129,7 @@ void main() {
129129
String _ansiCodesOutput() {
130130
final sb = StringBuffer();
131131
sb.write('Ansi color codes processed for ');
132-
final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
133-
sb.write(pen('console'));
132+
final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
133+
sb.write(ansi.write('console'));
134134
return sb.toString();
135135
}

packages/devtools_app/test/screens/logging/logging_screen_data_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
@TestOn('vm')
66
library;
77

8-
import 'package:ansicolor/ansicolor.dart';
98
import 'package:devtools_app/devtools_app.dart';
109
import 'package:devtools_app/src/screens/logging/_log_details.dart';
1110
import 'package:devtools_app/src/screens/logging/_logs_table.dart';
@@ -17,6 +16,8 @@ import 'package:flutter/material.dart';
1716
import 'package:flutter_test/flutter_test.dart';
1817
import 'package:mockito/mockito.dart';
1918

19+
import '../../test_infra/utils/ansi.dart';
20+
2021
void main() {
2122
late MockLoggingController mockLoggingController;
2223
const windowSize = Size(1000.0, 1000.0);
@@ -310,7 +311,7 @@ const jsonOutput = '{\n"Details": "of log event 9",\n"logEvent": "9"\n}\n';
310311
String _ansiCodesOutput() {
311312
final sb = StringBuffer();
312313
sb.write('Ansi color codes processed for ');
313-
final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
314-
sb.write(pen('log 5'));
314+
final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
315+
sb.write(ansi.write('log 5'));
315316
return sb.toString();
316317
}

packages/devtools_app/test/shared/ansi_up_test.dart

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
library;
77

88
import 'package:ansi_up/ansi_up.dart';
9-
import 'package:ansicolor/ansicolor.dart';
109
import 'package:devtools_app/devtools_app.dart';
1110
import 'package:devtools_app/src/shared/console/widgets/console_pane.dart';
1211
import 'package:devtools_app_shared/utils.dart';
@@ -16,22 +15,24 @@ import 'package:flutter/material.dart';
1615
import 'package:flutter_test/flutter_test.dart';
1716
import 'package:mockito/mockito.dart';
1817

18+
import '../test_infra/utils/ansi.dart';
19+
1920
void main() {
2021
group('ansi_up', () {
2122
test('test standard colors', () {
22-
final pen = AnsiPen();
23+
final ansi = AnsiWriter();
2324
final sb = StringBuffer();
2425
// Test the 16 color defaults.
2526
for (int c = 0; c < 16; c++) {
26-
pen
27+
ansi
2728
..reset()
2829
..white(bold: true)
2930
..xterm(c, bg: true);
30-
sb.write(pen('$c '));
31-
pen
31+
sb.write(ansi.write('$c '));
32+
ansi
3233
..reset()
3334
..xterm(c);
34-
sb.write(pen(' $c '));
35+
sb.write(ansi.write(' $c '));
3536
if (c == 7 || c == 15) {
3637
sb.writeln();
3738
}
@@ -43,15 +44,15 @@ void main() {
4344
for (int g = 0; g < 6; g += 3) {
4445
for (int b = 0; b < 6; b += 3) {
4546
final c = r * 36 + g * 6 + b + 16;
46-
pen
47+
ansi
4748
..reset()
4849
..rgb(r: r / 5, g: g / 5, b: b / 5, bg: true)
4950
..white(bold: true);
50-
sb.write(pen(' $c '));
51-
pen
51+
sb.write(ansi.write(' $c '));
52+
ansi
5253
..reset()
5354
..rgb(r: r / 5, g: g / 5, b: b / 5);
54-
sb.write(pen(' $c '));
55+
sb.write(ansi.write(' $c '));
5556
}
5657
sb.writeln();
5758
}
@@ -61,15 +62,15 @@ void main() {
6162
if (0 == c % 8) {
6263
sb.writeln();
6364
}
64-
pen
65+
ansi
6566
..reset()
6667
..gray(level: c / 23, bg: true)
6768
..white(bold: true);
68-
sb.write(pen(' ${c + 232} '));
69-
pen
69+
sb.write(ansi.write(' ${c + 232} '));
70+
ansi
7071
..reset()
7172
..gray(level: c / 23);
72-
sb.write(pen(' ${c + 232} '));
73+
sb.write(ansi.write(' ${c + 232} '));
7374
}
7475

7576
final output = StringBuffer();
@@ -114,8 +115,8 @@ void main() {
114115
String ansiCodesOutput() {
115116
final sb = StringBuffer();
116117
sb.write('Ansi color codes processed for ');
117-
final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
118-
sb.write(pen('log 5'));
118+
final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
119+
sb.write(ansi.write('log 5'));
119120
return sb.toString();
120121
}
121122

@@ -266,8 +267,8 @@ void main() {
266267
String ansiCodesOutput() {
267268
final sb = StringBuffer();
268269
sb.write('Ansi color codes processed for ');
269-
final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
270-
sb.write(pen('console'));
270+
final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true);
271+
sb.write(ansi.write('console'));
271272
return sb.toString();
272273
}
273274

packages/devtools_app/test/test_infra/fixtures/color_console_output_app.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import 'dart:async';
88
import 'dart:developer' as developer;
99

10-
import 'package:ansicolor/ansicolor.dart';
10+
import '../utils/ansi.dart';
1111

1212
void main() {
1313
// Start paused to avoid race conditions getting the initial output from the
@@ -17,19 +17,19 @@ void main() {
1717

1818
// Print out text exercising a wide range of ansi color styles.
1919
final sb = StringBuffer();
20-
final pen = AnsiPen();
20+
final ansi = AnsiWriter();
2121

2222
// Test the 16 color defaults.
2323
for (int c = 0; c < 16; c++) {
24-
pen
24+
ansi
2525
..reset()
2626
..white(bold: true)
2727
..xterm(c, bg: true);
28-
sb.write(pen('$c '));
29-
pen
28+
sb.write(ansi.write('$c '));
29+
ansi
3030
..reset()
3131
..xterm(c);
32-
sb.write(pen(' $c '));
32+
sb.write(ansi.write(' $c '));
3333
if (c == 7 || c == 15) {
3434
sb.writeln();
3535
}
@@ -41,15 +41,15 @@ void main() {
4141
for (int g = 0; g < 6; g += 3) {
4242
for (int b = 0; b < 6; b += 3) {
4343
final c = r * 36 + g * 6 + b + 16;
44-
pen
44+
ansi
4545
..reset()
4646
..rgb(r: r / 5, g: g / 5, b: b / 5, bg: true)
4747
..white(bold: true);
48-
sb.write(pen(' $c '));
49-
pen
48+
sb.write(ansi.write(' $c '));
49+
ansi
5050
..reset()
5151
..rgb(r: r / 5, g: g / 5, b: b / 5);
52-
sb.write(pen(' $c '));
52+
sb.write(ansi.write(' $c '));
5353
}
5454
sb.writeln();
5555
}
@@ -59,15 +59,15 @@ void main() {
5959
if (0 == c % 8) {
6060
sb.writeln();
6161
}
62-
pen
62+
ansi
6363
..reset()
6464
..gray(level: c / 23, bg: true)
6565
..white(bold: true);
66-
sb.write(pen(' ${c + 232} '));
67-
pen
66+
sb.write(ansi.write(' ${c + 232} '));
67+
ansi
6868
..reset()
6969
..gray(level: c / 23);
70-
sb.write(pen(' ${c + 232} '));
70+
sb.write(ansi.write(' ${c + 232} '));
7171
}
7272
print(sb.toString());
7373

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2024 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
class AnsiWriter {
6+
static const _escape = '\x1B[';
7+
8+
int? _foreground;
9+
int? _background;
10+
11+
/// Sets the pen color to the rgb value between 0.0..1.0.
12+
void rgb({num r = 1.0, num g = 1.0, num b = 1.0, bool bg = false}) {
13+
xterm(
14+
(r.clamp(0.0, 1.0) * 5).toInt() * 36 +
15+
(g.clamp(0.0, 1.0) * 5).toInt() * 6 +
16+
(b.clamp(0.0, 1.0) * 5).toInt() +
17+
16,
18+
bg: bg,
19+
);
20+
}
21+
22+
/// Sets the pen color to a grey scale value between 0.0 and 1.0.
23+
void gray({num level = 1.0, bool bg = false}) {
24+
xterm(232 + (level.clamp(0.0, 1.0) * 23).round(), bg: bg);
25+
}
26+
27+
void white({bool bg = false, bool bold = false}) {
28+
_standardColor(7, bold, bg);
29+
}
30+
31+
void _standardColor(int color, bool bold, bool bg) {
32+
xterm(color + (bold ? 8 : 0), bg: bg);
33+
}
34+
35+
/// Directly index the xterm 256 color palette.
36+
void xterm(int color, {bool bg = false}) {
37+
final c =
38+
color < 0
39+
? 0
40+
: color > 255
41+
? 255
42+
: color;
43+
44+
if (bg) {
45+
_background = c;
46+
} else {
47+
_foreground = c;
48+
}
49+
}
50+
51+
/// Write the [message] with the pen's current settings.
52+
String write(Object message) {
53+
final pen = StringBuffer();
54+
if (_foreground != null) {
55+
pen.write('${_escape}38;5;${_foreground}m');
56+
}
57+
if (_background != null) {
58+
pen.write('${_escape}48;5;${_background}m');
59+
}
60+
61+
const normal = '${_escape}0m';
62+
63+
return '$pen$message$normal';
64+
}
65+
66+
/// Resets the pen's attributes.
67+
void reset() {
68+
_background = null;
69+
_foreground = null;
70+
}
71+
}

0 commit comments

Comments
 (0)