Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6374 was 6374, checked in by rgrieder, 14 years ago

Fixed a problem in the Shell with the command history config file type.

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