@@ -491,33 +491,46 @@ String gray(text) {
491
491
/// that supports that.
492
492
///
493
493
/// Use this to highlight something interesting but neither good nor bad.
494
- String cyan (text) => sparkle ? "$ text " : "$ _cyan $ text $ _noColor " ;
494
+ String cyan (text) => _addColor ( text, _cyan) ;
495
495
496
496
/// Wraps [text] in the ANSI escape codes to color it green when on a platform
497
497
/// that supports that.
498
498
///
499
499
/// Use this to highlight something successful or otherwise positive.
500
- String green (text) => sparkle ? "$ text " : "$ _green $ text $ _noColor " ;
500
+ String green (text) => _addColor ( text, _green) ;
501
501
502
502
/// Wraps [text] in the ANSI escape codes to color it magenta when on a
503
503
/// platform that supports that.
504
504
///
505
505
/// Use this to highlight something risky that the user should be aware of but
506
506
/// may intend to do.
507
- String magenta (text) => sparkle ? "$ text " : "$ _magenta $ text $ _noColor " ;
507
+ String magenta (text) => _addColor ( text, _magenta) ;
508
508
509
509
/// Wraps [text] in the ANSI escape codes to color it red when on a platform
510
510
/// that supports that.
511
511
///
512
512
/// Use this to highlight unequivocal errors, problems, or failures.
513
- String red (text) => sparkle ? "$ text " : "$ _red $ text $ _noColor " ;
513
+ String red (text) => _addColor ( text, _red) ;
514
514
515
515
/// Wraps [text] in the ANSI escape codes to color it yellow when on a platform
516
516
/// that supports that.
517
517
///
518
518
/// Use this to highlight warnings, cautions or other things that are bad but
519
519
/// do not prevent the user's goal from being reached.
520
- String yellow (text) => sparkle ? "$text " : "$_yellow $text $_noColor " ;
520
+ String yellow (text) => _addColor (text, _yellow);
521
+
522
+ /// Returns [text] colored using the given [colorCode] .
523
+ ///
524
+ /// This is resilient to the text containing other colors or bold text.
525
+ String _addColor (Object text, String colorCode) {
526
+ if (sparkle) return text.toString ();
527
+ return colorCode +
528
+ text
529
+ .toString ()
530
+ .replaceAll (_none, _none + colorCode)
531
+ .replaceAll (_noColor, _none + colorCode) +
532
+ _noColor;
533
+ }
521
534
522
535
/// Log function that prints the message to stdout.
523
536
void _logToStdout (Entry entry) {
0 commit comments