From 88fa88777660904ca535ee08789ca30751366e12 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 18 Apr 2009 04:34:23 +0200 Subject: [PATCH] new feature: allow searching in help, lyrics and info screens --- src/ncmpcpp.cpp | 64 +++++++++++++++++++++++++++++------------------ src/scrollpad.cpp | 4 +-- src/scrollpad.h | 5 ++-- src/strbuffer.h | 35 ++++++++++++++++++++++---- 4 files changed, 74 insertions(+), 34 deletions(-) diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 55e96efb..49beebaf 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -1596,32 +1596,46 @@ int main(int argc, char *argv[]) { List *mList = myScreen->GetList(); - if (!mList) - continue; - - LockStatusbar(); - Statusbar() << "Find " << (Keypressed(input, Key.FindForward) ? "forward" : "backward") << ": "; - string findme = wFooter->GetString(mList->GetSearchConstraint()); - UnlockStatusbar(); - myPlaylist->UpdateTimer(); - - if (!findme.empty()) + if (mList) + { + LockStatusbar(); + Statusbar() << "Find " << (Keypressed(input, Key.FindForward) ? "forward" : "backward") << ": "; + string findme = wFooter->GetString(mList->GetSearchConstraint()); + UnlockStatusbar(); + myPlaylist->UpdateTimer(); + + if (!findme.empty()) + ShowMessage("Searching..."); + + bool success = mList->Search(findme, myScreen == mySearcher ? SearchEngine::StaticOptions : 0, REG_ICASE | Config.regex_type); + + if (findme.empty()) + continue; + + success ? ShowMessage("Searching finished!") : ShowMessage("Unable to find \"%s\"", findme.c_str()); + + if (Keypressed(input, Key.FindForward)) + mList->NextFound(Config.wrapped_search); + else + mList->PrevFound(Config.wrapped_search); + + if (myScreen == myPlaylist) + myPlaylist->EnableHighlighting(); + } + else if (myScreen == myHelp || myScreen == myLyrics || myScreen == myInfo) + { + LockStatusbar(); + Statusbar() << "Find: "; + string findme = wFooter->GetString(); + UnlockStatusbar(); + ShowMessage("Searching..."); - - bool success = mList->Search(findme, myScreen == mySearcher ? SearchEngine::StaticOptions : 0, REG_ICASE | Config.regex_type); - - if (findme.empty()) - continue; - - success ? ShowMessage("Searching finished!") : ShowMessage("Unable to find \"%s\"", findme.c_str()); - - if (Keypressed(input, Key.FindForward)) - mList->NextFound(Config.wrapped_search); - else - mList->PrevFound(Config.wrapped_search); - - if (myScreen == myPlaylist) - myPlaylist->EnableHighlighting(); + Screen *s = static_cast *>(myScreen); + s->Main()->RemoveFormatting(fmtReverse); + s->Main()->RemoveFormatting(fmtReverseEnd); + ShowMessage("%s", s->Main()->SetFormatting(fmtReverse, findme, fmtReverseEnd) || findme.empty() ? "Done!" : "No matching patterns found"); + s->Main()->Flush(); + } } else if (Keypressed(input, Key.NextFoundPosition) || Keypressed(input, Key.PrevFoundPosition)) { diff --git a/src/scrollpad.cpp b/src/scrollpad.cpp index 4472f6a8..9410facf 100644 --- a/src/scrollpad.cpp +++ b/src/scrollpad.cpp @@ -100,9 +100,9 @@ void Scrollpad::Flush() itsBuffer.SetTemp(0); } -void Scrollpad::SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each) +bool Scrollpad::SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each) { - itsBuffer.SetFormatting(vb, s, ve, for_each); + return itsBuffer.SetFormatting(vb, s, ve, for_each); } void Scrollpad::Recreate() diff --git a/src/scrollpad.h b/src/scrollpad.h index f459a856..24d7e18f 100644 --- a/src/scrollpad.h +++ b/src/scrollpad.h @@ -34,7 +34,8 @@ namespace NCurses virtual ~Scrollpad() { } void Flush(); - void SetFormatting(short, const std::basic_string &, short, bool for_each = 1); + bool SetFormatting(short, const std::basic_string &, short, bool for_each = 1); + void RemoveFormatting(short value) { itsBuffer.RemoveFormatting(value); } std::basic_string Content() { return itsBuffer.Str(); } virtual void Refresh(); @@ -52,7 +53,7 @@ namespace NCurses Scrollpad &operator<<(std::ostream &(*os)(std::ostream &)); # ifdef _UTF8 - void SetFormatting(short vb, const std::string &s, short ve, bool for_each = 1) { SetFormatting(vb, ToWString(s), ve, for_each); } + bool SetFormatting(short vb, const std::string &s, short ve, bool for_each = 1) { return SetFormatting(vb, ToWString(s), ve, for_each); } Scrollpad &operator<<(const std::string &s); # endif // _UTF8 diff --git a/src/strbuffer.h b/src/strbuffer.h index 8aae0bda..698c1d4b 100644 --- a/src/strbuffer.h +++ b/src/strbuffer.h @@ -39,6 +39,19 @@ namespace NCurses { return Position < f.Position; } + + struct hasValue + { + hasValue(short value) : itsValue(value) { } + + bool operator()(const FormatPos &fp) + { + return fp.Value == itsValue; + } + + private: + short itsValue; + }; }; std::basic_ostringstream itsString; @@ -50,7 +63,8 @@ namespace NCurses basic_buffer(const basic_buffer &b); std::basic_string Str() const; - void SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each = 1); + bool SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each = 1); + void RemoveFormatting(short value); void SetTemp(std::basic_string *); void Clear(); @@ -83,23 +97,34 @@ template std::basic_string NCurses::basic_buffer::Str() const return itsString.str(); } -template void NCurses::basic_buffer::SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each) +template bool NCurses::basic_buffer::SetFormatting(short vb, const std::basic_string &s, short ve, bool for_each) { + if (s.empty()) + return false; + bool result = false; std::basic_string base = itsString.str(); FormatPos fp; - for (size_t i = base.find(s); i != std::basic_string::npos; i = base.find(s)) + for (size_t i = base.find(s); i != std::basic_string::npos; i = base.find(s, i)) { - base[i] = 0; + result = true; fp.Value = vb; fp.Position = i; itsFormat.push_back(fp); + i += s.length(); fp.Value = ve; - fp.Position = i+s.length(); + fp.Position = i; itsFormat.push_back(fp); if (!for_each) break; } + itsFormat.sort(); + return result; +} + +template void NCurses::basic_buffer::RemoveFormatting(short value) +{ + itsFormat.remove_if(typename FormatPos::hasValue(value)); } template void NCurses::basic_buffer::SetTemp(std::basic_string *tmp)