Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1417 was 1334, checked in by landauf, 16 years ago
  • added colored output in the InGameConsole, depending on the output level (error = red, …).
  • increased performance of InGameConsole and Shell
File size: 10.6 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#include "CoreSettings.h"
34
35#define SHELL_UPDATE_LISTENERS(function) \
36    for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) \
37        (*it)->function()
38
39namespace orxonox
40{
41    Shell::Shell()
42    {
43        RegisterRootObject(Shell);
44
45        this->scrollPosition_ = 0;
46        this->maxHistoryLength_ = 100;
47        this->historyPosition_ = 0;
48        this->historyOffset_ = 0;
49        this->finishedLastLine_ = true;
50        this->bAddOutputLevel_ = false;
51
52        this->clearLines();
53
54        this->inputBuffer_.registerListener(this, &Shell::inputChanged, true);
55        this->inputBuffer_.registerListener(this, &Shell::execute, '\r', false);
56        this->inputBuffer_.registerListener(this, &Shell::hintandcomplete, '\t', true);
57        this->inputBuffer_.registerListener(this, &Shell::backspace, '\b', true);
58        this->inputBuffer_.registerListener(this, &Shell::deletechar, OIS::KC_DELETE);
59        this->inputBuffer_.registerListener(this, &Shell::exit, (char)27, true);
60        this->inputBuffer_.registerListener(this, &Shell::cursor_right, OIS::KC_RIGHT);
61        this->inputBuffer_.registerListener(this, &Shell::cursor_left, OIS::KC_LEFT);
62        this->inputBuffer_.registerListener(this, &Shell::cursor_end, OIS::KC_END);
63        this->inputBuffer_.registerListener(this, &Shell::cursor_home, OIS::KC_HOME);
64        this->inputBuffer_.registerListener(this, &Shell::history_up, OIS::KC_UP);
65        this->inputBuffer_.registerListener(this, &Shell::history_down, OIS::KC_DOWN);
66        this->inputBuffer_.registerListener(this, &Shell::scroll_up, OIS::KC_PGUP);
67        this->inputBuffer_.registerListener(this, &Shell::scroll_down, OIS::KC_PGDOWN);
68
69        this->outputBuffer_.registerListener(this);
70
71        this->setConfigValues();
72    }
73
74    Shell& Shell::createShell()
75    {
76        int level = CoreSettings::getSoftDebugLevel(OutputHandler::LD_Shell);
77        CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, -1);
78        static Shell instance;
79        CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, level);
80        return instance;
81    }
82
83    Shell& Shell::getInstance()
84    {
85        static Shell& instance = createShell();
86        return instance;
87    }
88
89    void Shell::setConfigValues()
90    {
91        SetConfigValue(maxHistoryLength_, 100);
92        SetConfigValue(historyOffset_, 0);
93        SetConfigValueVector(commandHistory_, std::vector<std::string>());
94
95        if (this->historyOffset_ >= this->maxHistoryLength_)
96            this->historyOffset_ = 0;
97
98        while (this->commandHistory_.size() > this->maxHistoryLength_)
99        {
100            unsigned int index = this->commandHistory_.size() - 1;
101            this->commandHistory_.erase(this->commandHistory_.begin() + index);
102            ModifyConfigValue(commandHistory_, remove, index);
103        }
104    }
105
106    void Shell::registerListener(ShellListener* listener)
107    {
108        this->listeners_.insert(this->listeners_.end(), listener);
109    }
110
111    void Shell::unregisterListener(ShellListener* listener)
112    {
113        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
114        {
115            if ((*it) == listener)
116                this->listeners_.erase(it++);
117            else
118                ++it;
119        }
120    }
121
122    void Shell::setCursorPosition(unsigned int cursor)
123    {
124        this->inputBuffer_.setCursorPosition(cursor);
125        SHELL_UPDATE_LISTENERS(cursorChanged);
126    }
127
128    void Shell::setInput(const std::string& input)
129    {
130        this->inputBuffer_.set(input);
131        this->inputChanged();
132    }
133
134    void Shell::addLine(const std::string& line, int level)
135    {
136        int original_level = OutputHandler::getOutStream().getOutputLevel();
137        OutputHandler::getOutStream().setOutputLevel(level);
138
139        if (!this->finishedLastLine_)
140            this->outputBuffer_ << std::endl;
141
142        this->outputBuffer_ << line << std::endl;
143        OutputHandler::getOutStream().setOutputLevel(original_level);
144    }
145
146    void Shell::clearLines()
147    {
148        this->lines_.clear();
149        this->scrollIterator_ = this->lines_.begin();
150
151        this->scrollPosition_ = 0;
152        this->finishedLastLine_ = true;
153
154        SHELL_UPDATE_LISTENERS(linesChanged);
155    }
156
157    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
158    {
159        if (this->scrollPosition_)
160            return this->scrollIterator_;
161        else
162            return this->lines_.begin();
163    }
164
165    std::list<std::string>::const_iterator Shell::getEndIterator() const
166    {
167        return this->lines_.end();
168    }
169
170    void Shell::addToHistory(const std::string& command)
171    {
172        ModifyConfigValue(commandHistory_, set, this->historyOffset_, command);
173        this->historyPosition_ = 0;
174        ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_);
175    }
176
177    std::string Shell::getFromHistory() const
178    {
179        unsigned int index = mod(((int)this->historyOffset_) - ((int)this->historyPosition_), this->maxHistoryLength_);
180        if (index < this->commandHistory_.size() && this->historyPosition_ != 0)
181            return this->commandHistory_[index];
182        else
183            return "";
184    }
185
186    void Shell::outputChanged()
187    {
188        std::string output;
189        bool newline;
190        do
191        {
192            newline = this->outputBuffer_.getLine(&output);
193
194            if (!newline && output == "")
195                break;
196
197            if (this->finishedLastLine_)
198            {
199                if (this->bAddOutputLevel_)
200                    output.insert(0, 1, (char)OutputHandler::getOutStream().getOutputLevel());
201
202                this->lines_.insert(this->lines_.begin(), output);
203
204                if (this->scrollPosition_)
205                    this->scrollPosition_++;
206                else
207                    this->scrollIterator_ = this->lines_.begin();
208
209                this->finishedLastLine_ = newline;
210
211                if (!this->scrollPosition_)
212                {
213                    SHELL_UPDATE_LISTENERS(lineAdded);
214                }
215            }
216            else
217            {
218                (*this->lines_.begin()) += output;
219                this->finishedLastLine_ = newline;
220                SHELL_UPDATE_LISTENERS(onlyLastLineChanged);
221            }
222
223        } while (newline);
224    }
225
226    void Shell::inputChanged()
227    {
228        SHELL_UPDATE_LISTENERS(inputChanged);
229        SHELL_UPDATE_LISTENERS(cursorChanged);
230    }
231
232    void Shell::execute()
233    {
234        this->addToHistory(this->inputBuffer_.get());
235        this->addLine(this->inputBuffer_.get(), 0);
236
237        if (!CommandExecutor::execute(this->inputBuffer_.get()))
238            this->addLine("Error: Can't execute \"" + this->inputBuffer_.get() + "\".", 1);
239
240        this->clear();
241    }
242
243    void Shell::hintandcomplete()
244    {
245        this->addLine(CommandExecutor::hint(this->inputBuffer_.get()), -1);
246        this->inputBuffer_.set(CommandExecutor::complete(this->inputBuffer_.get()));
247
248        this->inputChanged();
249    }
250
251    void Shell::backspace()
252    {
253        this->inputBuffer_.removeBehindCursor();
254        SHELL_UPDATE_LISTENERS(inputChanged);
255        SHELL_UPDATE_LISTENERS(cursorChanged);
256    }
257
258    void Shell::deletechar()
259    {
260        this->inputBuffer_.removeAtCursor();
261        SHELL_UPDATE_LISTENERS(inputChanged);
262    }
263
264    void Shell::clear()
265    {
266        this->inputBuffer_.clear();
267        this->historyPosition_ = 0;
268        SHELL_UPDATE_LISTENERS(inputChanged);
269        SHELL_UPDATE_LISTENERS(cursorChanged);
270    }
271
272    void Shell::cursor_right()
273    {
274        this->inputBuffer_.increaseCursor();
275        SHELL_UPDATE_LISTENERS(cursorChanged);
276    }
277
278    void Shell::cursor_left()
279    {
280        this->inputBuffer_.decreaseCursor();
281        SHELL_UPDATE_LISTENERS(cursorChanged);
282    }
283
284    void Shell::cursor_end()
285    {
286        this->inputBuffer_.setCursorToEnd();
287        SHELL_UPDATE_LISTENERS(cursorChanged);
288    }
289
290    void Shell::cursor_home()
291    {
292        this->inputBuffer_.setCursorToBegin();
293        SHELL_UPDATE_LISTENERS(cursorChanged);
294    }
295
296    void Shell::history_up()
297    {
298        if (this->historyPosition_ < this->commandHistory_.size())
299        {
300            this->historyPosition_++;
301            this->inputBuffer_.set(this->getFromHistory());
302        }
303    }
304
305    void Shell::history_down()
306    {
307        if (this->historyPosition_ > 0)
308        {
309            this->historyPosition_--;
310            this->inputBuffer_.set(this->getFromHistory());
311        }
312    }
313
314    void Shell::scroll_up()
315    {
316        if (this->scrollIterator_ != this->lines_.end())
317        {
318            ++this->scrollIterator_;
319            ++this->scrollPosition_;
320
321            SHELL_UPDATE_LISTENERS(linesChanged);
322        }
323    }
324
325    void Shell::scroll_down()
326    {
327        if (this->scrollIterator_ != this->lines_.begin())
328        {
329            --this->scrollIterator_;
330            --this->scrollPosition_;
331
332            SHELL_UPDATE_LISTENERS(linesChanged);
333        }
334    }
335
336    void Shell::exit()
337    {
338        if (this->inputBuffer_.getSize() > 0)
339        {
340            this->clear();
341            return;
342        }
343
344        this->clear();
345        this->scrollPosition_ = 0;
346        this->scrollIterator_ = this->lines_.begin();
347
348        SHELL_UPDATE_LISTENERS(exit);
349    }
350}
Note: See TracBrowser for help on using the repository browser.