Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/libraries/core/Shell.cc @ 6178

Last change on this file since 6178 was 6139, checked in by rgrieder, 15 years ago

Fixed a bug in Shell and changed parental order in InGameConsole.

  • Property svn:eol-style set to native
File size: 15.5 KB
RevLine 
[1505]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:
[6105]25 *      Reto Grieder
[1505]26 *
27 */
28
29#include "Shell.h"
[3196]30
31#include "util/OutputHandler.h"
[6105]32#include "util/StringUtils.h"
33#include "util/SubString.h"
[1505]34#include "CommandExecutor.h"
35#include "CoreIncludes.h"
36#include "ConfigValueIncludes.h"
37#include "ConsoleCommand.h"
38
39namespace orxonox
40{
[1747]41    SetConsoleCommandShortcut(OutputHandler, log);
42    SetConsoleCommandShortcut(OutputHandler, error);
43    SetConsoleCommandShortcut(OutputHandler, warning);
44    SetConsoleCommandShortcut(OutputHandler, info);
45    SetConsoleCommandShortcut(OutputHandler, debug);
46
[6105]47    Shell::Shell(const std::string& consoleName, bool bScrollable, bool bPrependOutputLevel)
48        : OutputListener(consoleName)
49        , inputBuffer_(new InputBuffer())
50        , consoleName_(consoleName)
51        , bPrependOutputLevel_(bPrependOutputLevel)
52        , bScrollable_(bScrollable)
[1505]53    {
54        RegisterRootObject(Shell);
55
56        this->scrollPosition_ = 0;
57        this->maxHistoryLength_ = 100;
58        this->historyPosition_ = 0;
59        this->historyOffset_ = 0;
[6105]60        this->bFinishedLastLine_ = true;
[1505]61
[6105]62        this->clearOutput();
[1755]63        this->configureInputBuffer();
[1505]64
[3280]65        // Get a config file for the command history
66        this->commandHistoryConfigFileType_ = ConfigFileManager::getInstance().getNewConfigFileType();
67        ConfigFileManager::getInstance().setFilename(this->commandHistoryConfigFileType_, "commandHistory.ini");
68
[6105]69        // Use a stringstream object to buffer the output and get it line by line in update()
70        this->outputStream_ = &this->outputBuffer_;
71
[1505]72        this->setConfigValues();
[1792]73
[6105]74        // Get the previous output and add it to the Shell
75        for (OutputHandler::OutputVectorIterator it = OutputHandler::getInstance().getOutputVectorBegin();
76            it != OutputHandler::getInstance().getOutputVectorEnd(); ++it)
77        {
78            if (it->first <= this->getSoftDebugLevel())
79            {
80                this->outputBuffer_ << it->second;
81                this->outputChanged(it->first);
82            }
83        }
84
85        // Register the shell as output listener
86        OutputHandler::getInstance().registerOutputListener(this);
[1505]87    }
88
[1755]89    Shell::~Shell()
90    {
[6105]91        OutputHandler::getInstance().unregisterOutputListener(this);
92        this->inputBuffer_->destroy();
[1755]93    }
94
[1505]95    void Shell::setConfigValues()
96    {
[6105]97        SetConfigValue(maxHistoryLength_, 100)
[3280]98            .callback(this, &Shell::commandHistoryLengthChanged);
[6105]99        SetConfigValue(historyOffset_, 0)
[3280]100            .callback(this, &Shell::commandHistoryOffsetChanged);
101        SetConfigValueVectorGeneric(commandHistoryConfigFileType_, commandHistory_, std::vector<std::string>());
[6105]102
103#ifdef ORXONOX_RELEASE
104        const unsigned int defaultLevel = 1;
105#else
106        const unsigned int defaultLevel = 3;
107#endif
108        SetConfigValueGeneric(ConfigFileType::Settings, softDebugLevel_, "softDebugLevel" + this->consoleName_, "OutputHandler", defaultLevel)
109            .description("The maximal level of debug output shown in the Shell");
110        this->setSoftDebugLevel(this->softDebugLevel_);
[1747]111    }
[1505]112
[1747]113    void Shell::commandHistoryOffsetChanged()
114    {
[1505]115        if (this->historyOffset_ >= this->maxHistoryLength_)
116            this->historyOffset_ = 0;
[1747]117    }
[1505]118
[1747]119    void Shell::commandHistoryLengthChanged()
120    {
121        this->commandHistoryOffsetChanged();
122
[1505]123        while (this->commandHistory_.size() > this->maxHistoryLength_)
124        {
125            unsigned int index = this->commandHistory_.size() - 1;
126            this->commandHistory_.erase(this->commandHistory_.begin() + index);
127            ModifyConfigValue(commandHistory_, remove, index);
128        }
129    }
130
[1755]131    void Shell::configureInputBuffer()
[1505]132    {
133        this->inputBuffer_->registerListener(this, &Shell::inputChanged, true);
[6105]134        this->inputBuffer_->registerListener(this, &Shell::execute,         '\r',   false);
135        this->inputBuffer_->registerListener(this, &Shell::execute,         '\n',   false);
136        this->inputBuffer_->registerListener(this, &Shell::hintAndComplete, '\t',   true);
137        this->inputBuffer_->registerListener(this, &Shell::backspace,       '\b',   true);
138        this->inputBuffer_->registerListener(this, &Shell::backspace,       '\177', true);
139        this->inputBuffer_->registerListener(this, &Shell::exit,            '\033', true); // escape
140        this->inputBuffer_->registerListener(this, &Shell::deleteChar,      KeyCode::Delete);
141        this->inputBuffer_->registerListener(this, &Shell::cursorRight,     KeyCode::Right);
142        this->inputBuffer_->registerListener(this, &Shell::cursorLeft,      KeyCode::Left);
143        this->inputBuffer_->registerListener(this, &Shell::cursorEnd,       KeyCode::End);
144        this->inputBuffer_->registerListener(this, &Shell::cursorHome,      KeyCode::Home);
145        this->inputBuffer_->registerListener(this, &Shell::historyUp,       KeyCode::Up);
146        this->inputBuffer_->registerListener(this, &Shell::historyDown,     KeyCode::Down);
147        if (this->bScrollable_)
148        {
149            this->inputBuffer_->registerListener(this, &Shell::scrollUp,    KeyCode::PageUp);
150            this->inputBuffer_->registerListener(this, &Shell::scrollDown,  KeyCode::PageDown);
151        }
152        else
153        {
154            this->inputBuffer_->registerListener(this, &Shell::historySearchUp,   KeyCode::PageUp);
155            this->inputBuffer_->registerListener(this, &Shell::historySearchDown, KeyCode::PageDown);
156        }
[1505]157    }
158
[6105]159    /*
[1505]160    void Shell::history()
161    {
162        Shell& instance = Shell::getInstance();
163
[3300]164        for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i)
[6105]165            instance.addOutputLine(instance.commandHistory_[i], -1);
[3300]166        for (unsigned int i =  0; i < instance.historyOffset_; ++i)
[6105]167            instance.addOutputLine(instance.commandHistory_[i], -1);
[1505]168    }
[6105]169    */
[1505]170
171    void Shell::registerListener(ShellListener* listener)
172    {
[6105]173        this->listeners_.push_back(listener);
[1505]174    }
175
176    void Shell::unregisterListener(ShellListener* listener)
177    {
178        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
179        {
180            if ((*it) == listener)
[6105]181                it = this->listeners_.erase(it);
[1505]182            else
183                ++it;
184        }
185    }
186
187    void Shell::setCursorPosition(unsigned int cursor)
188    {
189        this->inputBuffer_->setCursorPosition(cursor);
[6105]190        this->updateListeners<&ShellListener::cursorChanged>();
[1505]191    }
192
[6105]193    void Shell::addOutputLine(const std::string& line, int level)
[1505]194    {
[6105]195        // Make sure we really only have one line per line (no new lines!)
196        SubString lines(line, '\n');
197        for (unsigned i = 0; i < lines.size(); ++i)
198        {
199            if (level <= this->softDebugLevel_)
200                this->outputLines_.push_front(lines[i]);
201            this->updateListeners<&ShellListener::lineAdded>();
202        }
[1505]203    }
204
[6105]205    void Shell::clearOutput()
[1505]206    {
[6105]207        this->outputLines_.clear();
208        this->scrollIterator_ = this->outputLines_.begin();
[1505]209
210        this->scrollPosition_ = 0;
[6105]211        this->bFinishedLastLine_ = true;
[1505]212
[6105]213        this->updateListeners<&ShellListener::linesChanged>();
[1505]214    }
215
216    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
217    {
218        if (this->scrollPosition_)
219            return this->scrollIterator_;
220        else
[6105]221            return this->outputLines_.begin();
[1505]222    }
223
224    std::list<std::string>::const_iterator Shell::getEndIterator() const
225    {
[6105]226        return this->outputLines_.end();
[1505]227    }
228
229    void Shell::addToHistory(const std::string& command)
230    {
231        ModifyConfigValue(commandHistory_, set, this->historyOffset_, command);
232        this->historyPosition_ = 0;
233        ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_);
234    }
235
236    std::string Shell::getFromHistory() const
237    {
[3301]238        unsigned int index = mod(static_cast<int>(this->historyOffset_) - static_cast<int>(this->historyPosition_), this->maxHistoryLength_);
[1505]239        if (index < this->commandHistory_.size() && this->historyPosition_ != 0)
240            return this->commandHistory_[index];
241        else
242            return "";
243    }
244
[6105]245    void Shell::outputChanged(int level)
[1505]246    {
[6105]247        bool newline = false;
[1505]248        do
249        {
[6105]250            std::string output;
251            std::getline(this->outputBuffer_, output);
[1505]252
[6105]253            bool eof = this->outputBuffer_.eof();
254            bool fail = this->outputBuffer_.fail();
255            if (eof)
256                this->outputBuffer_.flush();
257            if (eof || fail)
258                this->outputBuffer_.clear();
259            newline = (!eof && !fail);
260
[1505]261            if (!newline && output == "")
262                break;
263
[6105]264            if (this->bFinishedLastLine_)
[1505]265            {
[6105]266                if (this->bPrependOutputLevel_)
[6139]267                {
268                    if (level == 0)
269                        output.insert(0, 1, static_cast<char>(-1));
270                    else
271                        output.insert(0, 1, static_cast<char>(level));
272                }
[1505]273
[6105]274                this->outputLines_.push_front(output);
[1505]275
276                if (this->scrollPosition_)
277                    this->scrollPosition_++;
278                else
[6105]279                    this->scrollIterator_ = this->outputLines_.begin();
[1505]280
[6105]281                this->bFinishedLastLine_ = newline;
[1505]282
283                if (!this->scrollPosition_)
284                {
[6105]285                    this->updateListeners<&ShellListener::lineAdded>();
[1505]286                }
287            }
288            else
289            {
[6105]290                (*this->outputLines_.begin()) += output;
291                this->bFinishedLastLine_ = newline;
292                this->updateListeners<&ShellListener::onlyLastLineChanged>();
[1505]293            }
294
295        } while (newline);
296    }
297
[6105]298    void Shell::clearInput()
299    {
300        this->inputBuffer_->clear();
301        this->historyPosition_ = 0;
302        this->updateListeners<&ShellListener::inputChanged>();
303        this->updateListeners<&ShellListener::cursorChanged>();
304    }
305
306    void Shell::setPromptPrefix(const std::string& str)
307    {
308    }
309
310
311    // ##########################################
312    // ###   InputBuffer callback functions   ###
313    // ##########################################
314
[1505]315    void Shell::inputChanged()
316    {
[6105]317        this->updateListeners<&ShellListener::inputChanged>();
318        this->updateListeners<&ShellListener::cursorChanged>();
[1505]319    }
320
321    void Shell::execute()
322    {
323        this->addToHistory(this->inputBuffer_->get());
[6105]324        this->updateListeners<&ShellListener::executed>();
[1505]325
326        if (!CommandExecutor::execute(this->inputBuffer_->get()))
[6105]327            this->addOutputLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
[1505]328
[6105]329        this->clearInput();
[1505]330    }
331
[6105]332    void Shell::hintAndComplete()
[1505]333    {
334        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
[6105]335        this->addOutputLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
[1505]336
337        this->inputChanged();
338    }
339
340    void Shell::backspace()
341    {
342        this->inputBuffer_->removeBehindCursor();
[6105]343        this->updateListeners<&ShellListener::inputChanged>();
344        this->updateListeners<&ShellListener::cursorChanged>();
[1505]345    }
346
[6105]347    void Shell::exit()
[1505]348    {
[6105]349        if (this->inputBuffer_->getSize() > 0)
350        {
351            this->clearInput();
352            return;
353        }
354
355        this->clearInput();
356        this->scrollPosition_ = 0;
357        this->scrollIterator_ = this->outputLines_.begin();
358
359        this->updateListeners<&ShellListener::exit>();
[1505]360    }
361
[6105]362    void Shell::deleteChar()
[1505]363    {
[6105]364        this->inputBuffer_->removeAtCursor();
365        this->updateListeners<&ShellListener::inputChanged>();
[1505]366    }
367
[6105]368    void Shell::cursorRight()
[1505]369    {
370        this->inputBuffer_->increaseCursor();
[6105]371        this->updateListeners<&ShellListener::cursorChanged>();
[1505]372    }
373
[6105]374    void Shell::cursorLeft()
[1505]375    {
376        this->inputBuffer_->decreaseCursor();
[6105]377        this->updateListeners<&ShellListener::cursorChanged>();
[1505]378    }
379
[6105]380    void Shell::cursorEnd()
[1505]381    {
382        this->inputBuffer_->setCursorToEnd();
[6105]383        this->updateListeners<&ShellListener::cursorChanged>();
[1505]384    }
385
[6105]386    void Shell::cursorHome()
[1505]387    {
388        this->inputBuffer_->setCursorToBegin();
[6105]389        this->updateListeners<&ShellListener::cursorChanged>();
[1505]390    }
391
[6105]392    void Shell::historyUp()
[1505]393    {
394        if (this->historyPosition_ < this->commandHistory_.size())
395        {
396            this->historyPosition_++;
397            this->inputBuffer_->set(this->getFromHistory());
398        }
399    }
400
[6105]401    void Shell::historyDown()
[1505]402    {
403        if (this->historyPosition_ > 0)
404        {
405            this->historyPosition_--;
406            this->inputBuffer_->set(this->getFromHistory());
407        }
408    }
409
[6105]410    void Shell::historySearchUp()
[1505]411    {
[6105]412        if (this->historyPosition_ == this->historyOffset_)
413            return;
414        unsigned int cursorPosition = this->getCursorPosition();
415        std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline until the cursor position
416        for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++)
[1505]417        {
[6105]418            if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // search case insensitive
419            {
420                this->historyPosition_ = newPos;
421                this->inputBuffer_->set(this->getFromHistory());
422                this->setCursorPosition(cursorPosition);
423                return;
424            }
425        }
426    }
[1505]427
[6105]428    void Shell::historySearchDown()
429    {
430        if (this->historyPosition_ == 0)
431            return;
432        unsigned int cursorPosition = this->getCursorPosition();
433        std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning
434        for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--)
435        {
436            if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // sear$
437            {
438                this->historyPosition_ = newPos;
439                this->inputBuffer_->set(this->getFromHistory());
440                this->setCursorPosition(cursorPosition);
441                return;
442            }
[1505]443        }
444    }
445
[6105]446    void Shell::scrollUp()
[1505]447    {
[6105]448        if (this->scrollIterator_ != this->outputLines_.end())
[1505]449        {
[6105]450            ++this->scrollIterator_;
451            ++this->scrollPosition_;
[1505]452
[6105]453            this->updateListeners<&ShellListener::linesChanged>();
[1505]454        }
455    }
456
[6105]457    void Shell::scrollDown()
[1505]458    {
[6105]459        if (this->scrollIterator_ != this->outputLines_.begin())
[1505]460        {
[6105]461            --this->scrollIterator_;
462            --this->scrollPosition_;
463
464            this->updateListeners<&ShellListener::linesChanged>();
[1505]465        }
466    }
467}
Note: See TracBrowser for help on using the repository browser.