Use StringView in UserInterface and NCursesUI

This commit is contained in:
Maxime Coste 2014-04-30 19:39:52 +01:00
parent bd4df27f2b
commit 4d45fa6588
4 changed files with 29 additions and 18 deletions

View File

@ -204,7 +204,7 @@ void NCursesUI::refresh()
} }
using Utf8Policy = utf8::InvalidBytePolicy::Pass; using Utf8Policy = utf8::InvalidBytePolicy::Pass;
using Utf8Iterator = utf8::utf8_iterator<String::const_iterator, Utf8Policy>; using Utf8Iterator = utf8::utf8_iterator<const char*, Utf8Policy>;
void addutf8str(WINDOW* win, Utf8Iterator begin, Utf8Iterator end) void addutf8str(WINDOW* win, Utf8Iterator begin, Utf8Iterator end)
{ {
waddstr(win, std::string(begin.base(), end.base()).c_str()); waddstr(win, std::string(begin.base(), end.base()).c_str());
@ -241,7 +241,8 @@ void NCursesUI::draw_line(const DisplayLine& line, CharCount col_index) const
set_color(stdscr, atom.colors); set_color(stdscr, atom.colors);
String content = atom.content(); String atom_content = atom.content();
StringView content = atom_content;
if (content[content.length()-1] == '\n' and if (content[content.length()-1] == '\n' and
content.char_length() - 1 < m_dimensions.column - col_index) content.char_length() - 1 < m_dimensions.column - col_index)
{ {
@ -431,9 +432,9 @@ void NCursesUI::draw_menu()
if (item_idx == m_selected_item) if (item_idx == m_selected_item)
wattron(m_menu_win, COLOR_PAIR(menu_fg)); wattron(m_menu_win, COLOR_PAIR(menu_fg));
auto& item = m_items[item_idx]; StringView item = m_items[item_idx];
auto begin = item.cbegin(); auto begin = item.begin();
auto end = utf8::advance(begin, item.cend(), column_width); auto end = utf8::advance(begin, item.end(), column_width);
addutf8str(m_menu_win, begin, end); addutf8str(m_menu_win, begin, end);
const CharCount pad = column_width - utf8::distance(begin, end); const CharCount pad = column_width - utf8::distance(begin, end);
waddstr(m_menu_win, String{' ' COMMA pad}.c_str()); waddstr(m_menu_win, String{' ' COMMA pad}.c_str());
@ -531,7 +532,7 @@ void NCursesUI::menu_hide()
m_dirty = true; m_dirty = true;
} }
static DisplayCoord compute_needed_size(const String& str) static DisplayCoord compute_needed_size(StringView str)
{ {
DisplayCoord res{1,0}; DisplayCoord res{1,0};
CharCount line_len = 0; CharCount line_len = 0;
@ -589,7 +590,7 @@ static DisplayCoord compute_pos(DisplayCoord anchor,
return pos; return pos;
} }
static std::vector<String> wrap_lines(const String& text, CharCount max_width) static std::vector<String> wrap_lines(StringView text, CharCount max_width)
{ {
enum CharCategory { Word, Blank, Eol }; enum CharCategory { Word, Blank, Eol };
static const auto categorize = [](Codepoint c) { static const auto categorize = [](Codepoint c) {
@ -597,7 +598,7 @@ static std::vector<String> wrap_lines(const String& text, CharCount max_width)
: is_eol(c) ? Eol : Word; : is_eol(c) ? Eol : Word;
}; };
using Utf8It = utf8::utf8_iterator<String::const_iterator>; using Utf8It = utf8::utf8_iterator<const char*>;
Utf8It word_begin{text.begin()}; Utf8It word_begin{text.begin()};
Utf8It word_end{word_begin}; Utf8It word_end{word_begin};
Utf8It end{text.end()}; Utf8It end{text.end()};
@ -629,7 +630,7 @@ static std::vector<String> wrap_lines(const String& text, CharCount max_width)
} }
template<bool assist = true> template<bool assist = true>
static String make_info_box(const String& title, const String& message, static String make_info_box(StringView title, StringView message,
CharCount max_width) CharCount max_width)
{ {
static const std::vector<String> assistant = static const std::vector<String> assistant =
@ -686,7 +687,7 @@ static String make_info_box(const String& title, const String& message,
return result; return result;
} }
void NCursesUI::info_show(const String& title, const String& content, void NCursesUI::info_show(StringView title, StringView content,
DisplayCoord anchor, ColorPair colors, DisplayCoord anchor, ColorPair colors,
MenuStyle style) MenuStyle style)
{ {
@ -697,8 +698,13 @@ void NCursesUI::info_show(const String& title, const String& content,
delwin(m_info_win); delwin(m_info_win);
} }
const String& info_box = style == MenuStyle::Inline ? StringView info_box = content;
content : make_info_box(title, content, m_dimensions.column); String fancy_info_box;
if (style == MenuStyle::Prompt)
{
fancy_info_box = make_info_box(title, content, m_dimensions.column);
info_box = fancy_info_box;
}
DisplayCoord size = compute_needed_size(info_box); DisplayCoord size = compute_needed_size(info_box);

View File

@ -32,7 +32,7 @@ public:
void menu_select(int selected) override; void menu_select(int selected) override;
void menu_hide() override; void menu_hide() override;
void info_show(const String& title, const String& content, void info_show(StringView title, StringView content,
DisplayCoord anchor, ColorPair colors, DisplayCoord anchor, ColorPair colors,
MenuStyle style) override; MenuStyle style) override;
void info_hide() override; void info_hide() override;

View File

@ -55,12 +55,17 @@ public:
write((const char*)&val, sizeof(val)); write((const char*)&val, sizeof(val));
} }
void write(const String& str) void write(StringView str)
{ {
write(str.length()); write(str.length());
write(str.c_str(), (int)str.length()); write(str.data(), (int)str.length());
}; };
void write(const String& str)
{
write(StringView{str});
}
template<typename T> template<typename T>
void write(memoryview<T> view) void write(memoryview<T> view)
{ {
@ -249,7 +254,7 @@ public:
void menu_select(int selected) override; void menu_select(int selected) override;
void menu_hide() override; void menu_hide() override;
void info_show(const String& title, const String& content, void info_show(StringView title, StringView content,
DisplayCoord anchor, ColorPair colors, DisplayCoord anchor, ColorPair colors,
MenuStyle style) override; MenuStyle style) override;
void info_hide() override; void info_hide() override;
@ -316,7 +321,7 @@ void RemoteUI::menu_hide()
msg.write(RemoteUIMsg::MenuHide); msg.write(RemoteUIMsg::MenuHide);
} }
void RemoteUI::info_show(const String& title, const String& content, void RemoteUI::info_show(StringView title, StringView content,
DisplayCoord anchor, ColorPair colors, DisplayCoord anchor, ColorPair colors,
MenuStyle style) MenuStyle style)
{ {

View File

@ -33,7 +33,7 @@ public:
virtual void menu_select(int selected) = 0; virtual void menu_select(int selected) = 0;
virtual void menu_hide() = 0; virtual void menu_hide() = 0;
virtual void info_show(const String& title, const String& content, virtual void info_show(StringView title, StringView content,
DisplayCoord anchor, ColorPair colors, DisplayCoord anchor, ColorPair colors,
MenuStyle style) = 0; MenuStyle style) = 0;
virtual void info_hide() = 0; virtual void info_hide() = 0;