toColorString function

String toColorString({
  1. Object message = '',
  2. Object expectedState = '',
  3. Object invalidState = '',
  4. ColorOutput colorOutput = ColorOutput.on,
  5. required Type errorType,
})

Transforms error/exception messages to a colorized output string.

Implementation

String toColorString(
    {Object message = '',
    Object expectedState = '',
    Object invalidState = '',
    ColorOutput colorOutput = ColorOutput.on,
    required Type errorType}) {
  final red = (colorOutput == ColorOutput.on) ? AnsiColor.red.code : '';
  final reset = (colorOutput == ColorOutput.on) ? AnsiColor.reset.code : '';
  final green = (colorOutput == ColorOutput.on) ? AnsiColor.green.code : '';
  final yellow = (colorOutput == ColorOutput.on) ? AnsiColor.yellow.code : '';

  final msg = message.toString().isEmpty
      ? ''
      : '${Error.safeToString(message)}$reset\n';

  final expected = expectedState.toString().isEmpty
      ? ''
      : ' $green Expected state: $reset${Error.safeToString(expectedState)}\n';

  final invalid = invalidState.toString().isEmpty
      ? ''
      : ' $yellow Invalid state: $reset${Error.safeToString(invalidState)}\n';

  return '$red$errorType: $msg$invalid$expected\n';
}