libinput Generic way of gathering user commandline input
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m49s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m49s
This commit is contained in:
108
ce/ce.c
108
ce/ce.c
@@ -1,12 +1,11 @@
|
||||
#include "arena_alloc.h"
|
||||
#include "context.h"
|
||||
#include "gapbuffer.h"
|
||||
#include "interp.h"
|
||||
#include "strbuf.h"
|
||||
#include <arena.h>
|
||||
#include <debugconsole.h>
|
||||
#include <kb.h>
|
||||
#include <list.h>
|
||||
#include <in_gb.h>
|
||||
#include <in_input.h>
|
||||
#include <malloc.h>
|
||||
#include <mprintf.h>
|
||||
#include <printf.h>
|
||||
@@ -14,14 +13,10 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <streams.h>
|
||||
#include <string.h>
|
||||
#include <system.h>
|
||||
#include <tcursor.h>
|
||||
#include <tscreen.h>
|
||||
|
||||
#define LINE_INIT_MAX 40
|
||||
#define PROMPT "$ "
|
||||
#define PROMPT "$ "
|
||||
|
||||
void* wmalloc (void* ctx, size_t size) {
|
||||
(void)ctx;
|
||||
@@ -51,7 +46,7 @@ void* warena_realloc (void* ctx, void* mem, size_t old, size_t new) {
|
||||
void warena_free (void* ctx, void* mem) { (void)ctx, (void)mem; }
|
||||
|
||||
struct edit_line {
|
||||
struct gapbuffer gb;
|
||||
struct in_gb gb;
|
||||
size_t cursor;
|
||||
};
|
||||
|
||||
@@ -67,99 +62,14 @@ static void exec_line (const char* line) {
|
||||
void app_main (void) {
|
||||
libprocess_self_init ();
|
||||
|
||||
struct edit_line edit_line;
|
||||
|
||||
const char* render = NULL;
|
||||
char line[1024];
|
||||
|
||||
while (interp_is_running ()) {
|
||||
gapbuffer_init (&warena_malloc, &arena, &edit_line.gb, LINE_INIT_MAX);
|
||||
edit_line.cursor = 0;
|
||||
memset (line, 0, sizeof (line));
|
||||
|
||||
mprintf (PROMPT);
|
||||
in_stream_read_line (PROMPT, line, sizeof (line) - 1);
|
||||
|
||||
for (;;) {
|
||||
uint8_t ch;
|
||||
|
||||
if (stream_read (process_get_pgid (), STREAM_IN, &ch, 1) <= 0) {
|
||||
sched ();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '\n')
|
||||
break;
|
||||
|
||||
gapbuffer_move (&edit_line.gb, edit_line.cursor);
|
||||
|
||||
switch (ch) {
|
||||
case '\b':
|
||||
if (edit_line.cursor > 0) {
|
||||
edit_line.cursor--;
|
||||
gapbuffer_backspace (&edit_line.gb);
|
||||
|
||||
mprintf (ANSIQ_CUR_LEFT (1));
|
||||
|
||||
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);
|
||||
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 (&warena_malloc, &arena, &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);
|
||||
}
|
||||
break;
|
||||
case KB_LEFT:
|
||||
if (edit_line.cursor > 0) {
|
||||
edit_line.cursor--;
|
||||
mprintf (ANSIQ_CUR_LEFT (1));
|
||||
}
|
||||
break;
|
||||
case KB_RIGHT:
|
||||
if (edit_line.cursor < gapbuffer_length (&edit_line.gb)) {
|
||||
edit_line.cursor++;
|
||||
mprintf (ANSIQ_CUR_RIGHT (1));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (isprint (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 (&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);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mprintf ("\n");
|
||||
|
||||
render = gapbuffer_string_at (&warena_malloc, &arena, &edit_line.gb, 0);
|
||||
|
||||
if (render != NULL)
|
||||
exec_line (render);
|
||||
|
||||
gapbuffer_fini (&warena_free, &arena, &edit_line.gb);
|
||||
arena_reset (&arena);
|
||||
if (line[0] != '\0')
|
||||
exec_line (line);
|
||||
}
|
||||
|
||||
arena_destroy (&arena);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user