geadline: move cursor

This commit is contained in:
kdx 2023-07-25 05:10:15 +02:00
parent ee80b05d10
commit 46d7e9c85c

View File

@ -3,11 +3,13 @@ geadline(prompt) {
size = 0, size = 0,
i = 0, i = 0,
c, c,
a,
buf = galloc(capacity); buf = galloc(capacity);
if (prompt) if (prompt)
putstr(prompt); putstr(prompt);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
[buf] = 0;
loop { loop {
red &c; red &c;
@ -16,34 +18,94 @@ geadline(prompt) {
free(buf); free(buf);
return 0; return 0;
} }
} else if (c == 0x007f) { } else if (c == 0x1b) {
if (size) { // ESC code
red &c; // skip [
red &c; // value
if ((c == 'A') & (size > 0)) {
loop {
if (i == 0)
break;
i = i - 1;
esccode('D');
}
} else if ((c == 'B') & (i < size)) {
loop {
if (i >= size)
break;
i = i + 1;
esccode('C');
}
} else if ((c == 'C') & (i < size)) {
i = i + 1;
esccode('C');
} else if ((c == 'D') & (i > 0)) {
i = i - 1;
esccode('D');
}
wrt '\a';
} else if (c == 0x7f) {
if (i) {
a = i - 1;
loop {
if (a >= size)
break;
[buf + a] = [buf + a + 1];
a = a + 1;
}
size = size - 1; size = size - 1;
i = i - 1; i = i - 1;
[buf + size] = 0; [buf + size] = 0;
wrt 0x1b; wrt '\r';
wrt 0x5b; if (prompt)
wrt 0x44; putstr(prompt);
putstr(buf);
wrt ' '; wrt ' ';
wrt 0x1b; esccode('D');
wrt 0x5b; a = size - i;
wrt 0x44; loop {
if (a == 0)
break;
a = a - 1;
esccode('D');
}
} }
} else { } else {
size = size + 1; size = size + 1;
if (size > capacity) { if (size >= capacity) {
buf = realloc(buf, capacity * 2); buf = realloc(buf, capacity * 2);
if (buf == NULL) if (buf == NULL)
return NULL; return NULL;
capacity = capacity * 2; capacity = capacity * 2;
} }
a = size - i - 1;
loop {
if ((a == 0xffff) | (a == 0))
break;
[buf + i + a] = [buf + i + a - 1];
a = a - 1;
}
[buf + i] = c; [buf + i] = c;
[buf + size] = 0;
putstr(buf + i);
a = strlen(buf + i) - 1;
loop {
if ((a == 0xffff) | (a == 0))
break;
a = a - 1;
esccode('D');
}
i = i + 1; i = i + 1;
[buf + i] = 0;
wrt c;
if (c == '\n') if (c == '\n')
return buf; return buf;
} }
} }
} }
esccode(c) {
wrt 0x1b;
wrt '[';
wrt c;
}