Skip to content

Commit

Permalink
Added Ctrl-u to readline
Browse files Browse the repository at this point in the history
Based on suggestion from Sebastian, see issue PromyLOPh#416. Fixed several issues
(multibyte, \0-termination) and refactored readline code while I’m at
it.
  • Loading branch information
PromyLOPh committed Jan 2, 2014
1 parent 0ef5d90 commit 9d5682f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion COPYING
Original file line number Diff line number Diff line change
@@ -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
Expand Down
47 changes: 27 additions & 20 deletions src/ui_readline.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}

Expand Down

0 comments on commit 9d5682f

Please sign in to comment.