From: Christoph Sommer Date: Wed, 26 Apr 2006 13:46:54 +0000 (+0000) Subject: Added history functionality to Console X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=3cf9d34deb5eb90bad99cd7baa0d391113c1b547;p=supertux.git Added history functionality to Console SVN-Revision: 3443 --- diff --git a/src/console.cpp b/src/console.cpp index 1b5f01ea9..4cfa87412 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -34,9 +34,8 @@ static const float FADE_SPEED = 1; Console::Console() - : vm(NULL), backgroundOffset(0), height(0), alpha(1.0), offset(0), - focused(false), stayOpen(0) -{ + : history_position(history.end()), vm(NULL), backgroundOffset(0), + height(0), alpha(1.0), offset(0), focused(false), stayOpen(0) { fontheight = 8; } @@ -158,6 +157,25 @@ Console::scroll(int numLines) } void +Console::show_history(int offset) +{ + while ((offset > 0) && (history_position != history.end())) { + history_position++; + offset--; + } + while ((offset < 0) && (history_position != history.begin())) { + history_position--; + offset++; + } + if (history_position == history.end()) { + inputBuffer.str(std::string()); + } else { + inputBuffer.str(*history_position); + inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out); + } +} + +void Console::autocomplete() { std::string cmdPart = inputBuffer.str(); @@ -210,7 +228,11 @@ Console::parse(std::string s) { // make sure we actually have something to parse if (s.length() == 0) return; - + + // add line to history + history.push_back(s); + history_position = history.end(); + // split line into list of args std::vector args; size_t start = 0; diff --git a/src/console.hpp b/src/console.hpp index 1194a7fe4..a51f86aff 100644 --- a/src/console.hpp +++ b/src/console.hpp @@ -51,6 +51,7 @@ public: void backspace(); /**< delete last character sent to the input stream */ void scroll(int offset); /**< scroll console text up or down by @c offset lines */ void autocomplete(); /**< autocomplete current command */ + void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */ void draw(DrawingContext& context); /**< draw the console in a DrawingContext */ void update(float elapsed_time); @@ -85,7 +86,9 @@ public: } private: - std::list lines; /**< backbuffer of lines sent to the console */ + std::list history; /**< command history. New lines get added to back. */ + std::list::iterator history_position; /**< item of command history that is currently displayed */ + std::list lines; /**< backbuffer of lines sent to the console. New lines get added to front. */ std::map > commands; /**< map of console commands and a list of associated ConsoleCommandReceivers */ std::auto_ptr background; /**< console background image */ diff --git a/src/control/joystickkeyboardcontroller.cpp b/src/control/joystickkeyboardcontroller.cpp index d31e5c43a..fcca5f51a 100644 --- a/src/control/joystickkeyboardcontroller.cpp +++ b/src/control/joystickkeyboardcontroller.cpp @@ -392,6 +392,12 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event) case SDLK_END: Console::instance->scroll(+65535); break; + case SDLK_UP: + Console::instance->show_history(-1); + break; + case SDLK_DOWN: + Console::instance->show_history(+1); + break; default: int c = event.key.keysym.unicode; if ((c >= 32) && (c <= 126)) {