Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/core/Shell.cc @ 1586

Last change on this file since 1586 was 1586, checked in by landauf, 17 years ago

moved Debug.h, OutputHandler and OutputBuffer to util, to make COUT(x) available everywhere

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