From 75c476f2008698ae842b99a61ac1b16d81b16728 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Thu, 11 Apr 2024 17:09:07 +0200 Subject: [PATCH] wip: commandline history --- console.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++------- console.h | 3 +++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/console.cpp b/console.cpp index 71e18f6..ef6310a 100644 --- a/console.cpp +++ b/console.cpp @@ -177,7 +177,14 @@ std::string console::read_line(const std::string & prompt) put_string(prompt); put_string(">"); - std::string str; + if (edit_lines_hist.empty() == false) + edit_lines_hist.erase(edit_lines_hist.begin()); + + while(edit_lines_hist.size() < n_edit_lines_hist) + edit_lines_hist.push_back(""); + + size_t line_nr = edit_lines_hist.size() - 1; + bool escape = false; for(;;) { auto c = wait_char(250); @@ -188,24 +195,60 @@ std::string console::read_line(const std::string & prompt) if (c.has_value() == false) continue; + if (c == 27) { + escape = true; + continue; + } + + if (escape) { + if (c == '[') + continue; + + escape = false; + + for(size_t i=0; i 0) + line_nr--; + else + line_nr = edit_lines_hist.size() - 1; + } + else if (c == 'B') { // down + if (line_nr < edit_lines_hist.size() - 1) + line_nr++; + else + line_nr = 0; + } + else { + continue; + } + + for(size_t i=0; i= 32) { - str += c.value(); + edit_lines_hist.at(line_nr) += c.value(); put_char(c.value()); } @@ -213,7 +256,7 @@ std::string console::read_line(const std::string & prompt) put_string_lf(""); - return str; + return edit_lines_hist.at(line_nr); } void console::debug(const std::string fmt, ...) diff --git a/console.h b/console.h index 4ba1e3b..5680350 100644 --- a/console.h +++ b/console.h @@ -47,6 +47,9 @@ protected: uint8_t tx { 0 }; uint8_t ty { 0 }; + const size_t n_edit_lines_hist { 8 }; // maximum number of previous edit-lines + std::vector edit_lines_hist; + std::string debug_buffer; virtual int wait_for_char_ll(const short timeout) = 0;