actions: make askYesNoQuestion use NC::Window::prompt

master
Andrzej Rybczak 12 years ago
parent cb578125cc
commit a8b46a8e47
  1. 38
      src/actions.cpp
  2. 6
      src/actions.h
  3. 21
      src/statusbar.cpp
  4. 20
      src/statusbar.h
  5. 4
      src/strbuffer.h
  6. 4
      src/tag_editor.cpp

@ -252,22 +252,15 @@ void setWindowsDimensions()
FooterHeight = Config.statusbar_visibility ? 2 : 1; FooterHeight = Config.statusbar_visibility ? 2 : 1;
} }
bool askYesNoQuestion(const boost::format &fmt, void (*callback)()) bool askYesNoQuestion(const boost::format &fmt)
{ {
using Global::wFooter;
Statusbar::ScopedLock lock; Statusbar::ScopedLock lock;
Statusbar::put() << fmt.str() << " [" << NC::Format::Bold << 'y' << NC::Format::NoBold << '/' << NC::Format::Bold << 'n' << NC::Format::NoBold << "]"; Statusbar::put() << fmt.str()
wFooter->refresh(); << " [" << NC::Format::Bold << 'y' << NC::Format::NoBold
int answer = 0; << '/' << NC::Format::Bold << 'n' << NC::Format::NoBold
do << "] ";
{ auto answer = Statusbar::Helpers::promptReturnOneOf({"y", "n"});
if (callback) return answer == "y";
callback();
answer = wFooter->readKey();
}
while (answer != 'y' && answer != 'n');
return answer == 'y';
} }
bool isMPDMusicDirSet() bool isMPDMusicDirSet()
@ -685,7 +678,7 @@ void DeleteBrowserItems::run()
question = boost::format("Delete %1% \"%2%\"?") question = boost::format("Delete %1% \"%2%\"?")
% itemTypeToString(item.type) % wideShorten(iname, COLS-question.size()-10); % itemTypeToString(item.type) % wideShorten(iname, COLS-question.size()-10);
} }
bool yes = askYesNoQuestion(question, Status::trace); bool yes = askYesNoQuestion(question);
if (yes) if (yes)
{ {
bool success = true; bool success = true;
@ -734,7 +727,7 @@ void DeleteStoredPlaylist::run()
else else
question = boost::format("Delete playlist \"%1%\"?") question = boost::format("Delete playlist \"%1%\"?")
% wideShorten(myPlaylistEditor->Playlists.current().value(), COLS-question.size()-10); % wideShorten(myPlaylistEditor->Playlists.current().value(), COLS-question.size()-10);
bool yes = askYesNoQuestion(question, Status::trace); bool yes = askYesNoQuestion(question);
if (yes) if (yes)
{ {
auto list = getSelectedOrCurrent(myPlaylistEditor->Playlists.begin(), myPlaylistEditor->Playlists.end(), myPlaylistEditor->Playlists.currentI()); auto list = getSelectedOrCurrent(myPlaylistEditor->Playlists.begin(), myPlaylistEditor->Playlists.end(), myPlaylistEditor->Playlists.currentI());
@ -806,8 +799,7 @@ void SavePlaylist::run()
if (e.code() == MPD_SERVER_ERROR_EXIST) if (e.code() == MPD_SERVER_ERROR_EXIST)
{ {
bool yes = askYesNoQuestion( bool yes = askYesNoQuestion(
boost::format("Playlist \"%1%\" already exists, overwrite?") % playlist_name, boost::format("Playlist \"%1%\" already exists, overwrite?") % playlist_name
Status::trace
); );
if (yes) if (yes)
{ {
@ -1761,7 +1753,7 @@ void CropMainPlaylist::run()
return; return;
bool yes = true; bool yes = true;
if (Config.ask_before_clearing_playlists) if (Config.ask_before_clearing_playlists)
yes = askYesNoQuestion("Do you really want to crop main playlist?", Status::trace); yes = askYesNoQuestion("Do you really want to crop main playlist?");
if (yes) if (yes)
{ {
Statusbar::print("Cropping playlist..."); Statusbar::print("Cropping playlist...");
@ -1787,8 +1779,7 @@ void CropPlaylist::run()
bool yes = true; bool yes = true;
if (Config.ask_before_clearing_playlists) if (Config.ask_before_clearing_playlists)
yes = askYesNoQuestion( yes = askYesNoQuestion(
boost::format("Do you really want to crop playlist \"%1%\"?") % playlist, boost::format("Do you really want to crop playlist \"%1%\"?") % playlist
Status::trace
); );
if (yes) if (yes)
{ {
@ -1803,7 +1794,7 @@ void ClearMainPlaylist::run()
{ {
bool yes = true; bool yes = true;
if (Config.ask_before_clearing_playlists) if (Config.ask_before_clearing_playlists)
yes = askYesNoQuestion("Do you really want to clear main playlist?", Status::trace); yes = askYesNoQuestion("Do you really want to clear main playlist?");
if (yes) if (yes)
{ {
auto delete_fun = boost::bind(&MPD::Connection::Delete, _1, _2); auto delete_fun = boost::bind(&MPD::Connection::Delete, _1, _2);
@ -1828,8 +1819,7 @@ void ClearPlaylist::run()
bool yes = true; bool yes = true;
if (Config.ask_before_clearing_playlists) if (Config.ask_before_clearing_playlists)
yes = askYesNoQuestion( yes = askYesNoQuestion(
boost::format("Do you really want to clear playlist \"%1%\"?") % playlist, boost::format("Do you really want to clear playlist \"%1%\"?") % playlist
Status::trace
); );
if (yes) if (yes)
{ {

@ -67,10 +67,10 @@ void setResizeFlags();
void resizeScreen(bool reload_main_window); void resizeScreen(bool reload_main_window);
void setWindowsDimensions(); void setWindowsDimensions();
bool askYesNoQuestion(const boost::format &question, void (*callback)()); bool askYesNoQuestion(const boost::format &question);
inline bool askYesNoQuestion(const std::string &question, void (*callback)()) inline bool askYesNoQuestion(const std::string &question)
{ {
return askYesNoQuestion(boost::format(question), callback); return askYesNoQuestion(boost::format(question));
} }
bool isMPDMusicDirSet(); bool isMPDMusicDirSet();

@ -199,6 +199,27 @@ bool Statusbar::Helpers::mainHook(const char *)
return true; return true;
} }
std::string Statusbar::Helpers::promptReturnOneOf(std::vector<std::string> &&values)
{
Statusbar::Helpers::ImmediatelyReturnOneOf prompt_hook(std::move(values));
NC::Window::ScopedPromptHook hook(*wFooter, prompt_hook);
int x = wFooter->getX(), y = wFooter->getY();
std::string result;
do
{
wFooter->goToXY(x, y);
result = wFooter->prompt();
}
while (!prompt_hook.isOneOf(result));
return result;
}
bool Statusbar::Helpers::ImmediatelyReturnOneOf::operator()(const char *s) const
{
Status::trace();
return !isOneOf(s);
}
bool Statusbar::Helpers::ApplyFilterImmediately::operator()(const char *s) bool Statusbar::Helpers::ApplyFilterImmediately::operator()(const char *s)
{ {
using Global::myScreen; using Global::myScreen;

@ -69,6 +69,26 @@ void mpd();
/// called each time user types another character while inside Window::getString /// called each time user types another character while inside Window::getString
bool mainHook(const char *); bool mainHook(const char *);
/// prompt and return one of the strings specified in the vector
std::string promptReturnOneOf(std::vector<std::string> &&values);
struct ImmediatelyReturnOneOf
{
ImmediatelyReturnOneOf(std::vector<std::string> arg)
: m_values(std::move(arg))
{ }
bool operator()(const char *s) const;
template <typename StringT>
bool isOneOf(StringT &&s) const {
return std::find(m_values.begin(), m_values.end(), std::forward<StringT>(s)) != m_values.end();
}
private:
std::vector<std::string> m_values;
};
/// called each time user changes current filter (while being inside Window::getString) /// called each time user changes current filter (while being inside Window::getString)
struct ApplyFilterImmediately struct ApplyFilterImmediately
{ {

@ -91,7 +91,7 @@ public:
typedef std::set<Property> Properties; typedef std::set<Property> Properties;
template <typename... Args> template <typename... Args>
BasicBuffer(Args... args) BasicBuffer(Args&&... args)
{ {
construct(std::forward<Args>(args)...); construct(std::forward<Args>(args)...);
} }
@ -190,7 +190,7 @@ public:
private: private:
void construct() { } void construct() { }
template <typename ArgT, typename... Args> template <typename ArgT, typename... Args>
void construct(ArgT &&arg, Args... args) void construct(ArgT &&arg, Args&&... args)
{ {
*this << std::forward<ArgT>(arg); *this << std::forward<ArgT>(arg);
construct(std::forward<Args>(args)...); construct(std::forward<Args>(args)...);

@ -468,7 +468,7 @@ void TagEditor::enterPressed()
if (w == TagTypes && id == 5) if (w == TagTypes && id == 5)
{ {
bool yes = Actions::askYesNoQuestion("Number tracks?", Status::trace); bool yes = Actions::askYesNoQuestion("Number tracks?");
if (yes) if (yes)
{ {
auto it = EditedSongs.begin(); auto it = EditedSongs.begin();
@ -963,7 +963,7 @@ bool TagEditor::ifAnyModifiedAskForDiscarding()
{ {
bool result = true; bool result = true;
if (isAnyModified(*Tags)) if (isAnyModified(*Tags))
result = Actions::askYesNoQuestion("There are pending changes, are you sure?", Status::trace); result = Actions::askYesNoQuestion("There are pending changes, are you sure?");
return result; return result;
} }

Loading…
Cancel
Save