Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6139 was 6139, checked in by rgrieder, 14 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
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 *      Reto Grieder
26 *
27 */
28
29#include "Shell.h"
30
31#include "util/OutputHandler.h"
32#include "util/StringUtils.h"
33#include "util/SubString.h"
34#include "CommandExecutor.h"
35#include "CoreIncludes.h"
36#include "ConfigValueIncludes.h"
37#include "ConsoleCommand.h"
38
39namespace orxonox
40{
41    SetConsoleCommandShortcut(OutputHandler, log);
42    SetConsoleCommandShortcut(OutputHandler, error);
43    SetConsoleCommandShortcut(OutputHandler, warning);
44    SetConsoleCommandShortcut(OutputHandler, info);
45    SetConsoleCommandShortcut(OutputHandler, debug);
46
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)
53    {
54        RegisterRootObject(Shell);
55
56        this->scrollPosition_ = 0;
57        this->maxHistoryLength_ = 100;
58        this->historyPosition_ = 0;
59        this->historyOffset_ = 0;
60        this->bFinishedLastLine_ = true;
61
62        this->clearOutput();
63        this->configureInputBuffer();
64
65        // Get a config file for the command history
66        this->commandHistoryConfigFileType_ = ConfigFileManager::getInstance().getNewConfigFileType();
67        ConfigFileManager::getInstance().setFilename(this->commandHistoryConfigFileType_, "commandHistory.ini");
68
69        // Use a stringstream object to buffer the output and get it line by line in update()
70        this->outputStream_ = &this->outputBuffer_;
71
72        this->setConfigValues();
73
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);
87    }
88
89    Shell::~Shell()
90    {
91        OutputHandler::getInstance().unregisterOutputListener(this);
92        this->inputBuffer_->destroy();
93    }
94
95    void Shell::setConfigValues()
96    {
97        SetConfigValue(maxHistoryLength_, 100)
98            .callback(this, &Shell::commandHistoryLengthChanged);
99        SetConfigValue(historyOffset_, 0)
100            .callback(this, &Shell::commandHistoryOffsetChanged);
101        SetConfigValueVectorGeneric(commandHistoryConfigFileType_, commandHistory_, std::vector<std::string>());
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_);
111    }
112
113    void Shell::commandHistoryOffsetChanged()
114    {
115        if (this->historyOffset_ >= this->maxHistoryLength_)
116            this->historyOffset_ = 0;
117    }
118
119    void Shell::commandHistoryLengthChanged()
120    {
121        this->commandHistoryOffsetChanged();
122
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
131    void Shell::configureInputBuffer()
132    {
133        this->inputBuffer_->registerListener(this, &Shell::inputChanged, true);
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        }
157    }
158
159    /*
160    void Shell::history()
161    {
162        Shell& instance = Shell::getInstance();
163
164        for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i)
165            instance.addOutputLine(instance.commandHistory_[i], -1);
166        for (unsigned int i =  0; i < instance.historyOffset_; ++i)
167            instance.addOutputLine(instance.commandHistory_[i], -1);
168    }
169    */
170
171    void Shell::registerListener(ShellListener* listener)
172    {
173        this->listeners_.push_back(listener);
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)
181                it = this->listeners_.erase(it);
182            else
183                ++it;
184        }
185    }
186
187    void Shell::setCursorPosition(unsigned int cursor)
188    {
189        this->inputBuffer_->setCursorPosition(cursor);
190        this->updateListeners<&ShellListener::cursorChanged>();
191    }
192
193    void Shell::addOutputLine(const std::string& line, int level)
194    {
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        }
203    }
204
205    void Shell::clearOutput()
206    {
207        this->outputLines_.clear();
208        this->scrollIterator_ = this->outputLines_.begin();
209
210        this->scrollPosition_ = 0;
211        this->bFinishedLastLine_ = true;
212
213        this->updateListeners<&ShellListener::linesChanged>();
214    }
215
216    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
217    {
218        if (this->scrollPosition_)
219            return this->scrollIterator_;
220        else
221            return this->outputLines_.begin();
222    }
223
224    std::list<std::string>::const_iterator Shell::getEndIterator() const
225    {
226        return this->outputLines_.end();
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    {
238        unsigned int index = mod(static_cast<int>(this->historyOffset_) - static_cast<int>(this->historyPosition_), this->maxHistoryLength_);
239        if (index < this->commandHistory_.size() && this->historyPosition_ != 0)
240            return this->commandHistory_[index];
241        else
242            return "";
243    }
244
245    void Shell::outputChanged(int level)
246    {
247        bool newline = false;
248        do
249        {
250            std::string output;
251            std::getline(this->outputBuffer_, output);
252
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
261            if (!newline && output == "")
262                break;
263
264            if (this->bFinishedLastLine_)
265            {
266                if (this->bPrependOutputLevel_)
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                }
273
274                this->outputLines_.push_front(output);
275
276                if (this->scrollPosition_)
277                    this->scrollPosition_++;
278                else
279                    this->scrollIterator_ = this->outputLines_.begin();
280
281                this->bFinishedLastLine_ = newline;
282
283                if (!this->scrollPosition_)
284                {
285                    this->updateListeners<&ShellListener::lineAdded>();
286                }
287            }
288            else
289            {
290                (*this->outputLines_.begin()) += output;
291                this->bFinishedLastLine_ = newline;
292                this->updateListeners<&ShellListener::onlyLastLineChanged>();
293            }
294
295        } while (newline);
296    }
297
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
315    void Shell::inputChanged()
316    {
317        this->updateListeners<&ShellListener::inputChanged>();
318        this->updateListeners<&ShellListener::cursorChanged>();
319    }
320
321    void Shell::execute()
322    {
323        this->addToHistory(this->inputBuffer_->get());
324        this->updateListeners<&ShellListener::executed>();
325
326        if (!CommandExecutor::execute(this->inputBuffer_->get()))
327            this->addOutputLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
328
329        this->clearInput();
330    }
331
332    void Shell::hintAndComplete()
333    {
334        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
335        this->addOutputLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
336
337        this->inputChanged();
338    }
339
340    void Shell::backspace()
341    {
342        this->inputBuffer_->removeBehindCursor();
343        this->updateListeners<&ShellListener::inputChanged>();
344        this->updateListeners<&ShellListener::cursorChanged>();
345    }
346
347    void Shell::exit()
348    {
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>();
360    }
361
362    void Shell::deleteChar()
363    {
364        this->inputBuffer_->removeAtCursor();
365        this->updateListeners<&ShellListener::inputChanged>();
366    }
367
368    void Shell::cursorRight()
369    {
370        this->inputBuffer_->increaseCursor();
371        this->updateListeners<&ShellListener::cursorChanged>();
372    }
373
374    void Shell::cursorLeft()
375    {
376        this->inputBuffer_->decreaseCursor();
377        this->updateListeners<&ShellListener::cursorChanged>();
378    }
379
380    void Shell::cursorEnd()
381    {
382        this->inputBuffer_->setCursorToEnd();
383        this->updateListeners<&ShellListener::cursorChanged>();
384    }
385
386    void Shell::cursorHome()
387    {
388        this->inputBuffer_->setCursorToBegin();
389        this->updateListeners<&ShellListener::cursorChanged>();
390    }
391
392    void Shell::historyUp()
393    {
394        if (this->historyPosition_ < this->commandHistory_.size())
395        {
396            this->historyPosition_++;
397            this->inputBuffer_->set(this->getFromHistory());
398        }
399    }
400
401    void Shell::historyDown()
402    {
403        if (this->historyPosition_ > 0)
404        {
405            this->historyPosition_--;
406            this->inputBuffer_->set(this->getFromHistory());
407        }
408    }
409
410    void Shell::historySearchUp()
411    {
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++)
417        {
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    }
427
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            }
443        }
444    }
445
446    void Shell::scrollUp()
447    {
448        if (this->scrollIterator_ != this->outputLines_.end())
449        {
450            ++this->scrollIterator_;
451            ++this->scrollPosition_;
452
453            this->updateListeners<&ShellListener::linesChanged>();
454        }
455    }
456
457    void Shell::scrollDown()
458    {
459        if (this->scrollIterator_ != this->outputLines_.begin())
460        {
461            --this->scrollIterator_;
462            --this->scrollPosition_;
463
464            this->updateListeners<&ShellListener::linesChanged>();
465        }
466    }
467}
Note: See TracBrowser for help on using the repository browser.