Even more sophisticated readline functionality
This commit is contained in:
@@ -23,7 +23,16 @@ Channel::~Channel() {
|
||||
driven_->channels_[chid_] = nullptr;
|
||||
}
|
||||
|
||||
void Channel::show_command() {
|
||||
void Channel::show_or_hide_command() {
|
||||
// Decide if we should hide the command, and if so, do it.
|
||||
if ((!readline_enabled_) || (driven_->stop_driver_) || (sb_out_->fill() > 0)) {
|
||||
int ccsize = current_prompt_.size() + current_command_.size();
|
||||
readline_echo_ += util::repeat_string("\b \b", ccsize);
|
||||
current_prompt_ = "";
|
||||
current_command_ = "";
|
||||
return;
|
||||
}
|
||||
|
||||
// If the prompt has changed, erase everything and start over.
|
||||
if (desired_prompt_ != current_prompt_) {
|
||||
int ccsize = current_prompt_.size() + current_command_.size();
|
||||
@@ -51,18 +60,9 @@ void Channel::show_command() {
|
||||
}
|
||||
}
|
||||
|
||||
void Channel::hide_command() {
|
||||
int ccsize = current_command_.size() + current_prompt_.size();
|
||||
if (ccsize > 0) {
|
||||
readline_echo_ += util::repeat_string("\b \b", ccsize);
|
||||
current_command_ = "";
|
||||
current_prompt_ = "";
|
||||
}
|
||||
}
|
||||
|
||||
void Channel::set_prompt(const std::string &p) {
|
||||
desired_prompt_ = p;
|
||||
if (readline_enabled_) show_command();
|
||||
show_or_hide_command();
|
||||
}
|
||||
|
||||
void Channel::feed_readline(int nbytes, const char *bytes) {
|
||||
@@ -72,7 +72,7 @@ void Channel::feed_readline(int nbytes, const char *bytes) {
|
||||
// Ignore newline immediately after carriage return.
|
||||
// Otherwise, crlf produces two newlines.
|
||||
} else if ((c == '\r') || (c == '\n')) {
|
||||
show_command();
|
||||
show_or_hide_command();
|
||||
readline_echo_ = readline_echo_ + " \n";
|
||||
sb_in_->write_bytes(desired_command_);
|
||||
sb_in_->write_uint8('\n');
|
||||
@@ -95,11 +95,7 @@ void Channel::feed_readline(int nbytes, const char *bytes) {
|
||||
}
|
||||
|
||||
void Channel::peek_outgoing(int *nbytes, const char **bytes) {
|
||||
if (sb_out_->fill() > 0) {
|
||||
hide_command();
|
||||
} else if (readline_enabled_) {
|
||||
show_command();
|
||||
}
|
||||
show_or_hide_command();
|
||||
if (readline_echo_.size() > 0) {
|
||||
*nbytes = readline_echo_.size();
|
||||
*bytes = readline_echo_.c_str();
|
||||
@@ -127,6 +123,7 @@ void Channel::set_readline(bool e) {
|
||||
if (e != readline_enabled_) {
|
||||
readline_enabled_ = e;
|
||||
desired_command_ = "";
|
||||
show_or_hide_command();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -162,8 +162,7 @@ private:
|
||||
void feed_readline(int nbytes, const char *bytes);
|
||||
void peek_outgoing(int *nbytes, const char **bytes);
|
||||
void sent_outgoing(int nbytes);
|
||||
void show_command();
|
||||
void hide_command();
|
||||
void show_or_hide_command();
|
||||
|
||||
private:
|
||||
static const int READLINE_MAX=512;
|
||||
|
||||
@@ -345,8 +345,8 @@ public:
|
||||
handle_lua_source();
|
||||
handle_new_closed_sockets();
|
||||
handle_new_outgoing_sockets();
|
||||
handle_console_input();
|
||||
handle_console_output();
|
||||
handle_console_input();
|
||||
int mstimeout = short_sleep_ ? 0 : 100;
|
||||
handle_socket_input_output(mstimeout);
|
||||
handle_clock();
|
||||
|
||||
@@ -155,7 +155,6 @@ void TextGame::do_quit_command(const StringVec &cmd) {
|
||||
return;
|
||||
}
|
||||
actor_id_ = 0;
|
||||
stop_driver();
|
||||
}
|
||||
|
||||
void TextGame::do_command(const StringVec &words) {
|
||||
@@ -191,7 +190,7 @@ void TextGame::channel_printbuffer() {
|
||||
printbuffer_line_ = printbuffer->first_line();
|
||||
}
|
||||
while (printbuffer_line_ < printbuffer->first_unchecked()) {
|
||||
stdostream() << "* " << printbuffer->nth(printbuffer_line_) << std::endl;
|
||||
stdostream() << printbuffer->nth(printbuffer_line_) << std::endl;
|
||||
printbuffer_line_ += 1;
|
||||
}
|
||||
if (printbuffer_line_ > printbuffer->first_line()) {
|
||||
|
||||
Reference in New Issue
Block a user