Skip to content

Commit

Permalink
remove the dep on package:ansicolor (#8599)
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew authored Dec 6, 2024
1 parent 2daf971 commit 0a77055
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 39 deletions.
1 change: 0 additions & 1 deletion packages/devtools_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
37 changes: 19 additions & 18 deletions packages/devtools_app/test/shared/ansi_up_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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());

Expand Down
71 changes: 71 additions & 0 deletions packages/devtools_app/test/test_infra/utils/ansi.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 0a77055

Please sign in to comment.