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

This commit is contained in:
Andrzej Rybczak
2014-10-31 20:04:20 +01:00
parent cb578125cc
commit a8b46a8e47
6 changed files with 62 additions and 31 deletions

View File

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

View File

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

View File

@@ -199,6 +199,27 @@ bool Statusbar::Helpers::mainHook(const char *)
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)
{
using Global::myScreen;

View File

@@ -69,6 +69,26 @@ void mpd();
/// called each time user types another character while inside Window::getString
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)
struct ApplyFilterImmediately
{

View File

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

View File

@@ -468,7 +468,7 @@ void TagEditor::enterPressed()
if (w == TagTypes && id == 5)
{
bool yes = Actions::askYesNoQuestion("Number tracks?", Status::trace);
bool yes = Actions::askYesNoQuestion("Number tracks?");
if (yes)
{
auto it = EditedSongs.begin();
@@ -963,7 +963,7 @@ bool TagEditor::ifAnyModifiedAskForDiscarding()
{
bool result = true;
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;
}