From 04e7446b2b5dc18e5fc8ba025d5aab612820693d Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Mon, 7 Nov 2016 15:48:50 -0800 Subject: [PATCH] Make the fire red on black-and-white terminals (#6748) --- .../flutter_tools/lib/src/base/logger.dart | 26 ++++++++++--------- .../lib/src/commands/daemon.dart | 4 +-- packages/flutter_tools/lib/src/globals.dart | 11 ++++++-- packages/flutter_tools/lib/src/hot.dart | 10 ++++++- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/logger.dart b/packages/flutter_tools/lib/src/base/logger.dart index c858a9f3ef..02cc1e693f 100644 --- a/packages/flutter_tools/lib/src/base/logger.dart +++ b/packages/flutter_tools/lib/src/base/logger.dart @@ -26,7 +26,7 @@ abstract class Logger { /// Display normal output of the command. This should be used for things like /// progress messages, success messages, or just normal command output. - void printStatus(String message, { bool emphasis: false, bool newline: true }); + void printStatus(String message, { bool emphasis: false, bool newline: true, String ansiAlternative }); /// Use this for verbose tracing output. Users can turn this output on in order /// to help diagnose issues with the toolchain or with their setup. @@ -60,14 +60,16 @@ class StdoutLogger extends Logger { } @override - void printStatus(String message, { bool emphasis: false, bool newline: true }) { + void printStatus(String message, { bool emphasis: false, bool newline: true, String ansiAlternative }) { _status?.cancel(); _status = null; - + if (terminal.supportsColor && ansiAlternative != null) + message = ansiAlternative; + if (emphasis) + message = terminal.bolden(message); if (newline) - stdout.writeln(emphasis ? terminal.writeBold(message) : message); - else - stdout.write(emphasis ? terminal.writeBold(message) : message); + message = '$message\n'; + stdout.write(message); } @override @@ -106,7 +108,7 @@ class BufferLogger extends Logger { void printError(String message, [StackTrace stackTrace]) => _error.writeln(message); @override - void printStatus(String message, { bool emphasis: false, bool newline: true }) { + void printStatus(String message, { bool emphasis: false, bool newline: true, String ansiAlternative }) { if (newline) _status.writeln(message); else @@ -139,7 +141,7 @@ class VerboseLogger extends Logger { } @override - void printStatus(String message, { bool emphasis: false, bool newline: true }) { + void printStatus(String message, { bool emphasis: false, bool newline: true, String ansiAlternative }) { _emit(_LogType.status, message); } @@ -168,7 +170,7 @@ class VerboseLogger extends Logger { } else { prefix = '+$millis ms'.padLeft(prefixWidth); if (millis >= 100) - prefix = terminal.writeBold(prefix); + prefix = terminal.bolden(prefix); } prefix = '[$prefix] '; @@ -176,11 +178,11 @@ class VerboseLogger extends Logger { String indentMessage = message.replaceAll('\n', '\n$indent'); if (type == _LogType.error) { - stderr.writeln(prefix + terminal.writeBold(indentMessage)); + stderr.writeln(prefix + terminal.bolden(indentMessage)); if (stackTrace != null) stderr.writeln(indent + stackTrace.toString().replaceAll('\n', '\n$indent')); } else if (type == _LogType.status) { - print(prefix + terminal.writeBold(indentMessage)); + print(prefix + terminal.bolden(indentMessage)); } else { print(prefix + indentMessage); } @@ -210,7 +212,7 @@ class AnsiTerminal { bool supportsColor; - String writeBold(String str) => supportsColor ? '$_bold$str$_reset' : str; + String bolden(String str) => supportsColor ? '$_bold$str$_reset' : str; String clearScreen() => supportsColor ? _clear : '\n\n'; diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 47919301e6..ae11b5235a 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -624,7 +624,7 @@ class NotifyingLogger extends Logger { } @override - void printStatus(String message, { bool emphasis: false, bool newline: true }) { + void printStatus(String message, { bool emphasis: false, bool newline: true, String ansiAlternative }) { _messageController.add(new LogMessage('status', message)); } @@ -698,7 +698,7 @@ class _AppRunLogger extends Logger { } @override - void printStatus(String message, { bool emphasis: false, bool newline: true }) { + void printStatus(String message, { bool emphasis: false, bool newline: true, String ansiAlternative }) { _sendLogEvent({ 'log': message }); } diff --git a/packages/flutter_tools/lib/src/globals.dart b/packages/flutter_tools/lib/src/globals.dart index e8fc28f346..0d7f4f9802 100644 --- a/packages/flutter_tools/lib/src/globals.dart +++ b/packages/flutter_tools/lib/src/globals.dart @@ -27,8 +27,15 @@ void printError(String message, [StackTrace stackTrace]) => logger.printError(me /// Display normal output of the command. This should be used for things like /// progress messages, success messages, or just normal command output. -void printStatus(String message, { bool emphasis: false, bool newline: true }) { - logger.printStatus(message, emphasis: emphasis, newline: newline); +/// +/// Set `emphasis` to true to make the output bold if it's supported. +/// +/// Set `newline` to false to skip the trailing linefeed. +/// +/// If `ansiAlternative` is provided, and the terminal supports color, that +/// string will be printed instead of the message. +void printStatus(String message, { bool emphasis: false, bool newline: true, String ansiAlternative }) { + logger.printStatus(message, emphasis: emphasis, newline: newline, ansiAlternative: ansiAlternative); } /// Use this for verbose tracing output. Users can turn this output on in order diff --git a/packages/flutter_tools/lib/src/hot.dart b/packages/flutter_tools/lib/src/hot.dart index 911a3f8df0..0fd5a99990 100644 --- a/packages/flutter_tools/lib/src/hot.dart +++ b/packages/flutter_tools/lib/src/hot.dart @@ -594,7 +594,15 @@ class HotRunner extends ResidentRunner { @override void printHelp({ @required bool details }) { - printStatus('🔥 To hot reload your app on the fly, press "r" or F5. To restart the app entirely, press "R".', emphasis: true); + const String fire = '🔥'; + const String redOnBlack = '\u001B[31;40m'; + const String bold = '\u001B[0;1m'; + const String reset = '\u001B[0m'; + printStatus( + '$fire To hot reload your app on the fly, press "r" or F5. To restart the app entirely, press "R".', + ansiAlternative: '$redOnBlack$fire$bold To ${redOnBlack}hot reload$bold your app on the fly, ' + 'press "r" or F5. To restart the app entirely, press "R".$reset' + ); printStatus('The Observatory debugger and profiler is available at: http://127.0.0.1:$_observatoryPort/'); if (details) { printStatus('To dump the widget hierarchy of the app (debugDumpApp), press "w".');