menu: generalize filtering, introduce interfaces
This commit is contained in:
@@ -26,6 +26,7 @@ ncmpcpp_SOURCES = \
|
|||||||
outputs.cpp \
|
outputs.cpp \
|
||||||
playlist.cpp \
|
playlist.cpp \
|
||||||
playlist_editor.cpp \
|
playlist_editor.cpp \
|
||||||
|
regexes.cpp \
|
||||||
screen.cpp \
|
screen.cpp \
|
||||||
scrollpad.cpp \
|
scrollpad.cpp \
|
||||||
search_engine.cpp \
|
search_engine.cpp \
|
||||||
@@ -60,6 +61,7 @@ noinst_HEADERS = \
|
|||||||
global.h \
|
global.h \
|
||||||
help.h \
|
help.h \
|
||||||
helpers.h \
|
helpers.h \
|
||||||
|
interfaces.h \
|
||||||
lastfm.h \
|
lastfm.h \
|
||||||
lastfm_service.h \
|
lastfm_service.h \
|
||||||
lyrics.h \
|
lyrics.h \
|
||||||
@@ -71,6 +73,8 @@ noinst_HEADERS = \
|
|||||||
mutable_song.h \
|
mutable_song.h \
|
||||||
outputs.h \
|
outputs.h \
|
||||||
playlist_editor.h \
|
playlist_editor.h \
|
||||||
|
regex_filter.h \
|
||||||
|
regexes.h \
|
||||||
screen.h \
|
screen.h \
|
||||||
scrollpad.h \
|
scrollpad.h \
|
||||||
search_engine.h \
|
search_engine.h \
|
||||||
|
|||||||
@@ -1991,7 +1991,7 @@ void ReversePlaylist::Run()
|
|||||||
|
|
||||||
bool ApplyFilter::canBeRun() const
|
bool ApplyFilter::canBeRun() const
|
||||||
{
|
{
|
||||||
return myScreen->GetList();
|
return dynamic_cast<Filterable *>(myScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ApplyFilter::Run()
|
void ApplyFilter::Run()
|
||||||
@@ -1999,20 +1999,21 @@ void ApplyFilter::Run()
|
|||||||
using Global::RedrawHeader;
|
using Global::RedrawHeader;
|
||||||
using Global::wFooter;
|
using Global::wFooter;
|
||||||
|
|
||||||
List *mList = myScreen->GetList();
|
Filterable *f = dynamic_cast<Filterable *>(myScreen);
|
||||||
assert(mList);
|
assert(f);
|
||||||
|
|
||||||
LockStatusbar();
|
LockStatusbar();
|
||||||
Statusbar() << fmtBold << "Apply filter: " << fmtBoldEnd;
|
Statusbar() << fmtBold << "Apply filter: " << fmtBoldEnd;
|
||||||
wFooter->SetGetStringHelper(StatusbarApplyFilterImmediately);
|
wFooter->SetGetStringHelper(std::bind(StatusbarApplyFilterImmediately, f, _1));
|
||||||
wFooter->GetString(mList->GetFilter());
|
wFooter->GetString(f->currentFilter());
|
||||||
wFooter->SetGetStringHelper(StatusbarGetStringHelper);
|
wFooter->SetGetStringHelper(StatusbarGetStringHelper);
|
||||||
UnlockStatusbar();
|
UnlockStatusbar();
|
||||||
|
|
||||||
if (mList->isFiltered())
|
std::string filter = f->currentFilter();
|
||||||
ShowMessage("Using filter \"%s\"", mList->GetFilter().c_str());
|
if (filter.empty())
|
||||||
else
|
|
||||||
ShowMessage("Filtering disabled");
|
ShowMessage("Filtering disabled");
|
||||||
|
else
|
||||||
|
ShowMessage("Using filter \"%s\"", filter.c_str());
|
||||||
|
|
||||||
if (myScreen == myPlaylist)
|
if (myScreen == myPlaylist)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -319,9 +319,21 @@ void Browser::GetSelectedSongs(MPD::SongList &v)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::ApplyFilter(const std::string &s)
|
std::string Browser::currentFilter()
|
||||||
{
|
{
|
||||||
w->ApplyFilter(s, itsBrowsedDir == "/" ? 0 : 1, REG_ICASE | Config.regex_type);
|
return RegexFilter<MPD::Item>::currentFilter(*w);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Browser::applyFilter(const std::string &filter)
|
||||||
|
{
|
||||||
|
w->ShowAll();
|
||||||
|
auto fun = [](const Regex &rx, Menu<MPD::Item> &menu, const Menu<MPD::Item>::Item &item) {
|
||||||
|
if (item.value().type == MPD::itDirectory && item.value().name == "..")
|
||||||
|
return true;
|
||||||
|
return rx.match(menu.Stringify(item));
|
||||||
|
};
|
||||||
|
auto rx = RegexFilter<MPD::Item>(filter, Config.regex_type, fun);
|
||||||
|
w->Filter(w->Begin(), w->End(), rx);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Browser::hasSupportedExtension(const std::string &file)
|
bool Browser::hasSupportedExtension(const std::string &file)
|
||||||
@@ -485,7 +497,6 @@ void Browser::GetLocalDirectory(MPD::ItemList &v, const std::string &directory,
|
|||||||
MPD::MutableSong *s = new MPD::MutableSong(mpd_song_begin(&file_pair));
|
MPD::MutableSong *s = new MPD::MutableSong(mpd_song_begin(&file_pair));
|
||||||
new_item.song = std::shared_ptr<MPD::Song>(s);
|
new_item.song = std::shared_ptr<MPD::Song>(s);
|
||||||
# ifdef HAVE_TAGLIB_H
|
# ifdef HAVE_TAGLIB_H
|
||||||
// FIXME
|
|
||||||
if (!recursively)
|
if (!recursively)
|
||||||
TagEditor::ReadTags(*s);
|
TagEditor::ReadTags(*s);
|
||||||
# endif // HAVE_TAGLIB_H
|
# endif // HAVE_TAGLIB_H
|
||||||
|
|||||||
@@ -21,10 +21,12 @@
|
|||||||
#ifndef _BROWSER_H
|
#ifndef _BROWSER_H
|
||||||
#define _BROWSER_H
|
#define _BROWSER_H
|
||||||
|
|
||||||
|
#include "interfaces.h"
|
||||||
#include "ncmpcpp.h"
|
#include "ncmpcpp.h"
|
||||||
|
#include "regex_filter.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
|
|
||||||
class Browser : public Screen< Menu<MPD::Item> >
|
class Browser : public Screen< Menu<MPD::Item> >, public Filterable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Browser() : itsBrowseLocally(0), itsScrollBeginning(0), itsBrowsedDir("/") { }
|
Browser() : itsBrowseLocally(0), itsScrollBeginning(0), itsBrowsedDir("/") { }
|
||||||
@@ -46,7 +48,8 @@ class Browser : public Screen< Menu<MPD::Item> >
|
|||||||
virtual void ReverseSelection();
|
virtual void ReverseSelection();
|
||||||
virtual void GetSelectedSongs(MPD::SongList &);
|
virtual void GetSelectedSongs(MPD::SongList &);
|
||||||
|
|
||||||
virtual void ApplyFilter(const std::string &);
|
virtual std::string currentFilter();
|
||||||
|
virtual void applyFilter(const std::string &filter);
|
||||||
|
|
||||||
virtual List *GetList() { return w; }
|
virtual List *GetList() { return w; }
|
||||||
|
|
||||||
|
|||||||
33
src/interfaces.h
Normal file
33
src/interfaces.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2008-2012 by Andrzej Rybczak *
|
||||||
|
* electricityispower@gmail.com *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program; if not, write to the *
|
||||||
|
* Free Software Foundation, Inc., *
|
||||||
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#ifndef _INTERFACES_H
|
||||||
|
#define _INTERFACES_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "gcc.h"
|
||||||
|
|
||||||
|
struct Filterable
|
||||||
|
{
|
||||||
|
virtual std::string currentFilter() = 0;
|
||||||
|
virtual void applyFilter(const std::string &filter) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _INTERFACES_H
|
||||||
@@ -527,9 +527,43 @@ void MediaLibrary::GetSelectedSongs(MPD::SongList &v)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MediaLibrary::ApplyFilter(const std::string &s)
|
std::string MediaLibrary::currentFilter()
|
||||||
{
|
{
|
||||||
GetList()->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
std::string filter;
|
||||||
|
if (w == Artists)
|
||||||
|
filter = RegexFilter<std::string>::currentFilter(*Artists);
|
||||||
|
else if (w == Albums)
|
||||||
|
filter = RegexFilter<SearchConstraints>::currentFilter(*Albums);
|
||||||
|
else if (w == Songs)
|
||||||
|
filter = RegexFilter<MPD::Song>::currentFilter(*Songs);
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaLibrary::applyFilter(const std::string &filter)
|
||||||
|
{
|
||||||
|
if (w == Artists)
|
||||||
|
{
|
||||||
|
Artists->ShowAll();
|
||||||
|
auto rx = RegexFilter<std::string>(filter, Config.regex_type);
|
||||||
|
Artists->Filter(Artists->Begin(), Artists->End(), rx);
|
||||||
|
}
|
||||||
|
else if (w == Albums)
|
||||||
|
{
|
||||||
|
Albums->ShowAll();
|
||||||
|
auto fun = [](const Regex &rx, Menu<SearchConstraints> &menu, const Menu<SearchConstraints>::Item &item) {
|
||||||
|
if (item.isSeparator() || item.value().Date == AllTracksMarker)
|
||||||
|
return true;
|
||||||
|
return rx.match(menu.Stringify(item));
|
||||||
|
};
|
||||||
|
auto rx = RegexFilter<SearchConstraints>(filter, Config.regex_type, fun);
|
||||||
|
Albums->Filter(Albums->Begin(), Albums->End(), rx);
|
||||||
|
}
|
||||||
|
else if (w == Songs)
|
||||||
|
{
|
||||||
|
Songs->ShowAll();
|
||||||
|
auto rx = RegexFilter<MPD::Song>(filter, Config.regex_type);
|
||||||
|
Songs->Filter(Songs->Begin(), Songs->End(), rx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MediaLibrary::isNextColumnAvailable()
|
bool MediaLibrary::isNextColumnAvailable()
|
||||||
@@ -647,7 +681,7 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
|
|||||||
|
|
||||||
if (!hasTwoColumns)
|
if (!hasTwoColumns)
|
||||||
{
|
{
|
||||||
Artists->ApplyFilter("");
|
Artists->ShowAll();
|
||||||
if (Artists->Empty())
|
if (Artists->Empty())
|
||||||
Update();
|
Update();
|
||||||
if (primary_tag != Artists->Current().value())
|
if (primary_tag != Artists->Current().value())
|
||||||
@@ -665,7 +699,7 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Albums->ApplyFilter("");
|
Albums->ShowAll();
|
||||||
if (Albums->Empty())
|
if (Albums->Empty())
|
||||||
Update();
|
Update();
|
||||||
|
|
||||||
@@ -688,7 +722,7 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Songs->ApplyFilter("");
|
Songs->ShowAll();
|
||||||
if (Songs->Empty())
|
if (Songs->Empty())
|
||||||
Update();
|
Update();
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,12 @@
|
|||||||
#ifndef _H_MEDIA_LIBRARY
|
#ifndef _H_MEDIA_LIBRARY
|
||||||
#define _H_MEDIA_LIBRARY
|
#define _H_MEDIA_LIBRARY
|
||||||
|
|
||||||
|
#include "interfaces.h"
|
||||||
#include "ncmpcpp.h"
|
#include "ncmpcpp.h"
|
||||||
|
#include "regex_filter.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
|
|
||||||
class MediaLibrary : public Screen<Window>
|
class MediaLibrary : public Screen<Window>, public Filterable
|
||||||
{
|
{
|
||||||
struct SearchConstraints
|
struct SearchConstraints
|
||||||
{
|
{
|
||||||
@@ -63,7 +65,8 @@ class MediaLibrary : public Screen<Window>
|
|||||||
virtual void ReverseSelection();
|
virtual void ReverseSelection();
|
||||||
virtual void GetSelectedSongs(MPD::SongList &);
|
virtual void GetSelectedSongs(MPD::SongList &);
|
||||||
|
|
||||||
virtual void ApplyFilter(const std::string &);
|
virtual std::string currentFilter();
|
||||||
|
virtual void applyFilter(const std::string &filter);
|
||||||
|
|
||||||
virtual List *GetList();
|
virtual List *GetList();
|
||||||
|
|
||||||
|
|||||||
14
src/menu.cpp
14
src/menu.cpp
@@ -27,12 +27,22 @@ template <> std::string Menu<std::string>::GetItem(size_t pos)
|
|||||||
std::string result;
|
std::string result;
|
||||||
if (m_options_ptr->at(pos))
|
if (m_options_ptr->at(pos))
|
||||||
{
|
{
|
||||||
if (m_get_string_helper)
|
if (m_item_stringifier)
|
||||||
result = m_get_string_helper((*m_options_ptr)[pos]->value());
|
result = m_item_stringifier((*m_options_ptr)[pos]->value());
|
||||||
else
|
else
|
||||||
result = (*m_options_ptr)[pos]->value();
|
result = (*m_options_ptr)[pos]->value();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <> std::string Menu<std::string>::Stringify(const Menu<std::string>::Item &item) const
|
||||||
|
{
|
||||||
|
std::string result;
|
||||||
|
if (m_item_stringifier)
|
||||||
|
result = m_item_stringifier(item.value());
|
||||||
|
else
|
||||||
|
result = item.value();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
157
src/menu.h
157
src/menu.h
@@ -22,14 +22,14 @@
|
|||||||
#define _MENU_H
|
#define _MENU_H
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <regex.h>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "window.h"
|
#include "regexes.h"
|
||||||
#include "strbuffer.h"
|
#include "strbuffer.h"
|
||||||
|
#include "window.h"
|
||||||
|
|
||||||
namespace NCurses {
|
namespace NCurses {
|
||||||
|
|
||||||
@@ -65,14 +65,6 @@ struct List
|
|||||||
///
|
///
|
||||||
virtual void PrevFound(bool wrap) = 0;
|
virtual void PrevFound(bool wrap) = 0;
|
||||||
|
|
||||||
/// @see Menu::ApplyFilter()
|
|
||||||
///
|
|
||||||
virtual void ApplyFilter(const std::string &filter, size_t beginning = 0, int flags = 0) = 0;
|
|
||||||
|
|
||||||
/// @see Menu::GetFilter()
|
|
||||||
///
|
|
||||||
virtual const std::string &GetFilter() = 0;
|
|
||||||
|
|
||||||
/// @see Menu::isFiltered()
|
/// @see Menu::isFiltered()
|
||||||
///
|
///
|
||||||
virtual bool isFiltered() = 0;
|
virtual bool isFiltered() = 0;
|
||||||
@@ -83,21 +75,6 @@ struct List
|
|||||||
///
|
///
|
||||||
template <typename T> struct Menu : public Window, public List
|
template <typename T> struct Menu : public Window, public List
|
||||||
{
|
{
|
||||||
/// Function helper prototype used to display each option on the screen.
|
|
||||||
/// If not set by setItemDisplayer(), menu won't display anything.
|
|
||||||
/// @see setItemDisplayer()
|
|
||||||
///
|
|
||||||
typedef std::function<void(Menu<T> &)> ItemDisplayer;
|
|
||||||
|
|
||||||
/// Function helper prototype used for converting items to strings.
|
|
||||||
/// If not set by SetItemStringifier(), searching and filtering
|
|
||||||
/// won't work (note that Menu<std::string> doesn't need this)
|
|
||||||
/// @see SetItemStringifier()
|
|
||||||
///
|
|
||||||
typedef std::function<std::string(const T &)> ItemStringifier;
|
|
||||||
|
|
||||||
/// Struct that holds each item in the list and its attributes
|
|
||||||
///
|
|
||||||
struct Item
|
struct Item
|
||||||
{
|
{
|
||||||
friend class Menu<T>;
|
friend class Menu<T>;
|
||||||
@@ -221,6 +198,21 @@ template <typename T> struct Menu : public Window, public List
|
|||||||
typedef std::reverse_iterator<ValueIterator> ReverseValueIterator;
|
typedef std::reverse_iterator<ValueIterator> ReverseValueIterator;
|
||||||
typedef std::reverse_iterator<ConstValueIterator> ConstReverseValueIterator;
|
typedef std::reverse_iterator<ConstValueIterator> ConstReverseValueIterator;
|
||||||
|
|
||||||
|
/// Function helper prototype used to display each option on the screen.
|
||||||
|
/// If not set by setItemDisplayer(), menu won't display anything.
|
||||||
|
/// @see setItemDisplayer()
|
||||||
|
///
|
||||||
|
typedef std::function<void(Menu<T> &)> ItemDisplayer;
|
||||||
|
|
||||||
|
/// Function helper prototype used for converting items to strings.
|
||||||
|
/// If not set by SetItemStringifier(), searching and filtering
|
||||||
|
/// won't work (note that Menu<std::string> doesn't need this)
|
||||||
|
/// @see SetItemStringifier()
|
||||||
|
///
|
||||||
|
typedef std::function<std::string(const T &)> ItemStringifier;
|
||||||
|
|
||||||
|
typedef std::function<bool(Menu<T> &, const Item &)> FilterFunction;
|
||||||
|
|
||||||
/// Constructs an empty menu with given parameters
|
/// Constructs an empty menu with given parameters
|
||||||
/// @param startx X position of left upper corner of constructed menu
|
/// @param startx X position of left upper corner of constructed menu
|
||||||
/// @param starty Y position of left upper corner of constructed menu
|
/// @param starty Y position of left upper corner of constructed menu
|
||||||
@@ -240,12 +232,12 @@ template <typename T> struct Menu : public Window, public List
|
|||||||
/// Sets helper function that is responsible for displaying items
|
/// Sets helper function that is responsible for displaying items
|
||||||
/// @param ptr function pointer that matches the ItemDisplayer prototype
|
/// @param ptr function pointer that matches the ItemDisplayer prototype
|
||||||
///
|
///
|
||||||
void setItemDisplayer(ItemDisplayer ptr) { m_item_displayer = ptr; }
|
void setItemDisplayer(const ItemDisplayer &f) { m_item_displayer = f; }
|
||||||
|
|
||||||
/// Sets helper function that is responsible for converting items to strings
|
/// Sets helper function that is responsible for converting items to strings
|
||||||
/// @param f function pointer that matches the ItemStringifier prototype
|
/// @param f function pointer that matches the ItemStringifier prototype
|
||||||
///
|
///
|
||||||
void SetItemStringifier(ItemStringifier f) { m_get_string_helper = f; }
|
void SetItemStringifier(const ItemStringifier &f) { m_item_stringifier = f; }
|
||||||
|
|
||||||
/// Reserves the size for internal container (this just calls std::vector::reserve())
|
/// Reserves the size for internal container (this just calls std::vector::reserve())
|
||||||
/// @param size requested size
|
/// @param size requested size
|
||||||
@@ -322,6 +314,8 @@ template <typename T> struct Menu : public Window, public List
|
|||||||
///
|
///
|
||||||
size_t Choice() const;
|
size_t Choice() const;
|
||||||
|
|
||||||
|
template <typename Iterator> void Filter(Iterator first, Iterator last, const FilterFunction &f);
|
||||||
|
|
||||||
/// Searches the list for a given contraint. It uses ItemStringifier to convert stored items
|
/// Searches the list for a given contraint. It uses ItemStringifier to convert stored items
|
||||||
/// into strings and then performs pattern matching. Note that this supports regular expressions.
|
/// into strings and then performs pattern matching. Note that this supports regular expressions.
|
||||||
/// @param constraint a search constraint to be used
|
/// @param constraint a search constraint to be used
|
||||||
@@ -347,22 +341,15 @@ template <typename T> struct Menu : public Window, public List
|
|||||||
///
|
///
|
||||||
virtual void PrevFound(bool wrap);
|
virtual void PrevFound(bool wrap);
|
||||||
|
|
||||||
/// Filters the list, showing only the items that matches the pattern. It uses
|
|
||||||
/// ItemStringifier to convert stored items into strings and then performs
|
|
||||||
/// pattern matching. Note that this supports regular expressions.
|
|
||||||
/// @param filter a pattern to be used in pattern matching
|
|
||||||
/// @param beginning beginning of range that has to be filtered
|
|
||||||
/// @param flags regex flags (REG_EXTENDED, REG_ICASE, REG_NOSUB, REG_NEWLINE)
|
|
||||||
///
|
|
||||||
virtual void ApplyFilter(const std::string &filter, size_t beginning = 0, int flags = 0);
|
|
||||||
|
|
||||||
/// @return const reference to currently used filter
|
/// @return const reference to currently used filter
|
||||||
///
|
///
|
||||||
virtual const std::string &GetFilter();
|
//virtual const std::string &GetFilter();
|
||||||
|
|
||||||
|
const FilterFunction &getFilter() { return m_filter; }
|
||||||
|
|
||||||
/// @return true if list is currently filtered, false otherwise
|
/// @return true if list is currently filtered, false otherwise
|
||||||
///
|
///
|
||||||
virtual bool isFiltered() { return m_options_ptr == &m_filtered_options; }
|
bool isFiltered() { return m_options_ptr == &m_filtered_options; }
|
||||||
|
|
||||||
/// Turns off filtering
|
/// Turns off filtering
|
||||||
///
|
///
|
||||||
@@ -380,6 +367,8 @@ template <typename T> struct Menu : public Window, public List
|
|||||||
///
|
///
|
||||||
std::string GetItem(size_t pos);
|
std::string GetItem(size_t pos);
|
||||||
|
|
||||||
|
std::string Stringify(const Item &item) const;
|
||||||
|
|
||||||
/// Refreshes the menu window
|
/// Refreshes the menu window
|
||||||
/// @see Window::Refresh()
|
/// @see Window::Refresh()
|
||||||
///
|
///
|
||||||
@@ -534,7 +523,7 @@ template <typename T> struct Menu : public Window, public List
|
|||||||
private:
|
private:
|
||||||
/// Clears filter, filtered data etc.
|
/// Clears filter, filtered data etc.
|
||||||
///
|
///
|
||||||
void ClearFiltered();
|
void clearFiltered();
|
||||||
|
|
||||||
bool isHighlightable(size_t pos)
|
bool isHighlightable(size_t pos)
|
||||||
{
|
{
|
||||||
@@ -542,9 +531,9 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
ItemDisplayer m_item_displayer;
|
ItemDisplayer m_item_displayer;
|
||||||
ItemStringifier m_get_string_helper;
|
ItemStringifier m_item_stringifier;
|
||||||
|
|
||||||
std::string m_filter;
|
FilterFunction m_filter;
|
||||||
std::string m_search_constraint;
|
std::string m_search_constraint;
|
||||||
|
|
||||||
std::vector<Item *> *m_options_ptr;
|
std::vector<Item *> *m_options_ptr;
|
||||||
@@ -568,10 +557,10 @@ private:
|
|||||||
Buffer m_selected_suffix;
|
Buffer m_selected_suffix;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Specialization of Menu<T>::GetItem for T = std::string, it's obvious
|
/// Specialization for T = std::string. It's obvious that if strings are stored,
|
||||||
/// that if strings are stored, we don't need extra function to convert
|
/// we don't need extra function to convert them to strings by default
|
||||||
/// them to strings by default
|
|
||||||
template <> std::string Menu<std::string>::GetItem(size_t pos);
|
template <> std::string Menu<std::string>::GetItem(size_t pos);
|
||||||
|
template <> std::string Menu<std::string>::Stringify(const Menu<std::string>::Item &item) const;
|
||||||
|
|
||||||
template <typename T> Menu<T>::Menu(size_t startx,
|
template <typename T> Menu<T>::Menu(size_t startx,
|
||||||
size_t starty,
|
size_t starty,
|
||||||
@@ -582,7 +571,7 @@ template <typename T> Menu<T>::Menu(size_t startx,
|
|||||||
Border border)
|
Border border)
|
||||||
: Window(startx, starty, width, height, title, color, border),
|
: Window(startx, starty, width, height, title, color, border),
|
||||||
m_item_displayer(0),
|
m_item_displayer(0),
|
||||||
m_get_string_helper(0),
|
m_item_stringifier(0),
|
||||||
m_options_ptr(&m_options),
|
m_options_ptr(&m_options),
|
||||||
m_beginning(0),
|
m_beginning(0),
|
||||||
m_highlight(0),
|
m_highlight(0),
|
||||||
@@ -667,6 +656,7 @@ template <typename T> void Menu<T>::Refresh()
|
|||||||
if (m_options_ptr->empty())
|
if (m_options_ptr->empty())
|
||||||
{
|
{
|
||||||
Window::Clear();
|
Window::Clear();
|
||||||
|
Window::Refresh();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -681,7 +671,7 @@ template <typename T> void Menu<T>::Refresh()
|
|||||||
if (!isHighlightable(m_highlight))
|
if (!isHighlightable(m_highlight))
|
||||||
{
|
{
|
||||||
Scroll(wUp);
|
Scroll(wUp);
|
||||||
if (isHighlightable(m_highlight))
|
if (!isHighlightable(m_highlight))
|
||||||
Scroll(wDown);
|
Scroll(wDown);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -822,7 +812,7 @@ template <typename T> void Menu<T>::Reset()
|
|||||||
m_beginning = 0;
|
m_beginning = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void Menu<T>::ClearFiltered()
|
template <typename T> void Menu<T>::clearFiltered()
|
||||||
{
|
{
|
||||||
m_filtered_options.clear();
|
m_filtered_options.clear();
|
||||||
m_filtered_positions.clear();
|
m_filtered_positions.clear();
|
||||||
@@ -833,10 +823,9 @@ template <typename T> void Menu<T>::Clear()
|
|||||||
{
|
{
|
||||||
for (auto it = m_options.begin(); it != m_options.end(); ++it)
|
for (auto it = m_options.begin(); it != m_options.end(); ++it)
|
||||||
delete *it;
|
delete *it;
|
||||||
|
clearFiltered();
|
||||||
m_options.clear();
|
m_options.clear();
|
||||||
m_found_positions.clear();
|
m_found_positions.clear();
|
||||||
m_filter.clear();
|
|
||||||
ClearFiltered();
|
|
||||||
m_options_ptr = &m_options;
|
m_options_ptr = &m_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -875,6 +864,24 @@ template <typename T> size_t Menu<T>::Choice() const
|
|||||||
return m_highlight;
|
return m_highlight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T> template <typename Iterator_>
|
||||||
|
void Menu<T>::Filter(Iterator_ first, Iterator_ last, const FilterFunction &f)
|
||||||
|
{
|
||||||
|
assert(m_options_ptr != &m_filtered_options);
|
||||||
|
clearFiltered();
|
||||||
|
m_filter = f;
|
||||||
|
for (auto it = first; it != last; ++it)
|
||||||
|
{
|
||||||
|
if (m_filter(*this, *it))
|
||||||
|
{
|
||||||
|
size_t pos = it-Begin();
|
||||||
|
m_filtered_positions.push_back(pos);
|
||||||
|
m_filtered_options.push_back(*it.base());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_options_ptr = &m_filtered_options;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T> void Menu<T>::ReverseSelection(size_t beginning)
|
template <typename T> void Menu<T>::ReverseSelection(size_t beginning)
|
||||||
{
|
{
|
||||||
auto it = m_options_ptr->begin()+beginning;
|
auto it = m_options_ptr->begin()+beginning;
|
||||||
@@ -889,16 +896,15 @@ template <typename T> bool Menu<T>::Search(const std::string &constraint, size_t
|
|||||||
if (constraint.empty())
|
if (constraint.empty())
|
||||||
return false;
|
return false;
|
||||||
m_search_constraint = constraint;
|
m_search_constraint = constraint;
|
||||||
regex_t rx;
|
Regex rx;
|
||||||
if (regcomp(&rx, m_search_constraint.c_str(), flags) == 0)
|
if (rx.compile(m_search_constraint, flags))
|
||||||
{
|
{
|
||||||
for (size_t i = beginning; i < m_options_ptr->size(); ++i)
|
for (size_t i = beginning; i < m_options_ptr->size(); ++i)
|
||||||
{
|
{
|
||||||
if (regexec(&rx, GetItem(i).c_str(), 0, 0, 0) == 0)
|
if (rx.match(GetItem(i)))
|
||||||
m_found_positions.insert(i);
|
m_found_positions.insert(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
regfree(&rx);
|
|
||||||
return !m_found_positions.empty();
|
return !m_found_positions.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -924,44 +930,19 @@ template <typename T> void Menu<T>::PrevFound(bool wrap)
|
|||||||
Highlight(*m_found_positions.rbegin());
|
Highlight(*m_found_positions.rbegin());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void Menu<T>::ApplyFilter(const std::string &filter, size_t beginning, int flags)
|
|
||||||
{
|
|
||||||
m_found_positions.clear();
|
|
||||||
ClearFiltered();
|
|
||||||
m_filter = filter;
|
|
||||||
if (m_filter.empty())
|
|
||||||
return;
|
|
||||||
for (size_t i = 0; i < beginning; ++i)
|
|
||||||
{
|
|
||||||
m_filtered_positions.push_back(i);
|
|
||||||
m_filtered_options.push_back(m_options[i]);
|
|
||||||
}
|
|
||||||
regex_t rx;
|
|
||||||
if (regcomp(&rx, m_filter.c_str(), flags) == 0)
|
|
||||||
{
|
|
||||||
for (size_t i = beginning; i < m_options.size(); ++i)
|
|
||||||
{
|
|
||||||
if (regexec(&rx, GetItem(i).c_str(), 0, 0, 0) == 0)
|
|
||||||
{
|
|
||||||
m_filtered_positions.push_back(i);
|
|
||||||
m_filtered_options.push_back(m_options[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
regfree(&rx);
|
|
||||||
m_options_ptr = &m_filtered_options;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> const std::string &Menu<T>::GetFilter()
|
|
||||||
{
|
|
||||||
return m_filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> std::string Menu<T>::GetItem(size_t pos)
|
template <typename T> std::string Menu<T>::GetItem(size_t pos)
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
if (m_get_string_helper)
|
if (m_item_stringifier)
|
||||||
result = m_get_string_helper((*m_options_ptr)[pos]->value());
|
result = m_item_stringifier((*m_options_ptr)[pos]->value());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> std::string Menu<T>::Stringify(const Item &item) const
|
||||||
|
{
|
||||||
|
std::string result;
|
||||||
|
if (m_item_stringifier)
|
||||||
|
result = m_item_stringifier(item.value());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
@@ -283,10 +284,22 @@ void Playlist::GetSelectedSongs(MPD::SongList &v)
|
|||||||
v.push_back(Items->at(*it).value());
|
v.push_back(Items->at(*it).value());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Playlist::ApplyFilter(const std::string &s)
|
std::string Playlist::currentFilter()
|
||||||
|
{
|
||||||
|
std::string filter;
|
||||||
|
if (w == Items)
|
||||||
|
filter = RegexFilter<MPD::Song>::currentFilter(*Items);
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Playlist::applyFilter(const std::string &filter)
|
||||||
{
|
{
|
||||||
if (w == Items)
|
if (w == Items)
|
||||||
Items->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
{
|
||||||
|
Items->ShowAll();
|
||||||
|
auto rx = RegexFilter<MPD::Song>(filter, Config.regex_type);
|
||||||
|
Items->Filter(Items->Begin(), Items->End(), rx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Playlist::isFiltered()
|
bool Playlist::isFiltered()
|
||||||
|
|||||||
@@ -21,13 +21,12 @@
|
|||||||
#ifndef _PLAYLIST_H
|
#ifndef _PLAYLIST_H
|
||||||
#define _PLAYLIST_H
|
#define _PLAYLIST_H
|
||||||
|
|
||||||
#include <sstream>
|
#include "interfaces.h"
|
||||||
|
|
||||||
#include "ncmpcpp.h"
|
#include "ncmpcpp.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
#include "song.h"
|
#include "song.h"
|
||||||
|
|
||||||
class Playlist : public Screen<Window>
|
class Playlist : public Screen<Window>, public Filterable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum Movement { mUp, mDown };
|
enum Movement { mUp, mDown };
|
||||||
@@ -52,7 +51,8 @@ class Playlist : public Screen<Window>
|
|||||||
virtual void ReverseSelection() { Items->ReverseSelection(); }
|
virtual void ReverseSelection() { Items->ReverseSelection(); }
|
||||||
virtual void GetSelectedSongs(MPD::SongList &);
|
virtual void GetSelectedSongs(MPD::SongList &);
|
||||||
|
|
||||||
virtual void ApplyFilter(const std::string &);
|
virtual std::string currentFilter();
|
||||||
|
virtual void applyFilter(const std::string &filter);
|
||||||
|
|
||||||
virtual List *GetList() { return w == Items ? Items : 0; }
|
virtual List *GetList() { return w == Items ? Items : 0; }
|
||||||
|
|
||||||
|
|||||||
@@ -416,9 +416,30 @@ void PlaylistEditor::GetSelectedSongs(MPD::SongList &v)
|
|||||||
v.push_back(Content->at(*it).value());
|
v.push_back(Content->at(*it).value());
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlaylistEditor::ApplyFilter(const std::string &s)
|
std::string PlaylistEditor::currentFilter()
|
||||||
{
|
{
|
||||||
GetList()->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
std::string filter;
|
||||||
|
if (w == Playlists)
|
||||||
|
filter = RegexFilter<std::string>::currentFilter(*Playlists);
|
||||||
|
else if (w == Content)
|
||||||
|
filter = RegexFilter<MPD::Song>::currentFilter(*Content);
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaylistEditor::applyFilter(const std::string &filter)
|
||||||
|
{
|
||||||
|
if (w == Playlists)
|
||||||
|
{
|
||||||
|
Playlists->ShowAll();
|
||||||
|
auto rx = RegexFilter<std::string>(filter, Config.regex_type);
|
||||||
|
Playlists->Filter(Playlists->Begin(), Playlists->End(), rx);
|
||||||
|
}
|
||||||
|
else if (w == Content)
|
||||||
|
{
|
||||||
|
Content->ShowAll();
|
||||||
|
auto rx = RegexFilter<MPD::Song>(filter, Config.regex_type);
|
||||||
|
Content->Filter(Content->Begin(), Content->End(), rx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlaylistEditor::Locate(const std::string &name)
|
void PlaylistEditor::Locate(const std::string &name)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
#include "playlist.h"
|
#include "playlist.h"
|
||||||
#include "ncmpcpp.h"
|
#include "ncmpcpp.h"
|
||||||
|
|
||||||
class PlaylistEditor : public Screen<Window>
|
class PlaylistEditor : public Screen<Window>, public Filterable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void SwitchTo();
|
virtual void SwitchTo();
|
||||||
@@ -47,7 +47,8 @@ class PlaylistEditor : public Screen<Window>
|
|||||||
virtual void ReverseSelection() { Content->ReverseSelection(); }
|
virtual void ReverseSelection() { Content->ReverseSelection(); }
|
||||||
virtual void GetSelectedSongs(MPD::SongList &);
|
virtual void GetSelectedSongs(MPD::SongList &);
|
||||||
|
|
||||||
virtual void ApplyFilter(const std::string &);
|
virtual std::string currentFilter();
|
||||||
|
virtual void applyFilter(const std::string &filter);
|
||||||
|
|
||||||
virtual void Locate(const std::string &);
|
virtual void Locate(const std::string &);
|
||||||
|
|
||||||
|
|||||||
61
src/regex_filter.h
Normal file
61
src/regex_filter.h
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2008-2012 by Andrzej Rybczak *
|
||||||
|
* electricityispower@gmail.com *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program; if not, write to the *
|
||||||
|
* Free Software Foundation, Inc., *
|
||||||
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#ifndef _REGEX_FILTER
|
||||||
|
#define _REGEX_FILTER
|
||||||
|
|
||||||
|
#include "menu.h"
|
||||||
|
|
||||||
|
template <typename T> struct RegexFilter
|
||||||
|
{
|
||||||
|
typedef NCurses::Menu<T> MenuT;
|
||||||
|
typedef typename NCurses::Menu<T>::Item MenuItem;
|
||||||
|
typedef std::function<bool(const Regex &, MenuT &menu, const MenuItem &)> FilterFunction;
|
||||||
|
|
||||||
|
RegexFilter(const std::string ®ex_, int cflags, FilterFunction custom_filter = 0)
|
||||||
|
: m_rx(regex_, cflags), m_custom_filter(custom_filter) { }
|
||||||
|
|
||||||
|
bool operator()(MenuT &menu, const MenuItem &item) {
|
||||||
|
if (m_rx.regex().empty())
|
||||||
|
return true;
|
||||||
|
if (!m_rx.error().empty())
|
||||||
|
return false;
|
||||||
|
if (m_custom_filter)
|
||||||
|
return m_custom_filter(m_rx, menu, item);
|
||||||
|
return m_rx.match(menu.Stringify(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string currentFilter(NCurses::Menu<T> &menu)
|
||||||
|
{
|
||||||
|
std::string filter;
|
||||||
|
auto rf = menu.getFilter().template target< RegexFilter<T> >();
|
||||||
|
if (rf)
|
||||||
|
filter = rf->m_rx.regex();
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Regex m_rx;
|
||||||
|
FilterFunction m_custom_filter;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
87
src/regexes.cpp
Normal file
87
src/regexes.cpp
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2008-2012 by Andrzej Rybczak *
|
||||||
|
* electricityispower@gmail.com *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program; if not, write to the *
|
||||||
|
* Free Software Foundation, Inc., *
|
||||||
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include "regexes.h"
|
||||||
|
|
||||||
|
Regex::Regex() : m_cflags(0), m_compiled(false) { }
|
||||||
|
|
||||||
|
Regex::Regex(const std::string ®ex_, int cflags)
|
||||||
|
: m_regex(regex_), m_cflags(cflags), m_compiled(false)
|
||||||
|
{
|
||||||
|
compile();
|
||||||
|
}
|
||||||
|
|
||||||
|
Regex::Regex (const Regex &rhs)
|
||||||
|
: m_regex(rhs.m_regex), m_cflags(rhs.m_cflags), m_compiled(false)
|
||||||
|
{
|
||||||
|
if (rhs.m_compiled)
|
||||||
|
compile();
|
||||||
|
}
|
||||||
|
|
||||||
|
Regex::~Regex()
|
||||||
|
{
|
||||||
|
if (m_compiled)
|
||||||
|
regfree(&m_rx);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Regex::compile()
|
||||||
|
{
|
||||||
|
if (m_compiled)
|
||||||
|
{
|
||||||
|
m_error.clear();
|
||||||
|
regfree(&m_rx);
|
||||||
|
}
|
||||||
|
int comp_res = regcomp(&m_rx, m_regex.c_str(), m_cflags);
|
||||||
|
bool result = true;
|
||||||
|
if (comp_res != 0)
|
||||||
|
{
|
||||||
|
char buf[256];
|
||||||
|
regerror(comp_res, &m_rx, buf, sizeof(buf));
|
||||||
|
m_error = buf;
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
m_compiled = result;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Regex::compile(const std::string ®ex_, int cflags)
|
||||||
|
{
|
||||||
|
m_regex = regex_;
|
||||||
|
m_cflags = cflags;
|
||||||
|
return compile();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Regex::match(const std::string &s) const
|
||||||
|
{
|
||||||
|
assert(m_compiled);
|
||||||
|
return regexec(&m_rx, s.c_str(), 0, 0, 0) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Regex &Regex::operator=(const Regex &rhs)
|
||||||
|
{
|
||||||
|
if (this == &rhs)
|
||||||
|
return *this;
|
||||||
|
m_regex = rhs.m_regex;
|
||||||
|
m_cflags = rhs.m_cflags;
|
||||||
|
if (rhs.m_compiled)
|
||||||
|
compile();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
62
src/regexes.h
Normal file
62
src/regexes.h
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2008-2012 by Andrzej Rybczak *
|
||||||
|
* electricityispower@gmail.com *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program; if not, write to the *
|
||||||
|
* Free Software Foundation, Inc., *
|
||||||
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#ifndef _REGEXES_H
|
||||||
|
#define _REGEXES_H
|
||||||
|
|
||||||
|
#include <regex.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct Regex
|
||||||
|
{
|
||||||
|
Regex();
|
||||||
|
Regex(const std::string ®ex, int cflags);
|
||||||
|
Regex(const Regex &rhs);
|
||||||
|
virtual ~Regex();
|
||||||
|
|
||||||
|
/// @return regular expression
|
||||||
|
const std::string ®ex() const { return m_regex; }
|
||||||
|
|
||||||
|
/// @return compilation error (if there was any)
|
||||||
|
const std::string &error() const { return m_error; }
|
||||||
|
|
||||||
|
/// compiles regular expression
|
||||||
|
/// @result true if compilation was successful, false otherwise
|
||||||
|
bool compile();
|
||||||
|
|
||||||
|
/// compiles regular expression
|
||||||
|
/// @result true if compilation was successful, false otherwise
|
||||||
|
bool compile(const std::string ®ex, int cflags);
|
||||||
|
|
||||||
|
/// tries to match compiled regex with given string
|
||||||
|
/// @return true if string was matched, false otherwise
|
||||||
|
bool match(const std::string &s) const;
|
||||||
|
|
||||||
|
Regex &operator=(const Regex &rhs);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_regex;
|
||||||
|
std::string m_error;
|
||||||
|
regex_t m_rx;
|
||||||
|
int m_cflags;
|
||||||
|
bool m_compiled;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _REGEXES_H
|
||||||
@@ -116,9 +116,6 @@ class BasicScreen
|
|||||||
///
|
///
|
||||||
virtual void GetSelectedSongs(GNUC_UNUSED MPD::SongList &v) { }
|
virtual void GetSelectedSongs(GNUC_UNUSED MPD::SongList &v) { }
|
||||||
|
|
||||||
/// Applies a filter to the screen
|
|
||||||
virtual void ApplyFilter(GNUC_UNUSED const std::string &filter) { }
|
|
||||||
|
|
||||||
/// @return pointer to instantiation of Menu template class
|
/// @return pointer to instantiation of Menu template class
|
||||||
/// cast to List if available or null pointer otherwise
|
/// cast to List if available or null pointer otherwise
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -156,6 +156,7 @@ void SearchEngine::EnterPressed()
|
|||||||
}
|
}
|
||||||
else if (option == SearchButton)
|
else if (option == SearchButton)
|
||||||
{
|
{
|
||||||
|
w->ShowAll();
|
||||||
ShowMessage("Searching...");
|
ShowMessage("Searching...");
|
||||||
if (w->Size() > StaticOptions)
|
if (w->Size() > StaticOptions)
|
||||||
Prepare();
|
Prepare();
|
||||||
@@ -260,9 +261,21 @@ void SearchEngine::GetSelectedSongs(MPD::SongList &v)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SearchEngine::ApplyFilter(const std::string &s)
|
std::string SearchEngine::currentFilter()
|
||||||
{
|
{
|
||||||
w->ApplyFilter(s, StaticOptions, REG_ICASE | Config.regex_type);
|
return RegexFilter<SEItem>::currentFilter(*w);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SearchEngine::applyFilter(const std::string &filter)
|
||||||
|
{
|
||||||
|
w->ShowAll();
|
||||||
|
auto fun = [](const Regex &rx, Menu<SEItem> &menu, const Menu<SEItem>::Item &item) {
|
||||||
|
if (item.isSeparator() || !item.value().isSong())
|
||||||
|
return true;
|
||||||
|
return rx.match(menu.Stringify(item));
|
||||||
|
};
|
||||||
|
auto rx = RegexFilter<SEItem>(filter, Config.regex_type, fun);
|
||||||
|
w->Filter(w->Begin(), w->End(), rx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SearchEngine::UpdateFoundList()
|
void SearchEngine::UpdateFoundList()
|
||||||
@@ -515,7 +528,7 @@ void SearchEngine::Search()
|
|||||||
std::string SearchEngine::SearchEngineOptionToString(const SEItem &ei)
|
std::string SearchEngine::SearchEngineOptionToString(const SEItem &ei)
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
if (!ei.isSong())
|
if (ei.isSong())
|
||||||
{
|
{
|
||||||
if (Config.columns_in_search_engine)
|
if (Config.columns_in_search_engine)
|
||||||
result = Playlist::SongInColumnsToString(ei.song());
|
result = Playlist::SongInColumnsToString(ei.song());
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "regex_filter.h"
|
||||||
|
#include "interfaces.h"
|
||||||
#include "mpdpp.h"
|
#include "mpdpp.h"
|
||||||
#include "ncmpcpp.h"
|
#include "ncmpcpp.h"
|
||||||
|
|
||||||
@@ -72,7 +74,7 @@ struct SEItem
|
|||||||
MPD::Song itsSong;
|
MPD::Song itsSong;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SearchEngine : public Screen< Menu<SEItem> >
|
class SearchEngine : public Screen< Menu<SEItem> >, public Filterable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void Resize();
|
virtual void Resize();
|
||||||
@@ -92,7 +94,8 @@ class SearchEngine : public Screen< Menu<SEItem> >
|
|||||||
virtual void ReverseSelection() { w->ReverseSelection(StaticOptions); }
|
virtual void ReverseSelection() { w->ReverseSelection(StaticOptions); }
|
||||||
virtual void GetSelectedSongs(MPD::SongList &);
|
virtual void GetSelectedSongs(MPD::SongList &);
|
||||||
|
|
||||||
virtual void ApplyFilter(const std::string &);
|
virtual std::string currentFilter();
|
||||||
|
virtual void applyFilter(const std::string &filter);
|
||||||
|
|
||||||
virtual List *GetList() { return w->Size() >= StaticOptions ? w : 0; }
|
virtual List *GetList() { return w->Size() >= StaticOptions ? w : 0; }
|
||||||
|
|
||||||
|
|||||||
@@ -392,7 +392,7 @@ void Configuration::SetDefaults()
|
|||||||
playlist_disable_highlight_delay = 5;
|
playlist_disable_highlight_delay = 5;
|
||||||
message_delay_time = 4;
|
message_delay_time = 4;
|
||||||
lyrics_db = 0;
|
lyrics_db = 0;
|
||||||
regex_type = 0;
|
regex_type = REG_ICASE;
|
||||||
lines_scrolled = 2;
|
lines_scrolled = 2;
|
||||||
search_engine_default_search_mode = 0;
|
search_engine_default_search_mode = 0;
|
||||||
visualizer_sync_interval = 30;
|
visualizer_sync_interval = 30;
|
||||||
@@ -953,7 +953,8 @@ void Configuration::Read()
|
|||||||
}
|
}
|
||||||
else if (name == "regular_expressions")
|
else if (name == "regular_expressions")
|
||||||
{
|
{
|
||||||
regex_type = REG_EXTENDED * (v != "basic");
|
if (v != "basic")
|
||||||
|
regex_type |= REG_EXTENDED;
|
||||||
}
|
}
|
||||||
else if (name == "lines_scrolled")
|
else if (name == "lines_scrolled")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -78,12 +78,13 @@ void StatusbarGetStringHelper(const std::wstring &)
|
|||||||
TraceMpdStatus();
|
TraceMpdStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusbarApplyFilterImmediately(const std::wstring &ws)
|
void StatusbarApplyFilterImmediately(Filterable *f, const std::wstring &ws)
|
||||||
{
|
{
|
||||||
static std::wstring cmp;
|
static std::wstring cmp;
|
||||||
if (cmp != ws)
|
if (cmp != ws)
|
||||||
{
|
{
|
||||||
myScreen->ApplyFilter(ToString((cmp = ws)));
|
cmp = ws;
|
||||||
|
f->applyFilter(ToString(cmp));
|
||||||
myScreen->RefreshWindow();
|
myScreen->RefreshWindow();
|
||||||
}
|
}
|
||||||
TraceMpdStatus();
|
TraceMpdStatus();
|
||||||
@@ -249,7 +250,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
|
|||||||
|
|
||||||
if (is_filtered)
|
if (is_filtered)
|
||||||
{
|
{
|
||||||
myPlaylist->ApplyFilter(myPlaylist->Items->GetFilter());
|
myPlaylist->applyFilter(myPlaylist->currentFilter());
|
||||||
if (myPlaylist->Items->Empty())
|
if (myPlaylist->Items->Empty())
|
||||||
myPlaylist->Items->ShowAll();
|
myPlaylist->Items->ShowAll();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#ifndef _STATUS_CHECKER_H
|
#ifndef _STATUS_CHECKER_H
|
||||||
#define _STATUS_CHECKER_H
|
#define _STATUS_CHECKER_H
|
||||||
|
|
||||||
|
#include "interfaces.h"
|
||||||
#include "mpdpp.h"
|
#include "mpdpp.h"
|
||||||
#include "ncmpcpp.h"
|
#include "ncmpcpp.h"
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ void ShowMessage(const char *, ...) GNUC_PRINTF(1, 2);
|
|||||||
|
|
||||||
void StatusbarMPDCallback();
|
void StatusbarMPDCallback();
|
||||||
void StatusbarGetStringHelper(const std::wstring &);
|
void StatusbarGetStringHelper(const std::wstring &);
|
||||||
void StatusbarApplyFilterImmediately(const std::wstring &);
|
void StatusbarApplyFilterImmediately(Filterable *f, const std::wstring &ws);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#ifdef HAVE_TAGLIB_H
|
#ifdef HAVE_TAGLIB_H
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
// taglib includes
|
// taglib includes
|
||||||
@@ -266,7 +267,7 @@ void TagEditor::Update()
|
|||||||
{
|
{
|
||||||
size_t slash = itsBrowsedDir.rfind("/");
|
size_t slash = itsBrowsedDir.rfind("/");
|
||||||
std::string parent = slash != std::string::npos ? itsBrowsedDir.substr(0, slash) : "/";
|
std::string parent = slash != std::string::npos ? itsBrowsedDir.substr(0, slash) : "/";
|
||||||
Dirs->AddItem(make_pair("[..]", parent));
|
Dirs->AddItem(make_pair("..", parent));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -771,14 +772,43 @@ void TagEditor::GetSelectedSongs(MPD::SongList &v)
|
|||||||
v.push_back(static_cast<MPD::Song>((*Tags)[*it].value()));
|
v.push_back(static_cast<MPD::Song>((*Tags)[*it].value()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TagEditor::ApplyFilter(const std::string &s)
|
std::string TagEditor::currentFilter()
|
||||||
|
{
|
||||||
|
std::string filter;
|
||||||
|
if (w == Dirs)
|
||||||
|
filter = RegexFilter<string_pair>::currentFilter(*Dirs);
|
||||||
|
else if (w == Albums)
|
||||||
|
filter = RegexFilter<string_pair>::currentFilter(*Albums);
|
||||||
|
else if (w == Tags)
|
||||||
|
filter = RegexFilter<MPD::MutableSong>::currentFilter(*Tags);
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TagEditor::applyFilter(const std::string &filter)
|
||||||
{
|
{
|
||||||
if (w == Dirs)
|
if (w == Dirs)
|
||||||
Dirs->ApplyFilter(s, 1, REG_ICASE | Config.regex_type);
|
{
|
||||||
|
Dirs->ShowAll();
|
||||||
|
auto fun = [](const Regex &rx, Menu<string_pair> &menu, const Menu<string_pair>::Item &item) {
|
||||||
|
if (item.value().first == "." || item.value().first == "..")
|
||||||
|
return true;
|
||||||
|
return rx.match(menu.Stringify(item));
|
||||||
|
};
|
||||||
|
auto rx = RegexFilter<string_pair>(filter, Config.regex_type, fun);
|
||||||
|
Dirs->Filter(Dirs->Begin(), Dirs->End(), rx);
|
||||||
|
}
|
||||||
else if (w == Albums)
|
else if (w == Albums)
|
||||||
Albums->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
{
|
||||||
|
Albums->ShowAll();
|
||||||
|
auto rx = RegexFilter<string_pair>(filter, Config.regex_type);
|
||||||
|
Albums->Filter(Albums->Begin(), Albums->End(), rx);
|
||||||
|
}
|
||||||
else if (w == Tags)
|
else if (w == Tags)
|
||||||
Tags->ApplyFilter(s, 0, REG_ICASE | Config.regex_type);
|
{
|
||||||
|
Tags->ShowAll();
|
||||||
|
auto rx = RegexFilter<MPD::MutableSong>(filter, Config.regex_type);
|
||||||
|
Tags->Filter(Tags->Begin(), Tags->End(), rx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List *TagEditor::GetList()
|
List *TagEditor::GetList()
|
||||||
|
|||||||
@@ -34,9 +34,10 @@
|
|||||||
|
|
||||||
#include "mpdpp.h"
|
#include "mpdpp.h"
|
||||||
#include "mutable_song.h"
|
#include "mutable_song.h"
|
||||||
|
#include "regex_filter.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
|
|
||||||
class TagEditor : public Screen<Window>
|
class TagEditor : public Screen<Window>, public Filterable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TagEditor() : FParser(0), FParserHelper(0), FParserLegend(0), FParserPreview(0), itsBrowsedDir("/") { }
|
TagEditor() : FParser(0), FParserHelper(0), FParserLegend(0), FParserPreview(0), itsBrowsedDir("/") { }
|
||||||
@@ -61,7 +62,8 @@ class TagEditor : public Screen<Window>
|
|||||||
virtual void ReverseSelection() { Tags->ReverseSelection(); }
|
virtual void ReverseSelection() { Tags->ReverseSelection(); }
|
||||||
virtual void GetSelectedSongs(MPD::SongList &);
|
virtual void GetSelectedSongs(MPD::SongList &);
|
||||||
|
|
||||||
virtual void ApplyFilter(const std::string &);
|
virtual std::string currentFilter();
|
||||||
|
virtual void applyFilter(const std::string &filter);
|
||||||
|
|
||||||
virtual List *GetList();
|
virtual List *GetList();
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
#include "curses.h"
|
#include "curses.h"
|
||||||
#include "gcc.h"
|
#include "gcc.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -148,7 +149,7 @@ namespace NCurses
|
|||||||
/// to obtain string from Window::GetString() function
|
/// to obtain string from Window::GetString() function
|
||||||
/// @see Window::GetString()
|
/// @see Window::GetString()
|
||||||
///
|
///
|
||||||
typedef void (*GetStringHelper)(const std::wstring &);
|
typedef std::function<void(const std::wstring &)> GetStringHelper;
|
||||||
|
|
||||||
/// Initializes curses screen and sets some additional attributes
|
/// Initializes curses screen and sets some additional attributes
|
||||||
/// @param window_title title of the window (has an effect only if pdcurses lib is used)
|
/// @param window_title title of the window (has an effect only if pdcurses lib is used)
|
||||||
|
|||||||
Reference in New Issue
Block a user