summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-11-18 20:12:16 +0000
committerMaxime Coste <frrrwww@gmail.com>2015-11-18 20:12:16 +0000
commit8d47cf06743b387b4c3fb3f039f747770cc1f4c9 (patch)
tree1892e8649e0bda550b53ca3ee97085c7da5d0ba9 /src
parent6925ff33b1c864e3acd11337cfd4aab98bc165a3 (diff)
Fix correctly ncurses menu size computations and avoid divide by 0
Diffstat (limited to 'src')
-rw-r--r--src/ncurses_ui.cc21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc
index d8e4ffa4..e89cdc4d 100644
--- a/src/ncurses_ui.cc
+++ b/src/ncurses_ui.cc
@@ -644,18 +644,23 @@ void NCursesUI::menu_show(ConstArrayView<DisplayLine> items,
const int item_count = items.size();
m_items.clear(); // make sure it is empty
m_items.reserve(item_count);
- CharCount longest = 0;
- const CharCount maxlen = min((int)maxsize.column-1, 200);
+ CharCount longest = 1;
+ for (auto& item : items)
+ longest = max(longest, item.length());
+
+ const bool is_prompt = style == MenuStyle::Prompt;
+ m_menu_columns = is_prompt ? max((int)((maxsize.column - 1) / longest), 1) : 1;
+
+ CharCount maxlen = maxsize.column-1;
+ if (m_menu_columns > 1)
+ maxlen = maxlen / m_menu_columns - 1;
+
for (auto& item : items)
{
m_items.push_back(item);
m_items.back().trim(0, maxlen, false);
- longest = max(longest, m_items.back().length());
+ kak_assert(m_items.back().length() <= maxlen);
}
- longest += 1;
-
- const bool is_prompt = style == MenuStyle::Prompt;
- m_menu_columns = is_prompt ? (int)((maxsize.column - 1) / longest) : 1;
int height = min(10, div_round_up(item_count, m_menu_columns));
@@ -665,7 +670,7 @@ void NCursesUI::menu_show(ConstArrayView<DisplayLine> items,
m_selected_item = item_count;
m_menu_top_line = 0;
- int width = is_prompt ? (int)maxsize.column : (int)longest;
+ auto width = is_prompt ? maxsize.column : min(longest+1, maxsize.column);
m_menu.create({line, anchor.column}, {height, width});
draw_menu();
}