Multiplexing: hide/show current line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
diff --git a/example.c b/example.c
index 74358c3..8959540 100644
--- a/example.c
+++ b/example.c
@@ -3,7 +3,6 @@
#include <string.h>
#include "linenoise.h"
-
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
diff --git a/linenoise.c b/linenoise.c
index 74182c6..fa998a0 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -10,7 +10,7 @@
*
* ------------------------------------------------------------------------
*
- * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
+ * Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
@@ -133,24 +133,6 @@ static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
static int history_len = 0;
static char **history = NULL;
-/* 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. */
- size_t buflen; /* Edited line buffer size. */
- const char *prompt; /* Prompt to display. */
- size_t plen; /* Prompt length. */
- size_t pos; /* Current cursor position. */
- size_t oldpos; /* Previous refresh cursor position. */
- size_t len; /* Current edited line length. */
- size_t cols; /* Number of columns in terminal. */
- size_t 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 */
@@ -175,6 +157,9 @@ enum KEY_ACTION{
static void linenoiseAtExit(void);
int linenoiseHistoryAdd(const char *line);
+#define REFRESH_CLEAN (1<<0) // Clean the old prompt from the screen
+#define REFRESH_WRITE (1<<1) // Rewrite the prompt on the screen.
+#define REFRESH_ALL (REFRESH_CLEAN|REFRESH_WRITE) // Do both.
static void refreshLine(struct linenoiseState *l);
/* Debugging macro. */
@@ -514,8 +499,11 @@ void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) {
/* Single line low level line refresh.
*
* Rewrite the currently edited line accordingly to the buffer content,
- * cursor position, and number of columns of the terminal. */
-static void refreshSingleLine(struct linenoiseState *l) {
+ * cursor position, and number of columns of the terminal.
+ *
+ * Flags is REFRESH_* macros. The function can just remove the old
+ * prompt, just write it, or both. */
+static void refreshSingleLine(struct linenoiseState *l, int flags) {
char seq[64];
size_t plen = strlen(l->prompt);
int fd = l->ofd;
@@ -535,23 +523,31 @@ static void refreshSingleLine(struct linenoiseState *l) {
abInit(&ab);
/* Cursor to left edge */
- snprintf(seq,64,"\r");
+ snprintf(seq,sizeof(seq),"\r");
abAppend(&ab,seq,strlen(seq));
- /* Write the prompt and the current buffer content */
- abAppend(&ab,l->prompt,strlen(l->prompt));
- if (maskmode == 1) {
- while (len--) abAppend(&ab,"*",1);
- } else {
- abAppend(&ab,buf,len);
+
+ if (flags & REFRESH_WRITE) {
+ /* Write the prompt and the current buffer content */
+ abAppend(&ab,l->prompt,strlen(l->prompt));
+ if (maskmode == 1) {
+ while (len--) abAppend(&ab,"*",1);
+ } else {
+ abAppend(&ab,buf,len);
+ }
+ /* Show hits if any. */
+ refreshShowHints(&ab,l,plen);
}
- /* Show hits if any. */
- refreshShowHints(&ab,l,plen);
+
/* Erase to right */
- snprintf(seq,64,"\x1b[0K");
- abAppend(&ab,seq,strlen(seq));
- /* Move cursor to original position. */
- snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen));
+ snprintf(seq,sizeof(seq),"\x1b[0K");
abAppend(&ab,seq,strlen(seq));
+
+ if (flags & REFRESH_WRITE) {
+ /* Move cursor to original position. */
+ snprintf(seq,sizeof(seq),"\r\x1b[%dC", (int)(pos+plen));
+ abAppend(&ab,seq,strlen(seq));
+ }
+
if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
abFree(&ab);
}
@@ -559,8 +555,11 @@ static void refreshSingleLine(struct linenoiseState *l) {
/* Multi line low level line refresh.
*
* Rewrite the currently edited line accordingly to the buffer content,
- * cursor position, and number of columns of the terminal. */
-static void refreshMultiLine(struct linenoiseState *l) {
+ * cursor position, and number of columns of the terminal.
+ *
+ * Flags is REFRESH_* macros. The function can just remove the old
+ * prompt, just write it, or both. */
+static void refreshMultiLine(struct linenoiseState *l, int flags) {
char seq[64];
int plen = strlen(l->prompt);
int rows = (plen+l->len+l->cols-1)/l->cols; /* rows used by current buf. */
@@ -577,70 +576,75 @@ static void refreshMultiLine(struct linenoiseState *l) {
/* First step: clear all the lines used before. To do so start by
* going to the last row. */
abInit(&ab);
- if (old_rows-rpos > 0) {
- lndebug("go down %d", old_rows-rpos);
- snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
- abAppend(&ab,seq,strlen(seq));
- }
- /* Now for every row clear it, go up. */
- for (j = 0; j < old_rows-1; j++) {
- lndebug("clear+up");
- snprintf(seq,64,"\r\x1b[0K\x1b[1A");
+ if (flags & REFRESH_CLEAN) {
+ if (old_rows-rpos > 0) {
+ lndebug("go down %d", old_rows-rpos);
+ snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
+ abAppend(&ab,seq,strlen(seq));
+ }
+
+ /* Now for every row clear it, go up. */
+ for (j = 0; j < old_rows-1; j++) {
+ lndebug("clear+up");
+ snprintf(seq,64,"\r\x1b[0K\x1b[1A");
+ abAppend(&ab,seq,strlen(seq));
+ }
+
+ /* Clean the top line. */
+ lndebug("clear");
+ snprintf(seq,64,"\r\x1b[0K");
abAppend(&ab,seq,strlen(seq));
}
- /* Clean the top line. */
- lndebug("clear");
- snprintf(seq,64,"\r\x1b[0K");
- abAppend(&ab,seq,strlen(seq));
+ if (flags & REFRESH_WRITE) {
+ /* Write the prompt and the current buffer content */
+ abAppend(&ab,l->prompt,strlen(l->prompt));
+ if (maskmode == 1) {
+ unsigned int i;
+ for (i = 0; i < l->len; i++) abAppend(&ab,"*",1);
+ } else {
+ abAppend(&ab,l->buf,l->len);
+ }
- /* Write the prompt and the current buffer content */
- abAppend(&ab,l->prompt,strlen(l->prompt));
- if (maskmode == 1) {
- unsigned int i;
- for (i = 0; i < l->len; i++) abAppend(&ab,"*",1);
- } else {
- abAppend(&ab,l->buf,l->len);
- }
+ /* Show hits if any. */
+ refreshShowHints(&ab,l,plen);
+
+ /* 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 &&
+ (l->pos+plen) % l->cols == 0)
+ {
+ lndebug("<newline>");
+ abAppend(&ab,"\n",1);
+ snprintf(seq,64,"\r");
+ abAppend(&ab,seq,strlen(seq));
+ rows++;
+ if (rows > (int)l->maxrows) l->maxrows = rows;
+ }
- /* Show hits if any. */
- refreshShowHints(&ab,l,plen);
-
- /* 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 &&
- (l->pos+plen) % l->cols == 0)
- {
- lndebug("<newline>");
- abAppend(&ab,"\n",1);
- snprintf(seq,64,"\r");
- abAppend(&ab,seq,strlen(seq));
- rows++;
- if (rows > (int)l->maxrows) l->maxrows = rows;
- }
+ /* Move cursor to right position. */
+ rpos2 = (plen+l->pos+l->cols)/l->cols; /* Current cursor relative row */
+ lndebug("rpos2 %d", rpos2);
- /* Move cursor to right position. */
- rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
- lndebug("rpos2 %d", rpos2);
+ /* Go up till we reach the expected positon. */
+ if (rows-rpos2 > 0) {
+ lndebug("go-up %d", rows-rpos2);
+ snprintf(seq,64,"\x1b[%dA", rows-rpos2);
+ abAppend(&ab,seq,strlen(seq));
+ }
- /* Go up till we reach the expected positon. */
- if (rows-rpos2 > 0) {
- lndebug("go-up %d", rows-rpos2);
- snprintf(seq,64,"\x1b[%dA", rows-rpos2);
+ /* Set column. */
+ col = (plen+(int)l->pos) % (int)l->cols;
+ lndebug("set col %d", 1+col);
+ if (col)
+ snprintf(seq,64,"\r\x1b[%dC", col);
+ else
+ snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq));
}
- /* Set column. */
- col = (plen+(int)l->pos) % (int)l->cols;
- lndebug("set col %d", 1+col);
- if (col)
- snprintf(seq,64,"\r\x1b[%dC", col);
- else
- snprintf(seq,64,"\r");
- abAppend(&ab,seq,strlen(seq));
-
lndebug("\n");
l->oldpos = l->pos;
@@ -652,9 +656,25 @@ static void refreshMultiLine(struct linenoiseState *l) {
* refreshMultiLine() according to the selected mode. */
static void refreshLine(struct linenoiseState *l) {
if (mlmode)
- refreshMultiLine(l);
+ refreshMultiLine(l,REFRESH_ALL);
+ else
+ refreshSingleLine(l,REFRESH_ALL);
+}
+
+/* Hide the current line, when using the multiplexing API. */
+void linenoiseHide(struct linenoiseState *l) {
+ if (mlmode)
+ refreshMultiLine(l,REFRESH_CLEAN);
+ else
+ refreshSingleLine(l,REFRESH_CLEAN);
+}
+
+/* Show the current line, when using the multiplexing API. */
+void linenoiseShow(struct linenoiseState *l) {
+ if (mlmode)
+ refreshMultiLine(l,REFRESH_WRITE);
else
- refreshSingleLine(l);
+ refreshSingleLine(l,REFRESH_WRITE);
}
/* Insert the character 'c' at cursor current position.
diff --git a/linenoise.h b/linenoise.h
index 6dfee73..cc9ec2d 100644
--- a/linenoise.h
+++ b/linenoise.h
@@ -7,7 +7,7 @@
*
* ------------------------------------------------------------------------
*
- * Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
+ * Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
@@ -43,11 +43,43 @@
extern "C" {
#endif
+#include <stddef.h> /* For size_t. */
+
+/* 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. */
+ size_t buflen; /* Edited line buffer size. */
+ const char *prompt; /* Prompt to display. */
+ size_t plen; /* Prompt length. */
+ size_t pos; /* Current cursor position. */
+ size_t oldpos; /* Previous refresh cursor position. */
+ size_t len; /* Current edited line length. */
+ size_t cols; /* Number of columns in terminal. */
+ size_t maxrows; /* Maximum num of rows used so far (multiline mode) */
+ int history_index; /* The history index we are currently editing. */
+};
+
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
+/* Non blocking API. */
+int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);
+char *linenoiseEditFeed(struct linenoiseState *l, int *len);
+void linenoiseEditStop(struct linenoiseState *l);
+void linenoiseHide(struct linenoiseState *l);
+void linenoiseShow(struct linenoiseState *l);
+
+/* Blocking API. */
+char *linenoise(const char *prompt);
+void linenoiseFree(void *ptr);
+
+/* Completion API. */
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
typedef void(linenoiseFreeHintsCallback)(void *);
@@ -56,12 +88,13 @@ void linenoiseSetHintsCallback(linenoiseHintsCallback *);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
-char *linenoise(const char *prompt);
-void linenoiseFree(void *ptr);
+/* History API. */
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
+
+/* Other utilities. */
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);