[edit] Add line-editing library, libedit

Now used by the 6s shell to enable backspace, return, etc. No cursor
movement yet or anything complex.
This commit is contained in:
2024-04-27 16:21:46 -07:00
parent 2d7cfc6c59
commit da9041823b
6 changed files with 238 additions and 18 deletions

View File

@@ -1,11 +1,13 @@
#include <j6/channel.hh>
#include <j6/init.h>
#include <j6/errors.h>
#include <j6/memutils.h>
#include <j6/protocols/service_locator.hh>
#include <j6/ring_buffer.hh>
#include <j6/syscalls.h>
#include <j6/syslog.hh>
#include <j6/types.h>
#include <edit/line.h>
extern "C" {
int main(int, const char **);
@@ -13,9 +15,12 @@ extern "C" {
j6_handle_t g_handle_sys = j6_handle_invalid;
const char prompt[] = "\r\nj6> ";
const char prompt[] = "\x1b[1;32mj6> \x1b[0m";
static constexpr size_t prompt_len = sizeof(prompt) - 1;
const char query[] = "\x1b[6n";
static constexpr size_t query_len = sizeof(prompt) - 1;
size_t
gather_command(j6::channel &cout, j6::ring_buffer &buf)
{
@@ -34,9 +39,15 @@ gather_command(j6::channel &cout, j6::ring_buffer &buf)
size_t i = 0;
bool newline = false;
bool eof = false;
while (i < n) {
start[i] = input[i];
outp[i] = input[i];
if (start[i] == 0x1b)
outp[i] = 'E';
if (start[i] == 4)
eof = true;
if (start[i++] == '\r') {
newline = true;
break;
@@ -46,6 +57,12 @@ gather_command(j6::channel &cout, j6::ring_buffer &buf)
cout.commit(i);
cout.consume(i);
if (eof) {
cout.reserve(query_len, &outp, true);
memcpy(outp, query, query_len);
cout.commit(query_len);
}
if (newline)
return total_len + i;
@@ -63,10 +80,12 @@ main(int argc, const char **argv)
if (g_handle_sys == j6_handle_invalid)
return 1;
/*
static constexpr size_t buffer_pages = 1;
j6::ring_buffer in_buffer {buffer_pages};
if (!in_buffer.valid())
return 2;
*/
j6_handle_t slp = j6_find_init_handle(j6::proto::sl::id);
if (slp == j6_handle_invalid)
@@ -93,15 +112,25 @@ main(int argc, const char **argv)
if (!cout)
return 5;
while (true) {
uint8_t *outp = nullptr;
cout->reserve(prompt_len, &outp, true);
for (size_t i = 0; i < prompt_len; ++i)
outp[i] = prompt[i];
cout->commit(prompt_len);
edit::line editor {prompt, prompt_len};
const char *inp = in_buffer.read_ptr();
size_t size = gather_command(*cout, in_buffer);
in_buffer.consume(size);
while (true) {
uint8_t *outb = nullptr;
char const *inc = nullptr;
size_t len = editor.output(&inc);
while (len) {
cout->reserve(len, &outb, true);
memcpy(outb, inc, len);
cout->commit(len);
len = editor.output(&inc);
}
char *outc = nullptr;
uint8_t const *inb = nullptr;
len = cout->get_block(&inb);
if (len) {
len = editor.input(reinterpret_cast<const char*>(inb), len);
cout->consume(len);
}
}
}