浏览代码

Shell: Add "history" command that shows command history.

Andreas Kling 6 年之前
父节点
当前提交
16a5a76445
共有 1 个文件被更改,包括 13 次插入1 次删除
  1. 13 1
      Shell/main.cpp

+ 13 - 1
Shell/main.cpp

@@ -19,6 +19,7 @@
 //#define SH_DEBUG
 
 GlobalState g;
+static LineEditor editor;
 
 static void prompt()
 {
@@ -112,6 +113,14 @@ static int sh_cd(int argc, char** argv)
     return 0;
 }
 
+static int sh_history(int, char**)
+{
+    for (int i = 0; i < editor.history().size(); ++i) {
+        printf("%6d  %s\n", i, editor.history()[i].characters());
+    }
+    return 0;
+}
+
 static bool handle_builtin(int argc, char** argv, int& retval)
 {
     if (argc == 0)
@@ -132,6 +141,10 @@ static bool handle_builtin(int argc, char** argv, int& retval)
         retval = sh_export(argc, argv);
         return true;
     }
+    if (!strcmp(argv[0], "history")) {
+        retval = sh_history(argc, argv);
+        return true;
+    }
     return false;
 }
 
@@ -387,7 +400,6 @@ int main(int argc, char** argv)
         free(cwd);
     }
 
-    LineEditor editor;
     for (;;) {
         prompt();
         auto line = editor.get_line();