From 933f0e82158554e811f9a2f72e7fb1e0a8359763 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 1 Nov 2018 06:26:40 -0700 Subject: linenoise: improve efficiency of warning flash. * linenoise/linenoise.c (usec_delay): return a Boolean indicator whether the delay was prematurely canceled by input. (flash): Use the return value of usec_delay to bail out of the loop early, canceling the flashing. Instead of doing full line refreshes to display and hide the exclamation mark, perform the flash by simply printing the exclamation mark at the current location and then erasing it with backspace-space-backspace. --- linenoise/linenoise.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index 9bec8212..3aaf7b80 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -1366,24 +1366,28 @@ static int find_nearest_paren(const wchar_t *s, int i) return nxt; } -static void usec_delay(lino_t *l, long usec) +static int usec_delay(lino_t *l, long usec) { #if HAVE_POLL struct pollfd pfd; + pfd.fd = lino_os.fileno_fn(l->tty_ifs); pfd.events = POLLIN; - poll(&pfd, 1, usec/1000); + pfd.revents = 0; + return poll(&pfd, 1, usec/1000) == 1; #elif HAVE_POSIX_NANOSLEEP struct timespec ts; (void) l; ts.tv_sec = usec / 1000000; ts.tv_nsec = (usec % 1000000) * 1000; nanosleep(&ts, 0); + return 0; #elif HAVE_POSIX_USLEEP (void) l; if (u >= 1000000) sleep(u / 1000000); usleep(u % 1000000); + return 0; #else #error portme #endif @@ -1408,20 +1412,20 @@ static void paren_jump(lino_t *l) static void flash(lino_t *l, int ch) { - int i; + int i, cancel = 0; + wchar_t on[2] = { ch }; + const wchar_t *off = L"\b \b"; if (l->dlen >= (int) nelem (l->data) - 1) return; - for (i = 0; i < 2; i++) { + for (i = 0; i < 2 && !cancel; i++) { if (i > 0) + cancel = usec_delay(l, LINENOISE_FLASH_DELAY); + lino_os.puts_fn(l->tty_ofs, on); + if (!cancel) usec_delay(l, LINENOISE_FLASH_DELAY); - l->data[l->dlen++] = ch; - l->data[l->dlen] = 0; - refresh_line(l); - usec_delay(l, LINENOISE_FLASH_DELAY); - l->data[--l->dlen] = 0; - refresh_line(l); + lino_os.puts_fn(l->tty_ofs, off); } } @@ -2090,8 +2094,10 @@ static int edit(lino_t *l, const wchar_t *prompt) l->error = lino_ioerr; goto out; } - if (l->dpos == l->dlen) + if (l->dpos == l->dlen) { + refresh_line(l); flash(l, '!'); + } break; } if (l->mlmode) @@ -2155,8 +2161,10 @@ static int edit(lino_t *l, const wchar_t *prompt) l->error = lino_ioerr; goto out; } - if (!paste && l->dpos == l->dlen) + if (!paste && l->dpos == l->dlen) { + refresh_line(l); flash(l, '!'); + } break; } if (l->mlmode) -- cgit v1.2.3