Skip to content

Commit 9d5682f

Browse files
committed
Added Ctrl-u to readline
Based on suggestion from Sebastian, see issue PromyLOPh#416. Fixed several issues (multibyte, \0-termination) and refactored readline code while I’m at it.
1 parent 0ef5d90 commit 9d5682f

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

COPYING

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2008-2011
1+
Copyright (c) 2008-2014
22
Lars-Dominik Braun <[email protected]>
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy

src/ui_readline.c

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2008-2011
2+
Copyright (c) 2008-2014
33
Lars-Dominik Braun <[email protected]>
44
55
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -53,7 +53,6 @@ static size_t BarReadlinePrevUtf8 (char *ptr) {
5353
*/
5454
size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
5555
BarReadlineFds_t *input, const BarReadlineFlags_t flags, int timeout) {
56-
size_t bufPos = 0;
5756
size_t bufLen = 0;
5857
unsigned char escapeState = 0;
5958
fd_set set;
@@ -107,9 +106,26 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
107106
if (echo) {
108107
fputs ("\n", stdout);
109108
}
109+
buf[bufLen] = '\0';
110110
return bufLen;
111111
break;
112112

113+
/* clear line */
114+
case 21:
115+
if (echo) {
116+
while (bufLen > 0) {
117+
const size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]);
118+
assert (bufLen >= moveSize);
119+
120+
/* move caret and delete character */
121+
fputs ("\033[D\033[K", stdout);
122+
bufLen -= moveSize;
123+
}
124+
fflush (stdout);
125+
}
126+
bufLen = 0;
127+
break;
128+
113129
/* escape */
114130
case 27:
115131
escapeState = 1;
@@ -122,28 +138,18 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
122138
/* backspace */
123139
case 8: /* ASCII BS */
124140
case 127: /* ASCII DEL */
125-
if (bufPos > 0) {
126-
size_t moveSize = BarReadlinePrevUtf8 (&buf[bufPos]);
127-
assert ((signed int) bufPos - (signed int) moveSize >= 0);
128-
memmove (&buf[bufPos-moveSize], &buf[bufPos], moveSize);
141+
if (bufLen > 0) {
142+
size_t moveSize = BarReadlinePrevUtf8 (&buf[bufLen]);
143+
assert (bufLen >= moveSize);
144+
memmove (&buf[bufLen-moveSize], &buf[bufLen], moveSize);
129145

130-
bufPos -= moveSize;
131146
bufLen -= moveSize;
132147

133-
buf[bufLen] = '\0';
134-
135148
/* move caret back and delete last character */
136149
if (echo) {
137150
fputs ("\033[D\033[K", stdout);
138151
fflush (stdout);
139152
}
140-
} else if (bufPos == 0 && buf[bufPos] != '\0') {
141-
/* delete char at position 0 but don't move cursor any further */
142-
buf[bufPos] = '\0';
143-
if (echo) {
144-
fputs ("\033[K", stdout);
145-
fflush (stdout);
146-
}
147153
}
148154
break;
149155

@@ -165,25 +171,26 @@ size_t BarReadline (char *buf, const size_t bufSize, const char *mask,
165171
break;
166172
}
167173
/* don't write beyond buffer's limits */
168-
if (bufPos < bufSize-1) {
169-
buf[bufPos] = chr;
170-
++bufPos;
174+
if (bufLen < bufSize-1) {
175+
buf[bufLen] = chr;
171176
++bufLen;
172177
if (echo) {
173178
putchar (chr);
174179
fflush (stdout);
175180
}
176181
/* buffer full => return if requested */
177-
if (bufPos >= bufSize-1 && (flags & BAR_RL_FULLRETURN)) {
182+
if (bufLen >= bufSize-1 && (flags & BAR_RL_FULLRETURN)) {
178183
if (echo) {
179184
fputs ("\n", stdout);
180185
}
186+
buf[bufLen] = '\0';
181187
return bufLen;
182188
}
183189
}
184190
break;
185191
} /* end switch */
186192
} /* end while */
193+
buf[0] = '\0';
187194
return 0;
188195
}
189196

0 commit comments

Comments
 (0)