Changeset 6180 for code/branches/presentation2/src/libraries/core
- Timestamp:
- Nov 29, 2009, 10:43:43 PM (15 years ago)
- Location:
- code/branches/presentation2/src/libraries/core
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2/src/libraries/core/IOConsole.cc
r6179 r6180 45 45 IOConsole* IOConsole::singletonPtr_s = NULL; 46 46 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 letter51 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 65 47 // ############################### 66 48 // ### ShellListener methods ### … … 78 60 void IOConsole::executed() 79 61 { 80 this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput() );62 this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput(), Shell::Command); 81 63 } 82 64 … … 109 91 110 92 IOConsole::IOConsole() 111 : shell_(new Shell("IOConsole", false , true))93 : shell_(new Shell("IOConsole", false)) 112 94 , buffer_(shell_->getInputBuffer()) 113 95 , cout_(std::cout.rdbuf()) … … 280 262 if (!this->origCout_.str().empty()) 281 263 { 282 this->shell_->addOutputLine(this->origCout_.str() );264 this->shell_->addOutputLine(this->origCout_.str(), Shell::None); 283 265 this->origCout_.str(""); 284 266 } 285 267 } 286 268 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 { 292 271 /* 293 272 // 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; 303 282 default: break; 304 283 } … … 306 285 307 286 // Print output line 308 this->cout_ << output;287 this->cout_ << text; 309 288 310 289 // Reset colour to white … … 419 398 this->cout_ << "\033[K"; 420 399 // Reprint the last output line 421 this->printOutputLine( *(this->shell_->getNewestLineIterator()));400 this->printOutputLine(this->shell_->getNewestLineIterator()->first); 422 401 // Restore cursor 423 402 this->cout_ << "\033[u"; … … 428 407 void IOConsole::lineAdded() 429 408 { 430 int newLines = this->shell_->getNewestLineIterator()-> size() / this->terminalWidth_ + 1;409 int newLines = this->shell_->getNewestLineIterator()->first.size() / this->terminalWidth_ + 1; 431 410 // Create new lines by scrolling the screen 432 411 this->cout_ << "\033[" << newLines << 'S'; … … 437 416 // Print the new output lines 438 417 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_)); 440 419 // Move cursor down 441 420 this->cout_ << "\033[1B\033[1G"; … … 472 451 //! Redirects std::cout, creates the corresponding Shell and changes the terminal mode 473 452 IOConsole::IOConsole() 474 : shell_(new Shell("IOConsole", false , true))453 : shell_(new Shell("IOConsole", false)) 475 454 , buffer_(shell_->getInputBuffer()) 476 455 , cout_(std::cout.rdbuf()) … … 613 592 if (!this->origCout_.str().empty()) 614 593 { 615 this->shell_->addOutputLine(this->origCout_.str() );594 this->shell_->addOutputLine(this->origCout_.str(), Shell::None); 616 595 this->origCout_.str(""); 617 596 } … … 619 598 620 599 //! 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 { 626 602 // Colour line 627 603 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; 635 615 } 636 616 637 617 // Print output line 638 this->writeText( output, pos, colour);618 this->writeText(text, pos, colour); 639 619 } 640 620 … … 783 763 void IOConsole::onlyLastLineChanged() 784 764 { 785 int newLineHeight = 1 + this->shell_->getNewestLineIterator()-> size() / this->terminalWidth_;765 int newLineHeight = 1 + this->shell_->getNewestLineIterator()->first.size() / this->terminalWidth_; 786 766 // Compute the number of new lines needed 787 767 int newLines = newLineHeight - this->lastOutputLineHeight_; … … 790 770 if (newLines > 0) // newLines < 0 is assumed impossible 791 771 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)); 793 774 } 794 775 … … 796 777 void IOConsole::lineAdded() 797 778 { 779 Shell::LineList::const_iterator it = this->shell_->getNewestLineIterator(); 798 780 // Scroll console 799 this->lastOutputLineHeight_ = 1 + this->shell_->getNewestLineIterator()->size() / this->terminalWidth_;781 this->lastOutputLineHeight_ = 1 + it->first.size() / this->terminalWidth_; 800 782 this->createNewOutputLines(this->lastOutputLineHeight_); 801 783 // Write the text 802 784 COORD pos = {0, this->inputLineRow_ - this->lastOutputLineHeight_}; 803 this->printOutputLine( *(this->shell_->getNewestLineIterator()), pos);785 this->printOutputLine(it->first, it->second, pos); 804 786 } 805 787 } -
code/branches/presentation2/src/libraries/core/IOConsole.h
r6179 r6180 87 87 bool willPrintStatusLines(); 88 88 void printInputLine(); 89 void printOutputLine(const std::string& line );89 void printOutputLine(const std::string& line, Shell::LineType type); 90 90 static void resetTerminalMode(); 91 91 … … 102 102 void writeText(const std::string& text, const COORD& pos, WORD attributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); 103 103 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); 105 105 106 106 static inline COORD makeCOORD(int x, int y) -
code/branches/presentation2/src/libraries/core/Shell.cc
r6139 r6180 45 45 SetConsoleCommandShortcut(OutputHandler, debug); 46 46 47 Shell::Shell(const std::string& consoleName, bool bScrollable , bool bPrependOutputLevel)47 Shell::Shell(const std::string& consoleName, bool bScrollable) 48 48 : OutputListener(consoleName) 49 49 , inputBuffer_(new InputBuffer()) 50 50 , consoleName_(consoleName) 51 , bPrependOutputLevel_(bPrependOutputLevel)52 51 , bScrollable_(bScrollable) 53 52 { … … 191 190 } 192 191 193 void Shell::addOutputLine(const std::string& line, int level)192 void Shell::addOutputLine(const std::string& line, LineType type) 194 193 { 195 194 // Make sure we really only have one line per line (no new lines!) … … 197 196 for (unsigned i = 0; i < lines.size(); ++i) 198 197 { 199 if (level <= this->softDebugLevel_) 200 this->outputLines_.push_front(lines[i]); 198 this->outputLines_.push_front(std::make_pair(lines[i], type)); 201 199 this->updateListeners<&ShellListener::lineAdded>(); 202 200 } … … 214 212 } 215 213 216 std::list<std::string>::const_iterator Shell::getNewestLineIterator() const214 Shell::LineList::const_iterator Shell::getNewestLineIterator() const 217 215 { 218 216 if (this->scrollPosition_) … … 222 220 } 223 221 224 std::list<std::string>::const_iterator Shell::getEndIterator() const222 Shell::LineList::const_iterator Shell::getEndIterator() const 225 223 { 226 224 return this->outputLines_.end(); … … 264 262 if (this->bFinishedLastLine_) 265 263 { 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))); 275 265 276 266 if (this->scrollPosition_) … … 288 278 else 289 279 { 290 (*this->outputLines_.begin())+= output;280 this->outputLines_.front().first += output; 291 281 this->bFinishedLastLine_ = newline; 292 282 this->updateListeners<&ShellListener::onlyLastLineChanged>(); … … 325 315 326 316 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); 328 318 329 319 this->clearInput(); … … 333 323 { 334 324 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); 336 326 337 327 this->inputChanged(); -
code/branches/presentation2/src/libraries/core/Shell.h
r6105 r6180 65 65 { 66 66 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); 68 82 ~Shell(); 69 83 … … 85 99 { return this->inputBuffer_->get(); } 86 100 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; 89 104 90 void addOutputLine(const std::string& line, int level = 0);105 void addOutputLine(const std::string& line, LineType type = None); 91 106 void clearOutput(); 92 107 … … 139 154 std::stringstream outputBuffer_; 140 155 bool bFinishedLastLine_; 141 std::list<std::string>outputLines_;142 std::list<std::string>::const_iteratorscrollIterator_;156 LineList outputLines_; 157 LineList::const_iterator scrollIterator_; 143 158 unsigned int scrollPosition_; 144 159 unsigned int historyPosition_; … … 147 162 std::string promptPrefix_; 148 163 const std::string consoleName_; 149 const bool bPrependOutputLevel_;150 164 const bool bScrollable_; 151 165
Note: See TracChangeset
for help on using the changeset viewer.