From c7251ebb981f341bfe80f254619220237c7769fc Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sun, 19 Apr 2009 22:19:52 +0200 Subject: [PATCH] improve formatting removal --- src/ncmpcpp.cpp | 3 +-- src/scrollpad.cpp | 25 ++++++++++++++++++++++++- src/scrollpad.h | 7 ++++++- src/status.cpp | 3 +-- src/strbuffer.h | 36 ++++++++++++++++++++++-------------- 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 49beebaf..ce842d95 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -1631,8 +1631,7 @@ int main(int argc, char *argv[]) ShowMessage("Searching..."); Screen *s = static_cast *>(myScreen); - s->Main()->RemoveFormatting(fmtReverse); - s->Main()->RemoveFormatting(fmtReverseEnd); + s->Main()->RemoveFormatting(); ShowMessage("%s", s->Main()->SetFormatting(fmtReverse, findme, fmtReverseEnd) || findme.empty() ? "Done!" : "No matching patterns found"); s->Main()->Flush(); } diff --git a/src/scrollpad.cpp b/src/scrollpad.cpp index 9410facf..7fa6e0a9 100644 --- a/src/scrollpad.cpp +++ b/src/scrollpad.cpp @@ -32,6 +32,9 @@ Scrollpad::Scrollpad(size_t startx, Border border) : Window(startx, starty, width, height, title, color, border), itsBeginning(0), + itsFoundForEach(0), + itsFoundValueBegin(0), + itsFoundValueEnd(0), itsRealHeight(1) { } @@ -102,7 +105,27 @@ void Scrollpad::Flush() bool Scrollpad::SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each) { - return itsBuffer.SetFormatting(vb, s, ve, for_each); + bool result = itsBuffer.SetFormatting(vb, s, ve, for_each); + if (result) + { + itsFoundForEach = for_each; + itsFoundValueBegin = vb; + itsFoundValueEnd = ve; + itsFoundPattern = s; + } + else + { + itsFoundForEach = 0; + itsFoundValueBegin = 0; + itsFoundValueEnd = 0; + itsFoundPattern.clear(); + } + return result; +} + +void Scrollpad::RemoveFormatting() +{ + itsBuffer.RemoveFormatting(itsFoundValueBegin, itsFoundPattern, itsFoundValueEnd, itsFoundForEach); } void Scrollpad::Recreate() diff --git a/src/scrollpad.h b/src/scrollpad.h index 24d7e18f..9fc1362a 100644 --- a/src/scrollpad.h +++ b/src/scrollpad.h @@ -35,7 +35,7 @@ namespace NCurses void Flush(); bool SetFormatting(short, const std::basic_string &, short, bool for_each = 1); - void RemoveFormatting(short value) { itsBuffer.RemoveFormatting(value); } + void RemoveFormatting(); std::basic_string Content() { return itsBuffer.Str(); } virtual void Refresh(); @@ -67,6 +67,11 @@ namespace NCurses int itsBeginning; + bool itsFoundForEach; + short itsFoundValueBegin; + short itsFoundValueEnd; + std::basic_string itsFoundPattern; + size_t itsRealHeight; }; } diff --git a/src/status.cpp b/src/status.cpp index 003820a9..fbbdf498 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -472,9 +472,8 @@ void NcmpcppStatusChanged(Connection *Mpd, StatusChanges changed, void *) } if (changed.StatusFlags && Config.header_visibility) { - static string switch_state; + string switch_state; - switch_state.clear(); if (mpd_repeat) switch_state += mpd_repeat; if (mpd_random) diff --git a/src/strbuffer.h b/src/strbuffer.h index 698c1d4b..7b4255d2 100644 --- a/src/strbuffer.h +++ b/src/strbuffer.h @@ -40,18 +40,10 @@ namespace NCurses return Position < f.Position; } - struct hasValue + bool operator==(const FormatPos &f) { - hasValue(short value) : itsValue(value) { } - - bool operator()(const FormatPos &fp) - { - return fp.Value == itsValue; - } - - private: - short itsValue; - }; + return Position == f.Position && Value == f.Value; + } }; std::basic_ostringstream itsString; @@ -64,7 +56,7 @@ namespace NCurses std::basic_string Str() const; bool SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each = 1); - void RemoveFormatting(short value); + void RemoveFormatting(short vb, const std::basic_string &s, short ve, bool for_each = 1); void SetTemp(std::basic_string *); void Clear(); @@ -122,9 +114,25 @@ template bool NCurses::basic_buffer::SetFormatting(short vb, con return result; } -template void NCurses::basic_buffer::RemoveFormatting(short value) +template void NCurses::basic_buffer::RemoveFormatting(short vb, const std::basic_string &s, short ve, bool for_each) { - itsFormat.remove_if(typename FormatPos::hasValue(value)); + if (s.empty()) + return; + std::basic_string base = itsString.str(); + FormatPos fp; + + for (size_t i = base.find(s); i != std::basic_string::npos; i = base.find(s, i)) + { + fp.Value = vb; + fp.Position = i; + itsFormat.remove(fp); + i += s.length(); + fp.Value = ve; + fp.Position = i; + itsFormat.remove(fp); + if (!for_each) + break; + } } template void NCurses::basic_buffer::SetTemp(std::basic_string *tmp)