Implement a dumb line reader for comint mode
This commit is contained in:
parent
353102f8cd
commit
9e45485053
1 changed files with 38 additions and 3 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include "fio.hpp"
|
#include "fio.hpp"
|
||||||
|
@ -389,8 +390,42 @@ class LineEdit {
|
||||||
RefreshState last_refresh;
|
RefreshState last_refresh;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Result<String> read_dumb(const char* prompt) {
|
||||||
|
std::cout << prompt << std::flush;
|
||||||
|
|
||||||
|
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) {
|
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));
|
auto rl = TRY(LineEdit::create(prompt));
|
||||||
|
|
||||||
return rl.read_one();
|
return rl.read_one();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue