From b5dfc7841a324ae5f577992312cf1c36df760f81 Mon Sep 17 00:00:00 2001 From: Brian Boru Date: Fri, 13 Jul 2018 22:44:24 +0300 Subject: [PATCH] default value, colored prompt --- src/example.c | 2 +- src/linenoise/linenoise.c | 62 ++++++++++++++++++++++++++++++++++++++--------- src/linenoise/linenoise.h | 2 +- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/example.c b/src/example.c index af4133d..0bc99db 100644 --- a/src/example.c +++ b/src/example.c @@ -21,7 +21,7 @@ int main (void) { char *line; linenoiseHistoryLoadFile("history.txt"); linenoiseACHook = linenoiseAC; - while ((line = linenoise("hello> ")) != NULL) { + while ((line = linenoise("\e[32mhello\e[37m> ", getenv("USER"))) != NULL) { if (line[0] != '\0') { if (!strcmp(line, "quit")) break; printf("echo: '%s'\n", line); diff --git a/src/linenoise/linenoise.c b/src/linenoise/linenoise.c index 1e55e66..33a9e97 100644 --- a/src/linenoise/linenoise.c +++ b/src/linenoise/linenoise.c @@ -87,6 +87,7 @@ #include #include #include +#include #include #include #include @@ -225,9 +226,9 @@ static void buildBrcBuf (const char *buf) { } -static int refreshLine (int fd, const char *prompt, const char *buf, size_t len, size_t pos, size_t cols) { +static int refreshLine (int fd, const char *prompt, size_t plen, const char *buf, size_t len, size_t pos, size_t cols) { char seq[64]; - size_t plen = strlen(prompt); + //size_t plen = strlen(prompt); size_t hipos = len; // if (linenoiseOptHilightBrackets && pos < len && strchr("()[]", buf[pos])) { @@ -444,13 +445,46 @@ static void doWordWork (char *buf, size_t *posp, size_t *lenp, int action) { } } +static size_t prompt_len (const char *prompt, size_t *nlen) { + const char *p = prompt; + size_t len = 0; + *nlen = 0; + while (*p) { + if (*p == '\e') { + const char *q = p+1; + ++len; + if (*q++ == '[') { + while (*q && isdigit(*q)) { + ++q; ++len; + } + *nlen += (uintptr_t)(++q) - (uintptr_t)p; + p = q; + len += 2; + continue; + } + ++len; + } + ++p; + ++len; + } + return len; +} + +#define clear() printf("\033[H\033[J") +#define gotoxy(x,y) printf("\033[%d;%dH", (x), (y)) -static int linenoisePrompt (int fd, char *buf, const char *prompt) { - size_t plen = strlen(prompt), pos = 0, len = 0, cols = getColumns(); +static int linenoisePrompt (int fd, char *buf, const char *prompt, const char *defval) { + //size_t plen = strlen(prompt), pos = 0, len = 0, cols = getColumns(), tmp_len; + size_t nlen = 0; + size_t plen = prompt_len(prompt, &nlen), pos = 0, len = 0, cols = getColumns(), tmp_len; size_t buflen = LINENOISE_MAX_LINE_LEN-1; /* make sure there is always space for the nulterm */ + plen -= nlen; int historyIndex = 0; // buf[0] = '\0'; + if (defval && (tmp_len = strlen(defval) < LINENOISE_MAX_LINE_LEN-1)) { + insertText(defval, buf, &pos, &len); + } /* the latest history entry is always our current buffer, that initially is just an empty string */ linenoiseHistoryAdd(""); if (write(fd, prompt, plen) == -1) return -1; @@ -458,23 +492,23 @@ static int linenoisePrompt (int fd, char *buf, const char *prompt) { char c, seq[2]; int nread; // - if (refreshLine(fd, prompt, buf, len, pos, cols)) return len; + if (refreshLine(fd, prompt, plen, buf, len, pos, cols)) return len; nread = read(fd, &c, 1); if (nread <= 0) return len; switch (c) { case 13: /* enter */ pos = len; - refreshLine(fd, prompt, buf, len, pos, cols); + refreshLine(fd, prompt, plen, buf, len, pos, cols); free(history[--historyLen]); return len; case 4: /* ctrl-d */ pos = len; - refreshLine(fd, prompt, buf, len, pos, cols); + refreshLine(fd, prompt, plen, buf, len, pos, cols); free(history[--historyLen]); return (len == 0) ? -1 : (int)len; case 3: /* ctrl-c */ pos = len; - refreshLine(fd, prompt, buf, len, pos, cols); + refreshLine(fd, prompt, plen, buf, len, pos, cols); free(history[--historyLen]); errno = EAGAIN; return -1; @@ -669,30 +703,34 @@ skip_special: do { if (read(fd, &c, 1) == -1) break; } while (isdigit(c) || c } -static int linenoiseRaw (char *buf, const char *prompt) { +static int linenoiseRaw (char *buf, const char *prompt, const char *defval) { int count; // /* if (buflen == 0) { errno = EINVAL; return -1; } */ if (enableRawMode(STDIN_FILENO) == -1) return -1; - count = linenoisePrompt(STDIN_FILENO, buf, prompt); + count = linenoisePrompt(STDIN_FILENO, buf, prompt, defval); disableRawMode(STDIN_FILENO); fprintf(stdout, "\n"); fflush(stdout); return count; } -char *linenoise (const char *prompt) { +char *linenoise (const char *prompt, const char *defval) { static char buf[LINENOISE_MAX_LINE_LEN]; // if (isUnsupportedTerm() && !isatty(STDIN_FILENO)) { int count; // fprintf(stdout, "%s", prompt); fflush(stdout); + if (defval && strlen(defval) < LINENOISE_MAX_LINE_LEN) { + strcpy(buf, defval); + fprintf(stdout, "%s", buf); fflush(stdout); + } if (fgets(buf, LINENOISE_MAX_LINE_LEN, stdin) == NULL) return NULL; count = strlen(buf)-1; while (count >= 0 && (buf[count] == '\n' || buf[count] == '\r')) buf[count--] = '\0'; } else { - if (linenoiseRaw(buf, prompt) < 0) return NULL; + if (linenoiseRaw(buf, prompt, defval) < 0) return NULL; } return buf; } diff --git a/src/linenoise/linenoise.h b/src/linenoise/linenoise.h index f0875b4..abff7bc 100644 --- a/src/linenoise/linenoise.h +++ b/src/linenoise/linenoise.h @@ -43,7 +43,7 @@ extern "C" { /* DO NOT free result; DO NOT call it recursive or in multithread app w/o locking */ -extern char *linenoise (const char *prompt); +extern char *linenoise (const char *prompt, const char *defval); extern int linenoiseHistoryAdd (const char *line); // !0: error extern int linenoiseHistorySetMaxLen (int len); // !0: error extern void linenoiseClearHistory (void); -- 2.11.4.GIT