From 0a7705592bfbdd233f3e356f67c5047b09be380b Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Thu, 5 Dec 2024 19:47:18 -0800 Subject: [PATCH] remove the dep on package:ansicolor (#8599) --- packages/devtools_app/pubspec.yaml | 1 - .../debugger/debugger_console_test.dart | 6 +- .../logging/logging_screen_data_test.dart | 7 +- .../test/shared/ansi_up_test.dart | 37 +++++----- .../fixtures/color_console_output_app.dart | 28 ++++---- .../test/test_infra/utils/ansi.dart | 71 +++++++++++++++++++ 6 files changed, 111 insertions(+), 39 deletions(-) create mode 100644 packages/devtools_app/test/test_infra/utils/ansi.dart diff --git a/packages/devtools_app/pubspec.yaml b/packages/devtools_app/pubspec.yaml index 5d6c05da71a..b52b5f72553 100644 --- a/packages/devtools_app/pubspec.yaml +++ b/packages/devtools_app/pubspec.yaml @@ -16,7 +16,6 @@ resolution: workspace dependencies: ansi_up: ^1.0.0 - ansicolor: ^2.0.0 async: ^2.0.0 # okay clock: ^1.1.1 # Okay collection: ^1.15.0 diff --git a/packages/devtools_app/test/screens/debugger/debugger_console_test.dart b/packages/devtools_app/test/screens/debugger/debugger_console_test.dart index ed5501b240a..0a43edd41a4 100644 --- a/packages/devtools_app/test/screens/debugger/debugger_console_test.dart +++ b/packages/devtools_app/test/screens/debugger/debugger_console_test.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:ansicolor/ansicolor.dart'; import 'package:devtools_app/devtools_app.dart'; import 'package:devtools_app/src/shared/console/widgets/console_pane.dart'; import 'package:devtools_app_shared/ui.dart'; @@ -14,6 +13,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; +import '../../test_infra/utils/ansi.dart'; import '../../test_infra/utils/test_utils.dart'; void main() { @@ -129,7 +129,7 @@ void main() { String _ansiCodesOutput() { final sb = StringBuffer(); sb.write('Ansi color codes processed for '); - final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); - sb.write(pen('console')); + final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); + sb.write(ansi.write('console')); return sb.toString(); } diff --git a/packages/devtools_app/test/screens/logging/logging_screen_data_test.dart b/packages/devtools_app/test/screens/logging/logging_screen_data_test.dart index 7c5e656a1b9..b86350dfcd2 100644 --- a/packages/devtools_app/test/screens/logging/logging_screen_data_test.dart +++ b/packages/devtools_app/test/screens/logging/logging_screen_data_test.dart @@ -5,7 +5,6 @@ @TestOn('vm') library; -import 'package:ansicolor/ansicolor.dart'; import 'package:devtools_app/devtools_app.dart'; import 'package:devtools_app/src/screens/logging/_log_details.dart'; import 'package:devtools_app/src/screens/logging/_logs_table.dart'; @@ -17,6 +16,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; +import '../../test_infra/utils/ansi.dart'; + void main() { late MockLoggingController mockLoggingController; const windowSize = Size(1000.0, 1000.0); @@ -310,7 +311,7 @@ const jsonOutput = '{\n"Details": "of log event 9",\n"logEvent": "9"\n}\n'; String _ansiCodesOutput() { final sb = StringBuffer(); sb.write('Ansi color codes processed for '); - final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); - sb.write(pen('log 5')); + final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); + sb.write(ansi.write('log 5')); return sb.toString(); } diff --git a/packages/devtools_app/test/shared/ansi_up_test.dart b/packages/devtools_app/test/shared/ansi_up_test.dart index e3ceebcd7ce..67598f29297 100644 --- a/packages/devtools_app/test/shared/ansi_up_test.dart +++ b/packages/devtools_app/test/shared/ansi_up_test.dart @@ -6,7 +6,6 @@ library; import 'package:ansi_up/ansi_up.dart'; -import 'package:ansicolor/ansicolor.dart'; import 'package:devtools_app/devtools_app.dart'; import 'package:devtools_app/src/shared/console/widgets/console_pane.dart'; import 'package:devtools_app_shared/utils.dart'; @@ -16,22 +15,24 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; +import '../test_infra/utils/ansi.dart'; + void main() { group('ansi_up', () { test('test standard colors', () { - final pen = AnsiPen(); + final ansi = AnsiWriter(); final sb = StringBuffer(); // Test the 16 color defaults. for (int c = 0; c < 16; c++) { - pen + ansi ..reset() ..white(bold: true) ..xterm(c, bg: true); - sb.write(pen('$c ')); - pen + sb.write(ansi.write('$c ')); + ansi ..reset() ..xterm(c); - sb.write(pen(' $c ')); + sb.write(ansi.write(' $c ')); if (c == 7 || c == 15) { sb.writeln(); } @@ -43,15 +44,15 @@ void main() { for (int g = 0; g < 6; g += 3) { for (int b = 0; b < 6; b += 3) { final c = r * 36 + g * 6 + b + 16; - pen + ansi ..reset() ..rgb(r: r / 5, g: g / 5, b: b / 5, bg: true) ..white(bold: true); - sb.write(pen(' $c ')); - pen + sb.write(ansi.write(' $c ')); + ansi ..reset() ..rgb(r: r / 5, g: g / 5, b: b / 5); - sb.write(pen(' $c ')); + sb.write(ansi.write(' $c ')); } sb.writeln(); } @@ -61,15 +62,15 @@ void main() { if (0 == c % 8) { sb.writeln(); } - pen + ansi ..reset() ..gray(level: c / 23, bg: true) ..white(bold: true); - sb.write(pen(' ${c + 232} ')); - pen + sb.write(ansi.write(' ${c + 232} ')); + ansi ..reset() ..gray(level: c / 23); - sb.write(pen(' ${c + 232} ')); + sb.write(ansi.write(' ${c + 232} ')); } final output = StringBuffer(); @@ -114,8 +115,8 @@ void main() { String ansiCodesOutput() { final sb = StringBuffer(); sb.write('Ansi color codes processed for '); - final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); - sb.write(pen('log 5')); + final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); + sb.write(ansi.write('log 5')); return sb.toString(); } @@ -266,8 +267,8 @@ void main() { String ansiCodesOutput() { final sb = StringBuffer(); sb.write('Ansi color codes processed for '); - final pen = AnsiPen()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); - sb.write(pen('console')); + final ansi = AnsiWriter()..rgb(r: 0.8, g: 0.3, b: 0.4, bg: true); + sb.write(ansi.write('console')); return sb.toString(); } diff --git a/packages/devtools_app/test/test_infra/fixtures/color_console_output_app.dart b/packages/devtools_app/test/test_infra/fixtures/color_console_output_app.dart index f1c304f7f14..71782836f3b 100644 --- a/packages/devtools_app/test/test_infra/fixtures/color_console_output_app.dart +++ b/packages/devtools_app/test/test_infra/fixtures/color_console_output_app.dart @@ -7,7 +7,7 @@ import 'dart:async'; import 'dart:developer' as developer; -import 'package:ansicolor/ansicolor.dart'; +import '../utils/ansi.dart'; void main() { // Start paused to avoid race conditions getting the initial output from the @@ -17,19 +17,19 @@ void main() { // Print out text exercising a wide range of ansi color styles. final sb = StringBuffer(); - final pen = AnsiPen(); + final ansi = AnsiWriter(); // Test the 16 color defaults. for (int c = 0; c < 16; c++) { - pen + ansi ..reset() ..white(bold: true) ..xterm(c, bg: true); - sb.write(pen('$c ')); - pen + sb.write(ansi.write('$c ')); + ansi ..reset() ..xterm(c); - sb.write(pen(' $c ')); + sb.write(ansi.write(' $c ')); if (c == 7 || c == 15) { sb.writeln(); } @@ -41,15 +41,15 @@ void main() { for (int g = 0; g < 6; g += 3) { for (int b = 0; b < 6; b += 3) { final c = r * 36 + g * 6 + b + 16; - pen + ansi ..reset() ..rgb(r: r / 5, g: g / 5, b: b / 5, bg: true) ..white(bold: true); - sb.write(pen(' $c ')); - pen + sb.write(ansi.write(' $c ')); + ansi ..reset() ..rgb(r: r / 5, g: g / 5, b: b / 5); - sb.write(pen(' $c ')); + sb.write(ansi.write(' $c ')); } sb.writeln(); } @@ -59,15 +59,15 @@ void main() { if (0 == c % 8) { sb.writeln(); } - pen + ansi ..reset() ..gray(level: c / 23, bg: true) ..white(bold: true); - sb.write(pen(' ${c + 232} ')); - pen + sb.write(ansi.write(' ${c + 232} ')); + ansi ..reset() ..gray(level: c / 23); - sb.write(pen(' ${c + 232} ')); + sb.write(ansi.write(' ${c + 232} ')); } print(sb.toString()); diff --git a/packages/devtools_app/test/test_infra/utils/ansi.dart b/packages/devtools_app/test/test_infra/utils/ansi.dart new file mode 100644 index 00000000000..1f9ab131361 --- /dev/null +++ b/packages/devtools_app/test/test_infra/utils/ansi.dart @@ -0,0 +1,71 @@ +// Copyright 2024 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +class AnsiWriter { + static const _escape = '\x1B['; + + int? _foreground; + int? _background; + + /// Sets the pen color to the rgb value between 0.0..1.0. + void rgb({num r = 1.0, num g = 1.0, num b = 1.0, bool bg = false}) { + xterm( + (r.clamp(0.0, 1.0) * 5).toInt() * 36 + + (g.clamp(0.0, 1.0) * 5).toInt() * 6 + + (b.clamp(0.0, 1.0) * 5).toInt() + + 16, + bg: bg, + ); + } + + /// Sets the pen color to a grey scale value between 0.0 and 1.0. + void gray({num level = 1.0, bool bg = false}) { + xterm(232 + (level.clamp(0.0, 1.0) * 23).round(), bg: bg); + } + + void white({bool bg = false, bool bold = false}) { + _standardColor(7, bold, bg); + } + + void _standardColor(int color, bool bold, bool bg) { + xterm(color + (bold ? 8 : 0), bg: bg); + } + + /// Directly index the xterm 256 color palette. + void xterm(int color, {bool bg = false}) { + final c = + color < 0 + ? 0 + : color > 255 + ? 255 + : color; + + if (bg) { + _background = c; + } else { + _foreground = c; + } + } + + /// Write the [message] with the pen's current settings. + String write(Object message) { + final pen = StringBuffer(); + if (_foreground != null) { + pen.write('${_escape}38;5;${_foreground}m'); + } + if (_background != null) { + pen.write('${_escape}48;5;${_background}m'); + } + + const normal = '${_escape}0m'; + + return '$pen$message$normal'; + } + + /// Resets the pen's attributes. + void reset() { + _background = null; + _foreground = null; + } +}