Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 31, 2009, 11:13:52 PM (14 years ago)
Author:
rgrieder
Message:

Sorted out some small stuff in the Shell and hopefully fixed IOConsole problems (flickering and too much output).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/console/src/libraries/core/Shell.cc

    r6004 r6010  
    2323 *      Fabian 'x3n' Landau
    2424 *   Co-authors:
    25  *      ...
     25 *      Reto Grieder
    2626 *
    2727 */
     
    4444    SetConsoleCommandShortcut(OutputHandler, debug);
    4545
    46     Shell::Shell(const std::string& consoleName, bool bScrollable)
    47         : inputBuffer_(new InputBuffer())
    48         , OutputListener(consoleName)
     46    Shell::Shell(const std::string& consoleName, bool bScrollable, bool bPrependOutputLevel)
     47        : OutputListener(consoleName)
     48        , inputBuffer_(new InputBuffer())
    4949        , consoleName_(consoleName)
     50        , bPrependOutputLevel_(bPrependOutputLevel)
    5051        , bScrollable_(bScrollable)
    5152    {
     
    5657        this->historyPosition_ = 0;
    5758        this->historyOffset_ = 0;
    58         this->finishedLastLine_ = true;
    59         this->bAddOutputLevel_ = false;
    60 
    61         this->clearLines();
     59        this->bFinishedLastLine_ = true;
     60
     61        this->clearOutput();
    6262        this->configureInputBuffer();
    6363
     
    131131    {
    132132        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);
     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::exit,            '\033', true); // escape
     139        this->inputBuffer_->registerListener(this, &Shell::deleteChar,      KeyCode::Delete);
     140        this->inputBuffer_->registerListener(this, &Shell::cursorRight,     KeyCode::Right);
     141        this->inputBuffer_->registerListener(this, &Shell::cursorLeft,      KeyCode::Left);
     142        this->inputBuffer_->registerListener(this, &Shell::cursorEnd,       KeyCode::End);
     143        this->inputBuffer_->registerListener(this, &Shell::cursorHome,      KeyCode::Home);
     144        this->inputBuffer_->registerListener(this, &Shell::historyUp,       KeyCode::Up);
     145        this->inputBuffer_->registerListener(this, &Shell::historyDown,     KeyCode::Down);
     146        if (this->bScrollable_)
     147        {
     148            this->inputBuffer_->registerListener(this, &Shell::scrollUp,    KeyCode::PageUp);
     149            this->inputBuffer_->registerListener(this, &Shell::scrollDown,  KeyCode::PageDown);
     150        }
     151        else
     152        {
     153            this->inputBuffer_->registerListener(this, &Shell::historySearchUp,   KeyCode::PageUp);
     154            this->inputBuffer_->registerListener(this, &Shell::historySearchDown, KeyCode::PageDown);
     155        }
    150156    }
    151157
     
    156162
    157163        for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i)
    158             instance.addLine(instance.commandHistory_[i], -1);
     164            instance.addOutputLine(instance.commandHistory_[i], -1);
    159165        for (unsigned int i =  0; i < instance.historyOffset_; ++i)
    160             instance.addLine(instance.commandHistory_[i], -1);
     166            instance.addOutputLine(instance.commandHistory_[i], -1);
    161167    }
    162168    */
     
    184190    }
    185191
    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)
     192    void Shell::addOutputLine(const std::string& line, int level)
    193193    {
    194194        if (level <= this->softDebugLevel_)
     
    197197    }
    198198
    199     void Shell::clearLines()
     199    void Shell::clearOutput()
    200200    {
    201201        this->outputLines_.clear();
     
    203203
    204204        this->scrollPosition_ = 0;
    205         this->finishedLastLine_ = true;
     205        this->bFinishedLastLine_ = true;
    206206
    207207        this->updateListeners<&ShellListener::linesChanged>();
     
    256256                break;
    257257
    258             if (this->finishedLastLine_)
     258            if (this->bFinishedLastLine_)
    259259            {
    260                 if (this->bAddOutputLevel_)
     260                if (this->bPrependOutputLevel_)
    261261                    output.insert(0, 1, static_cast<char>(level));
    262262
     
    268268                    this->scrollIterator_ = this->outputLines_.begin();
    269269
    270                 this->finishedLastLine_ = newline;
     270                this->bFinishedLastLine_ = newline;
    271271
    272272                if (!this->scrollPosition_)
     
    278278            {
    279279                (*this->outputLines_.begin()) += output;
    280                 this->finishedLastLine_ = newline;
     280                this->bFinishedLastLine_ = newline;
    281281                this->updateListeners<&ShellListener::onlyLastLineChanged>();
    282282            }
     
    285285    }
    286286
    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()
     287    void Shell::clearInput()
    326288    {
    327289        this->inputBuffer_->clear();
     
    331293    }
    332294
    333     void Shell::cursor_right()
     295    void Shell::setPromptPrefix(const std::string& str)
     296    {
     297    }
     298
     299
     300    // ##########################################
     301    // ###   InputBuffer callback functions   ###
     302    // ##########################################
     303
     304    void Shell::inputChanged()
     305    {
     306        this->updateListeners<&ShellListener::inputChanged>();
     307        this->updateListeners<&ShellListener::cursorChanged>();
     308    }
     309
     310    void Shell::execute()
     311    {
     312        this->addToHistory(this->inputBuffer_->get());
     313        this->updateListeners<&ShellListener::executed>();
     314
     315        if (!CommandExecutor::execute(this->inputBuffer_->get()))
     316            this->addOutputLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
     317
     318        this->clearInput();
     319    }
     320
     321    void Shell::hintAndComplete()
     322    {
     323        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
     324        this->addOutputLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
     325
     326        this->inputChanged();
     327    }
     328
     329    void Shell::backspace()
     330    {
     331        this->inputBuffer_->removeBehindCursor();
     332        this->updateListeners<&ShellListener::inputChanged>();
     333        this->updateListeners<&ShellListener::cursorChanged>();
     334    }
     335
     336    void Shell::exit()
     337    {
     338        if (this->inputBuffer_->getSize() > 0)
     339        {
     340            this->clearInput();
     341            return;
     342        }
     343
     344        this->clearInput();
     345        this->scrollPosition_ = 0;
     346        this->scrollIterator_ = this->outputLines_.begin();
     347
     348        this->updateListeners<&ShellListener::exit>();
     349    }
     350
     351    void Shell::deleteChar()
     352    {
     353        this->inputBuffer_->removeAtCursor();
     354        this->updateListeners<&ShellListener::inputChanged>();
     355    }
     356
     357    void Shell::cursorRight()
    334358    {
    335359        this->inputBuffer_->increaseCursor();
     
    337361    }
    338362
    339     void Shell::cursor_left()
     363    void Shell::cursorLeft()
    340364    {
    341365        this->inputBuffer_->decreaseCursor();
     
    343367    }
    344368
    345     void Shell::cursor_end()
     369    void Shell::cursorEnd()
    346370    {
    347371        this->inputBuffer_->setCursorToEnd();
     
    349373    }
    350374
    351     void Shell::cursor_home()
     375    void Shell::cursorHome()
    352376    {
    353377        this->inputBuffer_->setCursorToBegin();
     
    355379    }
    356380
    357     void Shell::history_up()
     381    void Shell::historyUp()
    358382    {
    359383        if (this->historyPosition_ < this->commandHistory_.size())
     
    364388    }
    365389
    366     void Shell::history_down()
     390    void Shell::historyDown()
    367391    {
    368392        if (this->historyPosition_ > 0)
     
    373397    }
    374398
    375     void Shell::history_search_up()
     399    void Shell::historySearchUp()
    376400    {
    377401        if (this->historyPosition_ == this->historyOffset_)
    378402            return;
    379403        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
     404        std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline until the cursor position
    381405        for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++)
    382406        {
     
    391415    }
    392416
    393     void Shell::history_search_down()
     417    void Shell::historySearchDown()
    394418    {
    395419        if (this->historyPosition_ == 0)
    396420            return;
    397421        unsigned int cursorPosition = this->getCursorPosition();
    398         std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginn$
     422        std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning
    399423        for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--)
    400424        {
     
    409433    }
    410434
    411     void Shell::scroll_up()
     435    void Shell::scrollUp()
    412436    {
    413437        if (this->scrollIterator_ != this->outputLines_.end())
     
    420444    }
    421445
    422     void Shell::scroll_down()
     446    void Shell::scrollDown()
    423447    {
    424448        if (this->scrollIterator_ != this->outputLines_.begin())
     
    430454        }
    431455    }
    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     }
    447456}
Note: See TracChangeset for help on using the changeset viewer.