Implement a dumb line reader for comint mode

This commit is contained in:
Konstantin Nazarov 2024-10-11 21:44:28 +01:00
parent 353102f8cd
commit 9e45485053
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22

View file

@ -5,6 +5,7 @@
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include "common.hpp"
#include "fio.hpp"
@ -389,8 +390,42 @@ class LineEdit {
RefreshState last_refresh;
};
Result<String> read_line(const char* prompt) {
auto rl = TRY(LineEdit::create(prompt));
Result<String> read_dumb(const char* prompt) {
std::cout << prompt << std::flush;
return rl.read_one();
String res = TRY(String::create(""));
int input_fd = STDIN_FILENO;
const size_t bufsize = 256;
char buf[bufsize];
bool endstr = false;
while (!endstr) {
size_t i = 0;
while (i < sizeof(buf) - 1) {
if (read(input_fd, buf + i, 1) != 1) break;
if (buf[i] == '\n') {
endstr = true;
break;
}
i++;
}
buf[i] = '\0';
res = TRY(res.concat(buf));
}
return res;
}
Result<String> read_line(const char* prompt) {
const char* term = std::getenv("TERM");
if (strncmp(term, "dumb", 4) == 0) {
return read_dumb(prompt);
} else {
auto rl = TRY(LineEdit::create(prompt));
return rl.read_one();
}
}