From 439e004cfadac3d8b2603ae9dd7b9229e0ce48c8 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 6 Oct 2012 16:59:27 +0200 Subject: [PATCH] restore BasicBuffer::write function --- src/helpers.cpp | 48 ++++++++++++++++++++++++++++++++++++ src/helpers.h | 29 +++++++++------------- src/status.cpp | 6 ++--- src/strbuffer.h | 65 ------------------------------------------------- 4 files changed, 62 insertions(+), 86 deletions(-) diff --git a/src/helpers.cpp b/src/helpers.cpp index f6bcf965..d3a73c57 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -130,3 +130,51 @@ std::wstring Scroller(const std::wstring &str, size_t &pos, size_t width) result = s; return result; } + +void writeCyclicBuffer(const NC::WBuffer &buf, NC::Window &w, size_t &start_pos, + size_t width, const std::wstring &separator) +{ + const auto &s = buf.str(); + size_t len = wideLength(s); + if (len > width) + { + len = 0; + const auto &ps = buf.properties(); + auto p = ps.begin(); + + // load attributes from before starting pos + for (; p != ps.end() && p->position() < start_pos; ++p) + w << *p; + + auto write_buffer = [&](size_t start) { + for (size_t i = start; i < s.length() && len < width; ++i) + { + for (; p != ps.end() && p->position() == i; ++p) + w << *p; + len += wcwidth(s[i]); + if (len > width) + break; + w << s[i]; + } + for (; p != ps.end(); ++p) + w << *p; + p = ps.begin(); + }; + + write_buffer(start_pos); + for (size_t i = 0; i < separator.length() && len < width; ++i) + { + len += wcwidth(separator[i]); + if (len > width) + break; + w << separator[i]; + } + write_buffer(0); + + ++start_pos; + if (start_pos >= s.length()) + start_pos = 0; + } + else + w << buf; +} diff --git a/src/helpers.h b/src/helpers.h index ca7abf20..6f5f1b34 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -337,13 +337,6 @@ template std::string getSharedDirectory(Iterator first, Iter return result; } -template struct StringConverter { - const char *operator()(const char *s) { return s; } -}; -template <> struct StringConverter { - std::wstring operator()(const char *s) { return ToWString(s); } -}; - template void stringToBuffer(Iterator first, Iterator last, NC::BasicBuffer &buf) { @@ -427,8 +420,6 @@ void stringToBuffer(const std::basic_string &s, NC::BasicBuffer &b template void ShowTime(T &buf, size_t length, bool short_names) { - StringConverter cnv; - const unsigned MINUTE = 60; const unsigned HOUR = 60*MINUTE; const unsigned DAY = 24*HOUR; @@ -437,37 +428,37 @@ template void ShowTime(T &buf, size_t length, bool short_names) unsigned years = length/YEAR; if (years) { - buf << years << cnv(short_names ? "y" : (years == 1 ? " year" : " years")); + buf << years << (short_names ? "y" : (years == 1 ? " year" : " years")); length -= years*YEAR; if (length) - buf << cnv(", "); + buf << ", "; } unsigned days = length/DAY; if (days) { - buf << days << cnv(short_names ? "d" : (days == 1 ? " day" : " days")); + buf << days << (short_names ? "d" : (days == 1 ? " day" : " days")); length -= days*DAY; if (length) - buf << cnv(", "); + buf << ", "; } unsigned hours = length/HOUR; if (hours) { - buf << hours << cnv(short_names ? "h" : (hours == 1 ? " hour" : " hours")); + buf << hours << (short_names ? "h" : (hours == 1 ? " hour" : " hours")); length -= hours*HOUR; if (length) - buf << cnv(", "); + buf << ", "; } unsigned minutes = length/MINUTE; if (minutes) { - buf << minutes << cnv(short_names ? "m" : (minutes == 1 ? " minute" : " minutes")); + buf << minutes << (short_names ? "m" : (minutes == 1 ? " minute" : " minutes")); length -= minutes*MINUTE; if (length) - buf << cnv(", "); + buf << ", "; } if (length) - buf << length << cnv(short_names ? "s" : (length == 1 ? " second" : " seconds")); + buf << length << (short_names ? "s" : (length == 1 ? " second" : " seconds")); } template void ShowTag(BufferT &buf, const std::string &tag) @@ -486,5 +477,7 @@ std::string Timestamp(time_t t); void markSongsInPlaylist(ProxySongList pl); std::wstring Scroller(const std::wstring &str, size_t &pos, size_t width); +void writeCyclicBuffer(const NC::WBuffer &buf, NC::Window &w, size_t &start_pos, + size_t width, const std::wstring &separator); #endif // NCMPCPP_HELPERS_H diff --git a/src/status.cpp b/src/status.cpp index edf22857..240093cd 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -344,11 +344,11 @@ void Status::Changes::elapsedTime() if (!Global::SeekingInProgress) *wHeader << NC::XY(0, 0) << wclrtoeol << tracklength; *wHeader << NC::XY(first_start, 0); - first.write(*wHeader, first_line_scroll_begin, COLS-tracklength.length()-VolumeState.length()-1, L" ** "); + writeCyclicBuffer(first, *wHeader, first_line_scroll_begin, COLS-tracklength.length()-VolumeState.length()-1, L" ** "); *wHeader << NC::XY(0, 1) << wclrtoeol << NC::fmtBold << player_state << NC::fmtBoldEnd; *wHeader << NC::XY(second_start, 1); - second.write(*wHeader, second_line_scroll_begin, COLS-player_state.length()-8-2, L" ** "); + writeCyclicBuffer(second, *wHeader, second_line_scroll_begin, COLS-player_state.length()-8-2, L" ** "); *wHeader << NC::XY(wHeader->getWidth()-VolumeState.length(), 0) << Config.volume_color << VolumeState << NC::clEnd; @@ -384,7 +384,7 @@ void Status::Changes::elapsedTime() NC::WBuffer np_song; stringToBuffer(ToWString(Charset::utf8ToLocale(np.toString(Config.song_status_format, Config.tags_separator, "$"))), 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" ** "); + writeCyclicBuffer(np_song, *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; } if (Progressbar::isUnlocked()) diff --git a/src/strbuffer.h b/src/strbuffer.h index 32372b46..98d89900 100644 --- a/src/strbuffer.h +++ b/src/strbuffer.h @@ -183,9 +183,6 @@ public: return *this; } - void write(GNUC_UNUSED Window &w, GNUC_UNUSED size_t &start_pos, GNUC_UNUSED size_t width, - GNUC_UNUSED const std::basic_string &separator) const { } - private: StringType m_string; Properties m_properties; @@ -217,68 +214,6 @@ Window operator<<(Window &w, const BasicBuffer &buffer) return w; } -/* - -template void BasicBuffer::write( - Window &w, - size_t &start_pos, - size_t width, - const std::basic_string &separator - ) const -{ - std::basic_string s = m_string; - size_t len = wideLength(s); - - if (len > width) - { - s += separator; - len = 0; - - auto lb = m_properties.begin(); - if (m_properties.back().Position > start_pos) // if there is no attributes from current position, don't load them - { - // load all attributes that are before start position - for (; lb->Position < start_pos; ++lb) - loadAttribute(w, lb->Value); - } - - for (size_t i = start_pos; i < s.length() && len < width; ++i) - { - while (i == lb->Position && lb != m_properties.end()) - { - loadAttribute(w, lb->Value); - ++lb; - } - if ((len += wcwidth(s[i])) > width) - break; - w << s[i]; - } - if (++start_pos >= s.length()) - start_pos = 0; - - if (len < width) - lb = m_properties.begin(); - for (size_t i = 0; len < width; ++i) - { - while (i == lb->Position && lb != m_properties.end()) - { - loadAttribute(w, lb->Value); - ++lb; - } - if ((len += wcwidth(s[i])) > width) - break; - w << s[i]; - } - // load all remained attributes to clean up - for (; lb != m_properties.end(); ++lb) - loadAttribute(w, lb->Value); - } - else - w << *this; -} - -*/ - } #endif // NCMPCPP_STRBUFFER_H