Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Shell.cc @ 1755

Last change on this file since 1755 was 1755, checked in by rgrieder, 16 years ago

merged gui back to trunk.
update the media repository!

  • Property svn:eol-style set to native
File size: 11.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 "Core.h"
34#include "ConsoleCommand.h"
35#include "util/OutputHandler.h"
36
37#define SHELL_UPDATE_LISTENERS(function) \
38    for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) \
39        (*(it++))->function()
40
41namespace orxonox
42{
43    SetConsoleCommand(Shell, clearShell, true);
44    SetConsoleCommand(Shell, history, true);
45
46    SetConsoleCommandShortcut(OutputHandler, log);
47    SetConsoleCommandShortcut(OutputHandler, error);
48    SetConsoleCommandShortcut(OutputHandler, warning);
49    SetConsoleCommandShortcut(OutputHandler, info);
50    SetConsoleCommandShortcut(OutputHandler, debug);
51
52    Shell::Shell()
53    {
54        RegisterRootObject(Shell);
55
56        this->scrollPosition_ = 0;
57        this->maxHistoryLength_ = 100;
58        this->historyPosition_ = 0;
59        this->historyOffset_ = 0;
60        this->finishedLastLine_ = true;
61        this->bAddOutputLevel_ = false;
62
63        this->clearLines();
64
65        this->inputBuffer_ = new InputBuffer();
66        this->configureInputBuffer();
67
68        this->outputBuffer_.registerListener(this);
69        OutputHandler::getOutStream().setOutputBuffer(this->outputBuffer_);
70
71        this->setConfigValues();
72    }
73
74    Shell::~Shell()
75    {
76        if (this->inputBuffer_)
77            delete this->inputBuffer_;
78    }
79
80    Shell& Shell::createShell()
81    {
82        int level = Core::getSoftDebugLevel(OutputHandler::LD_Shell);
83        Core::setSoftDebugLevel(OutputHandler::LD_Shell, -1);
84        static Shell instance;
85        Core::setSoftDebugLevel(OutputHandler::LD_Shell, level);
86        return instance;
87    }
88
89    Shell& Shell::getInstance()
90    {
91        static Shell& instance = createShell();
92        return instance;
93    }
94
95    void Shell::setConfigValues()
96    {
97        SetConfigValue(maxHistoryLength_, 100).callback(this, &Shell::commandHistoryLengthChanged);
98        SetConfigValue(historyOffset_, 0).callback(this, &Shell::commandHistoryOffsetChanged);
99        SetConfigValueVector(commandHistory_, std::vector<std::string>());
100    }
101
102    void Shell::commandHistoryOffsetChanged()
103    {
104        if (this->historyOffset_ >= this->maxHistoryLength_)
105            this->historyOffset_ = 0;
106    }
107
108    void Shell::commandHistoryLengthChanged()
109    {
110        this->commandHistoryOffsetChanged();
111
112        while (this->commandHistory_.size() > this->maxHistoryLength_)
113        {
114            unsigned int index = this->commandHistory_.size() - 1;
115            this->commandHistory_.erase(this->commandHistory_.begin() + index);
116            ModifyConfigValue(commandHistory_, remove, index);
117        }
118    }
119
120    void Shell::configureInputBuffer()
121    {
122        this->inputBuffer_->registerListener(this, &Shell::inputChanged, true);
123        this->inputBuffer_->registerListener(this, &Shell::execute, '\r', false);
124        this->inputBuffer_->registerListener(this, &Shell::hintandcomplete, '\t', true);
125        this->inputBuffer_->registerListener(this, &Shell::backspace, '\b', true);
126        this->inputBuffer_->registerListener(this, &Shell::deletechar, KeyCode::Delete);
127        this->inputBuffer_->registerListener(this, &Shell::exit, (char)27, true);
128        this->inputBuffer_->registerListener(this, &Shell::cursor_right, KeyCode::Right);
129        this->inputBuffer_->registerListener(this, &Shell::cursor_left, KeyCode::Left);
130        this->inputBuffer_->registerListener(this, &Shell::cursor_end, KeyCode::End);
131        this->inputBuffer_->registerListener(this, &Shell::cursor_home, KeyCode::Home);
132        this->inputBuffer_->registerListener(this, &Shell::history_up, KeyCode::Up);
133        this->inputBuffer_->registerListener(this, &Shell::history_down, KeyCode::Down);
134        this->inputBuffer_->registerListener(this, &Shell::scroll_up, KeyCode::PageUp);
135        this->inputBuffer_->registerListener(this, &Shell::scroll_down, KeyCode::PageDown);
136    }
137
138    void Shell::clearShell()
139    {
140        Shell::getInstance().clearLines();
141    }
142
143    void Shell::history()
144    {
145        Shell& instance = Shell::getInstance();
146
147        for (int i = instance.historyOffset_; i < (int)instance.commandHistory_.size(); ++i)
148            instance.addLine(instance.commandHistory_[i], -1);
149        for (int i =  0; i < (int)instance.historyOffset_; ++i)
150            instance.addLine(instance.commandHistory_[i], -1);
151    }
152
153    void Shell::registerListener(ShellListener* listener)
154    {
155        this->listeners_.insert(this->listeners_.end(), listener);
156    }
157
158    void Shell::unregisterListener(ShellListener* listener)
159    {
160        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
161        {
162            if ((*it) == listener)
163                this->listeners_.erase(it++);
164            else
165                ++it;
166        }
167    }
168
169    void Shell::setCursorPosition(unsigned int cursor)
170    {
171        this->inputBuffer_->setCursorPosition(cursor);
172        SHELL_UPDATE_LISTENERS(cursorChanged);
173    }
174
175    void Shell::setInput(const std::string& input)
176    {
177        this->inputBuffer_->set(input);
178        this->inputChanged();
179    }
180
181    void Shell::addLine(const std::string& line, int level)
182    {
183        int original_level = OutputHandler::getOutStream().getOutputLevel();
184        OutputHandler::getOutStream().setOutputLevel(level);
185
186        if (!this->finishedLastLine_)
187            this->outputBuffer_ << std::endl;
188
189        this->outputBuffer_ << line << std::endl;
190        OutputHandler::getOutStream().setOutputLevel(original_level);
191    }
192
193    void Shell::clearLines()
194    {
195        this->lines_.clear();
196        this->scrollIterator_ = this->lines_.begin();
197
198        this->scrollPosition_ = 0;
199        this->finishedLastLine_ = true;
200
201        SHELL_UPDATE_LISTENERS(linesChanged);
202    }
203
204    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
205    {
206        if (this->scrollPosition_)
207            return this->scrollIterator_;
208        else
209            return this->lines_.begin();
210    }
211
212    std::list<std::string>::const_iterator Shell::getEndIterator() const
213    {
214        return this->lines_.end();
215    }
216
217    void Shell::addToHistory(const std::string& command)
218    {
219        ModifyConfigValue(commandHistory_, set, this->historyOffset_, command);
220        this->historyPosition_ = 0;
221        ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_);
222    }
223
224    std::string Shell::getFromHistory() const
225    {
226        unsigned int index = mod(((int)this->historyOffset_) - ((int)this->historyPosition_), this->maxHistoryLength_);
227        if (index < this->commandHistory_.size() && this->historyPosition_ != 0)
228            return this->commandHistory_[index];
229        else
230            return "";
231    }
232
233    void Shell::outputChanged()
234    {
235        std::string output;
236        bool newline;
237        do
238        {
239            newline = this->outputBuffer_.getLine(&output);
240
241            if (!newline && output == "")
242                break;
243
244            if (this->finishedLastLine_)
245            {
246                if (this->bAddOutputLevel_)
247                    output.insert(0, 1, (char)OutputHandler::getOutStream().getOutputLevel());
248
249                this->lines_.insert(this->lines_.begin(), output);
250
251                if (this->scrollPosition_)
252                    this->scrollPosition_++;
253                else
254                    this->scrollIterator_ = this->lines_.begin();
255
256                this->finishedLastLine_ = newline;
257
258                if (!this->scrollPosition_)
259                {
260                    SHELL_UPDATE_LISTENERS(lineAdded);
261                }
262            }
263            else
264            {
265                (*this->lines_.begin()) += output;
266                this->finishedLastLine_ = newline;
267                SHELL_UPDATE_LISTENERS(onlyLastLineChanged);
268            }
269
270        } while (newline);
271    }
272
273    void Shell::inputChanged()
274    {
275        SHELL_UPDATE_LISTENERS(inputChanged);
276        SHELL_UPDATE_LISTENERS(cursorChanged);
277    }
278
279    void Shell::execute()
280    {
281        this->addToHistory(this->inputBuffer_->get());
282        this->addLine(this->inputBuffer_->get(), 0);
283
284        if (!CommandExecutor::execute(this->inputBuffer_->get()))
285            this->addLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
286
287        this->clear();
288    }
289
290    void Shell::hintandcomplete()
291    {
292        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
293        this->addLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
294
295        this->inputChanged();
296    }
297
298    void Shell::backspace()
299    {
300        this->inputBuffer_->removeBehindCursor();
301        SHELL_UPDATE_LISTENERS(inputChanged);
302        SHELL_UPDATE_LISTENERS(cursorChanged);
303    }
304
305    void Shell::deletechar()
306    {
307        this->inputBuffer_->removeAtCursor();
308        SHELL_UPDATE_LISTENERS(inputChanged);
309    }
310
311    void Shell::clear()
312    {
313        this->inputBuffer_->clear();
314        this->historyPosition_ = 0;
315        SHELL_UPDATE_LISTENERS(inputChanged);
316        SHELL_UPDATE_LISTENERS(cursorChanged);
317    }
318
319    void Shell::cursor_right()
320    {
321        this->inputBuffer_->increaseCursor();
322        SHELL_UPDATE_LISTENERS(cursorChanged);
323    }
324
325    void Shell::cursor_left()
326    {
327        this->inputBuffer_->decreaseCursor();
328        SHELL_UPDATE_LISTENERS(cursorChanged);
329    }
330
331    void Shell::cursor_end()
332    {
333        this->inputBuffer_->setCursorToEnd();
334        SHELL_UPDATE_LISTENERS(cursorChanged);
335    }
336
337    void Shell::cursor_home()
338    {
339        this->inputBuffer_->setCursorToBegin();
340        SHELL_UPDATE_LISTENERS(cursorChanged);
341    }
342
343    void Shell::history_up()
344    {
345        if (this->historyPosition_ < this->commandHistory_.size())
346        {
347            this->historyPosition_++;
348            this->inputBuffer_->set(this->getFromHistory());
349        }
350    }
351
352    void Shell::history_down()
353    {
354        if (this->historyPosition_ > 0)
355        {
356            this->historyPosition_--;
357            this->inputBuffer_->set(this->getFromHistory());
358        }
359    }
360
361    void Shell::scroll_up()
362    {
363        if (this->scrollIterator_ != this->lines_.end())
364        {
365            ++this->scrollIterator_;
366            ++this->scrollPosition_;
367
368            SHELL_UPDATE_LISTENERS(linesChanged);
369        }
370    }
371
372    void Shell::scroll_down()
373    {
374        if (this->scrollIterator_ != this->lines_.begin())
375        {
376            --this->scrollIterator_;
377            --this->scrollPosition_;
378
379            SHELL_UPDATE_LISTENERS(linesChanged);
380        }
381    }
382
383    void Shell::exit()
384    {
385        if (this->inputBuffer_->getSize() > 0)
386        {
387            this->clear();
388            return;
389        }
390
391        this->clear();
392        this->scrollPosition_ = 0;
393        this->scrollIterator_ = this->lines_.begin();
394
395        SHELL_UPDATE_LISTENERS(exit);
396    }
397}
Note: See TracBrowser for help on using the repository browser.