display: improve showSongs a bit

This commit is contained in:
Andrzej Rybczak
2012-09-12 19:36:16 +02:00
parent fd6a5889d0
commit 8e7bcd219b
5 changed files with 39 additions and 30 deletions

View File

@@ -108,12 +108,14 @@ void showSongs(NC::Menu<T> &menu, const MPD::Song &s, HasSongs &screen, const st
bool separate_albums, is_now_playing, is_selected, discard_colors;
setProperties(menu, s, screen, separate_albums, is_now_playing, is_selected, discard_colors);
size_t y = menu.getY();
std::string line = s.toString(format, "$");
for (auto it = line.begin(); it != line.end(); ++it)
{
if (*it == '$')
{
if (++it == line.end()) // end of format
++it;
if (it == line.end()) // end of format
{
menu << '$';
break;
@@ -125,25 +127,27 @@ void showSongs(NC::Menu<T> &menu, const MPD::Song &s, HasSongs &screen, const st
}
else if (*it == 'R') // right align
{
NC::WBuffer buf;
buf << L" ";
String2Buffer(ToWString(line.substr(it-line.begin()+1)), buf);
NC::Buffer buf;
buf << " ";
stringToBuffer(++it, line.end(), buf);
if (discard_colors)
buf.removeFormatting();
size_t x_off = menu.getWidth() - wideLength(ToWString(buf.str()));
if (is_now_playing)
buf << Config.now_playing_suffix;
menu << NC::XY(menu.getWidth()-buf.str().length()-(is_selected ? Config.selected_item_suffix_length : 0), menu.getY()) << buf;
if (separate_albums)
menu << NC::fmtUnderlineEnd;
return;
x_off -= Config.now_playing_suffix_length;
if (is_selected)
x_off -= Config.selected_item_suffix_length;
menu << NC::XY(x_off, y) << buf;
break;
}
else // not a color nor right align, just a random character
menu << *--it;
}
else if (*it == MPD::Song::FormatEscapeCharacter)
{
++it;
// treat '$' as a normal character if song format escape char is prepended to it
if (++it == line.end() || *it != '$')
if (it == line.end() || *it != '$')
--it;
menu << *it;
}

View File

@@ -327,15 +327,14 @@ template <> struct StringConverter<NC::Scrollpad> {
std::wstring operator()(const char *s) { return ToWString(s); }
};
template <typename CharT>
void String2Buffer(const std::basic_string<CharT> &s, NC::basic_buffer<CharT> &buf)
template <typename Iterator>
void stringToBuffer(Iterator first, Iterator last, NC::basic_buffer<typename Iterator::value_type> &buf)
{
StringConverter< NC::basic_buffer<CharT> > cnv;
for (auto it = s.begin(); it != s.end(); ++it)
for (auto it = first; it != last; ++it)
{
if (*it == '$')
{
if (++it == s.end())
if (++it == last)
{
buf << '$';
break;
@@ -361,9 +360,9 @@ void String2Buffer(const std::basic_string<CharT> &s, NC::basic_buffer<CharT> &b
buf << NC::fmtReverse;
break;
case '/':
if (++it == s.end())
if (++it == last)
{
buf << cnv("$/");
buf << '$' << '/';
break;
}
switch (*it)
@@ -394,7 +393,7 @@ void String2Buffer(const std::basic_string<CharT> &s, NC::basic_buffer<CharT> &b
else if (*it == MPD::Song::FormatEscapeCharacter)
{
// treat '$' as a normal character if song format escape char is prepended to it
if (++it == s.end() || *it != '$')
if (++it == last || *it != '$')
--it;
buf << *it;
}
@@ -403,6 +402,12 @@ void String2Buffer(const std::basic_string<CharT> &s, NC::basic_buffer<CharT> &b
}
}
template <typename CharT>
void stringToBuffer(const std::basic_string<CharT> &s, NC::basic_buffer<CharT> &buf)
{
stringToBuffer(s.begin(), s.end(), buf);
}
template <typename T> void ShowTime(T &buf, size_t length, bool short_names)
{
StringConverter<T> cnv;

View File

@@ -432,7 +432,7 @@ void Configuration::Read()
if (song_status_format.find("$") != std::string::npos)
{
NC::Buffer status_no_colors;
String2Buffer(song_status_format, status_no_colors);
stringToBuffer(song_status_format, status_no_colors);
song_status_format_no_colors = status_no_colors.str();
}
else
@@ -509,7 +509,7 @@ void Configuration::Read()
if (!v.empty())
{
browser_playlist_prefix.clear();
String2Buffer(v, browser_playlist_prefix);
stringToBuffer(v, browser_playlist_prefix);
}
}
else if (name == "progressbar_look")
@@ -545,7 +545,7 @@ void Configuration::Read()
if (!v.empty())
{
selected_item_prefix.clear();
String2Buffer(v, selected_item_prefix);
stringToBuffer(v, selected_item_prefix);
selected_item_prefix_length = wideLength(ToWString(selected_item_prefix.str()));
}
}
@@ -554,7 +554,7 @@ void Configuration::Read()
if (!v.empty())
{
selected_item_suffix.clear();
String2Buffer(v, selected_item_suffix);
stringToBuffer(v, selected_item_suffix);
selected_item_suffix_length = wideLength(ToWString(selected_item_suffix.str()));
}
}
@@ -563,7 +563,7 @@ void Configuration::Read()
if (!v.empty())
{
now_playing_prefix.clear();
String2Buffer(v, now_playing_prefix);
stringToBuffer(v, now_playing_prefix);
now_playing_prefix_length = wideLength(ToWString(now_playing_prefix.str()));
}
}
@@ -572,8 +572,8 @@ void Configuration::Read()
if (!v.empty())
{
now_playing_suffix.clear();
String2Buffer(ToWString(v), now_playing_suffix);
now_playing_suffix_length = wideLength(now_playing_suffix.str());
stringToBuffer(v, now_playing_suffix);
now_playing_suffix_length = wideLength(ToWString(now_playing_suffix.str()));
}
}
else if (name == "modified_item_prefix")
@@ -581,7 +581,7 @@ void Configuration::Read()
if (!v.empty())
{
modified_item_prefix.clear();
String2Buffer(v, modified_item_prefix);
stringToBuffer(v, modified_item_prefix);
}
}
else if (name == "color1")

View File

@@ -91,7 +91,7 @@ struct Configuration
NC::Buffer selected_item_prefix;
NC::Buffer selected_item_suffix;
NC::Buffer now_playing_prefix;
NC::WBuffer now_playing_suffix;
NC::Buffer now_playing_suffix;
NC::Buffer modified_item_prefix;
NC::Color color1;

View File

@@ -345,8 +345,8 @@ void Status::Changes::elapsedTime()
}
NC::WBuffer first, second;
String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, "$"))), first);
String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_second_line, "$"))), second);
stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, "$"))), first);
stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_second_line, "$"))), second);
size_t first_len = wideLength(first.str());
size_t first_margin = (std::max(tracklength.length()+1, VolumeState.length()))*2;
@@ -397,7 +397,7 @@ void Status::Changes::elapsedTime()
tracklength += "]";
}
NC::WBuffer np_song;
String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.song_status_format, "$"))), np_song);
stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.song_status_format, "$"))), np_song);
*wFooter << NC::XY(0, 1) << wclrtoeol << NC::fmtBold << player_state << NC::fmtBoldEnd;
np_song.write(*wFooter, playing_song_scroll_begin, wFooter->getWidth()-player_state.length()-tracklength.length(), L" ** ");
*wFooter << NC::fmtBold << NC::XY(wFooter->getWidth()-tracklength.length(), 1) << tracklength << NC::fmtBoldEnd;