CE Improve line editing logic, add KB_DELETE
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m18s

This commit is contained in:
2026-03-04 02:50:13 +01:00
parent 156ac10dcd
commit 0897f08212

38
ce/ce.c
View File

@@ -18,6 +18,7 @@
#include <tscreen.h>
#define LINE_INIT_MAX 40
#define LINE_TAIL_BATCH_MAX 1024
#define PROMPT "$ "
struct edit_line {
@@ -55,18 +56,32 @@ void app_main (void) {
if (ch == '\n')
break;
gapbuffer_move (&edit_line.gb, edit_line.cursor);
switch (ch) {
case '\b':
if (edit_line.cursor > 0) {
gapbuffer_backspace (&edit_line.gb);
edit_line.cursor--;
gapbuffer_backspace (&edit_line.gb);
mprintf ("\b");
mprintf (ANSIQ_CUR_LEFT (1));
char* tail = gapbuffer_string_at (&edit_line.gb, edit_line.cursor);
mprintf ("%s" ANSIQ_SCR_CLR2END, tail);
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
int move_back = (int)strlen (tail);
int move_back = strlen (tail);
if (move_back > 0)
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
}
break;
case KB_DELETE:
if (edit_line.cursor < gapbuffer_length (&edit_line.gb)) {
edit_line.gb.gap_end++;
char* tail = gapbuffer_string_at (&edit_line.gb, edit_line.cursor);
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
int move_back = strlen (tail);
if (move_back > 0)
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
}
@@ -84,21 +99,20 @@ void app_main (void) {
}
break;
default:
if (isascii (ch)) {
if (isprint (ch)) {
gapbuffer_insert (&edit_line.gb, ch);
edit_line.cursor++;
if (edit_line.cursor == gapbuffer_length (&edit_line.gb)) {
mprintf ("%c", ch);
} else {
char* tail = gapbuffer_string_at (&edit_line.gb, edit_line.cursor - 1);
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
int move_back = (int)strlen (tail) - 1;
if (move_back > 0)
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
size_t move_back = strlen (tail) - 1;
mprintf ("%s" ANSIQ_DYN_CUR_LEFT, tail, move_back);
}
}
break;
}
gapbuffer_move (&edit_line.gb, edit_line.cursor);
}
mprintf ("\n");