Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6180


Ignore:
Timestamp:
Nov 29, 2009, 10:43:43 PM (14 years ago)
Author:
rgrieder
Message:

Extended Shell line colouring in order to distinguish output from COUT, entered commands and hints.

Location:
code/branches/presentation2/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/src/libraries/core/IOConsole.cc

    r6179 r6180  
    4545    IOConsole* IOConsole::singletonPtr_s = NULL;
    4646
    47     //! Extracts the log level associated to a string (first character)
    48     int IOConsole::extractLogLevel(std::string* text)
    49     {
    50         // Handle line colouring by inspecting the first letter
    51         char level = 0;
    52         if (!text->empty())
    53         {
    54             level = (*text)[0];
    55             if (level == -1 || (level >= 1 && level <= 6))
    56             {
    57                 *text = text->substr(1);
    58                 if (level != -1)
    59                     return level;
    60             }
    61         }
    62         return 0;
    63     }
    64 
    6547    // ###############################
    6648    // ###  ShellListener methods  ###
     
    7860    void IOConsole::executed()
    7961    {
    80         this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput());
     62        this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput(), Shell::Command);
    8163    }
    8264
     
    10991
    11092    IOConsole::IOConsole()
    111         : shell_(new Shell("IOConsole", false, true))
     93        : shell_(new Shell("IOConsole", false))
    11294        , buffer_(shell_->getInputBuffer())
    11395        , cout_(std::cout.rdbuf())
     
    280262        if (!this->origCout_.str().empty())
    281263        {
    282             this->shell_->addOutputLine(this->origCout_.str());
     264            this->shell_->addOutputLine(this->origCout_.str(), Shell::None);
    283265            this->origCout_.str("");
    284266        }
    285267    }
    286268
    287     void IOConsole::printOutputLine(const std::string& text)
    288     {
    289         std::string output = text;
    290         /*int level =*/ this->extractLogLevel(&output);
    291 
     269    void IOConsole::printOutputLine(const std::string& text, Shell::LineType type)
     270    {
    292271/*
    293272        // Colour line
    294         switch (level)
    295         {
    296         case -1: this->cout_ << "\033[37m"; break;
    297         case  1: this->cout_ << "\033[91m"; break;
    298         case  2: this->cout_ << "\033[31m"; break;
    299         case  3: this->cout_ << "\033[34m"; break;
    300         case  4: this->cout_ << "\033[36m"; break;
    301         case  5: this->cout_ << "\033[35m"; break;
    302         case  6: this->cout_ << "\033[37m"; break;
     273        switch (type)
     274        {
     275        case Shell::None:    this->cout_ << "\033[37m"; break;
     276        case Shell::Error:  this->cout_ << "\033[91m"; break;
     277        case Shell::Warning: this->cout_ << "\033[31m"; break;
     278        case Shell::Info:    this->cout_ << "\033[34m"; break;
     279        case Shell::Debug:  this->cout_ << "\033[36m"; break;
     280        case Shell::Verbose: this->cout_ << "\033[35m"; break;
     281        case Shell::Ultra:  this->cout_ << "\033[37m"; break;
    303282        default: break;
    304283        }
     
    306285
    307286        // Print output line
    308         this->cout_ << output;
     287        this->cout_ << text;
    309288
    310289        // Reset colour to white
     
    419398        this->cout_ << "\033[K";
    420399        // Reprint the last output line
    421         this->printOutputLine(*(this->shell_->getNewestLineIterator()));
     400        this->printOutputLine(this->shell_->getNewestLineIterator()->first);
    422401        // Restore cursor
    423402        this->cout_ << "\033[u";
     
    428407    void IOConsole::lineAdded()
    429408    {
    430         int newLines = this->shell_->getNewestLineIterator()->size() / this->terminalWidth_ + 1;
     409        int newLines = this->shell_->getNewestLineIterator()->first.size() / this->terminalWidth_ + 1;
    431410        // Create new lines by scrolling the screen
    432411        this->cout_ << "\033[" << newLines << 'S';
     
    437416        // Print the new output lines
    438417        for (int i = 0; i < newLines; ++i)
    439             this->printOutputLine(this->shell_->getNewestLineIterator()->substr(i*this->terminalWidth_, this->terminalWidth_));
     418            this->printOutputLine(this->shell_->getNewestLineIterator()->first.substr(i*this->terminalWidth_, this->terminalWidth_));
    440419        // Move cursor down
    441420        this->cout_ << "\033[1B\033[1G";
     
    472451    //! Redirects std::cout, creates the corresponding Shell and changes the terminal mode
    473452    IOConsole::IOConsole()
    474         : shell_(new Shell("IOConsole", false, true))
     453        : shell_(new Shell("IOConsole", false))
    475454        , buffer_(shell_->getInputBuffer())
    476455        , cout_(std::cout.rdbuf())
     
    613592        if (!this->origCout_.str().empty())
    614593        {
    615             this->shell_->addOutputLine(this->origCout_.str());
     594            this->shell_->addOutputLine(this->origCout_.str(), Shell::None);
    616595            this->origCout_.str("");
    617596        }
     
    619598
    620599    //! Prints output text. Similar to writeText, but sets the colour according to the output level
    621     void IOConsole::printOutputLine(const std::string& text, const COORD& pos)
    622     {
    623         std::string output = text;
    624         int level = this->extractLogLevel(&output);
    625 
     600    void IOConsole::printOutputLine(const std::string& text, Shell::LineType type, const COORD& pos)
     601    {
    626602        // Colour line
    627603        WORD colour = 0;
    628         switch (level)
    629         {
    630         case  1: colour = FOREGROUND_INTENSITY                    | FOREGROUND_RED ; break;
    631         case  2: colour = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED ; break;
    632         case  3: colour = FOREGROUND_INTENSITY                                     ; break;
    633         case  4: colour = FOREGROUND_INTENSITY                                     ; break;
    634         default: colour =                        FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ; break;
     604        switch (type)
     605        {
     606        case Shell::Error:   colour = FOREGROUND_INTENSITY                    | FOREGROUND_RED; break;
     607        case Shell::Warning: colour = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED; break;
     608        case Shell::Info:
     609        case Shell::Debug:
     610        case Shell::Verbose:
     611        case Shell::Ultra:   colour = FOREGROUND_INTENSITY                                     ; break;
     612        case Shell::Command: colour =                        FOREGROUND_GREEN                  | FOREGROUND_BLUE; break;
     613        case Shell::Hint:    colour =                        FOREGROUND_GREEN | FOREGROUND_RED                  ; break;
     614        default:             colour =                        FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; break;
    635615        }
    636616
    637617        // Print output line
    638         this->writeText(output, pos, colour);
     618        this->writeText(text, pos, colour);
    639619    }
    640620
     
    783763    void IOConsole::onlyLastLineChanged()
    784764    {
    785         int newLineHeight = 1 + this->shell_->getNewestLineIterator()->size() / this->terminalWidth_;
     765        int newLineHeight = 1 + this->shell_->getNewestLineIterator()->first.size() / this->terminalWidth_;
    786766        // Compute the number of new lines needed
    787767        int newLines = newLineHeight - this->lastOutputLineHeight_;
     
    790770        if (newLines > 0) // newLines < 0 is assumed impossible
    791771            this->createNewOutputLines(newLines);
    792         this->printOutputLine(*(this->shell_->getNewestLineIterator()), makeCOORD(0, this->inputLineRow_ - newLineHeight));
     772        Shell::LineList::const_iterator it = this->shell_->getNewestLineIterator();
     773        this->printOutputLine(it->first, it->second, makeCOORD(0, this->inputLineRow_ - newLineHeight));
    793774    }
    794775
     
    796777    void IOConsole::lineAdded()
    797778    {
     779        Shell::LineList::const_iterator it = this->shell_->getNewestLineIterator();
    798780        // Scroll console
    799         this->lastOutputLineHeight_ = 1 + this->shell_->getNewestLineIterator()->size() / this->terminalWidth_;
     781        this->lastOutputLineHeight_ = 1 + it->first.size() / this->terminalWidth_;
    800782        this->createNewOutputLines(this->lastOutputLineHeight_);
    801783        // Write the text
    802784        COORD pos = {0, this->inputLineRow_ - this->lastOutputLineHeight_};
    803         this->printOutputLine(*(this->shell_->getNewestLineIterator()), pos);
     785        this->printOutputLine(it->first, it->second, pos);
    804786    }
    805787}
  • code/branches/presentation2/src/libraries/core/IOConsole.h

    r6179 r6180  
    8787        bool willPrintStatusLines();
    8888        void printInputLine();
    89         void printOutputLine(const std::string& line);
     89        void printOutputLine(const std::string& line, Shell::LineType type);
    9090        static void resetTerminalMode();
    9191
     
    102102        void writeText(const std::string& text, const COORD& pos, WORD attributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
    103103        void createNewOutputLines(int lines);
    104         void printOutputLine(const std::string& line, const COORD& pos);
     104        void printOutputLine(const std::string& line, Shell::LineType type, const COORD& pos);
    105105
    106106        static inline COORD makeCOORD(int x, int y)
  • code/branches/presentation2/src/libraries/core/Shell.cc

    r6139 r6180  
    4545    SetConsoleCommandShortcut(OutputHandler, debug);
    4646
    47     Shell::Shell(const std::string& consoleName, bool bScrollable, bool bPrependOutputLevel)
     47    Shell::Shell(const std::string& consoleName, bool bScrollable)
    4848        : OutputListener(consoleName)
    4949        , inputBuffer_(new InputBuffer())
    5050        , consoleName_(consoleName)
    51         , bPrependOutputLevel_(bPrependOutputLevel)
    5251        , bScrollable_(bScrollable)
    5352    {
     
    191190    }
    192191
    193     void Shell::addOutputLine(const std::string& line, int level)
     192    void Shell::addOutputLine(const std::string& line, LineType type)
    194193    {
    195194        // Make sure we really only have one line per line (no new lines!)
     
    197196        for (unsigned i = 0; i < lines.size(); ++i)
    198197        {
    199             if (level <= this->softDebugLevel_)
    200                 this->outputLines_.push_front(lines[i]);
     198            this->outputLines_.push_front(std::make_pair(lines[i], type));
    201199            this->updateListeners<&ShellListener::lineAdded>();
    202200        }
     
    214212    }
    215213
    216     std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
     214    Shell::LineList::const_iterator Shell::getNewestLineIterator() const
    217215    {
    218216        if (this->scrollPosition_)
     
    222220    }
    223221
    224     std::list<std::string>::const_iterator Shell::getEndIterator() const
     222    Shell::LineList::const_iterator Shell::getEndIterator() const
    225223    {
    226224        return this->outputLines_.end();
     
    264262            if (this->bFinishedLastLine_)
    265263            {
    266                 if (this->bPrependOutputLevel_)
    267                 {
    268                     if (level == 0)
    269                         output.insert(0, 1, static_cast<char>(-1));
    270                     else
    271                         output.insert(0, 1, static_cast<char>(level));
    272                 }
    273 
    274                 this->outputLines_.push_front(output);
     264                this->outputLines_.push_front(std::make_pair(output, static_cast<LineType>(level)));
    275265
    276266                if (this->scrollPosition_)
     
    288278            else
    289279            {
    290                 (*this->outputLines_.begin()) += output;
     280                this->outputLines_.front().first += output;
    291281                this->bFinishedLastLine_ = newline;
    292282                this->updateListeners<&ShellListener::onlyLastLineChanged>();
     
    325315
    326316        if (!CommandExecutor::execute(this->inputBuffer_->get()))
    327             this->addOutputLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
     317            this->addOutputLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", Error);
    328318
    329319        this->clearInput();
     
    333323    {
    334324        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
    335         this->addOutputLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
     325        this->addOutputLine(CommandExecutor::hint(this->inputBuffer_->get()), Hint);
    336326
    337327        this->inputChanged();
  • code/branches/presentation2/src/libraries/core/Shell.h

    r6105 r6180  
    6565    {
    6666        public:
    67             Shell(const std::string& consoleName, bool bScrollable, bool bPrependOutputLevel = false);
     67            enum LineType
     68            {
     69                None    = OutputLevel::None,
     70                Warning = OutputLevel::Warning,
     71                Error   = OutputLevel::Error,
     72                Info    = OutputLevel::Info,
     73                Debug   = OutputLevel::Debug,
     74                Verbose = OutputLevel::Verbose,
     75                Ultra   = OutputLevel::Ultra,
     76                Input,
     77                Command,
     78                Hint
     79            };
     80
     81            Shell(const std::string& consoleName, bool bScrollable);
    6882            ~Shell();
    6983
     
    8599                { return this->inputBuffer_->get(); }
    86100
    87             std::list<std::string>::const_iterator getNewestLineIterator() const;
    88             std::list<std::string>::const_iterator getEndIterator() const;
     101            typedef std::list<std::pair<std::string, LineType> > LineList;
     102            LineList::const_iterator getNewestLineIterator() const;
     103            LineList::const_iterator getEndIterator() const;
    89104
    90             void addOutputLine(const std::string& line, int level = 0);
     105            void addOutputLine(const std::string& line, LineType type = None);
    91106            void clearOutput();
    92107
     
    139154            std::stringstream         outputBuffer_;
    140155            bool                      bFinishedLastLine_;
    141             std::list<std::string>    outputLines_;
    142             std::list<std::string>::const_iterator scrollIterator_;
     156            LineList                  outputLines_;
     157            LineList::const_iterator scrollIterator_;
    143158            unsigned int              scrollPosition_;
    144159            unsigned int              historyPosition_;
     
    147162            std::string               promptPrefix_;
    148163            const std::string         consoleName_;
    149             const bool                bPrependOutputLevel_;
    150164            const bool                bScrollable_;
    151165
  • code/branches/presentation2/src/orxonox/overlays/InGameConsole.cc

    r6139 r6180  
    6868    */
    6969    InGameConsole::InGameConsole()
    70         : shell_(new Shell("InGameConsole", true, true))
     70        : shell_(new Shell("InGameConsole", true))
    7171        , consoleOverlay_(0)
    7272        , consoleOverlayContainer_(0)
     
    268268    void InGameConsole::linesChanged()
    269269    {
    270         std::list<std::string>::const_iterator it = this->shell_->getNewestLineIterator();
     270        Shell::LineList::const_iterator it = this->shell_->getNewestLineIterator();
    271271        int max = 0;
    272272        for (int i = 1; i < LINES; ++i)
     
    282282
    283283        for (int i = LINES - 1; i > max; --i)
    284             this->print("", i, true);
     284            this->print("", Shell::None, i, true);
    285285
    286286        for (int i = max; i >= 1; --i)
    287287        {
    288288            --it;
    289             this->print(*it, i, true);
     289            this->print(it->first, it->second, i, true);
    290290        }
    291291    }
     
    297297    {
    298298        if (LINES > 1)
    299             this->print(*this->shell_->getNewestLineIterator(), 1);
     299            this->print(this->shell_->getNewestLineIterator()->first, this->shell_->getNewestLineIterator()->second, 1);
    300300    }
    301301
     
    316316    {
    317317        if (LINES > 0)
    318             this->print(this->shell_->getInput(), 0);
     318            this->print(this->shell_->getInput(), Shell::Input, 0);
    319319
    320320        if (this->shell_->getInput() == "" || this->shell_->getInput().size() == 0)
     
    340340    void InGameConsole::executed()
    341341    {
    342         this->shell_->addOutputLine(this->shell_->getInput());
     342        this->shell_->addOutputLine(this->shell_->getInput(), Shell::Command);
    343343    }
    344344
     
    456456        @param s String to be printed
    457457    */
    458     void InGameConsole::print(const std::string& text, int index, bool alwaysShift)
    459     {
    460         char level = 0;
    461         if (text.size() > 0)
    462             level = text[0];
    463 
     458    void InGameConsole::print(const std::string& text, Shell::LineType type, int index, bool alwaysShift)
     459    {
    464460        std::string output = text;
    465 
    466         if (level >= -1 && level <= 5)
    467             output.erase(0, 1);
    468 
    469461        if (LINES > index)
    470462        {
    471             this->colourLine(level, index);
     463            this->colourLine(type, index);
    472464
    473465            if (index > 0)
     
    482474                    if (linesUsed > numLinesShifted_ || alwaysShift)
    483475                        this->shiftLines();
    484                     this->colourLine(level, index);
     476                    this->colourLine(type, index);
    485477                }
    486478                this->consoleOverlayTextAreas_[index]->setCaption(multi_cast<Ogre::DisplayString>(output));
     
    559551    }
    560552
    561     void InGameConsole::colourLine(int colourcode, int index)
    562     {
    563         if (colourcode == -1)
    564         {
    565             this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.90, 0.90, 0.90, 1.00));
    566             this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 1.00, 1.00, 1.00));
    567         }
    568         else if (colourcode == 1)
    569         {
    570             this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.25, 0.25, 1.00));
    571             this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.50, 0.50, 1.00));
    572         }
    573         else if (colourcode == 2)
    574         {
    575             this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.95, 0.50, 0.20, 1.00));
    576             this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.70, 0.50, 1.00));
    577         }
    578         else if (colourcode == 3)
    579         {
    580             this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.50, 0.50, 0.95, 1.00));
    581             this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.80, 1.00, 1.00));
    582         }
    583         else if (colourcode == 4)
    584         {
    585             this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.65, 0.48, 0.44, 1.00));
    586             this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(1.00, 0.90, 0.90, 1.00));
    587         }
    588         else if (colourcode == 5)
    589         {
    590             this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.40, 0.20, 0.40, 1.00));
    591             this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 0.60, 0.80, 1.00));
    592         }
    593         else
    594         {
    595             this->consoleOverlayTextAreas_[index]->setColourTop   (ColourValue(0.21, 0.69, 0.21, 1.00));
    596             this->consoleOverlayTextAreas_[index]->setColourBottom(ColourValue(0.80, 1.00, 0.80, 1.00));
    597         }
     553    void InGameConsole::colourLine(Shell::LineType type, int index)
     554    {
     555        ColourValue colourTop, colourBottom;
     556        switch (type)
     557        {
     558        case Shell::Error:   colourTop = ColourValue(0.95, 0.25, 0.25, 1.00);
     559                          colourBottom = ColourValue(1.00, 0.50, 0.50, 1.00); break;
     560
     561        case Shell::Warning: colourTop = ColourValue(0.95, 0.50, 0.20, 1.00);
     562                          colourBottom = ColourValue(1.00, 0.70, 0.50, 1.00); break;
     563
     564        case Shell::Info:    colourTop = ColourValue(0.50, 0.50, 0.95, 1.00);
     565                          colourBottom = ColourValue(0.80, 0.80, 1.00, 1.00); break;
     566
     567        case Shell::Debug:   colourTop = ColourValue(0.65, 0.48, 0.44, 1.00);
     568                          colourBottom = ColourValue(1.00, 0.90, 0.90, 1.00); break;
     569
     570        case Shell::Verbose: colourTop = ColourValue(0.40, 0.20, 0.40, 1.00);
     571                          colourBottom = ColourValue(0.80, 0.60, 0.80, 1.00); break;
     572
     573        case Shell::Ultra:   colourTop = ColourValue(0.21, 0.69, 0.21, 1.00);
     574                          colourBottom = ColourValue(0.80, 1.00, 0.80, 1.00); break;
     575
     576        case Shell::Command: colourTop = ColourValue(0.80, 0.80, 0.80, 1.00);
     577                          colourBottom = ColourValue(0.90, 0.90, 0.90, 0.90); break;
     578
     579        case Shell::Hint:    colourTop = ColourValue(0.80, 0.80, 0.80, 1.00);
     580                          colourBottom = ColourValue(0.90, 0.90, 0.90, 1.00); break;
     581
     582        default:             colourTop = ColourValue(0.90, 0.90, 0.90, 1.00);
     583                          colourBottom = ColourValue(1.00, 1.00, 1.00, 1.00); break;
     584        }
     585
     586        this->consoleOverlayTextAreas_[index]->setColourTop   (colourTop);
     587        this->consoleOverlayTextAreas_[index]->setColourBottom(colourBottom);
    598588    }
    599589
  • code/branches/presentation2/src/orxonox/overlays/InGameConsole.h

    r6105 r6180  
    7272
    7373        void shiftLines();
    74         void colourLine(int colourcode, int index);
     74        void colourLine(Shell::LineType type, int index);
    7575        void setCursorPosition(unsigned int pos);
    76         void print(const std::string& text, int index, bool alwaysShift = false);
     76        void print(const std::string& text, Shell::LineType type, int index, bool alwaysShift = false);
    7777
    7878        void windowResized(unsigned int newWidth, unsigned int newHeight);
Note: See TracChangeset for help on using the changeset viewer.