Changeset 6010 for code/branches/console/src/libraries/core/Shell.cc
- Timestamp:
- Oct 31, 2009, 11:13:52 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/console/src/libraries/core/Shell.cc
r6004 r6010 23 23 * Fabian 'x3n' Landau 24 24 * Co-authors: 25 * ...25 * Reto Grieder 26 26 * 27 27 */ … … 44 44 SetConsoleCommandShortcut(OutputHandler, debug); 45 45 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()) 49 49 , consoleName_(consoleName) 50 , bPrependOutputLevel_(bPrependOutputLevel) 50 51 , bScrollable_(bScrollable) 51 52 { … … 56 57 this->historyPosition_ = 0; 57 58 this->historyOffset_ = 0; 58 this->finishedLastLine_ = true; 59 this->bAddOutputLevel_ = false; 60 61 this->clearLines(); 59 this->bFinishedLastLine_ = true; 60 61 this->clearOutput(); 62 62 this->configureInputBuffer(); 63 63 … … 131 131 { 132 132 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 } 150 156 } 151 157 … … 156 162 157 163 for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i) 158 instance.add Line(instance.commandHistory_[i], -1);164 instance.addOutputLine(instance.commandHistory_[i], -1); 159 165 for (unsigned int i = 0; i < instance.historyOffset_; ++i) 160 instance.add Line(instance.commandHistory_[i], -1);166 instance.addOutputLine(instance.commandHistory_[i], -1); 161 167 } 162 168 */ … … 184 190 } 185 191 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) 193 193 { 194 194 if (level <= this->softDebugLevel_) … … 197 197 } 198 198 199 void Shell::clear Lines()199 void Shell::clearOutput() 200 200 { 201 201 this->outputLines_.clear(); … … 203 203 204 204 this->scrollPosition_ = 0; 205 this-> finishedLastLine_ = true;205 this->bFinishedLastLine_ = true; 206 206 207 207 this->updateListeners<&ShellListener::linesChanged>(); … … 256 256 break; 257 257 258 if (this-> finishedLastLine_)258 if (this->bFinishedLastLine_) 259 259 { 260 if (this->b AddOutputLevel_)260 if (this->bPrependOutputLevel_) 261 261 output.insert(0, 1, static_cast<char>(level)); 262 262 … … 268 268 this->scrollIterator_ = this->outputLines_.begin(); 269 269 270 this-> finishedLastLine_ = newline;270 this->bFinishedLastLine_ = newline; 271 271 272 272 if (!this->scrollPosition_) … … 278 278 { 279 279 (*this->outputLines_.begin()) += output; 280 this-> finishedLastLine_ = newline;280 this->bFinishedLastLine_ = newline; 281 281 this->updateListeners<&ShellListener::onlyLastLineChanged>(); 282 282 } … … 285 285 } 286 286 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() 326 288 { 327 289 this->inputBuffer_->clear(); … … 331 293 } 332 294 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() 334 358 { 335 359 this->inputBuffer_->increaseCursor(); … … 337 361 } 338 362 339 void Shell::cursor _left()363 void Shell::cursorLeft() 340 364 { 341 365 this->inputBuffer_->decreaseCursor(); … … 343 367 } 344 368 345 void Shell::cursor _end()369 void Shell::cursorEnd() 346 370 { 347 371 this->inputBuffer_->setCursorToEnd(); … … 349 373 } 350 374 351 void Shell::cursor _home()375 void Shell::cursorHome() 352 376 { 353 377 this->inputBuffer_->setCursorToBegin(); … … 355 379 } 356 380 357 void Shell::history _up()381 void Shell::historyUp() 358 382 { 359 383 if (this->historyPosition_ < this->commandHistory_.size()) … … 364 388 } 365 389 366 void Shell::history _down()390 void Shell::historyDown() 367 391 { 368 392 if (this->historyPosition_ > 0) … … 373 397 } 374 398 375 void Shell::history _search_up()399 void Shell::historySearchUp() 376 400 { 377 401 if (this->historyPosition_ == this->historyOffset_) 378 402 return; 379 403 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 until lthe cursor position404 std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline until the cursor position 381 405 for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++) 382 406 { … … 391 415 } 392 416 393 void Shell::history _search_down()417 void Shell::historySearchDown() 394 418 { 395 419 if (this->historyPosition_ == 0) 396 420 return; 397 421 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 399 423 for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--) 400 424 { … … 409 433 } 410 434 411 void Shell::scroll _up()435 void Shell::scrollUp() 412 436 { 413 437 if (this->scrollIterator_ != this->outputLines_.end()) … … 420 444 } 421 445 422 void Shell::scroll _down()446 void Shell::scrollDown() 423 447 { 424 448 if (this->scrollIterator_ != this->outputLines_.begin()) … … 430 454 } 431 455 } 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 }447 456 }
Note: See TracChangeset
for help on using the changeset viewer.