]> git.lizzy.rs Git - linenoise.git/blobdiff - linenoise.c
New feature: Accessor for the history max len.
[linenoise.git] / linenoise.c
index 5c1672a08c9523cf80345ed39c82b60c0eb48700..21f62f5061b6250203062642b2d9224db3768eaf 100644 (file)
  * flickering effect with some slow terminal, but the lesser sequences
  * the more compatible.
  *
- * CHA (Cursor Horizontal Absolute)
- *    Sequence: ESC [ n G
- *    Effect: moves cursor to column n (1 based)
- *
  * EL (Erase Line)
  *    Sequence: ESC [ n K
  *    Effect: if n is 0 or missing, clear from cursor to end of line
  *    Sequence: ESC [ n C
  *    Effect: moves cursor forward of n chars
  *
+ * CR (Carriage Return)
+ *    Sequence: \r
+ *    Effect: moves cursor to column 1
+ *
  * The following are used to clear the screen: ESC [ H ESC [ 2 J
  * This is actually composed of two sequences:
  *
@@ -187,6 +187,7 @@ void linenoiseHistoryFree(void) {
             free(history[j]);
         free(history);
         history = NULL;
+        history_len = 0;
     }
 }
 
@@ -268,7 +269,10 @@ static void linenoiseAtExit(void) {
     linenoiseHistoryFree();
 }
 
-/* gcc/glibc insists that we care about the return code of write! */
+/* gcc/glibc insists that we care about the return code of write!
+ * Clarification: This means that a void-cast like "(void) (EXPR)"
+ * does not work.
+ */
 #define IGNORE_RC(EXPR) if (EXPR) {}
 
 /* This is fdprintf() on some systems, but use a different
@@ -293,7 +297,7 @@ static void clearScreen(struct current *current)
 
 static void cursorToLeft(struct current *current)
 {
-    fd_printf(current->fd, "\x1b[1G");
+    fd_printf(current->fd, "\r");
 }
 
 static int outputChars(struct current *current, const char *buf, int len)
@@ -303,7 +307,7 @@ static int outputChars(struct current *current, const char *buf, int len)
 
 static void outputControlChar(struct current *current, char ch)
 {
-    fd_printf(current->fd, "\033[7m^%c\033[0m", ch);
+    fd_printf(current->fd, "\x1b[7m^%c\x1b[0m", ch);
 }
 
 static void eraseEol(struct current *current)
@@ -313,7 +317,7 @@ static void eraseEol(struct current *current)
 
 static void setCursorPos(struct current *current, int x)
 {
-    fd_printf(current->fd, "\x1b[1G\x1b[%dC", x);
+    fd_printf(current->fd, "\r\x1b[%dC", x);
 }
 
 /**
@@ -392,8 +396,8 @@ static int getWindowSize(struct current *current)
     if (current->cols == 0) {
         current->cols = 80;
 
-        /* Move cursor far right and report cursor position */
-        fd_printf(current->fd, "\x1b[999G" "\x1b[6n");
+        /* Move cursor far right and report cursor position, then back to the left */
+        fd_printf(current->fd, "\x1b[999C" "\x1b[6n");
 
         /* Parse the response: ESC [ rows ; cols R */
         if (fd_read_char(current->fd, 100) == 0x1b && fd_read_char(current->fd, 100) == '[') {
@@ -538,7 +542,7 @@ static int outputChars(struct current *current, const char *buf, int len)
 {
     COORD pos = { (SHORT)current->x, (SHORT)current->y };
     DWORD n;
-       
+
     WriteConsoleOutputCharacter(current->outh, buf, len, pos, &n);
     current->x += len;
     return 0;
@@ -955,7 +959,7 @@ static int linenoisePrompt(struct current *current) {
         /* Only autocomplete when the callback is set. It returns < 0 when
          * there was an error reading from fd. Otherwise it will return the
          * character that should be handled next. */
-        if (c == 9 && completionCallback != NULL) {
+        if (c == '\t' && current->pos == current->chars && completionCallback != NULL) {
             c = completeLine(current);
             /* Return on errors */
             if (c < 0) return current->len;
@@ -1226,10 +1230,10 @@ char *linenoise(const char *prompt)
     char buf[LINENOISE_MAX_LINE];
 
     if (enableRawMode(&current) == -1) {
-       printf("%s", prompt);
+        printf("%s", prompt);
         fflush(stdout);
         if (fgets(buf, sizeof(buf), stdin) == NULL) {
-               return NULL;
+            return NULL;
         }
         count = strlen(buf);
         if (count && buf[count-1] == '\n') {
@@ -1284,6 +1288,10 @@ int linenoiseHistoryAdd(const char *line) {
     return 1;
 }
 
+int linenoiseHistoryGetMaxLen(void) {
+    return history_max_len;
+}
+
 int linenoiseHistorySetMaxLen(int len) {
     char **newHistory;