summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-04-30 19:39:52 +0100
committerMaxime Coste <frrrwww@gmail.com>2014-04-30 19:39:52 +0100
commit4d45fa6588093da2ae6673f391223a1c439e324c (patch)
treebbdbd07965a592c3bff2b7bb7cf2f19bb219de23 /src
parentbd4df27f2b96020e0e87d9594b7dae27ffe43b20 (diff)
Use StringView in UserInterface and NCursesUI
Diffstat (limited to 'src')
-rw-r--r--src/ncurses.cc30
-rw-r--r--src/ncurses.hh2
-rw-r--r--src/remote.cc13
-rw-r--r--src/user_interface.hh2
4 files changed, 29 insertions, 18 deletions
diff --git a/src/ncurses.cc b/src/ncurses.cc
index 0553a7ff..fc538c1e 100644
--- a/src/ncurses.cc
+++ b/src/ncurses.cc
@@ -204,7 +204,7 @@ void NCursesUI::refresh()
}
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)
{
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);
- String content = atom.content();
+ String atom_content = atom.content();
+ StringView content = atom_content;
if (content[content.length()-1] == '\n' and
content.char_length() - 1 < m_dimensions.column - col_index)
{
@@ -431,9 +432,9 @@ void NCursesUI::draw_menu()
if (item_idx == m_selected_item)
wattron(m_menu_win, COLOR_PAIR(menu_fg));
- auto& item = m_items[item_idx];
- auto begin = item.cbegin();
- auto end = utf8::advance(begin, item.cend(), column_width);
+ StringView item = m_items[item_idx];
+ auto begin = item.begin();
+ auto end = utf8::advance(begin, item.end(), column_width);
addutf8str(m_menu_win, begin, end);
const CharCount pad = column_width - utf8::distance(begin, end);
waddstr(m_menu_win, String{' ' COMMA pad}.c_str());
@@ -531,7 +532,7 @@ void NCursesUI::menu_hide()
m_dirty = true;
}
-static DisplayCoord compute_needed_size(const String& str)
+static DisplayCoord compute_needed_size(StringView str)
{
DisplayCoord res{1,0};
CharCount line_len = 0;
@@ -589,7 +590,7 @@ static DisplayCoord compute_pos(DisplayCoord anchor,
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 };
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;
};
- using Utf8It = utf8::utf8_iterator<String::const_iterator>;
+ using Utf8It = utf8::utf8_iterator<const char*>;
Utf8It word_begin{text.begin()};
Utf8It word_end{word_begin};
Utf8It end{text.end()};
@@ -629,7 +630,7 @@ static std::vector<String> wrap_lines(const String& text, CharCount max_width)
}
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)
{
static const std::vector<String> assistant =
@@ -686,7 +687,7 @@ static String make_info_box(const String& title, const String& message,
return result;
}
-void NCursesUI::info_show(const String& title, const String& content,
+void NCursesUI::info_show(StringView title, StringView content,
DisplayCoord anchor, ColorPair colors,
MenuStyle style)
{
@@ -697,8 +698,13 @@ void NCursesUI::info_show(const String& title, const String& content,
delwin(m_info_win);
}
- const String& info_box = style == MenuStyle::Inline ?
- content : make_info_box(title, content, m_dimensions.column);
+ StringView info_box = content;
+ 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);
diff --git a/src/ncurses.hh b/src/ncurses.hh
index 4edd7502..ffbbb81b 100644
--- a/src/ncurses.hh
+++ b/src/ncurses.hh
@@ -32,7 +32,7 @@ public:
void menu_select(int selected) 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,
MenuStyle style) override;
void info_hide() override;
diff --git a/src/remote.cc b/src/remote.cc
index 9dcae5f5..867b281d 100644
--- a/src/remote.cc
+++ b/src/remote.cc
@@ -55,12 +55,17 @@ public:
write((const char*)&val, sizeof(val));
}
- void write(const String& str)
+ void write(StringView str)
{
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>
void write(memoryview<T> view)
{
@@ -249,7 +254,7 @@ public:
void menu_select(int selected) 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,
MenuStyle style) override;
void info_hide() override;
@@ -316,7 +321,7 @@ void RemoteUI::menu_hide()
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,
MenuStyle style)
{
diff --git a/src/user_interface.hh b/src/user_interface.hh
index 00366cb2..10ec5ef3 100644
--- a/src/user_interface.hh
+++ b/src/user_interface.hh
@@ -33,7 +33,7 @@ public:
virtual void menu_select(int selected) = 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,
MenuStyle style) = 0;
virtual void info_hide() = 0;