redactPath function

String redactPath(
  1. String input, {
  2. String? home,
})

Strips filesystem identity (username, project name, directory layout) from text that leaves the machine; <home>/…/lib/x.dart becomes <project>/lib/x.dart, package: URIs are untouched, and it is idempotent.

Implementation

String redactPath(String input, {String? home}) {
  if (input.isEmpty) return input;
  var s = input;
  final h = home ?? _homeDir();
  if (h != null && h.length > 1) {
    s = s.replaceAll('$h${Platform.pathSeparator}', '<home>${Platform.pathSeparator}');
    s = s.replaceAll('$h/', '<home>/');
  }
  s = s.replaceAll(_posixHomeRegex, '<home>/');
  s = s.replaceAll(_windowsHomeRegex, r'<home>\');
  s = s.replaceAllMapped(_posixProjectRegex, (m) => '<project>/${m[1]}/');
  s = s.replaceAllMapped(_windowsProjectRegex, (m) => '<project>\\${m[1]}\\');
  return s;
}