CE edit command
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m11s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m11s
This commit is contained in:
50
ce/ce.c
50
ce/ce.c
@@ -7,6 +7,7 @@
|
||||
#include "strbuf.h"
|
||||
#include <arena.h>
|
||||
#include <kb.h>
|
||||
#include <liballoc.h>
|
||||
#include <list.h>
|
||||
#include <printf.h>
|
||||
#include <stdbool.h>
|
||||
@@ -17,9 +18,35 @@
|
||||
#include <tcursor.h>
|
||||
#include <tscreen.h>
|
||||
|
||||
#define LINE_INIT_MAX 40
|
||||
#define LINE_TAIL_BATCH_MAX 1024
|
||||
#define PROMPT "$ "
|
||||
#define LINE_INIT_MAX 40
|
||||
#define PROMPT "$ "
|
||||
|
||||
void* wmalloc (void* ctx, size_t size) {
|
||||
(void)ctx;
|
||||
return malloc (size);
|
||||
}
|
||||
|
||||
void* wrealloc (void* ctx, void* mem, size_t old, size_t new) {
|
||||
(void)ctx, (void)old;
|
||||
return realloc (mem, new);
|
||||
}
|
||||
|
||||
void wfree (void* ctx, void* mem) {
|
||||
(void)ctx;
|
||||
free (mem);
|
||||
}
|
||||
|
||||
void* warena_malloc (void* ctx, size_t size) {
|
||||
struct arena* a = ctx;
|
||||
return arena_malloc (a, size);
|
||||
}
|
||||
|
||||
void* warena_realloc (void* ctx, void* mem, size_t old, size_t new) {
|
||||
struct arena* a = ctx;
|
||||
return arena_realloc (a, mem, old, new);
|
||||
}
|
||||
|
||||
void warena_free (void* ctx, void* mem) { (void)ctx, (void)mem; }
|
||||
|
||||
struct edit_line {
|
||||
struct gapbuffer gb;
|
||||
@@ -44,7 +71,7 @@ void app_main (void) {
|
||||
const char* render = NULL;
|
||||
|
||||
while (interp_is_running ()) {
|
||||
gapbuffer_init (&edit_line.gb, LINE_INIT_MAX);
|
||||
gapbuffer_init (&warena_malloc, &arena, &edit_line.gb, LINE_INIT_MAX);
|
||||
edit_line.cursor = 0;
|
||||
|
||||
mprintf (PROMPT);
|
||||
@@ -66,7 +93,8 @@ void app_main (void) {
|
||||
|
||||
mprintf (ANSIQ_CUR_LEFT (1));
|
||||
|
||||
char* tail = gapbuffer_string_at (&edit_line.gb, edit_line.cursor);
|
||||
char* tail =
|
||||
gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, edit_line.cursor);
|
||||
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
|
||||
|
||||
int move_back = strlen (tail);
|
||||
@@ -78,7 +106,8 @@ void app_main (void) {
|
||||
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);
|
||||
char* tail =
|
||||
gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, edit_line.cursor);
|
||||
mprintf ("%s" ANSIQ_SCR_CLR2LEND, tail);
|
||||
|
||||
int move_back = strlen (tail);
|
||||
@@ -100,13 +129,14 @@ void app_main (void) {
|
||||
break;
|
||||
default:
|
||||
if (isprint (ch)) {
|
||||
gapbuffer_insert (&edit_line.gb, ch);
|
||||
gapbuffer_insert (&warena_realloc, &arena, &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);
|
||||
char* tail =
|
||||
gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, edit_line.cursor - 1);
|
||||
size_t move_back = strlen (tail) - 1;
|
||||
mprintf ("%s" ANSIQ_DYN_CUR_LEFT, tail, move_back);
|
||||
}
|
||||
@@ -117,12 +147,12 @@ void app_main (void) {
|
||||
|
||||
mprintf ("\n");
|
||||
|
||||
render = gapbuffer_get_string (&edit_line.gb);
|
||||
render = gapbuffer_get_string (&warena_malloc, &arena, &edit_line.gb);
|
||||
|
||||
if (render != NULL)
|
||||
exec_line (render);
|
||||
|
||||
gapbuffer_fini (&edit_line.gb);
|
||||
gapbuffer_fini (&warena_free, &arena, &edit_line.gb);
|
||||
arena_reset (&arena);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user