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;
}
}
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();
{
// 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<std::string> args;
size_t start = 0;
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);
}
private:
- std::list<std::string> lines; /**< backbuffer of lines sent to the console */
+ std::list<std::string> history; /**< command history. New lines get added to back. */
+ std::list<std::string>::iterator history_position; /**< item of command history that is currently displayed */
+ std::list<std::string> lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
std::map<std::string, std::list<ConsoleCommandReceiver*> > commands; /**< map of console commands and a list of associated ConsoleCommandReceivers */
std::auto_ptr<Surface> background; /**< console background image */
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)) {