summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-11-01 06:26:40 -0700
committerKaz Kylheku <kaz@kylheku.com>2018-11-01 06:26:40 -0700
commit933f0e82158554e811f9a2f72e7fb1e0a8359763 (patch)
treea7bb5036c2ccac70e96a038a880741db5f46925c
parent1dd511525f1f43ac44dc5b21e02fd7eed000031e (diff)
downloadtxr-933f0e82158554e811f9a2f72e7fb1e0a8359763.tar.gz
txr-933f0e82158554e811f9a2f72e7fb1e0a8359763.tar.bz2
txr-933f0e82158554e811f9a2f72e7fb1e0a8359763.zip
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.
-rw-r--r--linenoise/linenoise.c32
1 files 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)