CE Implement line editing
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m8s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m8s
This commit is contained in:
@@ -6,6 +6,7 @@ $(eval $(call add_lib,libaux))
|
||||
$(eval $(call add_lib,libarena))
|
||||
$(eval $(call add_lib,libioutil))
|
||||
$(eval $(call add_include,libterminal))
|
||||
$(eval $(call add_include,libkb))
|
||||
|
||||
cflags += -DPRINTF_INCLUDE_CONFIG_H=1
|
||||
|
||||
|
||||
91
ce/ce.c
91
ce/ce.c
@@ -1,29 +1,29 @@
|
||||
#include "arena_alloc.h"
|
||||
#include "context.h"
|
||||
#include "gapbuffer.h"
|
||||
#include "interp.h"
|
||||
#include "mprintf.h"
|
||||
#include "self.h"
|
||||
#include "strbuf.h"
|
||||
#include <arena.h>
|
||||
#include <desc.h>
|
||||
#include <filereader.h>
|
||||
#include <filewriter.h>
|
||||
#include <liballoc.h>
|
||||
#include <kb.h>
|
||||
#include <list.h>
|
||||
#include <minmax.h>
|
||||
#include <path.h>
|
||||
#include <printf.h>
|
||||
#include <process.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <str_status.h>
|
||||
#include <string.h>
|
||||
#include <system.h>
|
||||
#include <terminal_cursor.h>
|
||||
#include <tcursor.h>
|
||||
#include <tscreen.h>
|
||||
|
||||
#define LINE_BUFFER_MAX 1024
|
||||
#define PROMPT "$ "
|
||||
#define LINE_INIT_MAX 2
|
||||
#define PROMPT "$ "
|
||||
|
||||
struct edit_line {
|
||||
struct gapbuffer gb;
|
||||
size_t cursor;
|
||||
};
|
||||
|
||||
static void exec_line (const char* line) {
|
||||
struct list_node_link* tokens = NULL;
|
||||
@@ -32,39 +32,84 @@ static void exec_line (const char* line) {
|
||||
|
||||
if (tokens != NULL)
|
||||
parse_and_execute (tokens);
|
||||
|
||||
arena_reset (&arena);
|
||||
}
|
||||
|
||||
void app_main (void) {
|
||||
e_pid = get_exec_pid ();
|
||||
e_pgid = get_procgroup (e_pid);
|
||||
|
||||
size_t line_cursor = 0;
|
||||
char line_buffer[LINE_BUFFER_MAX + 1];
|
||||
memset (line_buffer, 0, sizeof (line_buffer));
|
||||
struct edit_line edit_line;
|
||||
|
||||
const char* render = NULL;
|
||||
|
||||
while (interp_is_running ()) {
|
||||
gapbuffer_init (&edit_line.gb, LINE_INIT_MAX);
|
||||
edit_line.cursor = 0;
|
||||
|
||||
mprintf (PROMPT);
|
||||
|
||||
char ch = 0;
|
||||
uint8_t ch = 0;
|
||||
for (;;) {
|
||||
mail_receive (&ch, 1);
|
||||
|
||||
if (ch == '\n')
|
||||
break;
|
||||
|
||||
if (line_cursor < LINE_BUFFER_MAX) {
|
||||
line_buffer[line_cursor++] = ch;
|
||||
mprintf ("%c", ch);
|
||||
switch (ch) {
|
||||
case '\b':
|
||||
if (edit_line.cursor > 0) {
|
||||
gapbuffer_backspace (&edit_line.gb);
|
||||
edit_line.cursor--;
|
||||
|
||||
mprintf ("\b");
|
||||
|
||||
char* tail = gapbuffer_string_at (&edit_line.gb, edit_line.cursor);
|
||||
mprintf ("%s" ANSIQ_SCR_CLR2END, tail);
|
||||
|
||||
int move_back = (int)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 (isascii (ch)) {
|
||||
gapbuffer_insert (&edit_line.gb, ch);
|
||||
edit_line.cursor++;
|
||||
|
||||
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);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
gapbuffer_move (&edit_line.gb, edit_line.cursor);
|
||||
}
|
||||
|
||||
mprintf ("\n");
|
||||
exec_line (line_buffer);
|
||||
|
||||
line_cursor = 0;
|
||||
memset (line_buffer, 0, sizeof (line_buffer));
|
||||
render = gapbuffer_get_string (&edit_line.gb);
|
||||
|
||||
if (render != NULL)
|
||||
exec_line (render);
|
||||
|
||||
gapbuffer_fini (&edit_line.gb);
|
||||
arena_reset (&arena);
|
||||
}
|
||||
|
||||
arena_destroy (&arena);
|
||||
|
||||
126
ce/gapbuffer.c
Normal file
126
ce/gapbuffer.c
Normal file
@@ -0,0 +1,126 @@
|
||||
#include "gapbuffer.h"
|
||||
#include "arena_alloc.h"
|
||||
#include <liballoc.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void gapbuffer_init (struct gapbuffer* gb, size_t capacity) {
|
||||
gb->buffer = arena_malloc (&arena, capacity);
|
||||
|
||||
if (gb->buffer == NULL)
|
||||
return;
|
||||
|
||||
memset (gb->buffer, 0, capacity);
|
||||
gb->size = capacity;
|
||||
gb->gap_start = 0;
|
||||
gb->gap_end = capacity;
|
||||
}
|
||||
|
||||
void gapbuffer_fini (struct gapbuffer* gb) { memset (gb, 0, sizeof (*gb)); }
|
||||
|
||||
void gapbuffer_grow (struct gapbuffer* gb) {
|
||||
size_t old_size = gb->size;
|
||||
size_t new_size = gb->size * 2;
|
||||
|
||||
char* new = arena_realloc (&arena, gb->buffer, old_size, new_size);
|
||||
|
||||
if (new == NULL)
|
||||
return;
|
||||
|
||||
gb->buffer = new;
|
||||
|
||||
size_t post_gap_size = old_size - gb->gap_end;
|
||||
size_t new_gap_end = new_size - post_gap_size;
|
||||
|
||||
memmove (gb->buffer + new_gap_end, gb->buffer + gb->gap_end, post_gap_size);
|
||||
|
||||
gb->gap_end = new_gap_end;
|
||||
gb->size = new_size;
|
||||
}
|
||||
|
||||
void gapbuffer_move (struct gapbuffer* gb, size_t pos) {
|
||||
if (pos > (gb->size - (gb->gap_end - gb->gap_start)))
|
||||
pos = (gb->size - (gb->gap_end - gb->gap_start));
|
||||
|
||||
while (gb->gap_start > pos) {
|
||||
gb->gap_start--;
|
||||
gb->gap_end--;
|
||||
gb->buffer[gb->gap_end] = gb->buffer[gb->gap_start];
|
||||
}
|
||||
|
||||
while (gb->gap_start < pos) {
|
||||
gb->buffer[gb->gap_start] = gb->buffer[gb->gap_end];
|
||||
gb->gap_start++;
|
||||
gb->gap_end++;
|
||||
}
|
||||
}
|
||||
|
||||
void gapbuffer_insert (struct gapbuffer* gb, char c) {
|
||||
if (gb->gap_start == gb->gap_end)
|
||||
gapbuffer_grow (gb);
|
||||
|
||||
gb->buffer[gb->gap_start] = c;
|
||||
gb->gap_start++;
|
||||
}
|
||||
|
||||
void gapbuffer_backspace (struct gapbuffer* gb) {
|
||||
if (gb->gap_start > 0)
|
||||
gb->gap_start--;
|
||||
}
|
||||
|
||||
char* gapbuffer_get_string (struct gapbuffer* gb) {
|
||||
size_t size = gb->size - (gb->gap_end - gb->gap_start);
|
||||
|
||||
char* str = arena_malloc (&arena, size + 1);
|
||||
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
memcpy (str, gb->buffer, gb->gap_start);
|
||||
|
||||
memcpy (str + gb->gap_start, gb->buffer + gb->gap_end, gb->size - gb->gap_end);
|
||||
|
||||
str[size] = '\0';
|
||||
return str;
|
||||
}
|
||||
|
||||
size_t gapbuffer_length (struct gapbuffer* gb) { return gb->size - (gb->gap_end - gb->gap_start); }
|
||||
|
||||
char* gapbuffer_string_at (struct gapbuffer* gb, size_t pos) {
|
||||
size_t total_size = gapbuffer_length (gb);
|
||||
|
||||
if (pos >= total_size)
|
||||
return "";
|
||||
|
||||
size_t tail_size = total_size - pos;
|
||||
|
||||
char* res = arena_malloc (&arena, tail_size + 1);
|
||||
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
|
||||
size_t written = 0;
|
||||
|
||||
if (pos < gb->gap_start) {
|
||||
size_t copy = gb->gap_start - pos;
|
||||
|
||||
if (copy > tail_size)
|
||||
copy = tail_size;
|
||||
|
||||
memcpy (res, &gb->buffer[pos], copy);
|
||||
written += copy;
|
||||
pos += copy;
|
||||
}
|
||||
|
||||
if (written < tail_size) {
|
||||
size_t gap_size = gb->gap_end - gb->gap_start;
|
||||
size_t phys_pos = pos + gap_size;
|
||||
|
||||
size_t copy = tail_size - written;
|
||||
memcpy (&res[written], &gb->buffer[phys_pos], copy);
|
||||
written += copy;
|
||||
}
|
||||
|
||||
res[written] = '\0';
|
||||
return res;
|
||||
}
|
||||
31
ce/gapbuffer.h
Normal file
31
ce/gapbuffer.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef _GAPBUFFER_H
|
||||
#define _GAPBUFFER_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct gapbuffer {
|
||||
char* buffer;
|
||||
size_t size;
|
||||
size_t gap_start;
|
||||
size_t gap_end;
|
||||
};
|
||||
|
||||
void gapbuffer_init (struct gapbuffer* gb, size_t capacity);
|
||||
|
||||
void gapbuffer_fini (struct gapbuffer* gb);
|
||||
|
||||
void gapbuffer_move (struct gapbuffer* gb, size_t pos);
|
||||
|
||||
void gapbuffer_insert (struct gapbuffer* gb, char c);
|
||||
|
||||
void gapbuffer_backspace (struct gapbuffer* gb);
|
||||
|
||||
void gapbuffer_grow (struct gapbuffer* gb);
|
||||
|
||||
char* gapbuffer_get_string (struct gapbuffer* gb);
|
||||
|
||||
size_t gapbuffer_length (struct gapbuffer* gb);
|
||||
|
||||
char* gapbuffer_string_at (struct gapbuffer* gb, size_t pos);
|
||||
|
||||
#endif // _GAPBUFFER_H
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <desc.h>
|
||||
#include <filereader.h>
|
||||
#include <filewriter.h>
|
||||
#include <kb.h>
|
||||
#include <path.h>
|
||||
#include <process.h>
|
||||
#include <stddef.h>
|
||||
@@ -13,8 +14,6 @@
|
||||
#include <string.h>
|
||||
#include <system.h>
|
||||
|
||||
#define CTRL(x) ((x) - '@')
|
||||
|
||||
static bool run = true;
|
||||
|
||||
bool interp_is_running (void) { return run; }
|
||||
@@ -164,7 +163,7 @@ static void cmd_cancel_proc (void) {
|
||||
for (;;) {
|
||||
mail_receive (&ch, 1);
|
||||
|
||||
if (ch == CTRL ('C'))
|
||||
if (ch == KB_CTRL ('C'))
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user