From 254d43856da7c268f42b3604a233b3257345c4ac Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 29 Oct 2012 19:01:57 +0100 Subject: [PATCH] NCurses: refactor status line handling, and disambiguate status line from mode line --- src/ncurses.cc | 42 +++++++++++++++++++++++++----------------- src/ncurses.hh | 7 +++++-- src/remote.cc | 8 ++++---- src/remote.hh | 2 +- src/user_interface.hh | 2 +- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/ncurses.cc b/src/ncurses.cc index 1fdb1202..1e601a3b 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -146,7 +146,7 @@ void NCursesUI::update_dimensions() } void NCursesUI::draw(const DisplayBuffer& display_buffer, - const String& status_line) + const String& mode_line) { LineCount line_index = 0; for (const DisplayLine& line : display_buffer.lines()) @@ -195,13 +195,15 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer, } set_color(Color::Cyan, Color::Black); - static int last_status_length = 0; - move((int)m_dimensions.line, (int)(m_dimensions.column - last_status_length)); - clrtoeol(); - move((int)m_dimensions.line, (int)(m_dimensions.column - status_line.char_length())); - addstr(status_line.c_str()); - last_status_length = (int)status_line.length(); - + draw_status(); + CharCount status_len = mode_line.char_length(); + // only draw mode_line if it does not overlap one status line + if (m_dimensions.column - m_status_line.char_length() > status_len + 1) + { + move((int)m_dimensions.line, (int)(m_dimensions.column - status_len)); + addutf8str(Utf8Iterator(mode_line.begin()), + Utf8Iterator(mode_line.end())); + } redraw(m_menu_win); } @@ -259,25 +261,31 @@ Key NCursesUI::get_key() return Key::Invalid; } -void NCursesUI::print_status(const String& status, CharCount cursor_pos) +void NCursesUI::draw_status() { - int x,y; - getmaxyx(stdscr, y, x); - move(y-1, 0); + move((int)m_dimensions.line, 0); clrtoeol(); - if (cursor_pos == -1) - addutf8str(status.begin(), status.end()); + if (m_status_cursor == -1) + addutf8str(m_status_line.begin(), m_status_line.end()); else { - auto cursor_it = utf8::advance(status.begin(), status.end(), (int)cursor_pos); - auto end = status.end(); - addutf8str(status.begin(), cursor_it); + auto cursor_it = utf8::advance(m_status_line.begin(), m_status_line.end(), + (int)m_status_cursor); + auto end = m_status_line.end(); + addutf8str(m_status_line.begin(), cursor_it); set_attribute(A_REVERSE, 1); addch((cursor_it == end) ? ' ' : utf8::codepoint(cursor_it)); set_attribute(A_REVERSE, 0); if (cursor_it != end) addutf8str(utf8::next(cursor_it), end); } +} + +void NCursesUI::print_status(const String& status, CharCount cursor_pos) +{ + m_status_line = status; + m_status_cursor = cursor_pos; + draw_status(); redraw(m_menu_win); } diff --git a/src/ncurses.hh b/src/ncurses.hh index 9267191b..080316cb 100644 --- a/src/ncurses.hh +++ b/src/ncurses.hh @@ -20,7 +20,7 @@ public: NCursesUI& operator=(const NCursesUI&) = delete; void draw(const DisplayBuffer& display_buffer, - const String& status_line) override; + const String& mode_line) override; void print_status(const String& status, CharCount cursor_pos) override; bool is_key_available() override; @@ -38,6 +38,10 @@ private: DisplayCoord m_dimensions; void update_dimensions(); + String m_status_line; + CharCount m_status_cursor = -1; + void draw_status(); + MENU* m_menu = nullptr; WINDOW* m_menu_win = nullptr; std::vector m_items; @@ -46,7 +50,6 @@ private: DisplayCoord m_menu_pos; DisplayCoord m_menu_size; - int m_menu_fg; int m_menu_bg; }; diff --git a/src/remote.cc b/src/remote.cc index a5bd87b9..7a715274 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -183,12 +183,12 @@ void RemoteUI::menu_hide() } void RemoteUI::draw(const DisplayBuffer& display_buffer, - const String& status_line) + const String& mode_line) { Message msg(m_socket); write(msg, RemoteUIMsg::Draw); write(msg, display_buffer); - write(msg, status_line); + write(msg, mode_line); } static const Key::Modifiers resize_modifier = (Key::Modifiers)0x80; @@ -260,8 +260,8 @@ void RemoteClient::process_next_message() case RemoteUIMsg::Draw: { DisplayBuffer display_buffer = read(m_socket); - String status_line = read(m_socket); - m_ui->draw(display_buffer, status_line); + String mode_line = read(m_socket); + m_ui->draw(display_buffer, mode_line); break; } } diff --git a/src/remote.hh b/src/remote.hh index 8a3ca470..aa14eeb3 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -23,7 +23,7 @@ public: void menu_select(int selected) override; void menu_hide() override; void draw(const DisplayBuffer& display_buffer, - const String& status_line) override; + const String& mode_line) override; bool is_key_available() override; Key get_key() override; diff --git a/src/user_interface.hh b/src/user_interface.hh index 91e5c47c..3d474322 100644 --- a/src/user_interface.hh +++ b/src/user_interface.hh @@ -29,7 +29,7 @@ public: virtual void menu_select(int selected) = 0; virtual void menu_hide() = 0; virtual void draw(const DisplayBuffer& display_buffer, - const String& status_line) = 0; + const String& mode_line) = 0; virtual DisplayCoord dimensions() = 0; virtual bool is_key_available() = 0; virtual Key get_key() = 0;