Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Shell.cc @ 6333

Last change on this file since 6333 was 6105, checked in by rgrieder, 15 years ago

Merged console branch back to trunk.

  • Property svn:eol-style set to native
File size: 15.3 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_)
267                    output.insert(0, 1, static_cast<char>(level));
[1505]268
[6105]269                this->outputLines_.push_front(output);
[1505]270
271                if (this->scrollPosition_)
272                    this->scrollPosition_++;
273                else
[6105]274                    this->scrollIterator_ = this->outputLines_.begin();
[1505]275
[6105]276                this->bFinishedLastLine_ = newline;
[1505]277
278                if (!this->scrollPosition_)
279                {
[6105]280                    this->updateListeners<&ShellListener::lineAdded>();
[1505]281                }
282            }
283            else
284            {
[6105]285                (*this->outputLines_.begin()) += output;
286                this->bFinishedLastLine_ = newline;
287                this->updateListeners<&ShellListener::onlyLastLineChanged>();
[1505]288            }
289
290        } while (newline);
291    }
292
[6105]293    void Shell::clearInput()
294    {
295        this->inputBuffer_->clear();
296        this->historyPosition_ = 0;
297        this->updateListeners<&ShellListener::inputChanged>();
298        this->updateListeners<&ShellListener::cursorChanged>();
299    }
300
301    void Shell::setPromptPrefix(const std::string& str)
302    {
303    }
304
305
306    // ##########################################
307    // ###   InputBuffer callback functions   ###
308    // ##########################################
309
[1505]310    void Shell::inputChanged()
311    {
[6105]312        this->updateListeners<&ShellListener::inputChanged>();
313        this->updateListeners<&ShellListener::cursorChanged>();
[1505]314    }
315
316    void Shell::execute()
317    {
318        this->addToHistory(this->inputBuffer_->get());
[6105]319        this->updateListeners<&ShellListener::executed>();
[1505]320
321        if (!CommandExecutor::execute(this->inputBuffer_->get()))
[6105]322            this->addOutputLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
[1505]323
[6105]324        this->clearInput();
[1505]325    }
326
[6105]327    void Shell::hintAndComplete()
[1505]328    {
329        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
[6105]330        this->addOutputLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
[1505]331
332        this->inputChanged();
333    }
334
335    void Shell::backspace()
336    {
337        this->inputBuffer_->removeBehindCursor();
[6105]338        this->updateListeners<&ShellListener::inputChanged>();
339        this->updateListeners<&ShellListener::cursorChanged>();
[1505]340    }
341
[6105]342    void Shell::exit()
[1505]343    {
[6105]344        if (this->inputBuffer_->getSize() > 0)
345        {
346            this->clearInput();
347            return;
348        }
349
350        this->clearInput();
351        this->scrollPosition_ = 0;
352        this->scrollIterator_ = this->outputLines_.begin();
353
354        this->updateListeners<&ShellListener::exit>();
[1505]355    }
356
[6105]357    void Shell::deleteChar()
[1505]358    {
[6105]359        this->inputBuffer_->removeAtCursor();
360        this->updateListeners<&ShellListener::inputChanged>();
[1505]361    }
362
[6105]363    void Shell::cursorRight()
[1505]364    {
365        this->inputBuffer_->increaseCursor();
[6105]366        this->updateListeners<&ShellListener::cursorChanged>();
[1505]367    }
368
[6105]369    void Shell::cursorLeft()
[1505]370    {
371        this->inputBuffer_->decreaseCursor();
[6105]372        this->updateListeners<&ShellListener::cursorChanged>();
[1505]373    }
374
[6105]375    void Shell::cursorEnd()
[1505]376    {
377        this->inputBuffer_->setCursorToEnd();
[6105]378        this->updateListeners<&ShellListener::cursorChanged>();
[1505]379    }
380
[6105]381    void Shell::cursorHome()
[1505]382    {
383        this->inputBuffer_->setCursorToBegin();
[6105]384        this->updateListeners<&ShellListener::cursorChanged>();
[1505]385    }
386
[6105]387    void Shell::historyUp()
[1505]388    {
389        if (this->historyPosition_ < this->commandHistory_.size())
390        {
391            this->historyPosition_++;
392            this->inputBuffer_->set(this->getFromHistory());
393        }
394    }
395
[6105]396    void Shell::historyDown()
[1505]397    {
398        if (this->historyPosition_ > 0)
399        {
400            this->historyPosition_--;
401            this->inputBuffer_->set(this->getFromHistory());
402        }
403    }
404
[6105]405    void Shell::historySearchUp()
[1505]406    {
[6105]407        if (this->historyPosition_ == this->historyOffset_)
408            return;
409        unsigned int cursorPosition = this->getCursorPosition();
410        std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline until the cursor position
411        for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++)
[1505]412        {
[6105]413            if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // search case insensitive
414            {
415                this->historyPosition_ = newPos;
416                this->inputBuffer_->set(this->getFromHistory());
417                this->setCursorPosition(cursorPosition);
418                return;
419            }
420        }
421    }
[1505]422
[6105]423    void Shell::historySearchDown()
424    {
425        if (this->historyPosition_ == 0)
426            return;
427        unsigned int cursorPosition = this->getCursorPosition();
428        std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning
429        for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--)
430        {
431            if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // sear$
432            {
433                this->historyPosition_ = newPos;
434                this->inputBuffer_->set(this->getFromHistory());
435                this->setCursorPosition(cursorPosition);
436                return;
437            }
[1505]438        }
439    }
440
[6105]441    void Shell::scrollUp()
[1505]442    {
[6105]443        if (this->scrollIterator_ != this->outputLines_.end())
[1505]444        {
[6105]445            ++this->scrollIterator_;
446            ++this->scrollPosition_;
[1505]447
[6105]448            this->updateListeners<&ShellListener::linesChanged>();
[1505]449        }
450    }
451
[6105]452    void Shell::scrollDown()
[1505]453    {
[6105]454        if (this->scrollIterator_ != this->outputLines_.begin())
[1505]455        {
[6105]456            --this->scrollIterator_;
457            --this->scrollPosition_;
458
459            this->updateListeners<&ShellListener::linesChanged>();
[1505]460        }
461    }
462}
Note: See TracBrowser for help on using the repository browser.