diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2014-01-02 14:57:47 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2014-01-02 14:57:47 +0100 |
commit | 9d5682f8acf991b060119a39bc8aa447175cd294 (patch) | |
tree | bc1457447bc7e8c6e120828b177b2c5c5ffdce22 | |
parent | 0ef5d90cdfa80746cf36e387f16808e15d4e6259 (diff) | |
download | pianobar-9d5682f8acf991b060119a39bc8aa447175cd294.tar.gz pianobar-9d5682f8acf991b060119a39bc8aa447175cd294.tar.bz2 pianobar-9d5682f8acf991b060119a39bc8aa447175cd294.zip |
Added Ctrl-u to readline
Based on suggestion from Sebastian, see issue #416. Fixed several issues
(multibyte, \0-termination) and refactored readline code while I’m at
it.
-rw-r--r-- | COPYING | 2 | ||||
-rw-r--r-- | src/ui_readline.c | 47 |
2 files changed, 28 insertions, 21 deletions
@@ -1,4 +1,4 @@ -Copyright (c) 2008-2011 +Copyright (c) 2008-2014 Lars-Dominik Braun <lars@6xq.net> Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/src/ui_readline.c b/src/ui_readline.c index 6509f4e..eeb5c12 100644 --- a/src/ui_readline.c +++ b/src/ui_readline.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2011 +Copyright (c) 2008-2014 Lars-Dominik Braun <lars@6xq.net> Permission is hereby granted, free of charge, to any person obtaining a copy @@ -53,7 +53,6 @@ static size_t BarReadlinePrevUtf8 (char *ptr) { */ size_t BarReadline (char *buf, const size_t bufSize, const char *mask, BarReadlineFds_t *input, const BarReadlineFlags_t flags, int timeout) { - size_t bufPos = 0; size_t bufLen = 0; unsigned char escapeState = 0; fd_set set; @@ -107,9 +106,26 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask, if (echo) { fputs ("\n", stdout); } + buf[bufLen] = '\0'; return bufLen; break; + /* clear line */ + case 21: + if (echo) { + while (bufLen > 0) { + const size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]); + assert (bufLen >= moveSize); + + /* move caret and delete character */ + fputs ("\033[D\033[K", stdout); + bufLen -= moveSize; + } + fflush (stdout); + } + bufLen = 0; + break; + /* escape */ case 27: escapeState = 1; @@ -122,28 +138,18 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask, /* backspace */ case 8: /* ASCII BS */ case 127: /* ASCII DEL */ - if (bufPos > 0) { - size_t moveSize = BarReadlinePrevUtf8 (&buf[bufPos]); - assert ((signed int) bufPos - (signed int) moveSize >= 0); - memmove (&buf[bufPos-moveSize], &buf[bufPos], moveSize); + if (bufLen > 0) { + size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]); + assert (bufLen >= moveSize); + memmove (&buf[bufLen-moveSize], &buf[bufLen], moveSize); - bufPos -= moveSize; bufLen -= moveSize; - buf[bufLen] = '\0'; - /* move caret back and delete last character */ if (echo) { fputs ("\033[D\033[K", stdout); fflush (stdout); } - } else if (bufPos == 0 && buf[bufPos] != '\0') { - /* delete char at position 0 but don't move cursor any further */ - buf[bufPos] = '\0'; - if (echo) { - fputs ("\033[K", stdout); - fflush (stdout); - } } break; @@ -165,25 +171,26 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask, break; } /* don't write beyond buffer's limits */ - if (bufPos < bufSize-1) { - buf[bufPos] = chr; - ++bufPos; + if (bufLen < bufSize-1) { + buf[bufLen] = chr; ++bufLen; if (echo) { putchar (chr); fflush (stdout); } /* buffer full => return if requested */ - if (bufPos >= bufSize-1 && (flags & BAR_RL_FULLRETURN)) { + if (bufLen >= bufSize-1 && (flags & BAR_RL_FULLRETURN)) { if (echo) { fputs ("\n", stdout); } + buf[bufLen] = '\0'; return bufLen; } } break; } /* end switch */ } /* end while */ + buf[0] = '\0'; return 0; } |