浏览代码

Shell: Shorten $HOME to '~' in shell prompts

The "\w" substitution in shell prompts now uses '~' to represent the
user's home directory (technically, whatever $HOME contains.)
Andreas Kling 5 年之前
父节点
当前提交
fb2be5c404
共有 1 个文件被更改,包括 9 次插入2 次删除
  1. 9 2
      Shell/main.cpp

+ 9 - 2
Shell/main.cpp

@@ -88,9 +88,16 @@ static String prompt()
             case 'h':
                 builder.append(g.hostname);
                 break;
-            case 'w':
-                builder.append(g.cwd);
+            case 'w': {
+                String home_path = getenv("HOME");
+                if (g.cwd.starts_with(home_path)) {
+                    builder.append('~');
+                    builder.append(g.cwd.substring_view(home_path.length(), g.cwd.length() - home_path.length()));
+                } else {
+                    builder.append(g.cwd);
+                }
                 break;
+            }
             case 'p':
                 builder.append(g.uid == 0 ? '#' : '$');
                 break;