CE Improve line editing logic, add KB_DELETE
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m18s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m18s
This commit is contained in:
44
ce/ce.c
44
ce/ce.c
@@ -17,8 +17,9 @@
|
|||||||
#include <tcursor.h>
|
#include <tcursor.h>
|
||||||
#include <tscreen.h>
|
#include <tscreen.h>
|
||||||
|
|
||||||
#define LINE_INIT_MAX 40
|
#define LINE_INIT_MAX 40
|
||||||
#define PROMPT "$ "
|
#define LINE_TAIL_BATCH_MAX 1024
|
||||||
|
#define PROMPT "$ "
|
||||||
|
|
||||||
struct edit_line {
|
struct edit_line {
|
||||||
struct gapbuffer gb;
|
struct gapbuffer gb;
|
||||||
@@ -55,18 +56,32 @@ void app_main (void) {
|
|||||||
if (ch == '\n')
|
if (ch == '\n')
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
gapbuffer_move (&edit_line.gb, edit_line.cursor);
|
||||||
|
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '\b':
|
case '\b':
|
||||||
if (edit_line.cursor > 0) {
|
if (edit_line.cursor > 0) {
|
||||||
gapbuffer_backspace (&edit_line.gb);
|
|
||||||
edit_line.cursor--;
|
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);
|
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)
|
if (move_back > 0)
|
||||||
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
|
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
|
||||||
}
|
}
|
||||||
@@ -84,21 +99,20 @@ void app_main (void) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (isascii (ch)) {
|
if (isprint (ch)) {
|
||||||
gapbuffer_insert (&edit_line.gb, ch);
|
gapbuffer_insert (&edit_line.gb, ch);
|
||||||
edit_line.cursor++;
|
edit_line.cursor++;
|
||||||
|
|
||||||
char* tail = gapbuffer_string_at (&edit_line.gb, edit_line.cursor - 1);
|
if (edit_line.cursor == gapbuffer_length (&edit_line.gb)) {
|
||||||
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
|
mprintf ("%c", ch);
|
||||||
|
} else {
|
||||||
int move_back = (int)strlen (tail) - 1;
|
char* tail = gapbuffer_string_at (&edit_line.gb, edit_line.cursor - 1);
|
||||||
if (move_back > 0)
|
size_t move_back = strlen (tail) - 1;
|
||||||
mprintf (ANSIQ_DYN_CUR_LEFT, move_back);
|
mprintf ("%s" ANSIQ_DYN_CUR_LEFT, tail, move_back);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
gapbuffer_move (&edit_line.gb, edit_line.cursor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mprintf ("\n");
|
mprintf ("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user