Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1327 was 1327, checked in by landauf, 16 years ago

sync with notebook

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