NCurses: refactor status line handling, and disambiguate status line from mode line

This commit is contained in:
Maxime Coste 2012-10-29 19:01:57 +01:00
parent 6f2c69a0eb
commit 254d43856d
5 changed files with 36 additions and 25 deletions

View File

@ -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<Utf8Policy>(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);
}

View File

@ -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<ITEM*> m_items;
@ -46,7 +50,6 @@ private:
DisplayCoord m_menu_pos;
DisplayCoord m_menu_size;
int m_menu_fg;
int m_menu_bg;
};

View File

@ -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<DisplayBuffer>(m_socket);
String status_line = read<String>(m_socket);
m_ui->draw(display_buffer, status_line);
String mode_line = read<String>(m_socket);
m_ui->draw(display_buffer, mode_line);
break;
}
}

View File

@ -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;

View File

@ -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;