Ctrl-l support to clear the screen. New sequences added documented.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/linenoise.c b/linenoise.c
index d4d438b..dd43413 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -68,6 +68,17 @@
* 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;
diff --git a/linenoise.h b/linenoise.h
index f503854..76a703c 100644
--- a/linenoise.h
+++ b/linenoise.h
@@ -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 */