Add support for > and >> prompts with new readline

This commit is contained in:
2023-05-18 22:09:54 -04:00
parent eb43c99f47
commit d3c4c0801d
9 changed files with 47 additions and 9 deletions

View File

@@ -126,6 +126,10 @@ SharedChannel DrivenEngine::get_stdio_channel() {
return stdio_channel_;
}
void DrivenEngine::set_console_prompt(const eng::string &prompt) {
console_prompt_ = prompt;
}
util::LuaSourcePtr DrivenEngine::get_lua_source() {
return std::move(lua_source_);
}
@@ -382,6 +386,11 @@ bool DrivenEngine::drv_get_outgoing_empty(uint32_t chid) const {
return (v.size() == 0);
}
void DrivenEngine::drv_get_console_prompt(uint32_t *len, const char **data) const {
*len = console_prompt_.size();
*data = console_prompt_.c_str();
}
double DrivenEngine::drv_get_clock() const {
return clock_;
}
@@ -497,6 +506,10 @@ static bool drv_get_outgoing_empty(EngineWrapper *w, uint32_t chid) {
return w->engine->drv_get_outgoing_empty(chid);
}
static void drv_get_console_prompt(EngineWrapper *w, uint32_t *len, const char **data) {
return w->engine->drv_get_console_prompt(len, data);
}
static double drv_get_clock(EngineWrapper *w) {
return w->engine->drv_get_clock();
}
@@ -906,6 +919,7 @@ static void init_engine_wrapper_helper(EngineWrapper *w) {
w->get_channel_released = drv_get_channel_released;
w->get_outgoing = drv_get_outgoing;
w->get_outgoing_empty = drv_get_outgoing_empty;
w->get_console_prompt = drv_get_console_prompt;
w->get_clock = drv_get_clock;
w->get_rescan_lua_source = drv_get_rescan_lua_source;
w->get_stop_driver = drv_get_stop_driver;

View File

@@ -204,6 +204,10 @@ public:
//
std::ostream &stdostream() { return get_stdio_channel()->out()->ostream(); }
// Set the prompt for the console.
//
void set_console_prompt(const eng::string &prompt);
// Fetches the lua source, and takes ownership of it. The DrivenEngine
// no longer contains the source after calling this.
//
@@ -262,6 +266,7 @@ public:
bool drv_get_channel_released(uint32_t chid) const;
void drv_get_outgoing(uint32_t chid, uint32_t *len, const char **data) const;
bool drv_get_outgoing_empty(uint32_t chid) const;
void drv_get_console_prompt(uint32_t *len, const char **data) const;
double drv_get_clock() const;
bool drv_get_rescan_lua_source() const;
bool drv_get_stop_driver() const;
@@ -294,6 +299,7 @@ private:
bool rescan_lua_source_;
double clock_;
bool stop_driver_;
eng::string console_prompt_;
friend class Channel;
};

View File

@@ -88,6 +88,10 @@ struct EngineWrapper {
//
bool (*get_outgoing_empty)(EngineWrapper *w, uint32_t chid);
// Get the console prompt.
//
void (*get_console_prompt)(EngineWrapper *w, uint32_t *len, const char **data);
// Get the clock.
//
// Get the current time. This is equal to the last value passed

View File

@@ -77,7 +77,7 @@ public:
channel_ = new_outgoing_channel("nocert:localhost:8085");
// Set the console prompt
// get_stdio_channel()->set_prompt(console_.get_prompt());
set_console_prompt(console_.get_prompt());
// The driver loads the lua source automatically.
// However, we don't need it. Throw it out.
@@ -252,7 +252,7 @@ public:
eng::string line = get_stdio_channel()->in()->readline();
if (line == "") break;
console_.add(line);
// get_stdio_channel()->set_prompt(console_.get_prompt());
set_console_prompt(console_.get_prompt());
do_command(console_.get_command());
}

View File

@@ -51,7 +51,7 @@ public:
listen_port(8080);
// Set the console prompt.
// get_stdio_channel()->set_prompt(console_.get_prompt());
set_console_prompt(console_.get_prompt());
}
void do_luainvoke_command(const util::StringVec &words) {
@@ -172,7 +172,7 @@ public:
eng::string line = get_stdio_channel()->in()->readline();
if (line == "") break;
console_.add(line);
// get_stdio_channel()->set_prompt(console_.get_prompt());
set_console_prompt(console_.get_prompt());
do_command(console_.get_command());
}

View File

@@ -103,7 +103,7 @@ private:
world_->run_unittests();
actor_id_ = world_->create_login_actor();
stdostream() << "Login actor ID: " << actor_id_ << std::endl;
//get_stdio_channel()->set_prompt(console_.get_prompt());
set_console_prompt(console_.get_prompt());
}
void event_update()
@@ -113,7 +113,7 @@ private:
eng::string line = get_stdio_channel()->in()->readline();
if (line == "") break;
console_.add(line);
//get_stdio_channel()->set_prompt(console_.get_prompt());
set_console_prompt(console_.get_prompt());
do_command(console_.get_command());
if (print_channeler_.channel(world_->get_printbuffer(actor_id_), stdostream())) {
world_->invoke(print_channeler_.invocation(actor_id_));

View File

@@ -214,6 +214,11 @@ class Driver {
void handle_console_input() {
read_console_recently_ = false;
uint32_t promptlen;
const char *promptdata;
engw.get_console_prompt(&engw, &promptlen, &promptdata);
CodepointString prompt = ReadlineDevice::from_utf8(std::string_view(promptdata, promptlen), nullptr);
readline_device_.set_prompt(prompt);
while (true) {
CodepointString cps = console_read();
if (cps.size() == 0) break;

View File

@@ -117,6 +117,11 @@ void ReadlineDevice::set_print_callback(print_callback cb) {
print_cb_ = cb;
}
void ReadlineDevice::set_prompt(const CodepointString &prompt) {
desired_prompt_ = prompt;
echo_command();
}
void ReadlineDevice::erase_command() {
int ccsize = current_prompt_.size() + current_command_.size();
if (ccsize > 0) {
@@ -168,6 +173,7 @@ CodepointString ReadlineDevice::putcode(char32_t c) {
desired_command_.clear();
current_prompt_.clear();
current_command_.clear();
echo_command();
return result;
} else if ((c == '\b') || (c == 127)) {
int len = desired_command_.size();

View File

@@ -19,8 +19,9 @@ private:
CodepointString current_prompt_;
char32_t readline_lastc_;
void echo_command();
void erase_command();
void echo_command();
public:
ReadlineDevice();
@@ -30,6 +31,7 @@ public:
// change the prompt.
void set_prompt(const CodepointString &prompt);
void set_prompt_utf8(const std::string &prompt);
// Use this to print anything on the console.
void print(const CodepointString &cps);
@@ -44,8 +46,9 @@ public:
static std::string to_utf8(const CodepointString &cps);
// This can be used to convert UTF8 to a codepoint string.
// Some of the bytes may not be consumed. Returns the Codepoint
// string and the number of bytes consumed.
// Some of the bytes may not be consumed, if the source contains
// a partial utf-8 sequence. Returns the Codepoint string and the
// number of bytes consumed.
static CodepointString from_utf8(std::string_view source, int *consumed);
};