diff --git a/src/lineedit.cpp b/src/lineedit.cpp index 254cbfe..9d9d0a9 100644 --- a/src/lineedit.cpp +++ b/src/lineedit.cpp @@ -5,6 +5,7 @@ #include #include +#include #include "common.hpp" #include "fio.hpp" @@ -389,8 +390,42 @@ class LineEdit { RefreshState last_refresh; }; -Result read_line(const char* prompt) { - auto rl = TRY(LineEdit::create(prompt)); +Result 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 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(); + } }