minor changes

This commit is contained in:
Chu'vok
2024-09-17 10:35:24 +02:00
parent 313dc26fcc
commit 8674ca2790
2 changed files with 7 additions and 16 deletions

View File

@@ -38,6 +38,7 @@ int main(void) {
TEST(utflen("$€𐍈") == 3); TEST(utflen("$€𐍈") == 3);
TEST(utfpos(NULL, 0) == 0); TEST(utfpos(NULL, 0) == 0);
TEST(utfpos("ab", -1) == 0);
TEST(utfpos("äbc", 0) == 0); TEST(utfpos("äbc", 0) == 0);
TEST(utfpos("äbc", 1) == 2); TEST(utfpos("äbc", 1) == 2);
TEST(utfpos("äbc", 2) == 3); TEST(utfpos("äbc", 2) == 3);

22
tim.h
View File

@@ -455,11 +455,8 @@ static int utflen(const char* s) {
// index of utf8 code point at pos // index of utf8 code point at pos
static int utfpos(const char* s, int pos) { static int utfpos(const char* s, int pos) {
if (pos < 0) {
return 0;
}
int i = 0; int i = 0;
for (int n = 0; s && s[i]; i++) { for (int n = 0; pos >= 0 && s && s[i]; i++) {
n += (s[i] & 192) != 128; n += (s[i] & 192) != 128;
if (n == pos + 1) { if (n == pos + 1) {
return i; return i;
@@ -580,17 +577,10 @@ static void reset_terminal(void) {
static bool parse_input(struct event* restrict e, int n) { static bool parse_input(struct event* restrict e, int n) {
char* s = e->str; char* s = e->str;
if (s[0] == 127 && n == 1) { if (n == 1 || s[0] != 27) {
// xterm backspace
e->type = KEY_EVENT;
e->key = BACKSPACE_KEY;
return true;
}
if (s[0] != 27 || n == 1) {
// regular key press // regular key press
e->type = KEY_EVENT; e->type = KEY_EVENT;
e->key = utfchr(s); e->key = s[0] == 127 ? BACKSPACE_KEY : utfchr(s);
return true; return true;
} }
@@ -624,7 +614,7 @@ static bool parse_input(struct event* restrict e, int n) {
{"[6~", PAGEDOWN_KEY}, {"[6~", PAGEDOWN_KEY},
}; };
if (s[0] == 27 && (n == 3 || n == 4)) { if ((n == 3 || n == 4) && s[0] == 27) {
// key sequence // key sequence
for (int i = 0; i < ARRAY_SIZE(key_table); i++) { for (int i = 0; i < ARRAY_SIZE(key_table); i++) {
if (!memcmp(s + 1, key_table[i].s, n - 1)) { if (!memcmp(s + 1, key_table[i].s, n - 1)) {
@@ -1314,9 +1304,9 @@ static void render(void) {
if (new_line || wide_flank || skip) { if (new_line || wide_flank || skip) {
put_str(S("\33[")); put_str(S("\33["));
put_int((i / tim.w) + 1); put_int((i / tim.w) + 1);
put_str(S(";")); put_chr(';');
put_int((i % tim.w) + 1); put_int((i % tim.w) + 1);
put_str(S("H")); put_chr('H');
} }
wide = c.wide || wide_spill; wide = c.wide || wide_spill;
skip = false; skip = false;