aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/rc/input.c')
-rw-r--r--sys/cmd/rc/input.c68
1 files changed, 47 insertions, 21 deletions
diff --git a/sys/cmd/rc/input.c b/sys/cmd/rc/input.c
index 8ca3145..08363f0 100644
--- a/sys/cmd/rc/input.c
+++ b/sys/cmd/rc/input.c
@@ -133,7 +133,7 @@ insertcursor(int fd)
write(fd,"\e[6 q",5);
}
-/* Raw mode: 1960 magic shit. */
+/* raw mode: 1960 magic shit. */
static
int
enterraw(int fd)
@@ -143,7 +143,7 @@ enterraw(int fd)
if(!shell.interactive)
goto fatal;
- if(!mode.defer) {
+ if(!mode.defer){
atexit(doatexit);
mode.defer = 1;
}
@@ -167,7 +167,9 @@ enterraw(int fd)
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
/* put terminal in raw mode after flushing */
- if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
+ if(tcsetattr(fd,TCSAFLUSH,&raw) < 0)
+ goto fatal;
+
mode.raw = 1;
return 1;
@@ -180,13 +182,13 @@ static
void
exitraw(int fd)
{
- /* Don't even check the return value as it's too late. */
+ /* don't even check the return value as it's too late. */
if(mode.raw && tcsetattr(fd,TCSAFLUSH,&originalterm) != -1)
mode.raw = 0;
}
-/* Use the ESC [6n escape sequence to query the horizontal cursor position
- * and return it. On error -1 is returned, on success the position of the
+/* use the esc [6n escape sequence to query the horizontal cursor position
+ * and return it. on error -1 is returned, on success the position of the
* cursor. */
static
int
@@ -325,7 +327,7 @@ refreshsingleline(struct TerminalState *term)
char esc[64];
struct Buffer ab;
- int n;
+ int n, w;
rune r;
int fd = term->ofd;
intptr off = term->prompt.size;
@@ -334,13 +336,12 @@ refreshsingleline(struct TerminalState *term)
intptr pos = term->cursor.pos;
intptr col = term->cursor.len;
- assert(col);
- assert(off);
-
while((off+pos) >= term->cursor.cap){
n = utf8·decode(buf, &r);
+ w = utf8·runewidth(r);
+
buf+=n, len-=n;
- pos--, col--;
+ pos-=w, col-=w;
}
assert(buf <= term->edit.buf + len);
@@ -481,19 +482,18 @@ refreshline(struct TerminalState *term)
int
insertrune(struct TerminalState *term, int n, char *c)
{
+ int w;
rune r;
+ utf8·decode(c, &r);
+ w = utf8·runewidth(r);
+
if(term->edit.len + n <= term->edit.cap){
if(term->edit.pos == term->edit.len){
memcpy(term->edit.buf+term->edit.pos, c, n);
- term->edit.pos+=n, term->edit.len+=n;
- term->cursor.pos++, term->cursor.len++;
-
- utf8·decode(c, &r);
- if(wcwidth(r) < 1){
- exit(1);
- }
+ term->edit.pos += n, term->edit.len += n;
+ term->cursor.pos += w, term->cursor.len += w;
term->edit.buf[term->edit.len] = '\0';
@@ -968,6 +968,33 @@ action:
#define END(term) (Position){(term).edit.len, (term).cursor.len}
+static
+int
+size(char *s)
+{
+ rune c;
+ int n, len = 0;;
+ while((c=*s)){
+ if(c == '\033'){
+ n = 1;
+ esccode:
+ c = s[n];
+ if(!c) // we hit end of string in the middle of parsing an escape code!
+ return len;
+ if(c == 'm'){
+ s += n + 1;
+ continue; // outer loop
+ }
+ n++;
+ goto esccode;
+ }
+ n = utf8·decode(s, &c);
+ s += n;
+ len += utf8·runewidth(c);
+ }
+ return len;
+}
+
/* this function is the core of the line editing capability of linenoise.
* it expects 'fd' to be already in "raw mode" so that every key pressed
* will be returned asap to read().
@@ -1000,7 +1027,7 @@ interact(int ifd, int ofd, char *buf, intptr len, char *prompt)
term.prompt.s = prompt;
term.prompt.len = strlen(prompt);
- term.prompt.size = utf8·len(prompt);
+ term.prompt.size = size(prompt);
term.cursor.pos = 0;
term.cursor.len = 0;
@@ -1271,8 +1298,7 @@ printkeycode(void)
}
/*
- * This function calls the line editing function edit() using
- * the stdin set in raw mode
+ * this function calls the line editing function edit() using the stdin set in raw mode
*/
static
int