Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/libraries/core/Shell.cc @ 6004

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

De-singletonised Shell so that both consoles have their own Shell instance. However they share the history.
Also modified IOConsole to hopefully work with status lines.

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