Avoid wrapping lines with negative max width

This commit is contained in:
Maxime Coste 2015-09-07 23:29:01 +01:00
parent 53b65401f8
commit 29b508a894
2 changed files with 8 additions and 1 deletions

View File

@ -779,14 +779,18 @@ String make_info_box(StringView title, StringView message, CharCount max_width,
if (not assistant.empty()) if (not assistant.empty())
assistant_size = { (int)assistant.size(), assistant[0].char_length() }; assistant_size = { (int)assistant.size(), assistant[0].char_length() };
String result;
const CharCount max_bubble_width = max_width - assistant_size.column - 6; const CharCount max_bubble_width = max_width - assistant_size.column - 6;
if (max_bubble_width < 4)
return result;
Vector<StringView> lines = wrap_lines(message, max_bubble_width); Vector<StringView> lines = wrap_lines(message, max_bubble_width);
CharCount bubble_width = title.char_length() + 2; CharCount bubble_width = title.char_length() + 2;
for (auto& line : lines) for (auto& line : lines)
bubble_width = max(bubble_width, line.char_length()); bubble_width = max(bubble_width, line.char_length());
String result;
auto line_count = max(assistant_size.line-1, auto line_count = max(assistant_size.line-1,
LineCount{(int)lines.size()} + 2); LineCount{(int)lines.size()} + 2);
for (LineCount i = 0; i < line_count; ++i) for (LineCount i = 0; i < line_count; ++i)

View File

@ -218,6 +218,9 @@ String expand_tabs(StringView line, CharCount tabstop, CharCount col)
Vector<StringView> wrap_lines(StringView text, CharCount max_width) Vector<StringView> wrap_lines(StringView text, CharCount max_width)
{ {
if (max_width <= 0)
throw runtime_error("Invalid max width");
using Utf8It = utf8::iterator<const char*>; using Utf8It = utf8::iterator<const char*>;
Utf8It word_begin{text.begin()}; Utf8It word_begin{text.begin()};
Utf8It word_end{word_begin}; Utf8It word_end{word_begin};