Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5986 was 5986, checked in by scheusso, 15 years ago

added a new feature to the IOConsole: you can now search the history (like in bash shells) with PgUP
just enter a few signs/characters of a command you once typed in and press PgUP to complete the command
if the found command is not the one you were looking for just press PgUP again

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