Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.