From cf641476bf2b2525ad4e58aae98c6a9a7af49425 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Wed, 21 Apr 2021 07:43:03 +0200 Subject: [PATCH] More linebuffers to shell --- lib/uuid-console/src/shell.cpp | 27 ++++++++++++++++++++++----- lib/uuid-console/src/uuid/console.h | 10 ++++++---- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/uuid-console/src/shell.cpp b/lib/uuid-console/src/shell.cpp index a875e1e53..34496fc12 100644 --- a/lib/uuid-console/src/shell.cpp +++ b/lib/uuid-console/src/shell.cpp @@ -65,8 +65,10 @@ void Shell::start() { #endif line_buffer_.reserve(maximum_command_line_length_); - line_old_.reserve(maximum_command_line_length_); - line_old_.clear(); + for (uint8_t i = 0; i < MAX_LINES; i++) { + line_old_[i].reserve(maximum_command_line_length_); + line_old_[i].clear(); + } display_banner(); display_prompt(); shells_.insert(shared_from_this()); @@ -214,10 +216,20 @@ void Shell::loop_normal() { default: if (esc_) { if (c == 'A') { // cursor up - line_buffer_ = line_old_; + line_buffer_ = line_old_[line_no_]; + if (line_no_ < MAX_LINES - 1) { + line_no_++; + } cursor_ = 0; } else if (c == 'B') { // cursor down - line_buffer_.clear(); + if (line_no_) { + line_no_--; + } + if (line_no_) { + line_buffer_ = line_old_[line_no_ - 1]; + } else { + line_buffer_.clear(); + } cursor_ = 0; } else if (c == 'C') { // cursor right if (cursor_) { @@ -498,7 +510,12 @@ void Shell::process_command() { println(); return; } - line_old_ = line_buffer_; + uint8_t no = line_no_ ? line_no_ : MAX_LINES; + while (--no) { + line_old_[no] = line_old_[no - 1]; + } + line_no_ = 0; + line_old_[0] = line_buffer_; while (!line_buffer_.empty()) { size_t pos = line_buffer_.find(';'); std::string line1; diff --git a/lib/uuid-console/src/uuid/console.h b/lib/uuid-console/src/uuid/console.h index 1143ba80f..85e4a3031 100644 --- a/lib/uuid-console/src/uuid/console.h +++ b/lib/uuid-console/src/uuid/console.h @@ -61,8 +61,9 @@ class Commands; */ class Shell : public std::enable_shared_from_this, public uuid::log::Handler, public ::Stream { public: - static constexpr size_t MAX_COMMAND_LINE_LENGTH = 80; /*!< Maximum length of a command line. @since 0.1.0 */ - static constexpr size_t MAX_LOG_MESSAGES = 20; /*!< Maximum number of log messages to buffer before they are output. @since 0.1.0 */ + static constexpr size_t MAX_COMMAND_LINE_LENGTH = 80; /*!< Maximum length of a command line. @since 0.1.0 */ + static constexpr size_t MAX_LOG_MESSAGES = 20; /*!< Maximum number of log messages to buffer before they are output. @since 0.1.0 */ + static constexpr uint8_t MAX_LINES = 5; /*!< Maximum lines in buffer */ /** * Function to handle the response to a password entry prompt. @@ -903,8 +904,9 @@ class Shell : public std::enable_shared_from_this, public uuid::log::Hand unsigned long log_message_id_ = 0; /*!< The next identifier to use for queued log messages. @since 0.1.0 */ std::list log_messages_; /*!< Queued log messages, in the order they were received. @since 0.1.0 */ size_t maximum_log_messages_ = MAX_LOG_MESSAGES; /*!< Maximum command line length in bytes. @since 0.6.0 */ - std::string line_buffer_; /*!< Command line buffer. Limited to maximum_command_line_length() bytes. @since 0.1.0 */ - std::string line_old_; /*!< old Command line buffer.*/ + std::string line_buffer_; /*!< Command line buffer. Limited to maximum_command_line_length() bytes. @since 0.1.0 */ + std::string line_old_[MAX_LINES]; /*!< old Command line buffer.*/ + uint8_t line_no_ = 0; size_t maximum_command_line_length_ = MAX_COMMAND_LINE_LENGTH; /*!< Maximum command line length in bytes. @since 0.6.0 */ unsigned char previous_ = 0; /*!< Previous character that was entered on the command line. Used to detect CRLF line endings. @since 0.1.0 */ uint8_t cursor_ = 0; /*!< cursor position from end of line */