|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:colorize/colorize.dart'; |
| 8 | +import 'package:meta/meta.dart'; |
| 9 | + |
| 10 | +export 'package:colorize/colorize.dart' show Styles; |
| 11 | + |
| 12 | +/// True if color should be applied. |
| 13 | +/// |
| 14 | +/// Defaults to autodetecting stdout. |
| 15 | +@visibleForTesting |
| 16 | +bool useColorForOutput = stdout.supportsAnsiEscapes; |
| 17 | + |
| 18 | +String _colorizeIfAppropriate(String string, Styles color) { |
| 19 | + if (!useColorForOutput) { |
| 20 | + return string; |
| 21 | + } |
| 22 | + return Colorize(string).apply(color).toString(); |
| 23 | +} |
| 24 | + |
| 25 | +/// Prints [message] in green, if the environment supports color. |
| 26 | +void printSuccess(String message) { |
| 27 | + print(_colorizeIfAppropriate(message, Styles.GREEN)); |
| 28 | +} |
| 29 | + |
| 30 | +/// Prints [message] in yellow, if the environment supports color. |
| 31 | +void printWarning(String message) { |
| 32 | + print(_colorizeIfAppropriate(message, Styles.YELLOW)); |
| 33 | +} |
| 34 | + |
| 35 | +/// Prints [message] in red, if the environment supports color. |
| 36 | +void printError(String message) { |
| 37 | + print(_colorizeIfAppropriate(message, Styles.RED)); |
| 38 | +} |
| 39 | + |
| 40 | +/// Returns [message] with escapes to print it in [color], if the environment |
| 41 | +/// supports color. |
| 42 | +String colorizeString(String message, Styles color) { |
| 43 | + return _colorizeIfAppropriate(message, color); |
| 44 | +} |
0 commit comments