]> git.lizzy.rs Git - linenoise.git/commitdiff
Ctrl-l support to clear the screen. New sequences added documented.
authorantirez <antirez@gmail.com>
Wed, 1 Dec 2010 10:11:55 +0000 (11:11 +0100)
committerantirez <antirez@gmail.com>
Wed, 1 Dec 2010 10:11:55 +0000 (11:11 +0100)
linenoise.c
linenoise.h

index d4d438ba3dd480d1a9e987d7122ae2b8962b8577..dd43413661ca9677c51667ca657daeca50706307 100644 (file)
  * CUF (CUrsor Forward)
  *    Sequence: ESC [ n C
  *    Effect: moves cursor forward of n chars
+ *
+ * The following are used to clear the screen: ESC [ H ESC [ 2 J
+ * This is actually composed of two sequences:
+ *
+ * cursorhome
+ *    Sequence: ESC [ H
+ *    Effect: moves the cursor to upper left corner
+ *
+ * ED2 (Clear entire screen)
+ *    Sequence: ESC [ 2 J
+ *    Effect: clear the whole screen
  * 
  */
 
@@ -267,6 +278,10 @@ static int completeLine(int fd, const char *prompt, char *buf, size_t buflen, si
     return c; /* Return last read character */
 }
 
+void linenoiseClearScreen(void) {
+    write(STDIN_FILENO,"\x1b[H\x1b[2J",7);
+}
+
 static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt) {
     size_t plen = strlen(prompt);
     size_t pos = 0;
@@ -434,6 +449,9 @@ up_down_arrow:
             pos = len;
             refreshLine(fd,prompt,buf,len,pos,cols);
             break;
+        case 12: /* ctrl+l, clear screen */
+            linenoiseClearScreen();
+            refreshLine(fd,prompt,buf,len,pos,cols);
         }
     }
     return len;
index f503854b61f669eecdc8a6c62c439c9a7324ab3b..76a703c28480d91dade14fe0b7f6456813dc8893 100644 (file)
@@ -50,5 +50,6 @@ int linenoiseHistoryAdd(const char *line);
 int linenoiseHistorySetMaxLen(int len);
 int linenoiseHistorySave(char *filename);
 int linenoiseHistoryLoad(char *filename);
+void linenoiseClearScreen(void);
 
 #endif /* __LINENOISE_H */