From 8d47cf06743b387b4c3fb3f039f747770cc1f4c9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 18 Nov 2015 20:12:16 +0000 Subject: [PATCH] Fix correctly ncurses menu size computations and avoid divide by 0 --- src/ncurses_ui.cc | 21 +++++++++++++-------- 1 file 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 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 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(); }