From 110a20a9ae3816a399d4abcb480be954073df7a3 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 30 Oct 2020 14:34:45 +0100 Subject: [PATCH] simple line editor --- lib/uuid-console/src/shell.cpp | 30 +++++++++++++++++++++++++++-- lib/uuid-console/src/uuid/console.h | 3 ++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/uuid-console/src/shell.cpp b/lib/uuid-console/src/shell.cpp index af90adee7..3ea50d9a9 100644 --- a/lib/uuid-console/src/shell.cpp +++ b/lib/uuid-console/src/shell.cpp @@ -226,8 +226,34 @@ void Shell::loop_normal() { line_buffer_ = oldline_; display_prompt(); } - // cursor back, forw or down: Delete line - if (((c == 'B') || (c == 'C') || (c == 'D')) && (previous_ == '[')) { + // cursor back, delete cursor chars and move on char to rest + if ((c == 'D') && (previous_ == '[')) { + line_buffer_.pop_back(); + line_buffer_.pop_back(); + if (line_buffer_.length() > 0) { + uint8_t ch = line_buffer_.back(); + line_rest_.push_back(ch); + line_buffer_.pop_back(); + } + erase_current_line(); + prompt_displayed_ = false; + display_prompt(); + } + // cursor forward, delete cursor chars and add one from rest. + if ((c == 'C') && (previous_ == '[')) { + line_buffer_.pop_back(); + line_buffer_.pop_back(); + if (line_rest_.length() > 0) { + uint8_t ch = line_rest_.back(); + line_buffer_.push_back(ch); + line_rest_.pop_back(); + } + erase_current_line(); + prompt_displayed_ = false; + display_prompt(); + } + // cursor down(B): Delete line + if ((c == 'B') && (previous_ == '[')) { erase_current_line(); prompt_displayed_ = false; line_buffer_.clear(); diff --git a/lib/uuid-console/src/uuid/console.h b/lib/uuid-console/src/uuid/console.h index 9062dc09b..73e5f09fc 100644 --- a/lib/uuid-console/src/uuid/console.h +++ b/lib/uuid-console/src/uuid/console.h @@ -905,7 +905,8 @@ class Shell : public std::enable_shared_from_this, public uuid::log::Hand std::string line_buffer_; /*!< Command line buffer. Limited to maximum_command_line_length() bytes. @since 0.1.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 */ - std::string oldline_; /*!< old Command line buffer.*/ + std::string oldline_; /*!< old Command line buffer.*/ + std::string line_rest_; /*!< rest of Command line buffer if goinig back with cursor.*/ Mode mode_ = Mode::NORMAL; /*!< Current execution mode. @since 0.1.0 */ std::unique_ptr mode_data_ = nullptr; /*!< Data associated with the current execution mode. @since 0.1.0 */ bool stopped_ = false; /*!< Indicates that the shell has been stopped. @since 0.1.0 */