First steps of implementing command-line processing in the ascii driver.

This commit is contained in:
2025-12-18 19:42:05 -05:00
parent 3dd6894305
commit 2646e4d4ac
3 changed files with 71 additions and 34 deletions

View File

@@ -2,6 +2,10 @@
#define MAXLINE 512
static std::u32string white_(1, ' ');
static std::u32string newline_(1, '\n');
static std::u32string n_backspaces(int n) {
std::u32string result(3 * n, 0);
for (int i = 0; i < n; i++) {
@@ -24,8 +28,8 @@ void ReadlineDevice::set_print_callback(print_callback cb) {
print_cb_ = cb;
}
void ReadlineDevice::set_prompt(const std::u32string &prompt) {
desired_prompt_ = prompt;
void ReadlineDevice::set_prompt(std::string_view prompt) {
desired_prompt_ = drvutil::utf8_to_utf32(prompt, nullptr);
echo_command();
}
@@ -66,46 +70,46 @@ void ReadlineDevice::echo_command() {
}
}
std::u32string ReadlineDevice::putcode(char32_t c) {
std::string ReadlineDevice::putcode(char32_t c) {
if ((c == '\n') && (readline_lastc_ == '\r')) {
// Ignore newline immediately after carriage return.
// Otherwise, crlf produces two newlines.
return std::u32string();
return "";
} else if ((c == '\r') || (c == '\n')) {
std::u32string white(1, ' ');
std::u32string newline(1, '\n');
echo_command();
print_cb_(white + newline);
std::u32string result = desired_command_ + newline;
print_cb_(white_ + newline_);
std::u32string result = desired_command_ + newline_;
desired_command_.clear();
current_prompt_.clear();
current_command_.clear();
echo_command();
return result;
return drvutil::utf32_to_utf8(result);
} else if ((c == '\b') || (c == 127)) {
int len = desired_command_.size();
if (len > 0) {
desired_command_ = desired_command_.substr(0, len-1);
}
echo_command();
return std::u32string();
return "";
} else if ((c >= 32)&&(c <= 0x10FFFF)) {
int len = desired_command_.size();
if (len < MAXLINE) {
desired_command_ = desired_command_ + c;
}
echo_command();
return std::u32string();
return "";
}
readline_lastc_ = c;
return std::u32string();
return "";
}
void ReadlineDevice::print(const std::u32string &s) {
if (!s.empty()) {
erase_command();
print_cb_(s);
echo_command();
}
void ReadlineDevice::printline(std::string_view s) {
bool missing_newline = ((s.size() == 0) || (s[s.size() - 1] != '\n'));
std::u32string utf32 = drvutil::utf8_to_utf32(s, nullptr);
erase_command();
print_cb_(utf32);
if (missing_newline) print_cb_(newline_);
echo_command();
}