Преглед на файлове

Shell: Implement support for terminal clearing with ^L.

Make LineEditor::get_line() responsible for printing the prompt. That way
we can re-prompt after clearing the screen on ^L.

This makes the Serenity Terminal feel a little bit more like home :^)
Andreas Kling преди 6 години
родител
ревизия
253e391bfc
променени са 3 файла, в които са добавени 23 реда и са изтрити 11 реда
  1. 14 1
      Shell/LineEditor.cpp
  2. 1 1
      Shell/LineEditor.h
  3. 8 9
      Shell/main.cpp

+ 14 - 1
Shell/LineEditor.cpp

@@ -37,8 +37,11 @@ void LineEditor::append(const String& string)
     m_cursor = m_buffer.size();
     m_cursor = m_buffer.size();
 }
 }
 
 
-String LineEditor::get_line()
+String LineEditor::get_line(const String& prompt)
 {
 {
+    fputs(prompt.characters(), stdout);
+    fflush(stdout);
+
     m_history_cursor = m_history.size();
     m_history_cursor = m_history.size();
     m_cursor = 0;
     m_cursor = 0;
     for (;;) {
     for (;;) {
@@ -190,6 +193,16 @@ String LineEditor::get_line()
                     do_backspace();
                     do_backspace();
                 continue;
                 continue;
             }
             }
+            if (ch == 0xc) { // ^L
+                printf("\033[3J\033[H\033[2J"); // Clear screen.
+                fputs(prompt.characters(), stdout);
+                for (int i = 0; i < m_buffer.size(); ++i)
+                    fputc(m_buffer[i], stdout);
+                if (m_cursor < m_buffer.size())
+                    printf("\033[%dD", m_buffer.size() - m_cursor); // Move cursor N steps left.
+                fflush(stdout);
+                continue;
+            }
             putchar(ch);
             putchar(ch);
             fflush(stdout);
             fflush(stdout);
             if (ch == '\n') {
             if (ch == '\n') {

+ 1 - 1
Shell/LineEditor.h

@@ -8,7 +8,7 @@ public:
     LineEditor();
     LineEditor();
     ~LineEditor();
     ~LineEditor();
 
 
-    String get_line();
+    String get_line(const String& prompt);
 
 
     void add_to_history(const String&);
     void add_to_history(const String&);
     const Vector<String>& history() const { return m_history; }
     const Vector<String>& history() const { return m_history; }

+ 8 - 9
Shell/main.cpp

@@ -25,15 +25,15 @@
 GlobalState g;
 GlobalState g;
 static LineEditor editor;
 static LineEditor editor;
 
 
-static void prompt()
+static String prompt()
 {
 {
     if (g.uid == 0)
     if (g.uid == 0)
-        printf("# ");
-    else {
-        printf("\033]0;%s@%s:%s\007", g.username.characters(), g.hostname, g.cwd.characters());
-        printf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g.username.characters(), g.hostname, g.cwd.characters());
-    }
-    fflush(stdout);
+        return "# ";
+
+    StringBuilder builder;
+    builder.appendf("\033]0;%s@%s:%s\007", g.username.characters(), g.hostname, g.cwd.characters());
+    builder.appendf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g.username.characters(), g.hostname, g.cwd.characters());
+    return builder.to_string();
 }
 }
 
 
 static int sh_pwd(int, char**)
 static int sh_pwd(int, char**)
@@ -602,8 +602,7 @@ int main(int argc, char** argv)
     atexit(save_history);
     atexit(save_history);
 
 
     for (;;) {
     for (;;) {
-        prompt();
-        auto line = editor.get_line();
+        auto line = editor.get_line(prompt());
         if (line.is_empty())
         if (line.is_empty())
             continue;
             continue;
         run_command(line);
         run_command(line);