Make the forScript code paths ignore the ansiOutputEnabled property (dart-lang/io#36)
Assume these are being used for generating scripts and not runtime
behavior
diff --git a/pkgs/io/CHANGELOG.md b/pkgs/io/CHANGELOG.md
index 5a26062..93ca915 100644
--- a/pkgs/io/CHANGELOG.md
+++ b/pkgs/io/CHANGELOG.md
@@ -1,3 +1,11 @@
+## 0.3.2+1
+
+* `ansi.dart`
+
+ * The "forScript" code paths now ignore the `ansiOutputEnabled` value. Affects
+ the `escapeForScript` property on `AnsiCode` and the `wrap` and `wrapWith`
+ functions when `forScript` is true.
+
## 0.3.2
* `ansi.dart`
diff --git a/pkgs/io/lib/src/ansi_code.dart b/pkgs/io/lib/src/ansi_code.dart
index 87551ee..8b9c3a1 100644
--- a/pkgs/io/lib/src/ansi_code.dart
+++ b/pkgs/io/lib/src/ansi_code.dart
@@ -21,6 +21,13 @@
Zone.current[AnsiCode] as bool ??
(io.stdout.supportsAnsiEscapes && io.stderr.supportsAnsiEscapes);
+/// Returns `true` no formatting is required for [input].
+bool _isNoop(bool skip, String input, bool forScript) =>
+ skip ||
+ input == null ||
+ input.isEmpty ||
+ !((forScript ?? false) || ansiOutputEnabled);
+
/// Allows overriding [ansiOutputEnabled] to [enableAnsiOutput] for the code run
/// within [body].
T overrideAnsiOutput<T>(bool enableAnsiOutput, T body()) =>
@@ -82,19 +89,18 @@
/// Wraps [value] with the [escape] value for this code, followed by
/// [resetAll].
///
- /// If [forScript] is `true`, the return value is an unescaped literal.
+ /// If [forScript] is `true`, the return value is an unescaped literal. The
+ /// value of [ansiOutputEnabled] is also ignored.
///
/// Returns `value` unchanged if
/// * [value] is `null` or empty
- /// * [ansiOutputEnabled] is `false`
+ /// * both [ansiOutputEnabled] and [forScript] are `false`.
/// * [type] is [AnsiCodeType.reset]
- String wrap(String value, {bool forScript: false}) => (ansiOutputEnabled &&
- type != AnsiCodeType.reset &&
- value != null &&
- value.isNotEmpty)
- ? "${_escapeValue(forScript: forScript)}$value"
- "${reset._escapeValue(forScript: forScript)}"
- : value;
+ String wrap(String value, {bool forScript: false}) =>
+ _isNoop(type == AnsiCodeType.reset, value, forScript)
+ ? value
+ : "${_escapeValue(forScript: forScript)}$value"
+ "${reset._escapeValue(forScript: forScript)}";
@override
String toString() => "$name ${type._name} ($code)";
@@ -102,11 +108,12 @@
/// Returns a [String] formatted with [codes].
///
-/// If [forScript] is `true`, the return value is an unescaped literal.
+/// If [forScript] is `true`, the return value is an unescaped literal. The
+/// value of [ansiOutputEnabled] is also ignored.
///
/// Returns `value` unchanged if
/// * [value] is `null` or empty.
-/// * [ansiOutputEnabled] is `false`.
+/// * both [ansiOutputEnabled] and [forScript] are `false`.
/// * [codes] is empty.
///
/// Throws an [ArgumentError] if
@@ -119,7 +126,7 @@
// Eliminate duplicates
final myCodes = codes.toSet();
- if (myCodes.isEmpty || !ansiOutputEnabled || value == null || value.isEmpty) {
+ if (_isNoop(myCodes.isEmpty, value, forScript)) {
return value;
}
diff --git a/pkgs/io/pubspec.yaml b/pkgs/io/pubspec.yaml
index 36a714d..0e2818e 100644
--- a/pkgs/io/pubspec.yaml
+++ b/pkgs/io/pubspec.yaml
@@ -1,7 +1,7 @@
name: io
description: >
Utilities for the Dart VM Runtime.
-version: 0.3.2
+version: 0.3.2+1
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/io
diff --git a/pkgs/io/test/ansi_code_test.dart b/pkgs/io/test/ansi_code_test.dart
index 5c6805a..7d411ca 100644
--- a/pkgs/io/test/ansi_code_test.dart
+++ b/pkgs/io/test/ansi_code_test.dart
@@ -9,6 +9,7 @@
const _ansiEscapeLiteral = '\x1B';
const _ansiEscapeForScript = '\\033';
+const sampleInput = 'sample input';
void main() {
group('ansiOutputEnabled', () {
@@ -28,6 +29,19 @@
expect(ansiOutputEnabled, isFalse);
});
});
+
+ test('forScript variaents ignore `ansiOutputEnabled`', () {
+ final expected =
+ '$_ansiEscapeForScript[34m$sampleInput$_ansiEscapeForScript[0m';
+
+ for (var override in [true, false]) {
+ overrideAnsiOutput(override, () {
+ expect(blue.escapeForScript, '$_ansiEscapeForScript[34m');
+ expect(blue.wrap(sampleInput, forScript: true), expected);
+ expect(wrapWith(sampleInput, [blue], forScript: true), expected);
+ });
+ }
+ });
});
test('foreground and background colors match', () {
@@ -64,10 +78,8 @@
}
});
- final sampleInput = 'sample input';
-
for (var forScript in [true, false]) {
- group(forScript ? 'for script' : 'literal', () {
+ group(forScript ? 'forScript' : 'escaped', () {
final escapeLiteral =
forScript ? _ansiEscapeForScript : _ansiEscapeLiteral;