Implement unicode on console, move readline into driver

This commit is contained in:
2023-05-18 17:14:55 -04:00
parent 2b03ca2eb6
commit fd137e8e74
12 changed files with 371 additions and 162 deletions

View File

@@ -69,108 +69,22 @@ static DrivenEngine *make_engine(std::string_view kind) {
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Channel::Channel(DrivenEngine *de, int chid, int port, const eng::string &target, bool stop) {
chid_ = chid;
port_ = port;
closed_ = false;
target_ = target;
readline_lastc_ = 0;
desired_prompt_ = "";
stop_driver_ = stop;
sb_in_ = eng::make_shared<StreamBuffer>();
sb_out_ = eng::make_shared<StreamBuffer>();
sb_drvout_ = sb_out_;
}
void Channel::erase_command() {
int ccsize = current_prompt_.size() + current_command_.size();
if (ccsize > 0) {
sb_drvout_->write_bytes(util::repeat_string("\b \b", ccsize));
current_prompt_ = "";
current_command_ = "";
}
}
void Channel::echo_command() {
// If the prompt has changed, erase everything and start over.
if (desired_prompt_ != current_prompt_) {
int ccsize = current_prompt_.size() + current_command_.size();
sb_drvout_->write_bytes(util::repeat_string("\b \b", ccsize));
sb_drvout_->write_bytes(desired_prompt_);
current_command_ = "";
current_prompt_ = desired_prompt_;
}
// Find out how much of the command matches.
int match = sv::common_prefix_length(current_command_, desired_command_);
// Echo backspaces to remove the non-matching part.
int remove = current_command_.size() - match;
if (remove > 0) {
sb_drvout_->write_bytes(util::repeat_string("\b \b", remove));
current_command_ = current_command_.substr(0, match);
}
// Echo the new part.
eng::string newpart = desired_command_.substr(current_command_.size());
if (newpart != "") {
sb_drvout_->write_bytes(newpart);
current_command_ = desired_command_;
}
}
void Channel::set_prompt(const eng::string &p) {
desired_prompt_ = p;
}
void Channel::feed_readline(std::string_view data) {
int nbytes = data.size();
const char *bytes = data.data();
for (int i = 0; i < nbytes; i++) {
char c = bytes[i];
if ((c == '\n') && (readline_lastc_ == '\r')) {
// Ignore newline immediately after carriage return.
// Otherwise, crlf produces two newlines.
} else if ((c == '\r') || (c == '\n')) {
echo_command();
sb_drvout_->write_bytes(eng::string(" \n"));
sb_in_->write_bytes(desired_command_);
sb_in_->write_uint8('\n');
desired_command_ = "";
current_prompt_ = "";
current_command_ = "";
} else if ((c == '\b') || (c == 127)) {
int len = desired_command_.size();
if (len > 0) {
desired_command_ = desired_command_.substr(0, len-1);
}
} else if (c >= 32) {
int len = desired_command_.size();
if (len < READLINE_MAX) {
desired_command_ = desired_command_ + c;
}
}
readline_lastc_ = c;
}
}
std::string_view Channel::peek_outgoing() const {
return sb_drvout_->view();
}
void Channel::pump_readline() {
if (sb_drvout_ != sb_out_) {
if (!sb_out_->empty()) {
erase_command();
sb_out_->transfer_into(sb_drvout_.get());
}
echo_command();
}
return sb_out_->view();
}
void Channel::sent_outgoing(int nbytes) {
sb_drvout_->read_bytes(nbytes);
sb_out_->read_bytes(nbytes);
}
//////////////////////////////////////////////////////////////////////////////
@@ -232,7 +146,6 @@ void DrivenEngine::stop_driver() {
DrivenEngine::DrivenEngine() {
next_unused_chid_ = 1;
stdio_channel_ = eng::make_shared<Channel>(this, 0, 0, "", false);
stdio_channel_->sb_drvout_ = eng::make_shared<StreamBuffer>();
channels_[0] = stdio_channel_;
rescan_lua_source_ = false;
clock_ = 0.0;
@@ -492,7 +405,6 @@ bool DrivenEngine::drv_get_stop_driver() const {
void DrivenEngine::drv_initialize(uint32_t srcpklen, const char *srcpk, int argc, char **argv) {
drv_set_lua_source(srcpklen, srcpk);
event_init(argc, argv);
stdio_channel_->pump_readline();
}
void DrivenEngine::drv_clear_new_outgoing() {
@@ -504,14 +416,10 @@ void DrivenEngine::drv_sent_outgoing(uint32_t chid, uint32_t nbytes) {
}
void DrivenEngine::drv_recv_incoming(uint32_t chid, uint32_t nbytes, const char *bytes) {
std::string_view sbytes(bytes, nbytes);
if (nbytes > 0) {
std::string_view sbytes(bytes, nbytes);
Channel *ch = get_chid(chid);
if (ch->sb_drvout_ != ch->sb_out_) {
ch->feed_readline(sbytes);
} else {
ch->sb_in_->write_bytes(sbytes);
}
ch->sb_in_->write_bytes(sbytes);
}
}
@@ -532,7 +440,6 @@ uint32_t DrivenEngine::drv_notify_accept(uint32_t port) {
void DrivenEngine::drv_invoke_event_update(double clock) {
clock_ = clock;
event_update();
stdio_channel_->pump_readline();
}
void DrivenEngine::drv_set_lua_source(uint32_t srcpklen, const char *srcpk) {