From a1e3fa02e6d4bf635b4c1ec946ee4fdfe5a3c150 Mon Sep 17 00:00:00 2001 From: Justin Frank Date: Fri, 20 Jul 2018 10:30:47 -0700 Subject: [PATCH 1/2] added option for vertical menu layout in the ncurses ui --- src/main.cc | 1 + src/ncurses_ui.cc | 61 ++++++++++++++++++++++++++++++++++++----------- src/ncurses_ui.hh | 2 ++ 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/main.cc b/src/main.cc index 6fda5da6..e8e5564f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -396,6 +396,7 @@ void register_options() "The ncurses ui supports the following options:\n" " : :\n" " ncurses_assistant clippy|cat|dilbert|none|off\n" + " ncurses_menu_layout horizontal|vertical\n" " ncurses_status_on_top bool\n" " ncurses_set_title bool\n" " ncurses_enable_mouse bool\n" diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index e830c012..bf4517aa 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -741,16 +741,31 @@ void NCursesUI::draw_menu() const LineCount mark_height = min(div_round_up(sq(win_height), menu_lines), win_height); const LineCount top_line = m_menu.first_item / m_menu.columns; - const LineCount mark_line = (win_height - mark_height) * top_line / max(1_line, menu_lines - win_height); + + const int menu_cols = div_round_up(item_count, (int)m_menu.size.line); + const int first_col = m_menu.first_item / (int)m_menu.size.line; + + const LineCount mark_line = m_menu_vertical ? (win_height - mark_height) * first_col / max(1, menu_cols - m_menu.columns) : + (win_height - mark_height) * top_line / max(1_line, menu_lines - win_height); + for (auto line = 0_line; line < win_height; ++line) { wmove(m_menu.win, (int)line, 0); for (int col = 0; col < m_menu.columns; ++col) { - const int item_idx = (int)(top_line + line) * m_menu.columns - + col; - if (item_idx >= item_count) - break; + int item_idx; + if (m_menu_vertical) + { + item_idx = (first_col + col) * (int)m_menu.size.line + (int)line; + if (item_idx >= item_count) + continue; + } + else + { + item_idx = (int)(top_line + line) * m_menu.columns + col; + if (item_idx >= item_count) + break; + } const DisplayLine& item = m_menu.items[item_idx]; draw_line(m_menu.win, item, 0, column_width, @@ -883,15 +898,28 @@ void NCursesUI::menu_select(int selected) else { m_menu.selected_item = selected; - const LineCount win_height = m_menu.size.line; - const LineCount top_line = m_menu.first_item / m_menu.columns; - const LineCount menu_lines = div_round_up(item_count, m_menu.columns); - const LineCount selected_line = m_menu.selected_item / m_menu.columns; - kak_assert(menu_lines >= win_height); - if (selected_line < top_line) - m_menu.first_item = (int)selected_line * m_menu.columns; - if (selected_line >= top_line + win_height) - m_menu.first_item = (int)min(selected_line, menu_lines - win_height) * m_menu.columns; + if (m_menu_vertical) + { + const int menu_cols = div_round_up(item_count, (int)m_menu.size.line); + const int first_col = m_menu.first_item / (int)m_menu.size.line; + const int selected_col = m_menu.selected_item / (int)m_menu.size.line; + if (selected_col < first_col) + m_menu.first_item = selected_col * (int)m_menu.size.line; + if (selected_col >= first_col + m_menu.columns) + m_menu.first_item = min(selected_col, menu_cols - m_menu.columns) * (int)m_menu.size.line; + } + else + { + const LineCount menu_lines = div_round_up(item_count, m_menu.columns); + const LineCount win_height = m_menu.size.line; + const LineCount top_line = m_menu.first_item / m_menu.columns; + const LineCount selected_line = m_menu.selected_item / m_menu.columns; + kak_assert(menu_lines >= win_height); + if (selected_line < top_line) + m_menu.first_item = (int)selected_line * m_menu.columns; + if (selected_line >= top_line + win_height) + m_menu.first_item = (int)min(selected_line, menu_lines - win_height) * m_menu.columns; + } } draw_menu(); } @@ -1229,6 +1257,11 @@ void NCursesUI::set_ui_options(const Options& options) m_wheel_down_button = wheel_down_it != options.end() ? str_to_int_ifp(wheel_down_it->value).value_or(5) : 5; } + + { + auto it = options.find("ncurses_menu_layout"_sv); + m_menu_vertical = it != options.end() and it->value == "vertical"; + } } } diff --git a/src/ncurses_ui.hh b/src/ncurses_ui.hh index 69eee61c..76b70c5b 100644 --- a/src/ncurses_ui.hh +++ b/src/ncurses_ui.hh @@ -147,6 +147,8 @@ private: bool m_set_title = true; bool m_change_colors = true; + bool m_menu_vertical = false; + bool m_dirty = false; bool m_resize_pending = false; From c5a12256388b66335281ff76edf0c2fa1acd84cd Mon Sep 17 00:00:00 2001 From: Justin Frank Date: Fri, 10 Aug 2018 10:14:48 -0700 Subject: [PATCH 2/2] removed option and horizontal layout --- src/main.cc | 1 - src/ncurses_ui.cc | 54 ++++++++++------------------------------------- src/ncurses_ui.hh | 2 -- 3 files changed, 11 insertions(+), 46 deletions(-) diff --git a/src/main.cc b/src/main.cc index e8e5564f..6fda5da6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -396,7 +396,6 @@ void register_options() "The ncurses ui supports the following options:\n" " : :\n" " ncurses_assistant clippy|cat|dilbert|none|off\n" - " ncurses_menu_layout horizontal|vertical\n" " ncurses_status_on_top bool\n" " ncurses_set_title bool\n" " ncurses_enable_mouse bool\n" diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index bf4517aa..aa6b9d7d 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -740,32 +740,20 @@ void NCursesUI::draw_menu() const LineCount mark_height = min(div_round_up(sq(win_height), menu_lines), win_height); - const LineCount top_line = m_menu.first_item / m_menu.columns; const int menu_cols = div_round_up(item_count, (int)m_menu.size.line); const int first_col = m_menu.first_item / (int)m_menu.size.line; - const LineCount mark_line = m_menu_vertical ? (win_height - mark_height) * first_col / max(1, menu_cols - m_menu.columns) : - (win_height - mark_height) * top_line / max(1_line, menu_lines - win_height); + const LineCount mark_line = (win_height - mark_height) * first_col / max(1, menu_cols - m_menu.columns); for (auto line = 0_line; line < win_height; ++line) { wmove(m_menu.win, (int)line, 0); for (int col = 0; col < m_menu.columns; ++col) { - int item_idx; - if (m_menu_vertical) - { - item_idx = (first_col + col) * (int)m_menu.size.line + (int)line; - if (item_idx >= item_count) - continue; - } - else - { - item_idx = (int)(top_line + line) * m_menu.columns + col; - if (item_idx >= item_count) - break; - } + int item_idx = (first_col + col) * (int)m_menu.size.line + (int)line; + if (item_idx >= item_count) + continue; const DisplayLine& item = m_menu.items[item_idx]; draw_line(m_menu.win, item, 0, column_width, @@ -898,28 +886,13 @@ void NCursesUI::menu_select(int selected) else { m_menu.selected_item = selected; - if (m_menu_vertical) - { - const int menu_cols = div_round_up(item_count, (int)m_menu.size.line); - const int first_col = m_menu.first_item / (int)m_menu.size.line; - const int selected_col = m_menu.selected_item / (int)m_menu.size.line; - if (selected_col < first_col) - m_menu.first_item = selected_col * (int)m_menu.size.line; - if (selected_col >= first_col + m_menu.columns) - m_menu.first_item = min(selected_col, menu_cols - m_menu.columns) * (int)m_menu.size.line; - } - else - { - const LineCount menu_lines = div_round_up(item_count, m_menu.columns); - const LineCount win_height = m_menu.size.line; - const LineCount top_line = m_menu.first_item / m_menu.columns; - const LineCount selected_line = m_menu.selected_item / m_menu.columns; - kak_assert(menu_lines >= win_height); - if (selected_line < top_line) - m_menu.first_item = (int)selected_line * m_menu.columns; - if (selected_line >= top_line + win_height) - m_menu.first_item = (int)min(selected_line, menu_lines - win_height) * m_menu.columns; - } + const int menu_cols = div_round_up(item_count, (int)m_menu.size.line); + const int first_col = m_menu.first_item / (int)m_menu.size.line; + const int selected_col = m_menu.selected_item / (int)m_menu.size.line; + if (selected_col < first_col) + m_menu.first_item = selected_col * (int)m_menu.size.line; + if (selected_col >= first_col + m_menu.columns) + m_menu.first_item = min(selected_col, menu_cols - m_menu.columns) * (int)m_menu.size.line; } draw_menu(); } @@ -1257,11 +1230,6 @@ void NCursesUI::set_ui_options(const Options& options) m_wheel_down_button = wheel_down_it != options.end() ? str_to_int_ifp(wheel_down_it->value).value_or(5) : 5; } - - { - auto it = options.find("ncurses_menu_layout"_sv); - m_menu_vertical = it != options.end() and it->value == "vertical"; - } } } diff --git a/src/ncurses_ui.hh b/src/ncurses_ui.hh index 76b70c5b..69eee61c 100644 --- a/src/ncurses_ui.hh +++ b/src/ncurses_ui.hh @@ -147,8 +147,6 @@ private: bool m_set_title = true; bool m_change_colors = true; - bool m_menu_vertical = false; - bool m_dirty = false; bool m_resize_pending = false;