Procházet zdrojové kódy

Added proper command history for console

Thomas Buck před 11 roky
rodič
revize
62af5ce2ba
2 změnil soubory, kde provedl 61 přidání a 0 odebrání
  1. 6
    0
      include/Console.h
  2. 55
    0
      src/Console.cpp

+ 6
- 0
include/Console.h Zobrazit soubor

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

+ 55
- 0
src/Console.cpp Zobrazit soubor

27
     mInputBuffer[INPUT_BUFFER_SIZE] = '\0';
27
     mInputBuffer[INPUT_BUFFER_SIZE] = '\0';
28
     mInputBufferPointer = 0;
28
     mInputBufferPointer = 0;
29
     mPartialInput = NULL;
29
     mPartialInput = NULL;
30
+    mHistoryPointer = 0;
31
+    mUnfinishedInput = NULL;
30
 }
32
 }
31
 
33
 
32
 Console::~Console() {
34
 Console::~Console() {
36
     if (mPartialInput)
38
     if (mPartialInput)
37
         delete [] mPartialInput;
39
         delete [] mPartialInput;
38
 
40
 
41
+    if (mUnfinishedInput)
42
+        delete [] mUnfinishedInput;
43
+
39
     while (mHistory.size() > 0) {
44
     while (mHistory.size() > 0) {
40
         char *tmp = mHistory.back();
45
         char *tmp = mHistory.back();
41
         if (tmp != NULL)
46
         if (tmp != NULL)
42
             delete [] tmp;
47
             delete [] tmp;
43
         mHistory.pop_back();
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
 void Console::setVisible(bool visible) {
59
 void Console::setVisible(bool visible) {
121
         // Execute entered command
133
         // Execute entered command
122
         if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
134
         if ((mInputBufferPointer > 0) && (mInputBuffer[0] != '\0')) {
123
             mHistory.push_back(bufferString("> %s", mInputBuffer));
135
             mHistory.push_back(bufferString("> %s", mInputBuffer));
136
+            mCommandHistory.push_back(bufferString("%s", mInputBuffer));
124
             gOpenRaider->command(mInputBuffer);
137
             gOpenRaider->command(mInputBuffer);
125
         }
138
         }
126
 
139
 
131
             delete [] mPartialInput;
144
             delete [] mPartialInput;
132
             mPartialInput = NULL;
145
             mPartialInput = NULL;
133
         }
146
         }
147
+
148
+        mHistoryPointer = 0;
134
     }
149
     }
135
 
150
 
136
     //! \fixme only deleting the last byte is not valid for non-ASCII UTF-8 strings
151
     //! \fixme only deleting the last byte is not valid for non-ASCII UTF-8 strings
140
             mInputBuffer[mInputBufferPointer] = '\0';
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
 void Console::handleText(char *text, bool notFinished) {
200
 void Console::handleText(char *text, bool notFinished) {

Loading…
Zrušit
Uložit