Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/Shell.cc @ 1324

Last change on this file since 1324 was 1324, checked in by landauf, 16 years ago

Added a command-history to the console. you can scroll through all previously entered commands by pressing [up] key.
The history is stored in orxonox.ini in the section [Shell]. You can configure the maximal amount of stored commands (default: 100).
This allows you to use the history after orxonox was restarted.
The history uses a cyclic vector (with the configured size), meaning the newest command will overwrite the oldest entry (if the maximal size was reached).

additionally I fixed some bugs in ConfigValueContainer and added a mod-function to Math.h, that does basically the same as %, but without returning a negative value in case of a negative input.

-1 % 10 = -1
-11 % 10 = -1

mod(-1, 10) = 9
mod(-11, 10) = 9

File size: 9.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Shell.h"
30#include "CommandExecutor.h"
31#include "CoreIncludes.h"
32#include "ConfigValueIncludes.h"
33
34#define SHELL_UPDATE_LISTENERS(function) \
35    for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) \
36        (*it)->function()
37
38namespace orxonox
39{
40    Shell::Shell()
41    {
42        RegisterRootObject(Shell);
43
44        this->scrollPosition_ = 0;
45        this->maxHistoryLength_ = 100;
46        this->historyPosition_ = 0;
47        this->historyOffset_ = 0;
48        this->finishedLastLine_ = true;
49
50        this->clearLines();
51
52        this->inputBuffer_.registerListener(this, &Shell::inputChanged, true);
53        this->inputBuffer_.registerListener(this, &Shell::execute, '\r', false);
54        this->inputBuffer_.registerListener(this, &Shell::hintandcomplete, '\t', true);
55        this->inputBuffer_.registerListener(this, &Shell::backspace, '\b', true);
56        this->inputBuffer_.registerListener(this, &Shell::deletechar, OIS::KC_DELETE);
57        this->inputBuffer_.registerListener(this, &Shell::exit, (char)27, true);
58        this->inputBuffer_.registerListener(this, &Shell::cursor_right, OIS::KC_RIGHT);
59        this->inputBuffer_.registerListener(this, &Shell::cursor_left, OIS::KC_LEFT);
60        this->inputBuffer_.registerListener(this, &Shell::cursor_end, OIS::KC_END);
61        this->inputBuffer_.registerListener(this, &Shell::cursor_home, OIS::KC_HOME);
62        this->inputBuffer_.registerListener(this, &Shell::history_up, OIS::KC_UP);
63        this->inputBuffer_.registerListener(this, &Shell::history_down, OIS::KC_DOWN);
64        this->inputBuffer_.registerListener(this, &Shell::scroll_up, OIS::KC_PGUP);
65        this->inputBuffer_.registerListener(this, &Shell::scroll_down, OIS::KC_PGDOWN);
66
67        this->outputBuffer_.registerListener(this);
68
69        this->setConfigValues();
70    }
71
72    Shell& Shell::getInstance()
73    {
74        static Shell instance;
75        return instance;
76    }
77
78    void Shell::setConfigValues()
79    {
80        SetConfigValue(maxHistoryLength_, 100);
81        SetConfigValue(historyOffset_, 0);
82        SetConfigValueVector(commandHistory_, std::vector<std::string>(1, ""));
83
84        if (this->historyOffset_ >= this->maxHistoryLength_)
85            this->historyOffset_ = 0;
86
87        while (this->commandHistory_.size() > this->maxHistoryLength_)
88        {
89            unsigned int index = this->commandHistory_.size() - 1;
90            this->commandHistory_.erase(this->commandHistory_.begin() + index);
91            ModifyConfigValue(commandHistory_, remove, index);
92        }
93    }
94
95    void Shell::registerListener(ShellListener* listener)
96    {
97        this->listeners_.insert(this->listeners_.end(), listener);
98    }
99
100    void Shell::unregisterListener(ShellListener* listener)
101    {
102        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
103        {
104            if ((*it) == listener)
105                this->listeners_.erase(it++);
106            else
107                ++it;
108        }
109    }
110
111    void Shell::setCursorPosition(unsigned int cursor)
112    {
113        this->inputBuffer_.setCursorPosition(cursor);
114        SHELL_UPDATE_LISTENERS(cursorChanged);
115    }
116
117    void Shell::setInput(const std::string& input)
118    {
119        this->inputBuffer_.set(input);
120        this->inputChanged();
121    }
122
123    void Shell::addLine(const std::string& line, unsigned int level)
124    {
125        int original_level = OutputHandler::getOutStream().getOutputLevel();
126        OutputHandler::getOutStream().setOutputLevel(level);
127
128        if (!this->finishedLastLine_)
129            this->outputBuffer_ << std::endl;
130
131        this->outputBuffer_ << line << std::endl;
132        OutputHandler::getOutStream().setOutputLevel(original_level);
133    }
134
135    void Shell::clearLines()
136    {
137        this->lines_.clear();
138        this->scrollIterator_ = this->lines_.begin();
139
140        this->scrollPosition_ = 0;
141        this->finishedLastLine_ = true;
142
143        SHELL_UPDATE_LISTENERS(linesChanged);
144    }
145
146    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
147    {
148        if (this->scrollPosition_)
149            return this->scrollIterator_;
150        else
151            return this->lines_.begin();
152    }
153
154    std::list<std::string>::const_iterator Shell::getEndIterator() const
155    {
156        return this->lines_.end();
157    }
158
159    void Shell::addToHistory(const std::string& command)
160    {
161        ModifyConfigValue(commandHistory_, set, this->historyOffset_, command);
162        this->historyPosition_ = 0;
163        ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_);
164    }
165
166    std::string Shell::getFromHistory() const
167    {
168        unsigned int index = mod(((int)this->historyOffset_) - ((int)this->historyPosition_), this->maxHistoryLength_);
169        if (index < this->commandHistory_.size() && this->historyPosition_ != 0)
170            return this->commandHistory_[index];
171        else
172            return "";
173    }
174
175    void Shell::outputChanged()
176    {
177        std::string output;
178        bool newline;
179        do
180        {
181            newline = this->outputBuffer_.getLine(&output);
182
183            if (!newline && output == "")
184                break;
185
186            if (this->finishedLastLine_)
187            {
188                this->lines_.insert(this->lines_.begin(), output);
189
190                if (this->scrollPosition_)
191                    this->scrollPosition_++;
192                else
193                    this->scrollIterator_ = this->lines_.begin();
194
195                this->finishedLastLine_ = newline;
196                SHELL_UPDATE_LISTENERS(lineAdded);
197            }
198            else
199            {
200                (*this->lines_.begin()) += output;
201                this->finishedLastLine_ = newline;
202                SHELL_UPDATE_LISTENERS(onlyLastLineChanged);
203            }
204
205        } while (newline);
206    }
207
208    void Shell::inputChanged()
209    {
210        SHELL_UPDATE_LISTENERS(inputChanged);
211        SHELL_UPDATE_LISTENERS(cursorChanged);
212    }
213
214    void Shell::execute()
215    {
216        this->addToHistory(this->inputBuffer_.get());
217        this->addLine(this->inputBuffer_.get(), 0);
218
219        if (!CommandExecutor::execute(this->inputBuffer_.get()))
220            this->addLine("Error: Can't execute \"" + this->inputBuffer_.get() + "\".", 1);
221
222        this->clear();
223    }
224
225    void Shell::hintandcomplete()
226    {
227        this->addLine(CommandExecutor::hint(this->inputBuffer_.get()), 0);
228        this->inputBuffer_.set(CommandExecutor::complete(this->inputBuffer_.get()));
229
230        this->inputChanged();
231    }
232
233    void Shell::backspace()
234    {
235        this->inputBuffer_.removeBehindCursor();
236        SHELL_UPDATE_LISTENERS(inputChanged);
237        SHELL_UPDATE_LISTENERS(cursorChanged);
238    }
239
240    void Shell::deletechar()
241    {
242        this->inputBuffer_.removeAtCursor();
243        SHELL_UPDATE_LISTENERS(inputChanged);
244    }
245
246    void Shell::clear()
247    {
248        this->inputBuffer_.clear();
249        this->historyPosition_ = 0;
250        SHELL_UPDATE_LISTENERS(inputChanged);
251        SHELL_UPDATE_LISTENERS(cursorChanged);
252    }
253
254    void Shell::cursor_right()
255    {
256        this->inputBuffer_.increaseCursor();
257        SHELL_UPDATE_LISTENERS(cursorChanged);
258    }
259
260    void Shell::cursor_left()
261    {
262        this->inputBuffer_.decreaseCursor();
263        SHELL_UPDATE_LISTENERS(cursorChanged);
264    }
265
266    void Shell::cursor_end()
267    {
268        this->inputBuffer_.setCursorToEnd();
269        SHELL_UPDATE_LISTENERS(cursorChanged);
270    }
271
272    void Shell::cursor_home()
273    {
274        this->inputBuffer_.setCursorToBegin();
275        SHELL_UPDATE_LISTENERS(cursorChanged);
276    }
277
278    void Shell::history_up()
279    {
280        if (this->historyPosition_ < this->commandHistory_.size())
281        {
282            this->historyPosition_++;
283            this->inputBuffer_.set(this->getFromHistory());
284        }
285    }
286
287    void Shell::history_down()
288    {
289        if (this->historyPosition_ > 0)
290        {
291            this->historyPosition_--;
292            this->inputBuffer_.set(this->getFromHistory());
293        }
294    }
295
296    void Shell::scroll_up()
297    {
298        if (this->scrollIterator_ != this->lines_.end())
299        {
300            ++this->scrollIterator_;
301            ++this->scrollPosition_;
302
303            SHELL_UPDATE_LISTENERS(linesChanged);
304        }
305    }
306
307    void Shell::scroll_down()
308    {
309        if (this->scrollIterator_ != this->lines_.begin())
310        {
311            --this->scrollIterator_;
312            --this->scrollPosition_;
313
314            SHELL_UPDATE_LISTENERS(linesChanged);
315        }
316    }
317
318    void Shell::exit()
319    {
320        if (this->inputBuffer_.getSize() > 0)
321        {
322            this->clear();
323            return;
324        }
325
326        this->clear();
327        SHELL_UPDATE_LISTENERS(exit);
328    }
329}
Note: See TracBrowser for help on using the repository browser.