shellLine method

String shellLine(
  1. String line
)

Highlights a line of shell/bash.

Implementation

String shellLine(String line) {
  var out = line;

  // Full-line comment
  if (out.trimLeft().startsWith('#')) {
    return '${theme.gray}$out${theme.reset}';
  }

  // Partial comment
  final hash = out.indexOf('#');
  String? commentPart;
  if (hash > 0) {
    commentPart = out.substring(hash);
    out = out.substring(0, hash);
  }

  // Flags
  out = out.replaceAllMapped(
    RegExp(r'(\s|^)(--?[A-Za-z0-9][A-Za-z0-9\-]*)'),
    (m) => '${m[1]}${theme.accent}${m[2]}${theme.reset}',
  );

  // Strings
  out = out.replaceAllMapped(
    RegExp(r'"[^"]*"'),
    (m) => '${theme.highlight}${m[0]}${theme.reset}',
  );
  out = out.replaceAllMapped(
    RegExp(r"'[^']*'"),
    (m) => '${theme.highlight}${m[0]}${theme.reset}',
  );

  // Paths
  out = out.replaceAllMapped(
    RegExp(r'(/[^\s]+)'),
    (m) => '${theme.selection}${m[1]}${theme.reset}',
  );

  if (commentPart != null) {
    out = '$out ${theme.gray}$commentPart${theme.reset}';
  }
  return out;
}