Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed crash when displaying debug-level 4 and 5 in the InGameConsole. It works perfectly now, although it's not recommended, because you won't see much of your own input in the console any more ;)
however, performance is quite good. maybe i can improve it even more.

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