Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/Shell.cc @ 7219

Last change on this file since 7219 was 7219, checked in by landauf, 14 years ago

adapted all console commands to the new interface

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