getRelativeMemoryPath function

String getRelativeMemoryPath(
  1. String filePath
)

Get a short relative display path for a memory file. Prefers ~/... or ./... notation, returns the shorter one.

Implementation

String getRelativeMemoryPath(String filePath) {
  final homeDir = Platform.environment['HOME'] ?? '';
  final cwd = Directory.current.path;

  // Calculate relative paths
  final relativeToHome = filePath.startsWith(homeDir)
      ? '~${filePath.substring(homeDir.length)}'
      : null;

  final relativeToCwd = filePath.startsWith(cwd)
      ? './${_relativePath(cwd, filePath)}'
      : null;

  // Return the shorter path, or absolute if neither is applicable
  if (relativeToHome != null && relativeToCwd != null) {
    return relativeToHome.length <= relativeToCwd.length
        ? relativeToHome
        : relativeToCwd;
  }

  return relativeToHome ?? relativeToCwd ?? filePath;
}