Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(953)

Unified Diff: lib/src/log.dart

Issue 1852513004: Make life a little more sparkly. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: Code review changes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/command_runner.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/log.dart
diff --git a/lib/src/log.dart b/lib/src/log.dart
index 3f31a39d212d328de629256d83f980f134ca6d08..a235f05bca35d3a070b1ac047ddf239b39cef416 100644
--- a/lib/src/log.dart
+++ b/lib/src/log.dart
@@ -29,6 +29,9 @@ Verbosity verbosity = Verbosity.NORMAL;
/// Whether or not to log entries with prejudice.
bool withPrejudice = false;
+/// Whether or not to log entries sparklily.
+bool sparkle = false;
+
/// In cases where there's a ton of log spew, make sure we don't eat infinite
/// memory.
///
@@ -53,11 +56,15 @@ final _green = getSpecial('\u001b[32m');
final _magenta = getSpecial('\u001b[35m');
final _red = getSpecial('\u001b[31m');
final _yellow = getSpecial('\u001b[33m');
+final _blue = getSpecial('\u001b[34m');
final _gray = getSpecial('\u001b[1;30m');
final _none = getSpecial('\u001b[0m');
final _noColor = getSpecial('\u001b[39m');
final _bold = getSpecial('\u001b[1m');
+/// All color codees.
+var _allColors = [_cyan, _green, _magenta, _red, _yellow, _blue, ''];
+
/// An enum type for defining the different logging levels a given message can
/// be associated with.
///
@@ -243,15 +250,24 @@ final _capitalizedAnsiEscape = new RegExp(r'\u001b\[\d+(;\d+)?M');
/// Returns [string] formatted as it would be if it were logged.
String format(String string) {
- if (!withPrejudice) return string;
+ if (sparkle) {
+ string = string.replaceAllMapped(new RegExp(r'.'), (match) {
+ var char = "${choose(_allColors)}${match[0]}$_noColor";
+ return (withPrejudice || random.nextBool()) ? char : "$_bold$char$_none";
+ });
+ }
- // [toUpperCase] can corrupt terminal colorings, so fix them up using
- // [replaceAllMapped].
- string = string.toUpperCase().replaceAllMapped(_capitalizedAnsiEscape,
- (match) => match[0].toLowerCase());
+ if (withPrejudice) {
+ // [toUpperCase] can corrupt terminal colorings, so fix them up using
+ // [replaceAllMapped].
+ string = string.toUpperCase().replaceAllMapped(_capitalizedAnsiEscape,
+ (match) => match[0].toLowerCase());
- // Don't use [bold] because it's disabled under [withPrejudice].
- return "$_bold$string$_none";
+ // Don't use [bold] because it's disabled under [withPrejudice].
+ string = "$_bold$string$_none";
+ }
+
+ return string;
}
/// Logs an asynchronous IO operation.
@@ -454,7 +470,7 @@ void collapsible(String message, String template) {
///
/// This is disabled under [withPrejudice] since all text is bold with
/// prejudice.
-String bold(text) => withPrejudice ? text : "$_bold$text$_none";
+String bold(text) => (withPrejudice || sparkle) ? "$text" : "$_bold$text$_none";
/// Wraps [text] in the ANSI escape codes to make it gray when on a platform
/// that supports that.
@@ -463,40 +479,43 @@ String bold(text) => withPrejudice ? text : "$_bold$text$_none";
///
/// The gray marker also enables bold, so it needs to be handled specially with
/// [withPrejudice] to avoid disabling bolding entirely.
-String gray(text) =>
- withPrejudice ? "$_gray$text$_noColor" : "$_gray$text$_none";
+String gray(text) {
+ if (sparkle) return "$text";
+ if (withPrejudice) return "$_gray$text$_noColor";
+ return "$_gray$text$_none";
+}
/// Wraps [text] in the ANSI escape codes to color it cyan when on a platform
/// that supports that.
///
/// Use this to highlight something interesting but neither good nor bad.
-String cyan(text) => "$_cyan$text$_noColor";
+String cyan(text) => sparkle ? "$text" : "$_cyan$text$_noColor";
/// Wraps [text] in the ANSI escape codes to color it green when on a platform
/// that supports that.
///
/// Use this to highlight something successful or otherwise positive.
-String green(text) => "$_green$text$_noColor";
+String green(text) => sparkle ? "$text" : "$_green$text$_noColor";
/// Wraps [text] in the ANSI escape codes to color it magenta when on a
/// platform that supports that.
///
/// Use this to highlight something risky that the user should be aware of but
/// may intend to do.
-String magenta(text) => "$_magenta$text$_noColor";
+String magenta(text) => sparkle ? "$text" : "$_magenta$text$_noColor";
/// Wraps [text] in the ANSI escape codes to color it red when on a platform
/// that supports that.
///
/// Use this to highlight unequivocal errors, problems, or failures.
-String red(text) => "$_red$text$_noColor";
+String red(text) => sparkle ? "$text" : "$_red$text$_noColor";
/// Wraps [text] in the ANSI escape codes to color it yellow when on a platform
/// that supports that.
///
/// Use this to highlight warnings, cautions or other things that are bad but
/// do not prevent the user's goal from being reached.
-String yellow(text) => "$_yellow$text$_noColor";
+String yellow(text) => sparkle ? "$text" : "$_yellow$text$_noColor";
/// Log function that prints the message to stdout.
void _logToStdout(Entry entry) {
« no previous file with comments | « lib/src/command_runner.dart ('k') | lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698