From e20e7f11d3944f4e2aec0fc83519bfb028e566ee Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Fri, 7 Mar 2025 15:30:41 -0500 Subject: [PATCH 01/22] Reduce variable scope --- linenoise.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index 42db2e9..70099e5 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -371,7 +371,6 @@ inline void SendSequence(LPCWSTR seq) inline void InterpretEscSeq(void) { - int i; WORD attribute; CONSOLE_SCREEN_BUFFER_INFO Info; CONSOLE_CURSOR_INFO CursInfo; @@ -401,7 +400,7 @@ inline void InterpretEscSeq(void) { case 'm': if (es_argc == 0) es_argv[es_argc++] = 0; - for (i = 0; i < es_argc; i++) + for (int i = 0; i < es_argc; i++) { if (30 <= es_argv[i] && es_argv[i] <= 37) grm.foreground = es_argv[i] - 30; From 48b55c53e87666b40b1707f142f444c6834cc986 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Fri, 7 Mar 2025 16:25:43 -0500 Subject: [PATCH 02/22] Make most linenoise functions into methods on a class. --- example/example.cpp | 26 ++- linenoise.hpp | 498 ++++++++++++++++++++++++-------------------- 2 files changed, 290 insertions(+), 234 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 7be6f71..eaa4d89 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -7,14 +7,22 @@ int main(int argc, const char** argv) { const auto path = "history.txt"; +#ifdef _WIN32 + const char *prompt = "hello> "; +#else + const char *prompt = "\033[32mこんにちは\x1b[0m> "; +#endif + + linenoise::linenoiseState l(prompt); + // Enable the multi-line mode - linenoise::SetMultiLine(true); + l.EnableMultiLine(); // Set max length of the history - linenoise::SetHistoryMaxLen(4); + l.SetHistoryMaxLen(4); // Setup completion words every time when a user types - linenoise::SetCompletionCallback([](const char* editBuffer, std::vector& completions) { + l.SetCompletionCallback([](const char* editBuffer, std::vector& completions) { if (editBuffer[0] == 'h') { #ifdef _WIN32 completions.push_back("hello こんにちは"); @@ -27,15 +35,11 @@ int main(int argc, const char** argv) }); // Load history - linenoise::LoadHistory(path); + l.LoadHistory(path); while (true) { std::string line; -#ifdef _WIN32 - auto quit = linenoise::Readline("hello> ", line); -#else - auto quit = linenoise::Readline("\033[32mこんにちは\x1b[0m> ", line); -#endif + auto quit = l.Readline(line); if (quit) { break; @@ -44,10 +48,10 @@ int main(int argc, const char** argv) cout << "echo: '" << line << "'" << endl; // Add line to history - linenoise::AddHistory(line.c_str()); + l.AddHistory(line.c_str()); // Save history - linenoise::SaveHistory(path); + l.SaveHistory(path); } return 0; diff --git a/linenoise.hpp b/linenoise.hpp index 70099e5..b5fe26f 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -145,22 +145,95 @@ #pragma warning(push) #pragma warning(disable : 4996) #endif -#include -#include +#include +#include #include -#include -#include -#include -#include #include #include -#include #include +#include +#include +#include +#include +#include +#include +#include namespace linenoise { +#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 +#define LINENOISE_MAX_LINE 4096 + typedef std::function&)> CompletionCallback; +/* The linenoiseState structure represents the state during line editing, and + * provides methods by which user programs can act on that state. */ +class linenoiseState { + public: + linenoiseState(const char *prompt = NULL, int stdin_fd = STDIN_FILENO, + int stdout_fd = STDOUT_FILENO); + + void EnableMultiLine(); + void DisableMultiLine(); + + bool AddHistory(const char *line); + bool LoadHistory(const char *path); + bool SaveHistory(const char *path); + const std::vector &GetHistory() { return history; }; + bool SetHistoryMaxLen(size_t len); + + bool Readline(std::string &line); // Primary linenoise entry point + void RefreshLine(); // Restore current line + + /* Register a callback function to be called for tab-completion. */ + void SetCompletionCallback(CompletionCallback fn) { + completionCallback = fn; + }; + + std::string prompt = std::string("> "); /* Prompt to display. */ + std::mutex r_mutex; + + private: + std::string Readline(bool &quit); + std::string Readline(); + + bool linenoiseRaw(std::string &line); + int linenoiseEditInsert(const char *cbuf, int clen); + void linenoiseEditDelete(); + void linenoiseEditBackspace(); + void linenoiseEditDeletePrevWord(); + int linenoiseEdit(); + void linenoiseEditHistoryNext(int dir); + void linenoiseEditMoveLeft(); + void linenoiseEditMoveRight(); + void linenoiseEditMoveHome(); + void linenoiseEditMoveEnd(); + void refreshSingleLine(); + void refreshMultiLine(); + int completeLine(char *cbuf, int *c); + + bool isUnsupportedTerm(void); + void SetMultiLine(bool ml); + + CompletionCallback completionCallback; + + int ifd = STDIN_FILENO; /* Terminal stdin file descriptor. */ + int ofd = STDOUT_FILENO; /* Terminal stdout file descriptor. */ + char *buf = wbuf; /* Edited line buffer. */ + int buflen = LINENOISE_MAX_LINE; /* Edited line buffer size. */ + int pos = 0; /* Current cursor position. */ + int oldcolpos = 0; /* Previous refresh cursor column position. */ + int len = 0; /* Current edited line length. */ + int lcols = -1; /* Number of columns in terminal. */ + int maxrows = 0; /* Maximum num of rows used so far (multiline mode) */ + int history_index = -1; /* The history index we are currently editing. */ + char wbuf[LINENOISE_MAX_LINE] = {'\0'}; + std::string history_tmpbuf; + + size_t history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; + std::vector history; +}; + #ifdef _WIN32 namespace ansi { @@ -1059,8 +1132,6 @@ inline int win32_write(int fd, const void *buffer, unsigned int count) { } #endif // _WIN32 -#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 -#define LINENOISE_MAX_LINE 4096 static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; static CompletionCallback completionCallback; @@ -1073,23 +1144,6 @@ static bool atexit_registered = false; /* Register atexit just 1 time. */ static size_t history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; static std::vector history; -/* The linenoiseState structure represents the state during line editing. - * We pass this state to functions implementing specific editing - * functionalities. */ -struct linenoiseState { - int ifd; /* Terminal stdin file descriptor. */ - int ofd; /* Terminal stdout file descriptor. */ - char *buf; /* Edited line buffer. */ - int buflen; /* Edited line buffer size. */ - std::string prompt; /* Prompt to display. */ - int pos; /* Current cursor position. */ - int oldcolpos; /* Previous refresh cursor column position. */ - int len; /* Current edited line length. */ - int cols; /* Number of columns in terminal. */ - int maxrows; /* Maximum num of rows used so far (multiline mode) */ - int history_index; /* The history index we are currently editing. */ -}; - enum KEY_ACTION { KEY_NULL = 0, /* NULL */ CTRL_A = 1, /* Ctrl+a */ @@ -1114,7 +1168,6 @@ enum KEY_ACTION { void linenoiseAtExit(void); bool AddHistory(const char *line); -void refreshLine(struct linenoiseState *l); /* ============================ UTF8 utilities ============================== */ @@ -1572,13 +1625,13 @@ inline int unicodeReadUTF8Char(int fd, char* buf, int* cp) /* ======================= Low level terminal handling ====================== */ /* Set if to use or not the multi line mode. */ -inline void SetMultiLine(bool ml) { +void linenoiseState::SetMultiLine(bool ml) { mlmode = ml; } /* Return true if the terminal name is in the list of terminals we know are * not able to understand basic escape sequences. */ -inline bool isUnsupportedTerm(void) { +bool linenoiseState::isUnsupportedTerm(void) { #ifndef _WIN32 char *term = getenv("TERM"); int j; @@ -1762,12 +1815,12 @@ inline void linenoiseBeep(void) { * * The state of the editing is encapsulated into the pointed linenoiseState * structure as described in the structure definition. */ -inline int completeLine(struct linenoiseState *ls, char *cbuf, int *c) { +int linenoiseState::completeLine(char *cbuf, int *c) { std::vector lc; int nread = 0, nwritten; *c = 0; - completionCallback(ls->buf,lc); + completionCallback(buf,lc); if (lc.empty()) { linenoiseBeep(); } else { @@ -1776,26 +1829,27 @@ inline int completeLine(struct linenoiseState *ls, char *cbuf, int *c) { while(!stop) { /* Show completion or original buffer */ if (i < static_cast(lc.size())) { - struct linenoiseState saved = *ls; - - ls->len = ls->pos = static_cast(lc[i].size()); - ls->buf = &lc[i][0]; - refreshLine(ls); - ls->len = saved.len; - ls->pos = saved.pos; - ls->buf = saved.buf; - } else { - refreshLine(ls); + int old_len = len; + int old_pos = pos; + char *old_buf = buf; + len = pos = static_cast(lc[i].size()); + buf = &lc[i][0]; + RefreshLine(); + len = old_len; + pos = old_pos; + buf = old_buf; + } else { + RefreshLine(); } - //nread = read(ls->ifd,&c,1); + //nread = read(ifd,&c,1); #ifdef _WIN32 nread = win32read(c); if (nread == 1) { cbuf[0] = *c; } #else - nread = unicodeReadUTF8Char(ls->ifd,cbuf,c); + nread = unicodeReadUTF8Char(ifd,cbuf,c); #endif if (nread <= 0) { *c = -1; @@ -1809,14 +1863,14 @@ inline int completeLine(struct linenoiseState *ls, char *cbuf, int *c) { break; case 27: /* escape */ /* Re-show original buffer */ - if (i < static_cast(lc.size())) refreshLine(ls); + if (i < static_cast(lc.size())) RefreshLine(); stop = 1; break; default: /* Update buffer and return */ if (i < static_cast(lc.size())) { - nwritten = snprintf(ls->buf,ls->buflen,"%s",&lc[i][0]); - ls->len = ls->pos = nwritten; + nwritten = snprintf(buf,buflen,"%s",&lc[i][0]); + len = pos = nwritten; } stop = 1; break; @@ -1838,22 +1892,22 @@ inline void SetCompletionCallback(CompletionCallback fn) { * * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ -inline void refreshSingleLine(struct linenoiseState *l) { +void linenoiseState::refreshSingleLine() { char seq[64]; - int pcolwid = unicodeColumnPos(l->prompt.c_str(), static_cast(l->prompt.length())); - int fd = l->ofd; - char *buf = l->buf; - int len = l->len; - int pos = l->pos; + int pcolwid = unicodeColumnPos(prompt.c_str(), static_cast(prompt.length())); + int fd = ofd; + char *buf = buf; + int len = len; + int pos = pos; std::string ab; - while((pcolwid+unicodeColumnPos(buf, pos)) >= l->cols) { + while((pcolwid+unicodeColumnPos(buf, pos)) >= lcols) { int glen = unicodeGraphemeLen(buf, len, 0); buf += glen; len -= glen; pos -= glen; } - while (pcolwid+unicodeColumnPos(buf, len) > l->cols) { + while (pcolwid+unicodeColumnPos(buf, len) > lcols) { len -= unicodePrevGraphemeLen(buf, len); } @@ -1861,7 +1915,7 @@ inline void refreshSingleLine(struct linenoiseState *l) { snprintf(seq,64,"\r"); ab += seq; /* Write the prompt and the current buffer content */ - ab += l->prompt; + ab += prompt; ab.append(buf, len); /* Erase to right */ snprintf(seq,64,"\x1b[0K"); @@ -1876,21 +1930,21 @@ inline void refreshSingleLine(struct linenoiseState *l) { * * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ -inline void refreshMultiLine(struct linenoiseState *l) { +void linenoiseState::refreshMultiLine() { char seq[64]; - int pcolwid = unicodeColumnPos(l->prompt.c_str(), static_cast(l->prompt.length())); - int colpos = unicodeColumnPosForMultiLine(l->buf, l->len, l->len, l->cols, pcolwid); + int pcolwid = unicodeColumnPos(prompt.c_str(), static_cast(prompt.length())); + int colpos = unicodeColumnPosForMultiLine(buf, len, len, lcols, pcolwid); int colpos2; /* cursor column position. */ - int rows = (pcolwid+colpos+l->cols-1)/l->cols; /* rows used by current buf. */ - int rpos = (pcolwid+l->oldcolpos+l->cols)/l->cols; /* cursor relative row. */ + int rows = (pcolwid+colpos+lcols-1)/lcols; /* rows used by current buf. */ + int rpos = (pcolwid+oldcolpos+lcols)/lcols; /* cursor relative row. */ int rpos2; /* rpos after refresh. */ int col; /* column position, zero-based. */ - int old_rows = (int)l->maxrows; - int fd = l->ofd, j; + int old_rows = (int)maxrows; + int fd = ofd, j; std::string ab; /* Update maxrows if needed. */ - if (rows > (int)l->maxrows) l->maxrows = rows; + if (rows > (int)maxrows) maxrows = rows; /* First step: clear all the lines used before. To do so start by * going to the last row. */ @@ -1910,27 +1964,27 @@ inline void refreshMultiLine(struct linenoiseState *l) { ab += seq; /* Write the prompt and the current buffer content */ - ab += l->prompt; - ab.append(l->buf, l->len); + ab += prompt; + ab.append(buf, len); /* Get text width to cursor position */ - colpos2 = unicodeColumnPosForMultiLine(l->buf, l->len, l->pos, l->cols, pcolwid); + colpos2 = unicodeColumnPosForMultiLine(buf, len, pos, lcols, pcolwid); /* If we are at the very end of the screen with our prompt, we need to * emit a newline and move the prompt to the first column. */ - if (l->pos && - l->pos == l->len && - (colpos2+pcolwid) % l->cols == 0) + if (pos && + pos == len && + (colpos2+pcolwid) % lcols == 0) { ab += "\n"; snprintf(seq,64,"\r"); ab += seq; rows++; - if (rows > (int)l->maxrows) l->maxrows = rows; + if (rows > (int)maxrows) maxrows = rows; } /* Move cursor to right position. */ - rpos2 = (pcolwid+colpos2+l->cols)/l->cols; /* current cursor relative row. */ + rpos2 = (pcolwid+colpos2+lcols)/lcols; /* current cursor relative row. */ /* Go up till we reach the expected position. */ if (rows-rpos2 > 0) { @@ -1939,85 +1993,85 @@ inline void refreshMultiLine(struct linenoiseState *l) { } /* Set column. */ - col = (pcolwid + colpos2) % l->cols; + col = (pcolwid + colpos2) % lcols; if (col) snprintf(seq,64,"\r\x1b[%dC", col); else snprintf(seq,64,"\r"); ab += seq; - l->oldcolpos = colpos2; + oldcolpos = colpos2; if (write(fd,ab.c_str(), static_cast(ab.length())) == -1) {} /* Can't recover from write error. */ } /* Calls the two low level functions refreshSingleLine() or * refreshMultiLine() according to the selected mode. */ -inline void refreshLine(struct linenoiseState *l) { +void linenoiseState::RefreshLine() { if (mlmode) - refreshMultiLine(l); + refreshMultiLine(); else - refreshSingleLine(l); + refreshSingleLine(); } /* Insert the character 'c' at cursor current position. * * On error writing to the terminal -1 is returned, otherwise 0. */ -inline int linenoiseEditInsert(struct linenoiseState *l, const char* cbuf, int clen) { - if (l->len < l->buflen) { - if (l->len == l->pos) { - memcpy(&l->buf[l->pos],cbuf,clen); - l->pos+=clen; - l->len+=clen;; - l->buf[l->len] = '\0'; - if ((!mlmode && unicodeColumnPos(l->prompt.c_str(), static_cast(l->prompt.length()))+unicodeColumnPos(l->buf,l->len) < l->cols) /* || mlmode */) { +int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { + if (len < buflen) { + if (len == pos) { + memcpy(&buf[pos],cbuf,clen); + pos+=clen; + len+=clen;; + buf[len] = '\0'; + if ((!mlmode && unicodeColumnPos(prompt.c_str(), static_cast(prompt.length()))+unicodeColumnPos(buf,len) < lcols) /* || mlmode */) { /* Avoid a full update of the line in the * trivial case. */ - if (write(l->ofd,cbuf,clen) == -1) return -1; + if (write(ofd,cbuf,clen) == -1) return -1; } else { - refreshLine(l); + RefreshLine(); } } else { - memmove(l->buf+l->pos+clen,l->buf+l->pos,l->len-l->pos); - memcpy(&l->buf[l->pos],cbuf,clen); - l->pos+=clen; - l->len+=clen; - l->buf[l->len] = '\0'; - refreshLine(l); + memmove(buf+pos+clen,buf+pos,len-pos); + memcpy(&buf[pos],cbuf,clen); + pos+=clen; + len+=clen; + buf[len] = '\0'; + RefreshLine(); } } return 0; } /* Move cursor on the left. */ -inline void linenoiseEditMoveLeft(struct linenoiseState *l) { - if (l->pos > 0) { - l->pos -= unicodePrevGraphemeLen(l->buf, l->pos); - refreshLine(l); +void linenoiseState::linenoiseEditMoveLeft() { + if (pos > 0) { + pos -= unicodePrevGraphemeLen(buf, pos); + RefreshLine(); } } /* Move cursor on the right. */ -inline void linenoiseEditMoveRight(struct linenoiseState *l) { - if (l->pos != l->len) { - l->pos += unicodeGraphemeLen(l->buf, l->len, l->pos); - refreshLine(l); +void linenoiseState::linenoiseEditMoveRight() { + if (pos != len) { + pos += unicodeGraphemeLen(buf, len, pos); + RefreshLine(); } } /* Move cursor to the start of the line. */ -inline void linenoiseEditMoveHome(struct linenoiseState *l) { - if (l->pos != 0) { - l->pos = 0; - refreshLine(l); +void linenoiseState::linenoiseEditMoveHome() { + if (pos != 0) { + pos = 0; + RefreshLine(); } } /* Move cursor to the end of the line. */ -inline void linenoiseEditMoveEnd(struct linenoiseState *l) { - if (l->pos != l->len) { - l->pos = l->len; - refreshLine(l); +void linenoiseState::linenoiseEditMoveEnd() { + if (pos != len) { + pos = len; + RefreshLine(); } } @@ -2025,65 +2079,65 @@ inline void linenoiseEditMoveEnd(struct linenoiseState *l) { * entry as specified by 'dir'. */ #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 -inline void linenoiseEditHistoryNext(struct linenoiseState *l, int dir) { +void linenoiseState::linenoiseEditHistoryNext(int dir) { if (history.size() > 1) { /* Update the current history entry before to * overwrite it with the next one. */ - history[history.size() - 1 - l->history_index] = l->buf; + history[history.size() - 1 - history_index] = buf; /* Show the new entry */ - l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1; - if (l->history_index < 0) { - l->history_index = 0; + history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1; + if (history_index < 0) { + history_index = 0; return; - } else if (l->history_index >= (int)history.size()) { - l->history_index = static_cast(history.size())-1; + } else if (history_index >= (int)history.size()) { + history_index = static_cast(history.size())-1; return; } - memset(l->buf, 0, l->buflen); - strcpy(l->buf,history[history.size() - 1 - l->history_index].c_str()); - l->len = l->pos = static_cast(strlen(l->buf)); - refreshLine(l); + memset(buf, 0, buflen); + strcpy(buf,history[history.size() - 1 - history_index].c_str()); + len = pos = static_cast(strlen(buf)); + RefreshLine(); } } /* Delete the character at the right of the cursor without altering the cursor * position. Basically this is what happens with the "Delete" keyboard key. */ -inline void linenoiseEditDelete(struct linenoiseState *l) { - if (l->len > 0 && l->pos < l->len) { - int glen = unicodeGraphemeLen(l->buf,l->len,l->pos); - memmove(l->buf+l->pos,l->buf+l->pos+glen,l->len-l->pos-glen); - l->len-=glen; - l->buf[l->len] = '\0'; - refreshLine(l); +void linenoiseState::linenoiseEditDelete() { + if (len > 0 && pos < len) { + int glen = unicodeGraphemeLen(buf,len,pos); + memmove(buf+pos,buf+pos+glen,len-pos-glen); + len-=glen; + buf[len] = '\0'; + RefreshLine(); } } /* Backspace implementation. */ -inline void linenoiseEditBackspace(struct linenoiseState *l) { - if (l->pos > 0 && l->len > 0) { - int glen = unicodePrevGraphemeLen(l->buf,l->pos); - memmove(l->buf+l->pos-glen,l->buf+l->pos,l->len-l->pos); - l->pos-=glen; - l->len-=glen; - l->buf[l->len] = '\0'; - refreshLine(l); +void linenoiseState::linenoiseEditBackspace() { + if (pos > 0 && len > 0) { + int glen = unicodePrevGraphemeLen(buf,pos); + memmove(buf+pos-glen,buf+pos,len-pos); + pos-=glen; + len-=glen; + buf[len] = '\0'; + RefreshLine(); } } /* Delete the previous word, maintaining the cursor at the start of the * current word. */ -inline void linenoiseEditDeletePrevWord(struct linenoiseState *l) { - int old_pos = l->pos; +void linenoiseState::linenoiseEditDeletePrevWord() { + int old_pos = pos; int diff; - while (l->pos > 0 && l->buf[l->pos-1] == ' ') - l->pos--; - while (l->pos > 0 && l->buf[l->pos-1] != ' ') - l->pos--; - diff = old_pos - l->pos; - memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1); - l->len -= diff; - refreshLine(l); + while (pos > 0 && buf[pos-1] == ' ') + pos--; + while (pos > 0 && buf[pos-1] != ' ') + pos--; + diff = old_pos - pos; + memmove(buf+pos,buf+old_pos,len-old_pos+1); + len -= diff; + RefreshLine(); } /* This function is the core of the line editing capability of linenoise. @@ -2094,32 +2148,16 @@ inline void linenoiseEditDeletePrevWord(struct linenoiseState *l) { * when ctrl+d is typed. * * The function returns the length of the current buffer. */ -inline int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, int buflen, const char *prompt) +int linenoiseState::linenoiseEdit() + //int stdin_fd, int stdout_fd, char *buf, int buflen) { - struct linenoiseState l; - - /* Populate the linenoise state that we pass to functions implementing - * specific editing functionalities. */ - l.ifd = stdin_fd; - l.ofd = stdout_fd; - l.buf = buf; - l.buflen = buflen; - l.prompt = prompt; - l.oldcolpos = l.pos = 0; - l.len = 0; - l.cols = getColumns(stdin_fd, stdout_fd); - l.maxrows = 0; - l.history_index = 0; - - /* Buffer starts empty. */ - l.buf[0] = '\0'; - l.buflen--; /* Make sure there is always space for the nulterm */ + lcols = getColumns(ifd, ofd); /* The latest history entry is always our current buffer, that * initially is just an empty string. */ AddHistory(""); - if (write(l.ofd,prompt, static_cast(l.prompt.length())) == -1) return -1; + if (write(ofd, prompt.c_str(), static_cast(prompt.length())) == -1) return -1; while(1) { int c; char cbuf[4]; @@ -2132,17 +2170,17 @@ inline int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, int buflen, con cbuf[0] = c; } #else - nread = unicodeReadUTF8Char(l.ifd,cbuf,&c); + nread = unicodeReadUTF8Char(ifd,cbuf,&c); #endif - if (nread <= 0) return (int)l.len; + if (nread <= 0) return (int)len; /* 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) { - nread = completeLine(&l,cbuf,&c); + nread = completeLine(cbuf,&c); /* Return on errors */ - if (c < 0) return l.len; + if (c < 0) return len; /* Read next character when 0 */ if (c == 0) continue; } @@ -2150,83 +2188,83 @@ inline int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, int buflen, con switch(c) { case ENTER: /* enter */ if (!history.empty()) history.pop_back(); - if (mlmode) linenoiseEditMoveEnd(&l); - return (int)l.len; + if (mlmode) linenoiseEditMoveEnd(); + return (int)len; case CTRL_C: /* ctrl-c */ errno = EAGAIN; return -1; case BACKSPACE: /* backspace */ case 8: /* ctrl-h */ - linenoiseEditBackspace(&l); + linenoiseEditBackspace(); break; case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the line is empty, act as end-of-file. */ - if (l.len > 0) { - linenoiseEditDelete(&l); + if (len > 0) { + linenoiseEditDelete(); } else { history.pop_back(); return -1; } break; case CTRL_T: /* ctrl-t, swaps current character with previous. */ - if (l.pos > 0 && l.pos < l.len) { - char aux = buf[l.pos-1]; - buf[l.pos-1] = buf[l.pos]; - buf[l.pos] = aux; - if (l.pos != l.len-1) l.pos++; - refreshLine(&l); + if (pos > 0 && pos < len) { + char aux = buf[pos-1]; + buf[pos-1] = buf[pos]; + buf[pos] = aux; + if (pos != len-1) pos++; + RefreshLine(); } break; case CTRL_B: /* ctrl-b */ - linenoiseEditMoveLeft(&l); + linenoiseEditMoveLeft(); break; case CTRL_F: /* ctrl-f */ - linenoiseEditMoveRight(&l); + linenoiseEditMoveRight(); break; case CTRL_P: /* ctrl-p */ - linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV); + linenoiseEditHistoryNext(LINENOISE_HISTORY_PREV); break; case CTRL_N: /* ctrl-n */ - linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT); + linenoiseEditHistoryNext(LINENOISE_HISTORY_NEXT); break; case ESC: /* escape sequence */ /* Read the next two bytes representing the escape sequence. * Use two calls to handle slow terminals returning the two * chars at different times. */ - if (read(l.ifd,seq,1) == -1) break; - if (read(l.ifd,seq+1,1) == -1) break; + if (read(ifd,seq,1) == -1) break; + if (read(ifd,seq+1,1) == -1) break; /* ESC [ sequences. */ if (seq[0] == '[') { if (seq[1] >= '0' && seq[1] <= '9') { /* Extended escape, read additional byte. */ - if (read(l.ifd,seq+2,1) == -1) break; + if (read(ifd,seq+2,1) == -1) break; if (seq[2] == '~') { switch(seq[1]) { case '3': /* Delete key. */ - linenoiseEditDelete(&l); + linenoiseEditDelete(); break; } } } else { switch(seq[1]) { case 'A': /* Up */ - linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV); + linenoiseEditHistoryNext(LINENOISE_HISTORY_PREV); break; case 'B': /* Down */ - linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT); + linenoiseEditHistoryNext(LINENOISE_HISTORY_NEXT); break; case 'C': /* Right */ - linenoiseEditMoveRight(&l); + linenoiseEditMoveRight(); break; case 'D': /* Left */ - linenoiseEditMoveLeft(&l); + linenoiseEditMoveLeft(); break; case 'H': /* Home */ - linenoiseEditMoveHome(&l); + linenoiseEditMoveHome(); break; case 'F': /* End*/ - linenoiseEditMoveEnd(&l); + linenoiseEditMoveEnd(); break; } } @@ -2236,48 +2274,48 @@ inline int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, int buflen, con else if (seq[0] == 'O') { switch(seq[1]) { case 'H': /* Home */ - linenoiseEditMoveHome(&l); + linenoiseEditMoveHome(); break; case 'F': /* End*/ - linenoiseEditMoveEnd(&l); + linenoiseEditMoveEnd(); break; } } break; default: - if (linenoiseEditInsert(&l,cbuf,nread)) return -1; + if (linenoiseEditInsert(cbuf,nread)) return -1; break; case CTRL_U: /* Ctrl+u, delete the whole line. */ buf[0] = '\0'; - l.pos = l.len = 0; - refreshLine(&l); + pos = len = 0; + RefreshLine(); break; case CTRL_K: /* Ctrl+k, delete from current to end of line. */ - buf[l.pos] = '\0'; - l.len = l.pos; - refreshLine(&l); + buf[pos] = '\0'; + len = pos; + RefreshLine(); break; case CTRL_A: /* Ctrl+a, go to the start of the line */ - linenoiseEditMoveHome(&l); + linenoiseEditMoveHome(); break; case CTRL_E: /* ctrl+e, go to the end of the line */ - linenoiseEditMoveEnd(&l); + linenoiseEditMoveEnd(); break; case CTRL_L: /* ctrl+l, clear screen */ linenoiseClearScreen(); - refreshLine(&l); + RefreshLine(); break; case CTRL_W: /* ctrl+w, delete previous word */ - linenoiseEditDeletePrevWord(&l); + linenoiseEditDeletePrevWord(); break; } } - return l.len; + return len; } /* This function calls the line editing function linenoiseEdit() using * the STDIN file descriptor set in raw mode. */ -inline bool linenoiseRaw(const char *prompt, std::string& line) { +bool linenoiseState::linenoiseRaw(std::string& line) { bool quit = false; if (!isatty(STDIN_FILENO)) { @@ -2290,7 +2328,7 @@ inline bool linenoiseRaw(const char *prompt, std::string& line) { } char buf[LINENOISE_MAX_LINE]; - auto count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, LINENOISE_MAX_LINE, prompt); + auto count = linenoiseEdit(); if (count == -1) { quit = true; } else { @@ -2303,31 +2341,49 @@ inline bool linenoiseRaw(const char *prompt, std::string& line) { return quit; } +linenoiseState::linenoiseState(const char *prompt_str, int stdin_fd, int stdout_fd) { + /* Populate the linenoise state that we pass to functions implementing + * specific editing functionalities. */ + ifd = stdin_fd; + ofd = stdout_fd; + buf = wbuf; + prompt = (prompt_str) ? std::string(prompt_str) : std::string("> "); + + /* Buffer starts empty. */ + buf[0] = '\0'; + buflen--; /* Make sure there is always space for the nulterm */ +} + +void linenoiseState::EnableMultiLine() { SetMultiLine(true); } +void linenoiseState::DisableMultiLine() { SetMultiLine(false); } + /* The high level function that is the main API of the linenoise library. * This function checks if the terminal has basic capabilities, just checking * for a blacklist of stupid terminals, and later either calls the line * editing function or uses dummy fgets() so that you will be able to type * something even in the most desperate of the conditions. */ -inline bool Readline(const char *prompt, std::string& line) { +bool linenoiseState::Readline(std::string& line) { if (isUnsupportedTerm()) { - printf("%s",prompt); + printf("%s",prompt.c_str()); fflush(stdout); std::getline(std::cin, line); return false; } else { - return linenoiseRaw(prompt, line); + return linenoiseRaw(line); } + + return false; } -inline std::string Readline(const char *prompt, bool& quit) { +std::string linenoiseState::Readline(bool& quit) { std::string line; - quit = Readline(prompt, line); + quit = Readline(line); return line; } -inline std::string Readline(const char *prompt) { +std::string linenoiseState::Readline() { bool quit; // dummy - return Readline(prompt, quit); + return Readline(quit); } /* ================================ History ================================= */ @@ -2344,7 +2400,7 @@ inline void linenoiseAtExit(void) { * histories, but will work well for a few hundred of entries. * * Using a circular buffer is smarter, but a bit more complex to handle. */ -inline bool AddHistory(const char* line) { +bool linenoiseState::AddHistory(const char* line) { if (history_max_len == 0) return false; /* Don't add duplicated lines. */ @@ -2363,7 +2419,7 @@ inline bool AddHistory(const char* line) { * if there is already some history, the function will make sure to retain * just the latest 'len' elements if the new history length value is smaller * than the amount of items already inside the history. */ -inline bool SetHistoryMaxLen(size_t len) { +bool linenoiseState::SetHistoryMaxLen(size_t len) { if (len < 1) return false; history_max_len = len; if (len < history.size()) { @@ -2374,7 +2430,7 @@ inline bool SetHistoryMaxLen(size_t len) { /* Save the history in the specified file. On success *true* is returned * otherwise *false* is returned. */ -inline bool SaveHistory(const char* path) { +bool linenoiseState::SaveHistory(const char* path) { std::ofstream f(path); // TODO: need 'std::ios::binary'? if (!f) return false; for (const auto& h: history) { @@ -2388,7 +2444,7 @@ inline bool SaveHistory(const char* path) { * * If the file exists and the operation succeeded *true* is returned, otherwise * on error *false* is returned. */ -inline bool LoadHistory(const char* path) { +bool linenoiseState::LoadHistory(const char* path) { std::ifstream f(path); if (!f) return false; std::string line; @@ -2398,10 +2454,6 @@ inline bool LoadHistory(const char* path) { return true; } -inline const std::vector& GetHistory() { - return history; -} - } // namespace linenoise #ifdef _WIN32 From 27e4fd837bce24425495c426ab3b6b3049b3aa19 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:32:51 -0400 Subject: [PATCH 03/22] Support previous API style Implement a way to allow the previous API as seen in the old example.cpp to keep working. --- example/CMakeLists.txt | 1 + example/example_old.cpp | 54 +++++++++++++++++++++++++ linenoise.hpp | 88 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 example/example_old.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index c38e6dc..6a7c41a 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -3,3 +3,4 @@ include_directories(.) add_definitions("-std=c++1y") add_executable(example example.cpp) +add_executable(example_old example_old.cpp) diff --git a/example/example_old.cpp b/example/example_old.cpp new file mode 100644 index 0000000..7be6f71 --- /dev/null +++ b/example/example_old.cpp @@ -0,0 +1,54 @@ +#include +#include "../linenoise.hpp" + +using namespace std; + +int main(int argc, const char** argv) +{ + const auto path = "history.txt"; + + // Enable the multi-line mode + linenoise::SetMultiLine(true); + + // Set max length of the history + linenoise::SetHistoryMaxLen(4); + + // Setup completion words every time when a user types + linenoise::SetCompletionCallback([](const char* editBuffer, std::vector& completions) { + if (editBuffer[0] == 'h') { +#ifdef _WIN32 + completions.push_back("hello こんにちは"); + completions.push_back("hello こんにちは there"); +#else + completions.push_back("hello"); + completions.push_back("hello there"); +#endif + } + }); + + // Load history + linenoise::LoadHistory(path); + + while (true) { + std::string line; +#ifdef _WIN32 + auto quit = linenoise::Readline("hello> ", line); +#else + auto quit = linenoise::Readline("\033[32mこんにちは\x1b[0m> ", line); +#endif + + if (quit) { + break; + } + + cout << "echo: '" << line << "'" << endl; + + // Add line to history + linenoise::AddHistory(line.c_str()); + + // Save history + linenoise::SaveHistory(path); + } + + return 0; +} diff --git a/linenoise.hpp b/linenoise.hpp index b5fe26f..6c8545c 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -193,6 +193,9 @@ class linenoiseState { std::string prompt = std::string("> "); /* Prompt to display. */ std::mutex r_mutex; + // For functional interface + void SetMultiLine(bool ml); + private: std::string Readline(bool &quit); std::string Readline(); @@ -213,7 +216,6 @@ class linenoiseState { int completeLine(char *cbuf, int *c); bool isUnsupportedTerm(void); - void SetMultiLine(bool ml); CompletionCallback completionCallback; @@ -234,6 +236,21 @@ class linenoiseState { std::vector history; }; + +/* Older style public API */ +linenoiseState *lglobal = NULL; +void SetCompletionCallback(CompletionCallback fn); +void SetMultiLine(bool ml); +bool AddHistory(const char* line); +bool SetHistoryMaxLen(size_t len); +bool SaveHistory(const char* path); +bool LoadHistory(const char* path); +const std::vector& GetHistory(); +bool Readline(const char *prompt, std::string& line); +std::string Readline(const char *prompt, bool& quit); +std::string Readline(const char *prompt); + + #ifdef _WIN32 namespace ansi { @@ -1881,11 +1898,6 @@ int linenoiseState::completeLine(char *cbuf, int *c) { return nread; } -/* Register a callback function to be called for tab-completion. */ -inline void SetCompletionCallback(CompletionCallback fn) { - completionCallback = fn; -} - /* =========================== Line editing ================================= */ /* Single line low level line refresh. @@ -2391,6 +2403,11 @@ std::string linenoiseState::Readline() { /* At exit we'll try to fix the terminal to the initial conditions. */ inline void linenoiseAtExit(void) { disableRawMode(STDIN_FILENO); + + // If we're using the global, clean up + if (lglobal) + delete lglobal; + lglobal = NULL; } /* This is the API call to add a new entry in the linenoise history. @@ -2454,6 +2471,65 @@ bool linenoiseState::LoadHistory(const char* path) { return true; } + + +/* Function style interface */ +void SetCompletionCallback(CompletionCallback fn){ + if (!lglobal) + lglobal = new linenoiseState(); + lglobal->SetCompletionCallback(fn); +}; +void SetMultiLine(bool ml){ + if (!lglobal) + lglobal = new linenoiseState(); + lglobal->SetMultiLine(ml); +}; +bool AddHistory(const char* line){ + if (!lglobal) + lglobal = new linenoiseState(); + return lglobal->AddHistory(line); +}; +bool SetHistoryMaxLen(size_t len){ + if (!lglobal) + lglobal = new linenoiseState(); + return lglobal->SetHistoryMaxLen(len); +}; +bool SaveHistory(const char* path){ + if (!lglobal) + lglobal = new linenoiseState(); + return lglobal->SaveHistory(path); +}; +bool LoadHistory(const char* path){ + if (!lglobal) + lglobal = new linenoiseState(); + return lglobal->LoadHistory(path); +}; +const std::vector& GetHistory(){ + if (!lglobal) + lglobal = new linenoiseState(); + return lglobal->GetHistory(); +}; +bool Readline(const char *prompt, std::string& line){ + if (!lglobal) + lglobal = new linenoiseState(); + lglobal->prompt = std::string(prompt); + return lglobal->Readline(line); +}; +std::string Readline(const char *prompt, bool& quit){ + if (!lglobal) + lglobal = new linenoiseState(); + lglobal->prompt = std::string(prompt); + std::string line; + quit = lglobal->Readline(line); + return line; +}; +std::string Readline(const char *prompt){ + bool quit; // dummy + return Readline(prompt, quit); +}; + + + } // namespace linenoise #ifdef _WIN32 From 9bf9fb5a94191acc1291e973b8a83853c70db2b5 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:36:52 -0400 Subject: [PATCH 04/22] Add a mutex to protect RefreshLine if multiple threads need to call it. --- linenoise.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linenoise.hpp b/linenoise.hpp index 6c8545c..636bc9e 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -2020,10 +2020,12 @@ void linenoiseState::refreshMultiLine() { /* Calls the two low level functions refreshSingleLine() or * refreshMultiLine() according to the selected mode. */ void linenoiseState::RefreshLine() { + r_mutex.lock(); if (mlmode) refreshMultiLine(); else refreshSingleLine(); + r_mutex.unlock(); } /* Insert the character 'c' at cursor current position. From 1aface12a39a42ba616ce746dff5c19edd89ab32 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:42:57 -0400 Subject: [PATCH 05/22] Adjust how history and its up/down arrow browsing are handled. --- linenoise.hpp | 55 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index 636bc9e..a57da69 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -2094,24 +2094,33 @@ void linenoiseState::linenoiseEditMoveEnd() { #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 void linenoiseState::linenoiseEditHistoryNext(int dir) { - if (history.size() > 1) { - /* Update the current history entry before to - * overwrite it with the next one. */ - history[history.size() - 1 - history_index] = buf; - /* Show the new entry */ - history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1; - if (history_index < 0) { - history_index = 0; - return; - } else if (history_index >= (int)history.size()) { - history_index = static_cast(history.size())-1; - return; - } + int history_size = static_cast(history.size()); + if (!history_size) + return; + /* Show the new entry */ + if (history_index == -1) { + history_tmpbuf = std::string(buf); + history_index = history.size() - 1; + } else { + if (history_index == history_size) + history_tmpbuf = std::string(buf); + history_index += (dir == LINENOISE_HISTORY_PREV) ? -1 : 1; + } + if (history_index < 0) + history_index = history_size; + if (history_index == history_size) { memset(buf, 0, buflen); - strcpy(buf,history[history.size() - 1 - history_index].c_str()); - len = pos = static_cast(strlen(buf)); + strcpy(buf, history_tmpbuf.c_str()); + len = pos = static_cast(history_tmpbuf.size()); RefreshLine(); + return; } + if (history_index > history_size) + history_index = 0; + memset(buf, 0, buflen); + strcpy(buf, history[history_index].c_str()); + len = pos = static_cast(strlen(buf)); + RefreshLine(); } /* Delete the character at the right of the cursor without altering the cursor @@ -2167,10 +2176,6 @@ int linenoiseState::linenoiseEdit() { lcols = getColumns(ifd, ofd); - /* The latest history entry is always our current buffer, that - * initially is just an empty string. */ - AddHistory(""); - if (write(ofd, prompt.c_str(), static_cast(prompt.length())) == -1) return -1; while(1) { int c; @@ -2201,8 +2206,11 @@ int linenoiseState::linenoiseEdit() switch(c) { case ENTER: /* enter */ - if (!history.empty()) history.pop_back(); - if (mlmode) linenoiseEditMoveEnd(); + history_index = -1; + if (history.size() == history_max_len) + history.pop_back(); + if (mlmode) + linenoiseEditMoveEnd(); return (int)len; case CTRL_C: /* ctrl-c */ errno = EAGAIN; @@ -2420,7 +2428,10 @@ inline void linenoiseAtExit(void) { * * Using a circular buffer is smarter, but a bit more complex to handle. */ bool linenoiseState::AddHistory(const char* line) { - if (history_max_len == 0) return false; + if (history_max_len == 0) + return false; + if (!line || !strlen(line)) + return false; /* Don't add duplicated lines. */ if (!history.empty() && history.back() == line) return false; From 80fe6b2f4701470b8553dd8e9eb8b25088f7c9e6 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:46:17 -0400 Subject: [PATCH 06/22] We don't want to hang if we can't read anything from stdin --- linenoise.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/linenoise.hpp b/linenoise.hpp index a57da69..a7cbbe4 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -2342,7 +2342,12 @@ bool linenoiseState::linenoiseRaw(std::string& line) { if (!isatty(STDIN_FILENO)) { /* Not a tty: read from file / pipe. */ - std::getline(std::cin, line); + int c; + while ((c = getc(stdin)) != EOF) + line += c; + if (!line.length()) + quit = true; + } else { /* Interactive editing. */ if (enableRawMode(STDIN_FILENO) == false) { From 854ec25d1f42914d1fd10d89b9cbe6fb12dffeee Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 09:57:05 -0400 Subject: [PATCH 07/22] Add WipeLine and ClearScreen methods. --- linenoise.hpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/linenoise.hpp b/linenoise.hpp index a7cbbe4..0392af3 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -184,6 +184,8 @@ class linenoiseState { bool Readline(std::string &line); // Primary linenoise entry point void RefreshLine(); // Restore current line + void WipeLine(); // Temporarily removes line from screen - RefreshLine will restore + void ClearScreen(); // Clear terminal window /* Register a callback function to be called for tab-completion. */ void SetCompletionCallback(CompletionCallback fn) { @@ -1817,6 +1819,21 @@ inline void linenoiseClearScreen(void) { } } +/* Temporarily clear the line from the screen, + * without resetting its state. Used for + * temporarily clearing prompt and input while + * printing other content. */ +inline void linenoiseWipeLine() { + // Clear line + if (write(STDOUT_FILENO, "\33[2K", 4) < 0) { + /* nothing to do, just to avoid warning. */ + } + // Move cursor to the left + if (write(STDOUT_FILENO, "\r", 1) < 0) { + /* nothing to do, just to avoid warning. */ + } +} + /* Beep, used for completion when there is nothing to complete or when all * the choices were already shown. */ inline void linenoiseBeep(void) { @@ -2028,6 +2045,10 @@ void linenoiseState::RefreshLine() { r_mutex.unlock(); } +void linenoiseState::WipeLine() { linenoiseWipeLine(); } + +void linenoiseState::ClearScreen() { linenoiseClearScreen(); } + /* Insert the character 'c' at cursor current position. * * On error writing to the terminal -1 is returned, otherwise 0. */ From cab53d8ccc26761a69fc8a886379cbdfd0f1ecda Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 10:01:24 -0400 Subject: [PATCH 08/22] See if we can avoid using strcasecmp --- linenoise.hpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index 0392af3..fc93bed 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -1656,8 +1656,17 @@ bool linenoiseState::isUnsupportedTerm(void) { int j; if (term == NULL) return false; - for (j = 0; unsupported_term[j]; j++) - if (!strcasecmp(term,unsupported_term[j])) return true; + + std::string sterm(term); + // https://stackoverflow.com/a/313990 + std::transform(sterm.begin(), sterm.end(), sterm.begin(), [](unsigned char c){ return std::tolower(c); }); + for (j = 0; unsupported_term[j]; j++) { + std::string uterm(unsupported_term[j]); + // https://stackoverflow.com/a/313990 + std::transform(uterm.begin(), uterm.end(), uterm.begin(), [](unsigned char c){ return std::tolower(c); }); + if (uterm == uterm) + return true; + } #endif return false; } From debd295b8e7db0aa0f8034e377a7061181e3b3aa Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 10:08:55 -0400 Subject: [PATCH 09/22] Correct behavior for sending commands Fix from Chris McGregor (f4alt : Old linenoise behavior would send commands on newline. For subprocess use, like we do with nirt in MGED, this is very important as the alternative means collecting commands like "Command_0\nCommnand_1.." and expecting the process to parse the newlines. --- linenoise.hpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index fc93bed..f38b072 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -1939,6 +1939,9 @@ void linenoiseState::refreshSingleLine() { int pos = pos; std::string ab; + if (lcols <= 0) + lcols = getColumns(ifd, ofd); + while((pcolwid+unicodeColumnPos(buf, pos)) >= lcols) { int glen = unicodeGraphemeLen(buf, len, 0); buf += glen; @@ -1969,6 +1972,9 @@ void linenoiseState::refreshSingleLine() { * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ void linenoiseState::refreshMultiLine() { + if (lcols <= 0) + lcols = getColumns(ifd, ofd); + char seq[64]; int pcolwid = unicodeColumnPos(prompt.c_str(), static_cast(prompt.length())); int colpos = unicodeColumnPosForMultiLine(buf, len, len, lcols, pcolwid); @@ -2373,8 +2379,14 @@ bool linenoiseState::linenoiseRaw(std::string& line) { if (!isatty(STDIN_FILENO)) { /* Not a tty: read from file / pipe. */ int c; - while ((c = getc(stdin)) != EOF) - line += c; + while ((c = getc(stdin)) != EOF) { + if (c == '\r') // CRLF -> LF + continue; + if (c == '\n' || c == '\r') // send command + break; + + line += c; + } if (!line.length()) quit = true; From d3b0c33564be95b708bb8834d8a98ff5136c3cb9 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 10:47:39 -0400 Subject: [PATCH 10/22] Misc. fixes and adjustments --- linenoise.hpp | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index f38b072..af65281 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -217,8 +217,6 @@ class linenoiseState { void refreshMultiLine(); int completeLine(char *cbuf, int *c); - bool isUnsupportedTerm(void); - CompletionCallback completionCallback; int ifd = STDIN_FILENO; /* Terminal stdin file descriptor. */ @@ -1160,7 +1158,6 @@ static struct termios orig_termios; /* In order to restore at exit.*/ static bool rawmode = false; /* For atexit() function to check if restore is needed*/ static bool mlmode = false; /* Multi line mode. Default is single line. */ static bool atexit_registered = false; /* Register atexit just 1 time. */ -static size_t history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; static std::vector history; enum KEY_ACTION { @@ -1650,7 +1647,7 @@ void linenoiseState::SetMultiLine(bool ml) { /* Return true if the terminal name is in the list of terminals we know are * not able to understand basic escape sequences. */ -bool linenoiseState::isUnsupportedTerm(void) { +static bool isUnsupportedTerm(void) { #ifndef _WIN32 char *term = getenv("TERM"); int j; @@ -1934,9 +1931,6 @@ void linenoiseState::refreshSingleLine() { char seq[64]; int pcolwid = unicodeColumnPos(prompt.c_str(), static_cast(prompt.length())); int fd = ofd; - char *buf = buf; - int len = len; - int pos = pos; std::string ab; if (lcols <= 0) @@ -2208,10 +2202,7 @@ void linenoiseState::linenoiseEditDeletePrevWord() { * * The function returns the length of the current buffer. */ int linenoiseState::linenoiseEdit() - //int stdin_fd, int stdout_fd, char *buf, int buflen) { - lcols = getColumns(ifd, ofd); - if (write(ofd, prompt.c_str(), static_cast(prompt.length())) == -1) return -1; while(1) { int c; @@ -2266,9 +2257,9 @@ int linenoiseState::linenoiseEdit() break; case CTRL_T: /* ctrl-t, swaps current character with previous. */ if (pos > 0 && pos < len) { - char aux = buf[pos-1]; - buf[pos-1] = buf[pos]; - buf[pos] = aux; + char aux = wbuf[pos-1]; + wbuf[pos-1] = wbuf[pos]; + wbuf[pos] = aux; if (pos != len-1) pos++; RefreshLine(); } @@ -2344,12 +2335,12 @@ int linenoiseState::linenoiseEdit() if (linenoiseEditInsert(cbuf,nread)) return -1; break; case CTRL_U: /* Ctrl+u, delete the whole line. */ - buf[0] = '\0'; + wbuf[0] = '\0'; pos = len = 0; RefreshLine(); break; case CTRL_K: /* Ctrl+k, delete from current to end of line. */ - buf[pos] = '\0'; + wbuf[pos] = '\0'; len = pos; RefreshLine(); break; @@ -2396,7 +2387,13 @@ bool linenoiseState::linenoiseRaw(std::string& line) { return quit; } - char buf[LINENOISE_MAX_LINE]; + /* Buffer starts empty. Since we're potentially + * reusing the state, we need to reset these. */ + pos = 0; + len = 0; + buf[0] = '\0'; + wbuf[0] = '\0'; + auto count = linenoiseEdit(); if (count == -1) { quit = true; @@ -2496,11 +2493,11 @@ bool linenoiseState::AddHistory(const char* line) { * if there is already some history, the function will make sure to retain * just the latest 'len' elements if the new history length value is smaller * than the amount of items already inside the history. */ -bool linenoiseState::SetHistoryMaxLen(size_t len) { - if (len < 1) return false; - history_max_len = len; - if (len < history.size()) { - history.resize(len); +bool linenoiseState::SetHistoryMaxLen(size_t mlen) { + if (mlen < 1) return false; + history_max_len = mlen; + if (mlen < history.size()) { + history.resize(mlen); } return true; } From eb222da8a0585666744b4c473daec1212e8c09c4 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 10 Mar 2025 16:22:51 -0400 Subject: [PATCH 11/22] Compare against the correct string --- linenoise.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linenoise.hpp b/linenoise.hpp index af65281..5ad65c1 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -1661,7 +1661,7 @@ static bool isUnsupportedTerm(void) { std::string uterm(unsupported_term[j]); // https://stackoverflow.com/a/313990 std::transform(uterm.begin(), uterm.end(), uterm.begin(), [](unsigned char c){ return std::tolower(c); }); - if (uterm == uterm) + if (uterm == sterm) return true; } #endif From 1793d0e1078fe7d43b363843cc6f87fc60fb252d Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 08:27:14 -0400 Subject: [PATCH 12/22] Use inline --- linenoise.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linenoise.hpp b/linenoise.hpp index 5ad65c1..ac6fb6f 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -1647,7 +1647,7 @@ void linenoiseState::SetMultiLine(bool ml) { /* Return true if the terminal name is in the list of terminals we know are * not able to understand basic escape sequences. */ -static bool isUnsupportedTerm(void) { +inline bool isUnsupportedTerm(void) { #ifndef _WIN32 char *term = getenv("TERM"); int j; From 9ed31767f0b25b2632f464f0cceb6a33ea1ad579 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 08:44:59 -0400 Subject: [PATCH 13/22] Restore inlines to allow multiple includes Also add a compilation test case to examples to verify we can include more than once and create an executable. --- example/CMakeLists.txt | 3 ++ example/f1.cpp | 8 +++++ example/f2.cpp | 8 +++++ example/main.cpp | 9 +++++ linenoise.hpp | 78 +++++++++++++++++++++--------------------- 5 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 example/f1.cpp create mode 100644 example/f2.cpp create mode 100644 example/main.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 6a7c41a..6e20a08 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -4,3 +4,6 @@ add_definitions("-std=c++1y") add_executable(example example.cpp) add_executable(example_old example_old.cpp) + +add_executable(multiinclude main.cpp f1.cpp f2.cpp) + diff --git a/example/f1.cpp b/example/f1.cpp new file mode 100644 index 0000000..8f3dbf1 --- /dev/null +++ b/example/f1.cpp @@ -0,0 +1,8 @@ +#include "../linenoise.hpp" + +void f1() +{ + linenoise::linenoiseState l("p1"); + l.EnableMultiLine(); +} + diff --git a/example/f2.cpp b/example/f2.cpp new file mode 100644 index 0000000..b1dbb1b --- /dev/null +++ b/example/f2.cpp @@ -0,0 +1,8 @@ +#include "../linenoise.hpp" + +void f2() +{ + linenoise::linenoiseState l("p2"); + l.EnableMultiLine(); +} + diff --git a/example/main.cpp b/example/main.cpp new file mode 100644 index 0000000..63b7593 --- /dev/null +++ b/example/main.cpp @@ -0,0 +1,9 @@ +extern void f1(); +extern void f2(); + +int main(int argc, const char** argv) +{ + f1(); + f2(); + return 0; +} diff --git a/linenoise.hpp b/linenoise.hpp index ac6fb6f..c0a2a93 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -238,7 +238,7 @@ class linenoiseState { /* Older style public API */ -linenoiseState *lglobal = NULL; +static linenoiseState *lglobal = NULL; void SetCompletionCallback(CompletionCallback fn); void SetMultiLine(bool ml); bool AddHistory(const char* line); @@ -1641,7 +1641,7 @@ inline int unicodeReadUTF8Char(int fd, char* buf, int* cp) /* ======================= Low level terminal handling ====================== */ /* Set if to use or not the multi line mode. */ -void linenoiseState::SetMultiLine(bool ml) { +inline void linenoiseState::SetMultiLine(bool ml) { mlmode = ml; } @@ -1855,7 +1855,7 @@ inline void linenoiseBeep(void) { * * The state of the editing is encapsulated into the pointed linenoiseState * structure as described in the structure definition. */ -int linenoiseState::completeLine(char *cbuf, int *c) { +inline int linenoiseState::completeLine(char *cbuf, int *c) { std::vector lc; int nread = 0, nwritten; *c = 0; @@ -1927,7 +1927,7 @@ int linenoiseState::completeLine(char *cbuf, int *c) { * * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ -void linenoiseState::refreshSingleLine() { +inline void linenoiseState::refreshSingleLine() { char seq[64]; int pcolwid = unicodeColumnPos(prompt.c_str(), static_cast(prompt.length())); int fd = ofd; @@ -1965,7 +1965,7 @@ void linenoiseState::refreshSingleLine() { * * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ -void linenoiseState::refreshMultiLine() { +inline void linenoiseState::refreshMultiLine() { if (lcols <= 0) lcols = getColumns(ifd, ofd); @@ -2045,7 +2045,7 @@ void linenoiseState::refreshMultiLine() { /* Calls the two low level functions refreshSingleLine() or * refreshMultiLine() according to the selected mode. */ -void linenoiseState::RefreshLine() { +inline void linenoiseState::RefreshLine() { r_mutex.lock(); if (mlmode) refreshMultiLine(); @@ -2054,14 +2054,14 @@ void linenoiseState::RefreshLine() { r_mutex.unlock(); } -void linenoiseState::WipeLine() { linenoiseWipeLine(); } +inline void linenoiseState::WipeLine() { linenoiseWipeLine(); } -void linenoiseState::ClearScreen() { linenoiseClearScreen(); } +inline void linenoiseState::ClearScreen() { linenoiseClearScreen(); } /* Insert the character 'c' at cursor current position. * * On error writing to the terminal -1 is returned, otherwise 0. */ -int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { +inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { if (len < buflen) { if (len == pos) { memcpy(&buf[pos],cbuf,clen); @@ -2088,7 +2088,7 @@ int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { } /* Move cursor on the left. */ -void linenoiseState::linenoiseEditMoveLeft() { +inline void linenoiseState::linenoiseEditMoveLeft() { if (pos > 0) { pos -= unicodePrevGraphemeLen(buf, pos); RefreshLine(); @@ -2096,7 +2096,7 @@ void linenoiseState::linenoiseEditMoveLeft() { } /* Move cursor on the right. */ -void linenoiseState::linenoiseEditMoveRight() { +inline void linenoiseState::linenoiseEditMoveRight() { if (pos != len) { pos += unicodeGraphemeLen(buf, len, pos); RefreshLine(); @@ -2104,7 +2104,7 @@ void linenoiseState::linenoiseEditMoveRight() { } /* Move cursor to the start of the line. */ -void linenoiseState::linenoiseEditMoveHome() { +inline void linenoiseState::linenoiseEditMoveHome() { if (pos != 0) { pos = 0; RefreshLine(); @@ -2112,7 +2112,7 @@ void linenoiseState::linenoiseEditMoveHome() { } /* Move cursor to the end of the line. */ -void linenoiseState::linenoiseEditMoveEnd() { +inline void linenoiseState::linenoiseEditMoveEnd() { if (pos != len) { pos = len; RefreshLine(); @@ -2123,7 +2123,7 @@ void linenoiseState::linenoiseEditMoveEnd() { * entry as specified by 'dir'. */ #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 -void linenoiseState::linenoiseEditHistoryNext(int dir) { +inline void linenoiseState::linenoiseEditHistoryNext(int dir) { int history_size = static_cast(history.size()); if (!history_size) return; @@ -2155,7 +2155,7 @@ void linenoiseState::linenoiseEditHistoryNext(int dir) { /* Delete the character at the right of the cursor without altering the cursor * position. Basically this is what happens with the "Delete" keyboard key. */ -void linenoiseState::linenoiseEditDelete() { +inline void linenoiseState::linenoiseEditDelete() { if (len > 0 && pos < len) { int glen = unicodeGraphemeLen(buf,len,pos); memmove(buf+pos,buf+pos+glen,len-pos-glen); @@ -2166,7 +2166,7 @@ void linenoiseState::linenoiseEditDelete() { } /* Backspace implementation. */ -void linenoiseState::linenoiseEditBackspace() { +inline void linenoiseState::linenoiseEditBackspace() { if (pos > 0 && len > 0) { int glen = unicodePrevGraphemeLen(buf,pos); memmove(buf+pos-glen,buf+pos,len-pos); @@ -2179,7 +2179,7 @@ void linenoiseState::linenoiseEditBackspace() { /* Delete the previous word, maintaining the cursor at the start of the * current word. */ -void linenoiseState::linenoiseEditDeletePrevWord() { +inline void linenoiseState::linenoiseEditDeletePrevWord() { int old_pos = pos; int diff; @@ -2201,7 +2201,7 @@ void linenoiseState::linenoiseEditDeletePrevWord() { * when ctrl+d is typed. * * The function returns the length of the current buffer. */ -int linenoiseState::linenoiseEdit() +inline int linenoiseState::linenoiseEdit() { if (write(ofd, prompt.c_str(), static_cast(prompt.length())) == -1) return -1; while(1) { @@ -2364,7 +2364,7 @@ int linenoiseState::linenoiseEdit() /* This function calls the line editing function linenoiseEdit() using * the STDIN file descriptor set in raw mode. */ -bool linenoiseState::linenoiseRaw(std::string& line) { +inline bool linenoiseState::linenoiseRaw(std::string& line) { bool quit = false; if (!isatty(STDIN_FILENO)) { @@ -2407,7 +2407,7 @@ bool linenoiseState::linenoiseRaw(std::string& line) { return quit; } -linenoiseState::linenoiseState(const char *prompt_str, int stdin_fd, int stdout_fd) { +inline linenoiseState::linenoiseState(const char *prompt_str, int stdin_fd, int stdout_fd) { /* Populate the linenoise state that we pass to functions implementing * specific editing functionalities. */ ifd = stdin_fd; @@ -2420,15 +2420,15 @@ linenoiseState::linenoiseState(const char *prompt_str, int stdin_fd, int stdout_ buflen--; /* Make sure there is always space for the nulterm */ } -void linenoiseState::EnableMultiLine() { SetMultiLine(true); } -void linenoiseState::DisableMultiLine() { SetMultiLine(false); } +inline void linenoiseState::EnableMultiLine() { SetMultiLine(true); } +inline void linenoiseState::DisableMultiLine() { SetMultiLine(false); } /* The high level function that is the main API of the linenoise library. * This function checks if the terminal has basic capabilities, just checking * for a blacklist of stupid terminals, and later either calls the line * editing function or uses dummy fgets() so that you will be able to type * something even in the most desperate of the conditions. */ -bool linenoiseState::Readline(std::string& line) { +inline bool linenoiseState::Readline(std::string& line) { if (isUnsupportedTerm()) { printf("%s",prompt.c_str()); fflush(stdout); @@ -2441,13 +2441,13 @@ bool linenoiseState::Readline(std::string& line) { return false; } -std::string linenoiseState::Readline(bool& quit) { +inline std::string linenoiseState::Readline(bool& quit) { std::string line; quit = Readline(line); return line; } -std::string linenoiseState::Readline() { +inline std::string linenoiseState::Readline() { bool quit; // dummy return Readline(quit); } @@ -2471,7 +2471,7 @@ inline void linenoiseAtExit(void) { * histories, but will work well for a few hundred of entries. * * Using a circular buffer is smarter, but a bit more complex to handle. */ -bool linenoiseState::AddHistory(const char* line) { +inline bool linenoiseState::AddHistory(const char* line) { if (history_max_len == 0) return false; if (!line || !strlen(line)) @@ -2493,7 +2493,7 @@ bool linenoiseState::AddHistory(const char* line) { * if there is already some history, the function will make sure to retain * just the latest 'len' elements if the new history length value is smaller * than the amount of items already inside the history. */ -bool linenoiseState::SetHistoryMaxLen(size_t mlen) { +inline bool linenoiseState::SetHistoryMaxLen(size_t mlen) { if (mlen < 1) return false; history_max_len = mlen; if (mlen < history.size()) { @@ -2504,7 +2504,7 @@ bool linenoiseState::SetHistoryMaxLen(size_t mlen) { /* Save the history in the specified file. On success *true* is returned * otherwise *false* is returned. */ -bool linenoiseState::SaveHistory(const char* path) { +inline bool linenoiseState::SaveHistory(const char* path) { std::ofstream f(path); // TODO: need 'std::ios::binary'? if (!f) return false; for (const auto& h: history) { @@ -2518,7 +2518,7 @@ bool linenoiseState::SaveHistory(const char* path) { * * If the file exists and the operation succeeded *true* is returned, otherwise * on error *false* is returned. */ -bool linenoiseState::LoadHistory(const char* path) { +inline bool linenoiseState::LoadHistory(const char* path) { std::ifstream f(path); if (!f) return false; std::string line; @@ -2531,48 +2531,48 @@ bool linenoiseState::LoadHistory(const char* path) { /* Function style interface */ -void SetCompletionCallback(CompletionCallback fn){ +inline void SetCompletionCallback(CompletionCallback fn){ if (!lglobal) lglobal = new linenoiseState(); lglobal->SetCompletionCallback(fn); }; -void SetMultiLine(bool ml){ +inline void SetMultiLine(bool ml){ if (!lglobal) lglobal = new linenoiseState(); lglobal->SetMultiLine(ml); }; -bool AddHistory(const char* line){ +inline bool AddHistory(const char* line){ if (!lglobal) lglobal = new linenoiseState(); return lglobal->AddHistory(line); }; -bool SetHistoryMaxLen(size_t len){ +inline bool SetHistoryMaxLen(size_t len){ if (!lglobal) lglobal = new linenoiseState(); return lglobal->SetHistoryMaxLen(len); }; -bool SaveHistory(const char* path){ +inline bool SaveHistory(const char* path){ if (!lglobal) lglobal = new linenoiseState(); return lglobal->SaveHistory(path); }; -bool LoadHistory(const char* path){ +inline bool LoadHistory(const char* path){ if (!lglobal) lglobal = new linenoiseState(); return lglobal->LoadHistory(path); }; -const std::vector& GetHistory(){ +inline const std::vector& GetHistory(){ if (!lglobal) lglobal = new linenoiseState(); return lglobal->GetHistory(); }; -bool Readline(const char *prompt, std::string& line){ +inline bool Readline(const char *prompt, std::string& line){ if (!lglobal) lglobal = new linenoiseState(); lglobal->prompt = std::string(prompt); return lglobal->Readline(line); }; -std::string Readline(const char *prompt, bool& quit){ +inline std::string Readline(const char *prompt, bool& quit){ if (!lglobal) lglobal = new linenoiseState(); lglobal->prompt = std::string(prompt); @@ -2580,7 +2580,7 @@ std::string Readline(const char *prompt, bool& quit){ quit = lglobal->Readline(line); return line; }; -std::string Readline(const char *prompt){ +inline std::string Readline(const char *prompt){ bool quit; // dummy return Readline(prompt, quit); }; From 658c26f429d7f56cbaa7db7d8c07e71bd1f274f6 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 09:20:49 -0400 Subject: [PATCH 14/22] Add an l_ prefix to all class member variables. --- linenoise.hpp | 342 +++++++++++++++++++++++++------------------------- 1 file changed, 171 insertions(+), 171 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index c0a2a93..d05095d 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -179,7 +179,7 @@ class linenoiseState { bool AddHistory(const char *line); bool LoadHistory(const char *path); bool SaveHistory(const char *path); - const std::vector &GetHistory() { return history; }; + const std::vector &GetHistory() { return l_history; }; bool SetHistoryMaxLen(size_t len); bool Readline(std::string &line); // Primary linenoise entry point @@ -192,8 +192,8 @@ class linenoiseState { completionCallback = fn; }; - std::string prompt = std::string("> "); /* Prompt to display. */ - std::mutex r_mutex; + std::string l_prompt = std::string("> "); /* Prompt to display. */ + std::mutex l_mutex; // For functional interface void SetMultiLine(bool ml); @@ -219,21 +219,21 @@ class linenoiseState { CompletionCallback completionCallback; - int ifd = STDIN_FILENO; /* Terminal stdin file descriptor. */ - int ofd = STDOUT_FILENO; /* Terminal stdout file descriptor. */ - char *buf = wbuf; /* Edited line buffer. */ - int buflen = LINENOISE_MAX_LINE; /* Edited line buffer size. */ - int pos = 0; /* Current cursor position. */ - int oldcolpos = 0; /* Previous refresh cursor column position. */ - int len = 0; /* Current edited line length. */ - int lcols = -1; /* Number of columns in terminal. */ - int maxrows = 0; /* Maximum num of rows used so far (multiline mode) */ - int history_index = -1; /* The history index we are currently editing. */ - char wbuf[LINENOISE_MAX_LINE] = {'\0'}; - std::string history_tmpbuf; - - size_t history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; - std::vector history; + int l_ifd = STDIN_FILENO; /* Terminal stdin file descriptor. */ + int l_ofd = STDOUT_FILENO; /* Terminal stdout file descriptor. */ + char *l_buf = l_wbuf; /* Edited line buffer. */ + int l_buflen = LINENOISE_MAX_LINE; /* Edited line buffer size. */ + int l_pos = 0; /* Current cursor position. */ + int l_oldcolpos = 0; /* Previous refresh cursor column position. */ + int l_len = 0; /* Current edited line length. */ + int l_cols = -1; /* Number of columns in terminal. */ + int l_maxrows = 0; /* Maximum num of rows used so far (multiline mode) */ + int l_history_index = -1; /* The history index we are currently editing. */ + char l_wbuf[LINENOISE_MAX_LINE] = {'\0'}; + std::string l_history_tmpbuf; + + size_t l_history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; + std::vector l_history; }; @@ -1860,7 +1860,7 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { int nread = 0, nwritten; *c = 0; - completionCallback(buf,lc); + completionCallback(l_buf,lc); if (lc.empty()) { linenoiseBeep(); } else { @@ -1869,27 +1869,27 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { while(!stop) { /* Show completion or original buffer */ if (i < static_cast(lc.size())) { - int old_len = len; - int old_pos = pos; - char *old_buf = buf; - len = pos = static_cast(lc[i].size()); - buf = &lc[i][0]; + int old_len = l_len; + int old_pos = l_pos; + char *old_buf = l_buf; + l_len = l_pos = static_cast(lc[i].size()); + l_buf = &lc[i][0]; RefreshLine(); - len = old_len; - pos = old_pos; - buf = old_buf; + l_len = old_len; + l_pos = old_pos; + l_buf = old_buf; } else { RefreshLine(); } - //nread = read(ifd,&c,1); + //nread = read(l_ifd,&c,1); #ifdef _WIN32 nread = win32read(c); if (nread == 1) { cbuf[0] = *c; } #else - nread = unicodeReadUTF8Char(ifd,cbuf,c); + nread = unicodeReadUTF8Char(l_ifd,cbuf,c); #endif if (nread <= 0) { *c = -1; @@ -1909,8 +1909,8 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { default: /* Update buffer and return */ if (i < static_cast(lc.size())) { - nwritten = snprintf(buf,buflen,"%s",&lc[i][0]); - len = pos = nwritten; + nwritten = snprintf(l_buf,l_buflen,"%s",&lc[i][0]); + l_len = l_pos = nwritten; } stop = 1; break; @@ -1929,34 +1929,34 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { * cursor position, and number of columns of the terminal. */ inline void linenoiseState::refreshSingleLine() { char seq[64]; - int pcolwid = unicodeColumnPos(prompt.c_str(), static_cast(prompt.length())); - int fd = ofd; + int pcolwid = unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length())); + int fd = l_ofd; std::string ab; - if (lcols <= 0) - lcols = getColumns(ifd, ofd); + if (l_cols <= 0) + l_cols = getColumns(l_ifd, l_ofd); - while((pcolwid+unicodeColumnPos(buf, pos)) >= lcols) { - int glen = unicodeGraphemeLen(buf, len, 0); - buf += glen; - len -= glen; - pos -= glen; + while((pcolwid+unicodeColumnPos(l_buf, l_pos)) >= l_cols) { + int glen = unicodeGraphemeLen(l_buf, l_len, 0); + l_buf += glen; + l_len -= glen; + l_pos -= glen; } - while (pcolwid+unicodeColumnPos(buf, len) > lcols) { - len -= unicodePrevGraphemeLen(buf, len); + while (pcolwid+unicodeColumnPos(l_buf, l_len) > l_cols) { + l_len -= unicodePrevGraphemeLen(l_buf, l_len); } /* Cursor to left edge */ snprintf(seq,64,"\r"); ab += seq; /* Write the prompt and the current buffer content */ - ab += prompt; - ab.append(buf, len); + ab += l_prompt; + ab.append(l_buf, l_len); /* Erase to right */ snprintf(seq,64,"\x1b[0K"); ab += seq; /* Move cursor to original position. */ - snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(buf, pos)+pcolwid)); + snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(l_buf, l_pos)+pcolwid)); ab += seq; if (write(fd,ab.c_str(), static_cast(ab.length())) == -1) {} /* Can't recover from write error. */ } @@ -1966,23 +1966,23 @@ inline void linenoiseState::refreshSingleLine() { * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ inline void linenoiseState::refreshMultiLine() { - if (lcols <= 0) - lcols = getColumns(ifd, ofd); + if (l_cols <= 0) + l_cols = getColumns(l_ifd, l_ofd); char seq[64]; - int pcolwid = unicodeColumnPos(prompt.c_str(), static_cast(prompt.length())); - int colpos = unicodeColumnPosForMultiLine(buf, len, len, lcols, pcolwid); + int pcolwid = unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length())); + int colpos = unicodeColumnPosForMultiLine(l_buf, l_len, l_len, l_cols, pcolwid); int colpos2; /* cursor column position. */ - int rows = (pcolwid+colpos+lcols-1)/lcols; /* rows used by current buf. */ - int rpos = (pcolwid+oldcolpos+lcols)/lcols; /* cursor relative row. */ + int rows = (pcolwid+colpos+l_cols-1)/l_cols; /* rows used by current buf. */ + int rpos = (pcolwid+l_oldcolpos+l_cols)/l_cols; /* cursor relative row. */ int rpos2; /* rpos after refresh. */ int col; /* column position, zero-based. */ - int old_rows = (int)maxrows; - int fd = ofd, j; + int old_rows = (int)l_maxrows; + int fd = l_ofd; std::string ab; /* Update maxrows if needed. */ - if (rows > (int)maxrows) maxrows = rows; + if (rows > (int)l_maxrows) l_maxrows = rows; /* First step: clear all the lines used before. To do so start by * going to the last row. */ @@ -1992,7 +1992,7 @@ inline void linenoiseState::refreshMultiLine() { } /* Now for every row clear it, go up. */ - for (j = 0; j < old_rows-1; j++) { + for (int j = 0; j < old_rows-1; j++) { snprintf(seq,64,"\r\x1b[0K\x1b[1A"); ab += seq; } @@ -2002,27 +2002,27 @@ inline void linenoiseState::refreshMultiLine() { ab += seq; /* Write the prompt and the current buffer content */ - ab += prompt; - ab.append(buf, len); + ab += l_prompt; + ab.append(l_buf, l_len); /* Get text width to cursor position */ - colpos2 = unicodeColumnPosForMultiLine(buf, len, pos, lcols, pcolwid); + colpos2 = unicodeColumnPosForMultiLine(l_buf, l_len, l_pos, l_cols, pcolwid); /* If we are at the very end of the screen with our prompt, we need to * emit a newline and move the prompt to the first column. */ - if (pos && - pos == len && - (colpos2+pcolwid) % lcols == 0) + if (l_pos && + l_pos == l_len && + (colpos2+pcolwid) % l_cols == 0) { ab += "\n"; snprintf(seq,64,"\r"); ab += seq; rows++; - if (rows > (int)maxrows) maxrows = rows; + if (rows > (int)l_maxrows) l_maxrows = rows; } /* Move cursor to right position. */ - rpos2 = (pcolwid+colpos2+lcols)/lcols; /* current cursor relative row. */ + rpos2 = (pcolwid+colpos2+l_cols)/l_cols; /* current cursor relative row. */ /* Go up till we reach the expected position. */ if (rows-rpos2 > 0) { @@ -2031,14 +2031,14 @@ inline void linenoiseState::refreshMultiLine() { } /* Set column. */ - col = (pcolwid + colpos2) % lcols; + col = (pcolwid + colpos2) % l_cols; if (col) snprintf(seq,64,"\r\x1b[%dC", col); else snprintf(seq,64,"\r"); ab += seq; - oldcolpos = colpos2; + l_oldcolpos = colpos2; if (write(fd,ab.c_str(), static_cast(ab.length())) == -1) {} /* Can't recover from write error. */ } @@ -2046,12 +2046,12 @@ inline void linenoiseState::refreshMultiLine() { /* Calls the two low level functions refreshSingleLine() or * refreshMultiLine() according to the selected mode. */ inline void linenoiseState::RefreshLine() { - r_mutex.lock(); + l_mutex.lock(); if (mlmode) refreshMultiLine(); else refreshSingleLine(); - r_mutex.unlock(); + l_mutex.unlock(); } inline void linenoiseState::WipeLine() { linenoiseWipeLine(); } @@ -2062,25 +2062,25 @@ inline void linenoiseState::ClearScreen() { linenoiseClearScreen(); } * * On error writing to the terminal -1 is returned, otherwise 0. */ inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { - if (len < buflen) { - if (len == pos) { - memcpy(&buf[pos],cbuf,clen); - pos+=clen; - len+=clen;; - buf[len] = '\0'; - if ((!mlmode && unicodeColumnPos(prompt.c_str(), static_cast(prompt.length()))+unicodeColumnPos(buf,len) < lcols) /* || mlmode */) { + if (l_len < l_buflen) { + if (l_len == l_pos) { + memcpy(&l_buf[l_pos],cbuf,clen); + l_pos+=clen; + l_len+=clen;; + l_buf[l_len] = '\0'; + if ((!mlmode && unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length()))+unicodeColumnPos(l_buf,l_len) < l_cols) /* || mlmode */) { /* Avoid a full update of the line in the * trivial case. */ - if (write(ofd,cbuf,clen) == -1) return -1; + if (write(l_ofd,cbuf,clen) == -1) return -1; } else { RefreshLine(); } } else { - memmove(buf+pos+clen,buf+pos,len-pos); - memcpy(&buf[pos],cbuf,clen); - pos+=clen; - len+=clen; - buf[len] = '\0'; + memmove(l_buf+l_pos+clen,l_buf+l_pos,l_len-l_pos); + memcpy(&l_buf[l_pos],cbuf,clen); + l_pos+=clen; + l_len+=clen; + l_buf[l_len] = '\0'; RefreshLine(); } } @@ -2089,32 +2089,32 @@ inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { /* Move cursor on the left. */ inline void linenoiseState::linenoiseEditMoveLeft() { - if (pos > 0) { - pos -= unicodePrevGraphemeLen(buf, pos); + if (l_pos > 0) { + l_pos -= unicodePrevGraphemeLen(l_buf, l_pos); RefreshLine(); } } /* Move cursor on the right. */ inline void linenoiseState::linenoiseEditMoveRight() { - if (pos != len) { - pos += unicodeGraphemeLen(buf, len, pos); + if (l_pos != l_len) { + l_pos += unicodeGraphemeLen(l_buf, l_len, l_pos); RefreshLine(); } } /* Move cursor to the start of the line. */ inline void linenoiseState::linenoiseEditMoveHome() { - if (pos != 0) { - pos = 0; + if (l_pos != 0) { + l_pos = 0; RefreshLine(); } } /* Move cursor to the end of the line. */ inline void linenoiseState::linenoiseEditMoveEnd() { - if (pos != len) { - pos = len; + if (l_pos != l_len) { + l_pos = l_len; RefreshLine(); } } @@ -2124,55 +2124,55 @@ inline void linenoiseState::linenoiseEditMoveEnd() { #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 inline void linenoiseState::linenoiseEditHistoryNext(int dir) { - int history_size = static_cast(history.size()); - if (!history_size) + int l_history_size = static_cast(l_history.size()); + if (!l_history_size) return; /* Show the new entry */ - if (history_index == -1) { - history_tmpbuf = std::string(buf); - history_index = history.size() - 1; + if (l_history_index == -1) { + l_history_tmpbuf = std::string(l_buf); + l_history_index = l_history.size() - 1; } else { - if (history_index == history_size) - history_tmpbuf = std::string(buf); - history_index += (dir == LINENOISE_HISTORY_PREV) ? -1 : 1; + if (l_history_index == l_history_size) + l_history_tmpbuf = std::string(l_buf); + l_history_index += (dir == LINENOISE_HISTORY_PREV) ? -1 : 1; } - if (history_index < 0) - history_index = history_size; - if (history_index == history_size) { - memset(buf, 0, buflen); - strcpy(buf, history_tmpbuf.c_str()); - len = pos = static_cast(history_tmpbuf.size()); + if (l_history_index < 0) + l_history_index = l_history_size; + if (l_history_index == l_history_size) { + memset(l_buf, 0, l_buflen); + strcpy(l_buf, l_history_tmpbuf.c_str()); + l_len = l_pos = static_cast(l_history_tmpbuf.size()); RefreshLine(); return; } - if (history_index > history_size) - history_index = 0; - memset(buf, 0, buflen); - strcpy(buf, history[history_index].c_str()); - len = pos = static_cast(strlen(buf)); + if (l_history_index > l_history_size) + l_history_index = 0; + memset(l_buf, 0, l_buflen); + strcpy(l_buf, l_history[l_history_index].c_str()); + l_len = l_pos = static_cast(strlen(l_buf)); RefreshLine(); } /* Delete the character at the right of the cursor without altering the cursor * position. Basically this is what happens with the "Delete" keyboard key. */ inline void linenoiseState::linenoiseEditDelete() { - if (len > 0 && pos < len) { - int glen = unicodeGraphemeLen(buf,len,pos); - memmove(buf+pos,buf+pos+glen,len-pos-glen); - len-=glen; - buf[len] = '\0'; + if (l_len > 0 && l_pos < l_len) { + int glen = unicodeGraphemeLen(l_buf,l_len,l_pos); + memmove(l_buf+l_pos,l_buf+l_pos+glen,l_len-l_pos-glen); + l_len-=glen; + l_buf[l_len] = '\0'; RefreshLine(); } } /* Backspace implementation. */ inline void linenoiseState::linenoiseEditBackspace() { - if (pos > 0 && len > 0) { - int glen = unicodePrevGraphemeLen(buf,pos); - memmove(buf+pos-glen,buf+pos,len-pos); - pos-=glen; - len-=glen; - buf[len] = '\0'; + if (l_pos > 0 && l_len > 0) { + int glen = unicodePrevGraphemeLen(l_buf,l_pos); + memmove(l_buf+l_pos-glen,l_buf+l_pos,l_len-l_pos); + l_pos-=glen; + l_len-=glen; + l_buf[l_len] = '\0'; RefreshLine(); } } @@ -2180,16 +2180,16 @@ inline void linenoiseState::linenoiseEditBackspace() { /* Delete the previous word, maintaining the cursor at the start of the * current word. */ inline void linenoiseState::linenoiseEditDeletePrevWord() { - int old_pos = pos; + int old_pos = l_pos; int diff; - while (pos > 0 && buf[pos-1] == ' ') - pos--; - while (pos > 0 && buf[pos-1] != ' ') - pos--; - diff = old_pos - pos; - memmove(buf+pos,buf+old_pos,len-old_pos+1); - len -= diff; + while (l_pos > 0 && l_buf[l_pos-1] == ' ') + l_pos--; + while (l_pos > 0 && l_buf[l_pos-1] != ' ') + l_pos--; + diff = old_pos - l_pos; + memmove(l_buf+l_pos,l_buf+old_pos,l_len-old_pos+1); + l_len -= diff; RefreshLine(); } @@ -2203,7 +2203,7 @@ inline void linenoiseState::linenoiseEditDeletePrevWord() { * The function returns the length of the current buffer. */ inline int linenoiseState::linenoiseEdit() { - if (write(ofd, prompt.c_str(), static_cast(prompt.length())) == -1) return -1; + if (write(l_ofd, l_prompt.c_str(), static_cast(l_prompt.length())) == -1) return -1; while(1) { int c; char cbuf[4]; @@ -2216,9 +2216,9 @@ inline int linenoiseState::linenoiseEdit() cbuf[0] = c; } #else - nread = unicodeReadUTF8Char(ifd,cbuf,&c); + nread = unicodeReadUTF8Char(l_ifd,cbuf,&c); #endif - if (nread <= 0) return (int)len; + if (nread <= 0) return (int)l_len; /* Only autocomplete when the callback is set. It returns < 0 when * there was an error reading from fd. Otherwise it will return the @@ -2226,19 +2226,19 @@ inline int linenoiseState::linenoiseEdit() if (c == 9 && completionCallback != NULL) { nread = completeLine(cbuf,&c); /* Return on errors */ - if (c < 0) return len; + if (c < 0) return l_len; /* Read next character when 0 */ if (c == 0) continue; } switch(c) { case ENTER: /* enter */ - history_index = -1; - if (history.size() == history_max_len) - history.pop_back(); + l_history_index = -1; + if (l_history.size() == l_history_max_len) + l_history.pop_back(); if (mlmode) linenoiseEditMoveEnd(); - return (int)len; + return (int)l_len; case CTRL_C: /* ctrl-c */ errno = EAGAIN; return -1; @@ -2248,19 +2248,19 @@ inline int linenoiseState::linenoiseEdit() break; case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the line is empty, act as end-of-file. */ - if (len > 0) { + if (l_len > 0) { linenoiseEditDelete(); } else { - history.pop_back(); + l_history.pop_back(); return -1; } break; case CTRL_T: /* ctrl-t, swaps current character with previous. */ - if (pos > 0 && pos < len) { - char aux = wbuf[pos-1]; - wbuf[pos-1] = wbuf[pos]; - wbuf[pos] = aux; - if (pos != len-1) pos++; + if (l_pos > 0 && l_pos < l_len) { + char aux = l_wbuf[l_pos-1]; + l_wbuf[l_pos-1] = l_wbuf[l_pos]; + l_wbuf[l_pos] = aux; + if (l_pos != l_len-1) l_pos++; RefreshLine(); } break; @@ -2280,14 +2280,14 @@ inline int linenoiseState::linenoiseEdit() /* Read the next two bytes representing the escape sequence. * Use two calls to handle slow terminals returning the two * chars at different times. */ - if (read(ifd,seq,1) == -1) break; - if (read(ifd,seq+1,1) == -1) break; + if (read(l_ifd,seq,1) == -1) break; + if (read(l_ifd,seq+1,1) == -1) break; /* ESC [ sequences. */ if (seq[0] == '[') { if (seq[1] >= '0' && seq[1] <= '9') { /* Extended escape, read additional byte. */ - if (read(ifd,seq+2,1) == -1) break; + if (read(l_ifd,seq+2,1) == -1) break; if (seq[2] == '~') { switch(seq[1]) { case '3': /* Delete key. */ @@ -2335,13 +2335,13 @@ inline int linenoiseState::linenoiseEdit() if (linenoiseEditInsert(cbuf,nread)) return -1; break; case CTRL_U: /* Ctrl+u, delete the whole line. */ - wbuf[0] = '\0'; - pos = len = 0; + l_wbuf[0] = '\0'; + l_pos = l_len = 0; RefreshLine(); break; case CTRL_K: /* Ctrl+k, delete from current to end of line. */ - wbuf[pos] = '\0'; - len = pos; + l_wbuf[l_pos] = '\0'; + l_len = l_pos; RefreshLine(); break; case CTRL_A: /* Ctrl+a, go to the start of the line */ @@ -2359,7 +2359,7 @@ inline int linenoiseState::linenoiseEdit() break; } } - return len; + return l_len; } /* This function calls the line editing function linenoiseEdit() using @@ -2389,16 +2389,16 @@ inline bool linenoiseState::linenoiseRaw(std::string& line) { /* Buffer starts empty. Since we're potentially * reusing the state, we need to reset these. */ - pos = 0; - len = 0; - buf[0] = '\0'; - wbuf[0] = '\0'; + l_pos = 0; + l_len = 0; + l_buf[0] = '\0'; + l_wbuf[0] = '\0'; auto count = linenoiseEdit(); if (count == -1) { quit = true; } else { - line.assign(buf, count); + line.assign(l_buf, count); } disableRawMode(STDIN_FILENO); @@ -2410,14 +2410,14 @@ inline bool linenoiseState::linenoiseRaw(std::string& line) { inline linenoiseState::linenoiseState(const char *prompt_str, int stdin_fd, int stdout_fd) { /* Populate the linenoise state that we pass to functions implementing * specific editing functionalities. */ - ifd = stdin_fd; - ofd = stdout_fd; - buf = wbuf; - prompt = (prompt_str) ? std::string(prompt_str) : std::string("> "); + l_ifd = stdin_fd; + l_ofd = stdout_fd; + l_buf = l_wbuf; + l_prompt = (prompt_str) ? std::string(prompt_str) : std::string("> "); /* Buffer starts empty. */ - buf[0] = '\0'; - buflen--; /* Make sure there is always space for the nulterm */ + l_buf[0] = '\0'; + l_buflen--; /* Make sure there is always space for the nulterm */ } inline void linenoiseState::EnableMultiLine() { SetMultiLine(true); } @@ -2430,7 +2430,7 @@ inline void linenoiseState::DisableMultiLine() { SetMultiLine(false); } * something even in the most desperate of the conditions. */ inline bool linenoiseState::Readline(std::string& line) { if (isUnsupportedTerm()) { - printf("%s",prompt.c_str()); + printf("%s", l_prompt.c_str()); fflush(stdout); std::getline(std::cin, line); return false; @@ -2472,19 +2472,19 @@ inline void linenoiseAtExit(void) { * * Using a circular buffer is smarter, but a bit more complex to handle. */ inline bool linenoiseState::AddHistory(const char* line) { - if (history_max_len == 0) + if (l_history_max_len == 0) return false; if (!line || !strlen(line)) return false; /* Don't add duplicated lines. */ - if (!history.empty() && history.back() == line) return false; + if (!l_history.empty() && history.back() == line) return false; /* If we reached the max length, remove the older line. */ - if (history.size() == history_max_len) { - history.erase(history.begin()); + if (l_history.size() == l_history_max_len) { + l_history.erase(l_history.begin()); } - history.push_back(line); + l_history.push_back(line); return true; } @@ -2495,9 +2495,9 @@ inline bool linenoiseState::AddHistory(const char* line) { * than the amount of items already inside the history. */ inline bool linenoiseState::SetHistoryMaxLen(size_t mlen) { if (mlen < 1) return false; - history_max_len = mlen; - if (mlen < history.size()) { - history.resize(mlen); + l_history_max_len = mlen; + if (mlen < l_history.size()) { + l_history.resize(mlen); } return true; } @@ -2569,13 +2569,13 @@ inline const std::vector& GetHistory(){ inline bool Readline(const char *prompt, std::string& line){ if (!lglobal) lglobal = new linenoiseState(); - lglobal->prompt = std::string(prompt); + lglobal->l_prompt = std::string(prompt); return lglobal->Readline(line); }; inline std::string Readline(const char *prompt, bool& quit){ if (!lglobal) lglobal = new linenoiseState(); - lglobal->prompt = std::string(prompt); + lglobal->l_prompt = std::string(prompt); std::string line; quit = lglobal->Readline(line); return line; From e17e9552afffc9854d164c01a68ac0d46b1735d8 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:13:12 -0400 Subject: [PATCH 15/22] Stray static global mess with history rename. Add some comments. --- linenoise.hpp | 53 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index d05095d..ae851bb 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -163,6 +163,7 @@ namespace linenoise { #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 +#define LINENOISE_HISTORY_DO_SETUP -INT_MAX typedef std::function&)> CompletionCallback; @@ -228,7 +229,7 @@ class linenoiseState { int l_len = 0; /* Current edited line length. */ int l_cols = -1; /* Number of columns in terminal. */ int l_maxrows = 0; /* Maximum num of rows used so far (multiline mode) */ - int l_history_index = -1; /* The history index we are currently editing. */ + int l_history_index = LINENOISE_HISTORY_DO_SETUP; /* The history index we are currently editing. */ char l_wbuf[LINENOISE_MAX_LINE] = {'\0'}; std::string l_history_tmpbuf; @@ -1158,7 +1159,6 @@ static struct termios orig_termios; /* In order to restore at exit.*/ static bool rawmode = false; /* For atexit() function to check if restore is needed*/ static bool mlmode = false; /* Multi line mode. Default is single line. */ static bool atexit_registered = false; /* Register atexit just 1 time. */ -static std::vector history; enum KEY_ACTION { KEY_NULL = 0, /* NULL */ @@ -2124,29 +2124,46 @@ inline void linenoiseState::linenoiseEditMoveEnd() { #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 inline void linenoiseState::linenoiseEditHistoryNext(int dir) { - int l_history_size = static_cast(l_history.size()); - if (!l_history_size) + int index_cnt = static_cast(l_history.size()); + // If we have no history, there's nothing to do + if (!index_cnt) return; - /* Show the new entry */ - if (l_history_index == -1) { + + /* There are two cases where we need to stash the current + * state of the working buffer - if we're in an initialization + * situation, or if the current index position is the current + * working buffer. */ + if (l_history_index == LINENOISE_HISTORY_DO_SETUP) + l_history_index = index_cnt; + if (l_history_index == index_cnt) l_history_tmpbuf = std::string(l_buf); - l_history_index = l_history.size() - 1; - } else { - if (l_history_index == l_history_size) - l_history_tmpbuf = std::string(l_buf); - l_history_index += (dir == LINENOISE_HISTORY_PREV) ? -1 : 1; - } + + // Now that we're ready, and the working buffer state is saved + // to restore as part of the up/down cycling, adjust our index + l_history_index += (dir == LINENOISE_HISTORY_PREV) ? -1 : 1; + + // If we've gone down past the last history entry, circle back + // around to the current working line if (l_history_index < 0) - l_history_index = l_history_size; - if (l_history_index == l_history_size) { + l_history_index = index_cnt; + + // If we are pointing to the current working line, retrieve its + // state from l_history_tmpbuf and return + if (l_history_index == index_cnt) { memset(l_buf, 0, l_buflen); strcpy(l_buf, l_history_tmpbuf.c_str()); l_len = l_pos = static_cast(l_history_tmpbuf.size()); RefreshLine(); return; } - if (l_history_index > l_history_size) + + // If we went up past the working line, circle back to the first + // history entry + if (l_history_index > index_cnt) l_history_index = 0; + + // Special cases handled - put the currently selected history + // line into the buffer memset(l_buf, 0, l_buflen); strcpy(l_buf, l_history[l_history_index].c_str()); l_len = l_pos = static_cast(strlen(l_buf)); @@ -2233,7 +2250,7 @@ inline int linenoiseState::linenoiseEdit() switch(c) { case ENTER: /* enter */ - l_history_index = -1; + l_history_index = LINENOISE_HISTORY_DO_SETUP; if (l_history.size() == l_history_max_len) l_history.pop_back(); if (mlmode) @@ -2478,7 +2495,7 @@ inline bool linenoiseState::AddHistory(const char* line) { return false; /* Don't add duplicated lines. */ - if (!l_history.empty() && history.back() == line) return false; + if (!l_history.empty() && l_history.back() == line) return false; /* If we reached the max length, remove the older line. */ if (l_history.size() == l_history_max_len) { @@ -2507,7 +2524,7 @@ inline bool linenoiseState::SetHistoryMaxLen(size_t mlen) { inline bool linenoiseState::SaveHistory(const char* path) { std::ofstream f(path); // TODO: need 'std::ios::binary'? if (!f) return false; - for (const auto& h: history) { + for (const auto& h: l_history) { f << h << std::endl; } return true; From b5d3c583a5fb266be3331063dfe64902da4baee4 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:18:22 -0400 Subject: [PATCH 16/22] Eliminate a couple more globals --- linenoise.hpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index ae851bb..893c766 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -232,6 +232,7 @@ class linenoiseState { int l_history_index = LINENOISE_HISTORY_DO_SETUP; /* The history index we are currently editing. */ char l_wbuf[LINENOISE_MAX_LINE] = {'\0'}; std::string l_history_tmpbuf; + bool l_mlmode = false; /* Multi line mode. Default is single line. */ size_t l_history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; std::vector l_history; @@ -1150,14 +1151,10 @@ inline int win32_write(int fd, const void *buffer, unsigned int count) { } #endif // _WIN32 -static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; -static CompletionCallback completionCallback; - #ifndef _WIN32 static struct termios orig_termios; /* In order to restore at exit.*/ #endif static bool rawmode = false; /* For atexit() function to check if restore is needed*/ -static bool mlmode = false; /* Multi line mode. Default is single line. */ static bool atexit_registered = false; /* Register atexit just 1 time. */ enum KEY_ACTION { @@ -1642,12 +1639,13 @@ inline int unicodeReadUTF8Char(int fd, char* buf, int* cp) /* Set if to use or not the multi line mode. */ inline void linenoiseState::SetMultiLine(bool ml) { - mlmode = ml; + l_mlmode = ml; } /* Return true if the terminal name is in the list of terminals we know are * not able to understand basic escape sequences. */ inline bool isUnsupportedTerm(void) { + static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; #ifndef _WIN32 char *term = getenv("TERM"); int j; @@ -2047,7 +2045,7 @@ inline void linenoiseState::refreshMultiLine() { * refreshMultiLine() according to the selected mode. */ inline void linenoiseState::RefreshLine() { l_mutex.lock(); - if (mlmode) + if (l_mlmode) refreshMultiLine(); else refreshSingleLine(); @@ -2068,7 +2066,7 @@ inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { l_pos+=clen; l_len+=clen;; l_buf[l_len] = '\0'; - if ((!mlmode && unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length()))+unicodeColumnPos(l_buf,l_len) < l_cols) /* || mlmode */) { + if ((!l_mlmode && unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length()))+unicodeColumnPos(l_buf,l_len) < l_cols) /* || l_mlmode */) { /* Avoid a full update of the line in the * trivial case. */ if (write(l_ofd,cbuf,clen) == -1) return -1; @@ -2253,7 +2251,7 @@ inline int linenoiseState::linenoiseEdit() l_history_index = LINENOISE_HISTORY_DO_SETUP; if (l_history.size() == l_history_max_len) l_history.pop_back(); - if (mlmode) + if (l_mlmode) linenoiseEditMoveEnd(); return (int)l_len; case CTRL_C: /* ctrl-c */ From a66dda23bdc2563c07bdb8fc782e43c3de1db173 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:29:37 -0400 Subject: [PATCH 17/22] Back out history changes --- linenoise.hpp | 74 ++++++++++++++++----------------------------------- 1 file changed, 23 insertions(+), 51 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index 893c766..f4b3bf2 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -229,7 +229,7 @@ class linenoiseState { int l_len = 0; /* Current edited line length. */ int l_cols = -1; /* Number of columns in terminal. */ int l_maxrows = 0; /* Maximum num of rows used so far (multiline mode) */ - int l_history_index = LINENOISE_HISTORY_DO_SETUP; /* The history index we are currently editing. */ + int l_history_index = 0; /* The history index we are currently editing. */ char l_wbuf[LINENOISE_MAX_LINE] = {'\0'}; std::string l_history_tmpbuf; bool l_mlmode = false; /* Multi line mode. Default is single line. */ @@ -2122,50 +2122,24 @@ inline void linenoiseState::linenoiseEditMoveEnd() { #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 inline void linenoiseState::linenoiseEditHistoryNext(int dir) { - int index_cnt = static_cast(l_history.size()); - // If we have no history, there's nothing to do - if (!index_cnt) - return; - - /* There are two cases where we need to stash the current - * state of the working buffer - if we're in an initialization - * situation, or if the current index position is the current - * working buffer. */ - if (l_history_index == LINENOISE_HISTORY_DO_SETUP) - l_history_index = index_cnt; - if (l_history_index == index_cnt) - l_history_tmpbuf = std::string(l_buf); - - // Now that we're ready, and the working buffer state is saved - // to restore as part of the up/down cycling, adjust our index - l_history_index += (dir == LINENOISE_HISTORY_PREV) ? -1 : 1; - - // If we've gone down past the last history entry, circle back - // around to the current working line - if (l_history_index < 0) - l_history_index = index_cnt; - - // If we are pointing to the current working line, retrieve its - // state from l_history_tmpbuf and return - if (l_history_index == index_cnt) { + if (l_history.size() > 1) { + /* Update the current history entry before to + * overwrite it with the next one. */ + l_history[l_history.size() - 1 - l_history_index] = l_buf; + /* Show the new entry */ + l_history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1; + if (l_history_index < 0) { + l_history_index = 0; + return; + } else if (l_history_index >= (int)l_history.size()) { + l_history_index = static_cast(l_history.size())-1; + return; + } memset(l_buf, 0, l_buflen); - strcpy(l_buf, l_history_tmpbuf.c_str()); - l_len = l_pos = static_cast(l_history_tmpbuf.size()); + strcpy(l_buf,l_history[l_history.size() - 1 - l_history_index].c_str()); + l_len = l_pos = static_cast(strlen(l_buf)); RefreshLine(); - return; } - - // If we went up past the working line, circle back to the first - // history entry - if (l_history_index > index_cnt) - l_history_index = 0; - - // Special cases handled - put the currently selected history - // line into the buffer - memset(l_buf, 0, l_buflen); - strcpy(l_buf, l_history[l_history_index].c_str()); - l_len = l_pos = static_cast(strlen(l_buf)); - RefreshLine(); } /* Delete the character at the right of the cursor without altering the cursor @@ -2218,6 +2192,10 @@ inline void linenoiseState::linenoiseEditDeletePrevWord() { * The function returns the length of the current buffer. */ inline int linenoiseState::linenoiseEdit() { + /* The latest history entry is always our current buffer, that + * initially is just an empty string. */ + AddHistory(""); + if (write(l_ofd, l_prompt.c_str(), static_cast(l_prompt.length())) == -1) return -1; while(1) { int c; @@ -2248,11 +2226,8 @@ inline int linenoiseState::linenoiseEdit() switch(c) { case ENTER: /* enter */ - l_history_index = LINENOISE_HISTORY_DO_SETUP; - if (l_history.size() == l_history_max_len) - l_history.pop_back(); - if (l_mlmode) - linenoiseEditMoveEnd(); + if (!l_history.empty()) l_history.pop_back(); + if (l_mlmode) linenoiseEditMoveEnd(); return (int)l_len; case CTRL_C: /* ctrl-c */ errno = EAGAIN; @@ -2487,10 +2462,7 @@ inline void linenoiseAtExit(void) { * * Using a circular buffer is smarter, but a bit more complex to handle. */ inline bool linenoiseState::AddHistory(const char* line) { - if (l_history_max_len == 0) - return false; - if (!line || !strlen(line)) - return false; + if (l_history_max_len == 0) return false; /* Don't add duplicated lines. */ if (!l_history.empty() && l_history.back() == line) return false; From 507d8314cc4c9f10589a88f097ad3546101b5c5a Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:46:21 -0400 Subject: [PATCH 18/22] Use suffix character for class variables --- linenoise.hpp | 336 +++++++++++++++++++++++++------------------------- 1 file changed, 171 insertions(+), 165 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index f4b3bf2..303afc9 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -180,7 +180,7 @@ class linenoiseState { bool AddHistory(const char *line); bool LoadHistory(const char *path); bool SaveHistory(const char *path); - const std::vector &GetHistory() { return l_history; }; + const std::vector &GetHistory() { return history_; }; bool SetHistoryMaxLen(size_t len); bool Readline(std::string &line); // Primary linenoise entry point @@ -188,14 +188,14 @@ class linenoiseState { void WipeLine(); // Temporarily removes line from screen - RefreshLine will restore void ClearScreen(); // Clear terminal window + void SetPrompt(const char *p); + void SetPrompt(std::string &p); + /* Register a callback function to be called for tab-completion. */ void SetCompletionCallback(CompletionCallback fn) { completionCallback = fn; }; - std::string l_prompt = std::string("> "); /* Prompt to display. */ - std::mutex l_mutex; - // For functional interface void SetMultiLine(bool ml); @@ -220,22 +220,24 @@ class linenoiseState { CompletionCallback completionCallback; - int l_ifd = STDIN_FILENO; /* Terminal stdin file descriptor. */ - int l_ofd = STDOUT_FILENO; /* Terminal stdout file descriptor. */ - char *l_buf = l_wbuf; /* Edited line buffer. */ - int l_buflen = LINENOISE_MAX_LINE; /* Edited line buffer size. */ - int l_pos = 0; /* Current cursor position. */ - int l_oldcolpos = 0; /* Previous refresh cursor column position. */ - int l_len = 0; /* Current edited line length. */ - int l_cols = -1; /* Number of columns in terminal. */ - int l_maxrows = 0; /* Maximum num of rows used so far (multiline mode) */ - int l_history_index = 0; /* The history index we are currently editing. */ - char l_wbuf[LINENOISE_MAX_LINE] = {'\0'}; - std::string l_history_tmpbuf; - bool l_mlmode = false; /* Multi line mode. Default is single line. */ - - size_t l_history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; - std::vector l_history; + int ifd_ = STDIN_FILENO; /* Terminal stdin file descriptor. */ + int ofd_ = STDOUT_FILENO; /* Terminal stdout file descriptor. */ + char *buf_ = wbuf_; /* Edited line buffer. */ + int buf_len_ = LINENOISE_MAX_LINE; /* Edited line buffer size. */ + int pos_ = 0; /* Current cursor position. */ + int oldcolpos_ = 0; /* Previous refresh cursor column position. */ + int len_ = 0; /* Current edited line length. */ + int cols_ = -1; /* Number of columns in terminal. */ + int maxrows_ = 0; /* Maximum num of rows used so far (multiline mode) */ + int history_index_ = 0; /* The history index we are currently editing. */ + char wbuf_[LINENOISE_MAX_LINE] = {'\0'}; + std::string history_tmpbuf_; + bool mlmode_ = false; /* Multi line mode. Default is single line. */ + std::mutex mutex_; + std::string prompt_ = std::string("> "); /* Prompt to display. */ + + size_t history_max_len_ = LINENOISE_DEFAULT_HISTORY_MAX_LEN; + std::vector history_; }; @@ -1639,7 +1641,7 @@ inline int unicodeReadUTF8Char(int fd, char* buf, int* cp) /* Set if to use or not the multi line mode. */ inline void linenoiseState::SetMultiLine(bool ml) { - l_mlmode = ml; + mlmode_ = ml; } /* Return true if the terminal name is in the list of terminals we know are @@ -1858,7 +1860,7 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { int nread = 0, nwritten; *c = 0; - completionCallback(l_buf,lc); + completionCallback(buf_,lc); if (lc.empty()) { linenoiseBeep(); } else { @@ -1867,27 +1869,27 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { while(!stop) { /* Show completion or original buffer */ if (i < static_cast(lc.size())) { - int old_len = l_len; - int old_pos = l_pos; - char *old_buf = l_buf; - l_len = l_pos = static_cast(lc[i].size()); - l_buf = &lc[i][0]; + int old_len = len_; + int old_pos = pos_; + char *old_buf = buf_; + len_ = pos_ = static_cast(lc[i].size()); + buf_ = &lc[i][0]; RefreshLine(); - l_len = old_len; - l_pos = old_pos; - l_buf = old_buf; + len_ = old_len; + pos_ = old_pos; + buf_ = old_buf; } else { RefreshLine(); } - //nread = read(l_ifd,&c,1); + //nread = read(ifd_,&c,1); #ifdef _WIN32 nread = win32read(c); if (nread == 1) { cbuf[0] = *c; } #else - nread = unicodeReadUTF8Char(l_ifd,cbuf,c); + nread = unicodeReadUTF8Char(ifd_,cbuf,c); #endif if (nread <= 0) { *c = -1; @@ -1907,8 +1909,8 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { default: /* Update buffer and return */ if (i < static_cast(lc.size())) { - nwritten = snprintf(l_buf,l_buflen,"%s",&lc[i][0]); - l_len = l_pos = nwritten; + nwritten = snprintf(buf_,buf_len_,"%s",&lc[i][0]); + len_ = pos_ = nwritten; } stop = 1; break; @@ -1927,34 +1929,34 @@ inline int linenoiseState::completeLine(char *cbuf, int *c) { * cursor position, and number of columns of the terminal. */ inline void linenoiseState::refreshSingleLine() { char seq[64]; - int pcolwid = unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length())); - int fd = l_ofd; + int pcolwid = unicodeColumnPos(prompt_.c_str(), static_cast(prompt_.length())); + int fd = ofd_; std::string ab; - if (l_cols <= 0) - l_cols = getColumns(l_ifd, l_ofd); + if (cols_ <= 0) + cols_ = getColumns(ifd_, ofd_); - while((pcolwid+unicodeColumnPos(l_buf, l_pos)) >= l_cols) { - int glen = unicodeGraphemeLen(l_buf, l_len, 0); - l_buf += glen; - l_len -= glen; - l_pos -= glen; + while((pcolwid+unicodeColumnPos(buf_, pos_)) >= cols_) { + int glen = unicodeGraphemeLen(buf_, len_, 0); + buf_ += glen; + len_ -= glen; + pos_ -= glen; } - while (pcolwid+unicodeColumnPos(l_buf, l_len) > l_cols) { - l_len -= unicodePrevGraphemeLen(l_buf, l_len); + while (pcolwid+unicodeColumnPos(buf_, len_) > cols_) { + len_ -= unicodePrevGraphemeLen(buf_, len_); } /* Cursor to left edge */ snprintf(seq,64,"\r"); ab += seq; /* Write the prompt and the current buffer content */ - ab += l_prompt; - ab.append(l_buf, l_len); + ab += prompt_; + ab.append(buf_, len_); /* Erase to right */ snprintf(seq,64,"\x1b[0K"); ab += seq; /* Move cursor to original position. */ - snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(l_buf, l_pos)+pcolwid)); + snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(buf_, pos_)+pcolwid)); ab += seq; if (write(fd,ab.c_str(), static_cast(ab.length())) == -1) {} /* Can't recover from write error. */ } @@ -1964,23 +1966,23 @@ inline void linenoiseState::refreshSingleLine() { * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ inline void linenoiseState::refreshMultiLine() { - if (l_cols <= 0) - l_cols = getColumns(l_ifd, l_ofd); + if (cols_ <= 0) + cols_ = getColumns(ifd_, ofd_); char seq[64]; - int pcolwid = unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length())); - int colpos = unicodeColumnPosForMultiLine(l_buf, l_len, l_len, l_cols, pcolwid); + int pcolwid = unicodeColumnPos(prompt_.c_str(), static_cast(prompt_.length())); + int colpos = unicodeColumnPosForMultiLine(buf_, len_, len_, cols_, pcolwid); int colpos2; /* cursor column position. */ - int rows = (pcolwid+colpos+l_cols-1)/l_cols; /* rows used by current buf. */ - int rpos = (pcolwid+l_oldcolpos+l_cols)/l_cols; /* cursor relative row. */ + int rows = (pcolwid+colpos+cols_-1)/cols_; /* rows used by current buf. */ + int rpos = (pcolwid+oldcolpos_+cols_)/cols_; /* cursor relative row. */ int rpos2; /* rpos after refresh. */ int col; /* column position, zero-based. */ - int old_rows = (int)l_maxrows; - int fd = l_ofd; + int old_rows = (int)maxrows_; + int fd = ofd_; std::string ab; /* Update maxrows if needed. */ - if (rows > (int)l_maxrows) l_maxrows = rows; + if (rows > (int)maxrows_) maxrows_ = rows; /* First step: clear all the lines used before. To do so start by * going to the last row. */ @@ -2000,27 +2002,27 @@ inline void linenoiseState::refreshMultiLine() { ab += seq; /* Write the prompt and the current buffer content */ - ab += l_prompt; - ab.append(l_buf, l_len); + ab += prompt_; + ab.append(buf_, len_); /* Get text width to cursor position */ - colpos2 = unicodeColumnPosForMultiLine(l_buf, l_len, l_pos, l_cols, pcolwid); + colpos2 = unicodeColumnPosForMultiLine(buf_, len_, pos_, cols_, pcolwid); /* If we are at the very end of the screen with our prompt, we need to * emit a newline and move the prompt to the first column. */ - if (l_pos && - l_pos == l_len && - (colpos2+pcolwid) % l_cols == 0) + if (pos_ && + pos_ == len_ && + (colpos2+pcolwid) % cols_ == 0) { ab += "\n"; snprintf(seq,64,"\r"); ab += seq; rows++; - if (rows > (int)l_maxrows) l_maxrows = rows; + if (rows > (int)maxrows_) maxrows_ = rows; } /* Move cursor to right position. */ - rpos2 = (pcolwid+colpos2+l_cols)/l_cols; /* current cursor relative row. */ + rpos2 = (pcolwid+colpos2+cols_)/cols_; /* current cursor relative row. */ /* Go up till we reach the expected position. */ if (rows-rpos2 > 0) { @@ -2029,14 +2031,14 @@ inline void linenoiseState::refreshMultiLine() { } /* Set column. */ - col = (pcolwid + colpos2) % l_cols; + col = (pcolwid + colpos2) % cols_; if (col) snprintf(seq,64,"\r\x1b[%dC", col); else snprintf(seq,64,"\r"); ab += seq; - l_oldcolpos = colpos2; + oldcolpos_ = colpos2; if (write(fd,ab.c_str(), static_cast(ab.length())) == -1) {} /* Can't recover from write error. */ } @@ -2044,41 +2046,44 @@ inline void linenoiseState::refreshMultiLine() { /* Calls the two low level functions refreshSingleLine() or * refreshMultiLine() according to the selected mode. */ inline void linenoiseState::RefreshLine() { - l_mutex.lock(); - if (l_mlmode) + mutex_.lock(); + if (mlmode_) refreshMultiLine(); else refreshSingleLine(); - l_mutex.unlock(); + mutex_.unlock(); } inline void linenoiseState::WipeLine() { linenoiseWipeLine(); } inline void linenoiseState::ClearScreen() { linenoiseClearScreen(); } +inline void linenoiseState::SetPrompt(const char *p) { prompt_ = std::string(p); } +inline void linenoiseState::SetPrompt(std::string &p) { prompt_ = p; } + /* Insert the character 'c' at cursor current position. * * On error writing to the terminal -1 is returned, otherwise 0. */ inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { - if (l_len < l_buflen) { - if (l_len == l_pos) { - memcpy(&l_buf[l_pos],cbuf,clen); - l_pos+=clen; - l_len+=clen;; - l_buf[l_len] = '\0'; - if ((!l_mlmode && unicodeColumnPos(l_prompt.c_str(), static_cast(l_prompt.length()))+unicodeColumnPos(l_buf,l_len) < l_cols) /* || l_mlmode */) { + if (len_ < buf_len_) { + if (len_ == pos_) { + memcpy(&buf_[pos_],cbuf,clen); + pos_+=clen; + len_+=clen;; + buf_[len_] = '\0'; + if ((!mlmode_ && unicodeColumnPos(prompt_.c_str(), static_cast(prompt_.length()))+unicodeColumnPos(buf_,len_) < cols_) /* || mlmode_ */) { /* Avoid a full update of the line in the * trivial case. */ - if (write(l_ofd,cbuf,clen) == -1) return -1; + if (write(ofd_,cbuf,clen) == -1) return -1; } else { RefreshLine(); } } else { - memmove(l_buf+l_pos+clen,l_buf+l_pos,l_len-l_pos); - memcpy(&l_buf[l_pos],cbuf,clen); - l_pos+=clen; - l_len+=clen; - l_buf[l_len] = '\0'; + memmove(buf_+pos_+clen,buf_+pos_,len_-pos_); + memcpy(&buf_[pos_],cbuf,clen); + pos_+=clen; + len_+=clen; + buf_[len_] = '\0'; RefreshLine(); } } @@ -2087,32 +2092,32 @@ inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { /* Move cursor on the left. */ inline void linenoiseState::linenoiseEditMoveLeft() { - if (l_pos > 0) { - l_pos -= unicodePrevGraphemeLen(l_buf, l_pos); + if (pos_ > 0) { + pos_ -= unicodePrevGraphemeLen(buf_, pos_); RefreshLine(); } } /* Move cursor on the right. */ inline void linenoiseState::linenoiseEditMoveRight() { - if (l_pos != l_len) { - l_pos += unicodeGraphemeLen(l_buf, l_len, l_pos); + if (pos_ != len_) { + pos_ += unicodeGraphemeLen(buf_, len_, pos_); RefreshLine(); } } /* Move cursor to the start of the line. */ inline void linenoiseState::linenoiseEditMoveHome() { - if (l_pos != 0) { - l_pos = 0; + if (pos_ != 0) { + pos_ = 0; RefreshLine(); } } /* Move cursor to the end of the line. */ inline void linenoiseState::linenoiseEditMoveEnd() { - if (l_pos != l_len) { - l_pos = l_len; + if (pos_ != len_) { + pos_ = len_; RefreshLine(); } } @@ -2122,22 +2127,22 @@ inline void linenoiseState::linenoiseEditMoveEnd() { #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 inline void linenoiseState::linenoiseEditHistoryNext(int dir) { - if (l_history.size() > 1) { + if (history_.size() > 1) { /* Update the current history entry before to * overwrite it with the next one. */ - l_history[l_history.size() - 1 - l_history_index] = l_buf; + history_[history_.size() - 1 - history_index_] = buf_; /* Show the new entry */ - l_history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1; - if (l_history_index < 0) { - l_history_index = 0; + history_index_ += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1; + if (history_index_ < 0) { + history_index_ = 0; return; - } else if (l_history_index >= (int)l_history.size()) { - l_history_index = static_cast(l_history.size())-1; + } else if (history_index_ >= (int)history_.size()) { + history_index_ = static_cast(history_.size())-1; return; } - memset(l_buf, 0, l_buflen); - strcpy(l_buf,l_history[l_history.size() - 1 - l_history_index].c_str()); - l_len = l_pos = static_cast(strlen(l_buf)); + memset(buf_, 0, buf_len_); + strcpy(buf_,history_[history_.size() - 1 - history_index_].c_str()); + len_ = pos_ = static_cast(strlen(buf_)); RefreshLine(); } } @@ -2145,23 +2150,23 @@ inline void linenoiseState::linenoiseEditHistoryNext(int dir) { /* Delete the character at the right of the cursor without altering the cursor * position. Basically this is what happens with the "Delete" keyboard key. */ inline void linenoiseState::linenoiseEditDelete() { - if (l_len > 0 && l_pos < l_len) { - int glen = unicodeGraphemeLen(l_buf,l_len,l_pos); - memmove(l_buf+l_pos,l_buf+l_pos+glen,l_len-l_pos-glen); - l_len-=glen; - l_buf[l_len] = '\0'; + if (len_ > 0 && pos_ < len_) { + int glen = unicodeGraphemeLen(buf_,len_,pos_); + memmove(buf_+pos_,buf_+pos_+glen,len_-pos_-glen); + len_-=glen; + buf_[len_] = '\0'; RefreshLine(); } } /* Backspace implementation. */ inline void linenoiseState::linenoiseEditBackspace() { - if (l_pos > 0 && l_len > 0) { - int glen = unicodePrevGraphemeLen(l_buf,l_pos); - memmove(l_buf+l_pos-glen,l_buf+l_pos,l_len-l_pos); - l_pos-=glen; - l_len-=glen; - l_buf[l_len] = '\0'; + if (pos_ > 0 && len_ > 0) { + int glen = unicodePrevGraphemeLen(buf_,pos_); + memmove(buf_+pos_-glen,buf_+pos_,len_-pos_); + pos_-=glen; + len_-=glen; + buf_[len_] = '\0'; RefreshLine(); } } @@ -2169,16 +2174,16 @@ inline void linenoiseState::linenoiseEditBackspace() { /* Delete the previous word, maintaining the cursor at the start of the * current word. */ inline void linenoiseState::linenoiseEditDeletePrevWord() { - int old_pos = l_pos; + int old_pos = pos_; int diff; - while (l_pos > 0 && l_buf[l_pos-1] == ' ') - l_pos--; - while (l_pos > 0 && l_buf[l_pos-1] != ' ') - l_pos--; - diff = old_pos - l_pos; - memmove(l_buf+l_pos,l_buf+old_pos,l_len-old_pos+1); - l_len -= diff; + while (pos_ > 0 && buf_[pos_-1] == ' ') + pos_--; + while (pos_ > 0 && buf_[pos_-1] != ' ') + pos_--; + diff = old_pos - pos_; + memmove(buf_+pos_,buf_+old_pos,len_-old_pos+1); + len_ -= diff; RefreshLine(); } @@ -2196,7 +2201,7 @@ inline int linenoiseState::linenoiseEdit() * initially is just an empty string. */ AddHistory(""); - if (write(l_ofd, l_prompt.c_str(), static_cast(l_prompt.length())) == -1) return -1; + if (write(ofd_, prompt_.c_str(), static_cast(prompt_.length())) == -1) return -1; while(1) { int c; char cbuf[4]; @@ -2209,9 +2214,9 @@ inline int linenoiseState::linenoiseEdit() cbuf[0] = c; } #else - nread = unicodeReadUTF8Char(l_ifd,cbuf,&c); + nread = unicodeReadUTF8Char(ifd_,cbuf,&c); #endif - if (nread <= 0) return (int)l_len; + if (nread <= 0) return (int)len_; /* Only autocomplete when the callback is set. It returns < 0 when * there was an error reading from fd. Otherwise it will return the @@ -2219,16 +2224,16 @@ inline int linenoiseState::linenoiseEdit() if (c == 9 && completionCallback != NULL) { nread = completeLine(cbuf,&c); /* Return on errors */ - if (c < 0) return l_len; + if (c < 0) return len_; /* Read next character when 0 */ if (c == 0) continue; } switch(c) { case ENTER: /* enter */ - if (!l_history.empty()) l_history.pop_back(); - if (l_mlmode) linenoiseEditMoveEnd(); - return (int)l_len; + if (!history_.empty()) history_.pop_back(); + if (mlmode_) linenoiseEditMoveEnd(); + return (int)len_; case CTRL_C: /* ctrl-c */ errno = EAGAIN; return -1; @@ -2238,19 +2243,19 @@ inline int linenoiseState::linenoiseEdit() break; case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the line is empty, act as end-of-file. */ - if (l_len > 0) { + if (len_ > 0) { linenoiseEditDelete(); } else { - l_history.pop_back(); + history_.pop_back(); return -1; } break; case CTRL_T: /* ctrl-t, swaps current character with previous. */ - if (l_pos > 0 && l_pos < l_len) { - char aux = l_wbuf[l_pos-1]; - l_wbuf[l_pos-1] = l_wbuf[l_pos]; - l_wbuf[l_pos] = aux; - if (l_pos != l_len-1) l_pos++; + if (pos_ > 0 && pos_ < len_) { + char aux = wbuf_[pos_-1]; + wbuf_[pos_-1] = wbuf_[pos_]; + wbuf_[pos_] = aux; + if (pos_ != len_-1) pos_++; RefreshLine(); } break; @@ -2270,14 +2275,14 @@ inline int linenoiseState::linenoiseEdit() /* Read the next two bytes representing the escape sequence. * Use two calls to handle slow terminals returning the two * chars at different times. */ - if (read(l_ifd,seq,1) == -1) break; - if (read(l_ifd,seq+1,1) == -1) break; + if (read(ifd_,seq,1) == -1) break; + if (read(ifd_,seq+1,1) == -1) break; /* ESC [ sequences. */ if (seq[0] == '[') { if (seq[1] >= '0' && seq[1] <= '9') { /* Extended escape, read additional byte. */ - if (read(l_ifd,seq+2,1) == -1) break; + if (read(ifd_,seq+2,1) == -1) break; if (seq[2] == '~') { switch(seq[1]) { case '3': /* Delete key. */ @@ -2325,13 +2330,13 @@ inline int linenoiseState::linenoiseEdit() if (linenoiseEditInsert(cbuf,nread)) return -1; break; case CTRL_U: /* Ctrl+u, delete the whole line. */ - l_wbuf[0] = '\0'; - l_pos = l_len = 0; + wbuf_[0] = '\0'; + pos_ = len_ = 0; RefreshLine(); break; case CTRL_K: /* Ctrl+k, delete from current to end of line. */ - l_wbuf[l_pos] = '\0'; - l_len = l_pos; + wbuf_[pos_] = '\0'; + len_ = pos_; RefreshLine(); break; case CTRL_A: /* Ctrl+a, go to the start of the line */ @@ -2349,7 +2354,7 @@ inline int linenoiseState::linenoiseEdit() break; } } - return l_len; + return len_; } /* This function calls the line editing function linenoiseEdit() using @@ -2379,16 +2384,16 @@ inline bool linenoiseState::linenoiseRaw(std::string& line) { /* Buffer starts empty. Since we're potentially * reusing the state, we need to reset these. */ - l_pos = 0; - l_len = 0; - l_buf[0] = '\0'; - l_wbuf[0] = '\0'; + pos_ = 0; + len_ = 0; + buf_[0] = '\0'; + wbuf_[0] = '\0'; auto count = linenoiseEdit(); if (count == -1) { quit = true; } else { - line.assign(l_buf, count); + line.assign(buf_, count); } disableRawMode(STDIN_FILENO); @@ -2400,14 +2405,15 @@ inline bool linenoiseState::linenoiseRaw(std::string& line) { inline linenoiseState::linenoiseState(const char *prompt_str, int stdin_fd, int stdout_fd) { /* Populate the linenoise state that we pass to functions implementing * specific editing functionalities. */ - l_ifd = stdin_fd; - l_ofd = stdout_fd; - l_buf = l_wbuf; - l_prompt = (prompt_str) ? std::string(prompt_str) : std::string("> "); + ifd_ = stdin_fd; + ofd_ = stdout_fd; + buf_ = wbuf_; + std::string p = (prompt_str) ? std::string(prompt_str) : std::string("> "); + SetPrompt(p); /* Buffer starts empty. */ - l_buf[0] = '\0'; - l_buflen--; /* Make sure there is always space for the nulterm */ + buf_[0] = '\0'; + buf_len_--; /* Make sure there is always space for the nulterm */ } inline void linenoiseState::EnableMultiLine() { SetMultiLine(true); } @@ -2420,7 +2426,7 @@ inline void linenoiseState::DisableMultiLine() { SetMultiLine(false); } * something even in the most desperate of the conditions. */ inline bool linenoiseState::Readline(std::string& line) { if (isUnsupportedTerm()) { - printf("%s", l_prompt.c_str()); + printf("%s", prompt_.c_str()); fflush(stdout); std::getline(std::cin, line); return false; @@ -2462,16 +2468,16 @@ inline void linenoiseAtExit(void) { * * Using a circular buffer is smarter, but a bit more complex to handle. */ inline bool linenoiseState::AddHistory(const char* line) { - if (l_history_max_len == 0) return false; + if (history_max_len_ == 0) return false; /* Don't add duplicated lines. */ - if (!l_history.empty() && l_history.back() == line) return false; + if (!history_.empty() && history_.back() == line) return false; /* If we reached the max length, remove the older line. */ - if (l_history.size() == l_history_max_len) { - l_history.erase(l_history.begin()); + if (history_.size() == history_max_len_) { + history_.erase(history_.begin()); } - l_history.push_back(line); + history_.push_back(line); return true; } @@ -2482,9 +2488,9 @@ inline bool linenoiseState::AddHistory(const char* line) { * than the amount of items already inside the history. */ inline bool linenoiseState::SetHistoryMaxLen(size_t mlen) { if (mlen < 1) return false; - l_history_max_len = mlen; - if (mlen < l_history.size()) { - l_history.resize(mlen); + history_max_len_ = mlen; + if (mlen < history_.size()) { + history_.resize(mlen); } return true; } @@ -2494,7 +2500,7 @@ inline bool linenoiseState::SetHistoryMaxLen(size_t mlen) { inline bool linenoiseState::SaveHistory(const char* path) { std::ofstream f(path); // TODO: need 'std::ios::binary'? if (!f) return false; - for (const auto& h: l_history) { + for (const auto& h: history_) { f << h << std::endl; } return true; @@ -2556,13 +2562,13 @@ inline const std::vector& GetHistory(){ inline bool Readline(const char *prompt, std::string& line){ if (!lglobal) lglobal = new linenoiseState(); - lglobal->l_prompt = std::string(prompt); + lglobal->SetPrompt(prompt); return lglobal->Readline(line); }; inline std::string Readline(const char *prompt, bool& quit){ if (!lglobal) lglobal = new linenoiseState(); - lglobal->l_prompt = std::string(prompt); + lglobal->SetPrompt(prompt); std::string line; quit = lglobal->Readline(line); return line; From b68295f0cd6f724ae9a7621ecd7a43c716b4d1d2 Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 10:51:29 -0400 Subject: [PATCH 19/22] Remove unnecessary prefixes from internal class method names --- linenoise.hpp | 92 +++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index 303afc9..f87654b 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -203,17 +203,17 @@ class linenoiseState { std::string Readline(bool &quit); std::string Readline(); - bool linenoiseRaw(std::string &line); - int linenoiseEditInsert(const char *cbuf, int clen); - void linenoiseEditDelete(); - void linenoiseEditBackspace(); - void linenoiseEditDeletePrevWord(); - int linenoiseEdit(); - void linenoiseEditHistoryNext(int dir); - void linenoiseEditMoveLeft(); - void linenoiseEditMoveRight(); - void linenoiseEditMoveHome(); - void linenoiseEditMoveEnd(); + bool Raw(std::string &line); + int EditInsert(const char *cbuf, int clen); + void EditDelete(); + void EditBackspace(); + void EditDeletePrevWord(); + int Edit(); + void EditHistoryNext(int dir); + void EditMoveLeft(); + void EditMoveRight(); + void EditMoveHome(); + void EditMoveEnd(); void refreshSingleLine(); void refreshMultiLine(); int completeLine(char *cbuf, int *c); @@ -1849,7 +1849,7 @@ inline void linenoiseBeep(void) { /* ============================== Completion ================================ */ -/* This is an helper function for linenoiseEdit() and is called when the +/* This is an helper function for Edit() and is called when the * user types the key in order to complete the string currently in the * input. * @@ -2064,7 +2064,7 @@ inline void linenoiseState::SetPrompt(std::string &p) { prompt_ = p; } /* Insert the character 'c' at cursor current position. * * On error writing to the terminal -1 is returned, otherwise 0. */ -inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { +inline int linenoiseState::EditInsert(const char* cbuf, int clen) { if (len_ < buf_len_) { if (len_ == pos_) { memcpy(&buf_[pos_],cbuf,clen); @@ -2091,7 +2091,7 @@ inline int linenoiseState::linenoiseEditInsert(const char* cbuf, int clen) { } /* Move cursor on the left. */ -inline void linenoiseState::linenoiseEditMoveLeft() { +inline void linenoiseState::EditMoveLeft() { if (pos_ > 0) { pos_ -= unicodePrevGraphemeLen(buf_, pos_); RefreshLine(); @@ -2099,7 +2099,7 @@ inline void linenoiseState::linenoiseEditMoveLeft() { } /* Move cursor on the right. */ -inline void linenoiseState::linenoiseEditMoveRight() { +inline void linenoiseState::EditMoveRight() { if (pos_ != len_) { pos_ += unicodeGraphemeLen(buf_, len_, pos_); RefreshLine(); @@ -2107,7 +2107,7 @@ inline void linenoiseState::linenoiseEditMoveRight() { } /* Move cursor to the start of the line. */ -inline void linenoiseState::linenoiseEditMoveHome() { +inline void linenoiseState::EditMoveHome() { if (pos_ != 0) { pos_ = 0; RefreshLine(); @@ -2115,7 +2115,7 @@ inline void linenoiseState::linenoiseEditMoveHome() { } /* Move cursor to the end of the line. */ -inline void linenoiseState::linenoiseEditMoveEnd() { +inline void linenoiseState::EditMoveEnd() { if (pos_ != len_) { pos_ = len_; RefreshLine(); @@ -2126,7 +2126,7 @@ inline void linenoiseState::linenoiseEditMoveEnd() { * entry as specified by 'dir'. */ #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 -inline void linenoiseState::linenoiseEditHistoryNext(int dir) { +inline void linenoiseState::EditHistoryNext(int dir) { if (history_.size() > 1) { /* Update the current history entry before to * overwrite it with the next one. */ @@ -2149,7 +2149,7 @@ inline void linenoiseState::linenoiseEditHistoryNext(int dir) { /* Delete the character at the right of the cursor without altering the cursor * position. Basically this is what happens with the "Delete" keyboard key. */ -inline void linenoiseState::linenoiseEditDelete() { +inline void linenoiseState::EditDelete() { if (len_ > 0 && pos_ < len_) { int glen = unicodeGraphemeLen(buf_,len_,pos_); memmove(buf_+pos_,buf_+pos_+glen,len_-pos_-glen); @@ -2160,7 +2160,7 @@ inline void linenoiseState::linenoiseEditDelete() { } /* Backspace implementation. */ -inline void linenoiseState::linenoiseEditBackspace() { +inline void linenoiseState::EditBackspace() { if (pos_ > 0 && len_ > 0) { int glen = unicodePrevGraphemeLen(buf_,pos_); memmove(buf_+pos_-glen,buf_+pos_,len_-pos_); @@ -2173,7 +2173,7 @@ inline void linenoiseState::linenoiseEditBackspace() { /* Delete the previous word, maintaining the cursor at the start of the * current word. */ -inline void linenoiseState::linenoiseEditDeletePrevWord() { +inline void linenoiseState::EditDeletePrevWord() { int old_pos = pos_; int diff; @@ -2195,7 +2195,7 @@ inline void linenoiseState::linenoiseEditDeletePrevWord() { * when ctrl+d is typed. * * The function returns the length of the current buffer. */ -inline int linenoiseState::linenoiseEdit() +inline int linenoiseState::Edit() { /* The latest history entry is always our current buffer, that * initially is just an empty string. */ @@ -2232,19 +2232,19 @@ inline int linenoiseState::linenoiseEdit() switch(c) { case ENTER: /* enter */ if (!history_.empty()) history_.pop_back(); - if (mlmode_) linenoiseEditMoveEnd(); + if (mlmode_) EditMoveEnd(); return (int)len_; case CTRL_C: /* ctrl-c */ errno = EAGAIN; return -1; case BACKSPACE: /* backspace */ case 8: /* ctrl-h */ - linenoiseEditBackspace(); + EditBackspace(); break; case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the line is empty, act as end-of-file. */ if (len_ > 0) { - linenoiseEditDelete(); + EditDelete(); } else { history_.pop_back(); return -1; @@ -2260,16 +2260,16 @@ inline int linenoiseState::linenoiseEdit() } break; case CTRL_B: /* ctrl-b */ - linenoiseEditMoveLeft(); + EditMoveLeft(); break; case CTRL_F: /* ctrl-f */ - linenoiseEditMoveRight(); + EditMoveRight(); break; case CTRL_P: /* ctrl-p */ - linenoiseEditHistoryNext(LINENOISE_HISTORY_PREV); + EditHistoryNext(LINENOISE_HISTORY_PREV); break; case CTRL_N: /* ctrl-n */ - linenoiseEditHistoryNext(LINENOISE_HISTORY_NEXT); + EditHistoryNext(LINENOISE_HISTORY_NEXT); break; case ESC: /* escape sequence */ /* Read the next two bytes representing the escape sequence. @@ -2286,29 +2286,29 @@ inline int linenoiseState::linenoiseEdit() if (seq[2] == '~') { switch(seq[1]) { case '3': /* Delete key. */ - linenoiseEditDelete(); + EditDelete(); break; } } } else { switch(seq[1]) { case 'A': /* Up */ - linenoiseEditHistoryNext(LINENOISE_HISTORY_PREV); + EditHistoryNext(LINENOISE_HISTORY_PREV); break; case 'B': /* Down */ - linenoiseEditHistoryNext(LINENOISE_HISTORY_NEXT); + EditHistoryNext(LINENOISE_HISTORY_NEXT); break; case 'C': /* Right */ - linenoiseEditMoveRight(); + EditMoveRight(); break; case 'D': /* Left */ - linenoiseEditMoveLeft(); + EditMoveLeft(); break; case 'H': /* Home */ - linenoiseEditMoveHome(); + EditMoveHome(); break; case 'F': /* End*/ - linenoiseEditMoveEnd(); + EditMoveEnd(); break; } } @@ -2318,16 +2318,16 @@ inline int linenoiseState::linenoiseEdit() else if (seq[0] == 'O') { switch(seq[1]) { case 'H': /* Home */ - linenoiseEditMoveHome(); + EditMoveHome(); break; case 'F': /* End*/ - linenoiseEditMoveEnd(); + EditMoveEnd(); break; } } break; default: - if (linenoiseEditInsert(cbuf,nread)) return -1; + if (EditInsert(cbuf,nread)) return -1; break; case CTRL_U: /* Ctrl+u, delete the whole line. */ wbuf_[0] = '\0'; @@ -2340,26 +2340,26 @@ inline int linenoiseState::linenoiseEdit() RefreshLine(); break; case CTRL_A: /* Ctrl+a, go to the start of the line */ - linenoiseEditMoveHome(); + EditMoveHome(); break; case CTRL_E: /* ctrl+e, go to the end of the line */ - linenoiseEditMoveEnd(); + EditMoveEnd(); break; case CTRL_L: /* ctrl+l, clear screen */ linenoiseClearScreen(); RefreshLine(); break; case CTRL_W: /* ctrl+w, delete previous word */ - linenoiseEditDeletePrevWord(); + EditDeletePrevWord(); break; } } return len_; } -/* This function calls the line editing function linenoiseEdit() using +/* This function calls the line editing function Edit() using * the STDIN file descriptor set in raw mode. */ -inline bool linenoiseState::linenoiseRaw(std::string& line) { +inline bool linenoiseState::Raw(std::string& line) { bool quit = false; if (!isatty(STDIN_FILENO)) { @@ -2389,7 +2389,7 @@ inline bool linenoiseState::linenoiseRaw(std::string& line) { buf_[0] = '\0'; wbuf_[0] = '\0'; - auto count = linenoiseEdit(); + auto count = Edit(); if (count == -1) { quit = true; } else { @@ -2431,7 +2431,7 @@ inline bool linenoiseState::Readline(std::string& line) { std::getline(std::cin, line); return false; } else { - return linenoiseRaw(line); + return Raw(line); } return false; From 58ae27f7a72a10df3edb13b1426d112268b63b2c Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 12:20:31 -0400 Subject: [PATCH 20/22] Clean up class API slightly. --- linenoise.hpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/linenoise.hpp b/linenoise.hpp index f87654b..555fd6f 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -196,9 +196,6 @@ class linenoiseState { completionCallback = fn; }; - // For functional interface - void SetMultiLine(bool ml); - private: std::string Readline(bool &quit); std::string Readline(); @@ -1639,11 +1636,6 @@ inline int unicodeReadUTF8Char(int fd, char* buf, int* cp) /* ======================= Low level terminal handling ====================== */ -/* Set if to use or not the multi line mode. */ -inline void linenoiseState::SetMultiLine(bool ml) { - mlmode_ = ml; -} - /* Return true if the terminal name is in the list of terminals we know are * not able to understand basic escape sequences. */ inline bool isUnsupportedTerm(void) { @@ -2416,8 +2408,8 @@ inline linenoiseState::linenoiseState(const char *prompt_str, int stdin_fd, int buf_len_--; /* Make sure there is always space for the nulterm */ } -inline void linenoiseState::EnableMultiLine() { SetMultiLine(true); } -inline void linenoiseState::DisableMultiLine() { SetMultiLine(false); } +inline void linenoiseState::EnableMultiLine() { mlmode_ = true; } +inline void linenoiseState::DisableMultiLine() { mlmode_ = false; } /* The high level function that is the main API of the linenoise library. * This function checks if the terminal has basic capabilities, just checking @@ -2532,7 +2524,11 @@ inline void SetCompletionCallback(CompletionCallback fn){ inline void SetMultiLine(bool ml){ if (!lglobal) lglobal = new linenoiseState(); - lglobal->SetMultiLine(ml); + if (ml) { + lglobal->EnableMultiLine(); + } else { + lglobal->DisableMultiLine(); + } }; inline bool AddHistory(const char* line){ if (!lglobal) From 060e10ce34a5941678b4fa5ac87a12e8e5b8d72a Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 12:21:51 -0400 Subject: [PATCH 21/22] Unused define --- linenoise.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/linenoise.hpp b/linenoise.hpp index 555fd6f..dbf3365 100644 --- a/linenoise.hpp +++ b/linenoise.hpp @@ -163,7 +163,6 @@ namespace linenoise { #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 -#define LINENOISE_HISTORY_DO_SETUP -INT_MAX typedef std::function&)> CompletionCallback; From 421ef3e3f706b0f18bf90d3427204ca56bf1586b Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Tue, 11 Mar 2025 14:00:33 -0400 Subject: [PATCH 22/22] Move multiinclude test files to subdir --- example/CMakeLists.txt | 6 +++++- example/{ => multiinclude}/f1.cpp | 0 example/{ => multiinclude}/f2.cpp | 0 example/{ => multiinclude}/main.cpp | 0 4 files changed, 5 insertions(+), 1 deletion(-) rename example/{ => multiinclude}/f1.cpp (100%) rename example/{ => multiinclude}/f2.cpp (100%) rename example/{ => multiinclude}/main.cpp (100%) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 6e20a08..9b8a54b 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -5,5 +5,9 @@ add_definitions("-std=c++1y") add_executable(example example.cpp) add_executable(example_old example_old.cpp) -add_executable(multiinclude main.cpp f1.cpp f2.cpp) +add_executable(multiinclude + multiinclude/main.cpp + multiinclude/f1.cpp + multiinclude/f2.cpp +) diff --git a/example/f1.cpp b/example/multiinclude/f1.cpp similarity index 100% rename from example/f1.cpp rename to example/multiinclude/f1.cpp diff --git a/example/f2.cpp b/example/multiinclude/f2.cpp similarity index 100% rename from example/f2.cpp rename to example/multiinclude/f2.cpp diff --git a/example/main.cpp b/example/multiinclude/main.cpp similarity index 100% rename from example/main.cpp rename to example/multiinclude/main.cpp