Browse Source

Added proper command history for console

Thomas Buck 10 years ago
parent
commit
62af5ce2ba
2 changed files with 61 additions and 0 deletions
  1. 6
    0
      include/Console.h
  2. 55
    0
      src/Console.cpp

+ 6
- 0
include/Console.h View File

@@ -43,11 +43,17 @@ public:
43 43
 
44 44
 private:
45 45
 
46
+    void moveInHistory(bool up);
47
+
46 48
     bool mVisible;
47 49
     char *mInputBuffer;
48 50
     size_t mInputBufferPointer;
49 51
     char *mPartialInput;
50 52
     std::vector<char *> mHistory;
53
+
54
+    size_t mHistoryPointer;
55
+    std::vector<char *> mCommandHistory;
56
+    char *mUnfinishedInput;
51 57
 };
52 58
 
53 59
 #endif

+ 55
- 0
src/Console.cpp View File

@@ -27,6 +27,8 @@ Console::Console() {
27 27
     mInputBuffer[INPUT_BUFFER_SIZE] = '\0';
28 28
     mInputBufferPointer = 0;
29 29
     mPartialInput = NULL;
30
+    mHistoryPointer = 0;
31
+    mUnfinishedInput = NULL;
30 32
 }
31 33
 
32 34
 Console::~Console() {
@@ -36,12 +38,22 @@ Console::~Console() {
36 38
     if (mPartialInput)
37 39
         delete [] mPartialInput;
38 40
 
41
+    if (mUnfinishedInput)
42
+        delete [] mUnfinishedInput;
43
+
39 44
     while (mHistory.size() > 0) {
40 45
         char *tmp = mHistory.back();
41 46
         if (tmp != NULL)
42 47
             delete [] tmp;
43 48
         mHistory.pop_back();
44 49
     }
50
+
51
+    while (mCommandHistory.size() > 0) {
52
+        char *tmp = mCommandHistory.back();
53
+        if (tmp != NULL)
54
+            delete [] tmp;
55
+        mCommandHistory.pop_back();
56
+    }
45 57
 }
46 58
 
47 59
 void Console::setVisible(bool visible) {
@@ -121,6 +133,7 @@ void Console::handleKeyboard(KeyboardButton key, bool pressed) {
121 133
         // Execute entered command
122 134
         if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
123 135
             mHistory.push_back(bufferString("> %s", mInputBuffer));
136
+            mCommandHistory.push_back(bufferString("%s", mInputBuffer));
124 137
             gOpenRaider->command(mInputBuffer);
125 138
         }
126 139
 
@@ -131,6 +144,8 @@ void Console::handleKeyboard(KeyboardButton key, bool pressed) {
131 144
             delete [] mPartialInput;
132 145
             mPartialInput = NULL;
133 146
         }
147
+
148
+        mHistoryPointer = 0;
134 149
     }
135 150
 
136 151
     //! \fixme only deleting the last byte is not valid for non-ASCII UTF-8 strings
@@ -140,6 +155,46 @@ void Console::handleKeyboard(KeyboardButton key, bool pressed) {
140 155
             mInputBuffer[mInputBufferPointer] = '\0';
141 156
         }
142 157
     }
158
+
159
+    if (pressed && ((key == up) || (key == down))) {
160
+        moveInHistory(key == up);
161
+    }
162
+}
163
+
164
+void Console::moveInHistory(bool up) {
165
+    if (mCommandHistory.size() == 0)
166
+        return;
167
+
168
+    if (up) {
169
+        if (mHistoryPointer < mCommandHistory.size()) {
170
+            mHistoryPointer++;
171
+            if (mHistoryPointer == 1) {
172
+                mUnfinishedInput = bufferString("%s", mInputBuffer);
173
+            }
174
+        } else {
175
+            return;
176
+        }
177
+    } else {
178
+        if (mHistoryPointer > 0)
179
+            mHistoryPointer--;
180
+        else
181
+            return;
182
+    }
183
+
184
+    if ((mHistoryPointer > 0) && (mHistoryPointer <= mCommandHistory.size())) {
185
+        strcpy(mInputBuffer, mCommandHistory[mCommandHistory.size() - mHistoryPointer]);
186
+        mInputBufferPointer = strlen(mInputBuffer);
187
+    } else {
188
+        if (mUnfinishedInput != NULL) {
189
+            strcpy(mInputBuffer, mUnfinishedInput);
190
+            mInputBufferPointer = strlen(mInputBuffer);
191
+            delete [] mUnfinishedInput;
192
+            mUnfinishedInput = NULL;
193
+        } else {
194
+            mInputBuffer[0] = '\0';
195
+            mInputBufferPointer = 0;
196
+        }
197
+    }
143 198
 }
144 199
 
145 200
 void Console::handleText(char *text, bool notFinished) {

Loading…
Cancel
Save