formatDiff method

String formatDiff(
  1. String diff, {
  2. bool colorize = true,
})

Formats a unified diff string with optional ANSI colors.

Implementation

String formatDiff(String diff, {bool colorize = true}) {
  if (!colorize) return diff;
  final lines = diff.split('\n');
  final buf = StringBuffer();
  for (final line in lines) {
    if (line.startsWith('+')) {
      buf.writeln('${theme.success}$line${theme.reset}');
    } else if (line.startsWith('-')) {
      buf.writeln('${theme.error}$line${theme.reset}');
    } else if (line.startsWith('@@')) {
      buf.writeln('${theme.info}$line${theme.reset}');
    } else {
      buf.writeln(line);
    }
  }
  return buf.toString().trimRight();
}