From e5aada2bf01e51753829215c0a3035aa8bd8135a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 5 Jul 2008 10:17:39 +0000 Subject: ncurses updates, refactored say --- src/server/console.cc | 91 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 18 deletions(-) (limited to 'src/server/console.cc') diff --git a/src/server/console.cc b/src/server/console.cc index 6b24c0c..cdaa1f9 100644 --- a/src/server/console.cc +++ b/src/server/console.cc @@ -21,6 +21,8 @@ namespace server { bool console_initialized = false; bool console_updated = false; +const size_t MAXHISTOLINES = 512; + Console server_console; #ifdef HAVE_CURSES @@ -63,7 +65,11 @@ void Console::init() con_print << "Initializing console..." << std::endl; #ifdef HAVE_CURSES - server_console.console_lastrefresh = 1; + server_console.history.clear(); + server_console.history.push_back(""); + server_console.history_pos = server_console.history.rbegin(); + server_console.input_pos = 0; + server_console.draw(); #endif // HAVE_CURSES } @@ -87,14 +93,8 @@ void Console::resize() endwin(); refresh(); - - draw_background(); - draw_text(); - draw_input(); - wrefresh(stdwin); - console_updated = false; - console_lastrefresh = 0; + draw(); } void Console::flush() @@ -199,7 +199,7 @@ void Console::draw_text() int bottom = (int) consoleinterface_text.size() - console_scroll; int current_line = 0; - // parse cons0ole text, wrap long lines + // parse console text, wrap long lines for (std::deque::iterator it = consoleinterface_text.begin(); it != consoleinterface_text.end() && current_line < bottom; it++) { if (current_line >= bottom - height) { std::string linedata(*it); @@ -312,18 +312,21 @@ void Console::draw_text() void Console::draw_input() { - wmove(stdwin, stdwin->_maxy, 0); + color_set(0, NULL); + attron(A_BOLD); + // draw input text + mvaddstr(stdwin->_maxy, 0, ">"); + mvaddstr(stdwin->_maxy, 1, (*history_pos).c_str()); + // fill the remainder with spaces + for (int i=1 + (*history_pos).size(); i < stdwin->_maxx; i++) + addch(' '); + // move the cursor to input position + move(stdwin->_maxy, 1 + (*history_pos).size()); } void Console::draw() { - flush(); - - if (console_lastrefresh < 0.1) { - return; - } - - if (!(console_initialized && console_updated && stdwin->_maxx && stdwin->_maxy)) + if (!console_initialized) return; draw_background(); @@ -337,17 +340,69 @@ void Console::draw() void Console::frame(float seconds) { + flush(); + if (!console_initialized) return; console_lastrefresh += seconds; + bool input_updated = false; int key = wgetch(stdwin); while (key != ERR) { + if (key == KEY_BACKSPACE || key == 8 || key == 127) { + if ((*history_pos).size() && input_pos) { + (*history_pos).erase(input_pos-1, 1); + input_pos--; + input_updated = true; + } + break; + } else if (key == KEY_STAB || key ==9) { + core::CommandBuffer::complete( (*history_pos), input_pos); + input_updated = true; + break; + } else if (key == KEY_LEFT) { + } else if (key == KEY_HOME) { + } else if (key == KEY_RIGHT) { + } else if (key == KEY_END) { + } else if (key == KEY_UP) { + } else if (key == KEY_DOWN) { + } else if (key == KEY_ENTER || key == '\n') { + if ((*history_pos).size()) { + // store input into history + while (history.size() >= MAXHISTOLINES) { + history.pop_front(); + } + + core::cmd() << (*history_pos) << std::endl; + con_print << "^B>" << (*history_pos) << std::endl; + (*history.rbegin()) = (*history_pos); + + history.push_back(""); + history_pos = history.rbegin(); + input_pos = 0; + input_updated = true; + } + break; + } else if (key == KEY_PPAGE) { + } else if (key == KEY_NPAGE) { + } else if ((key >= 32) && (key < 127) && ((*history_pos).size() < MAXCMDSIZE)) { + if (input_pos == (*history_pos).size()) { + (*history_pos) += (char)key; + } else { + (*history_pos).insert(input_pos, 1, (char)key); + } + input_pos++; + input_updated = true; + } key = wgetch(stdwin); } - draw(); + if (console_updated) { + draw(); + } else if (input_updated) { + draw_input(); + } } #endif // HAVE_CURSES -- cgit v1.2.3