window: add ScopedStringHelper and use it where appropriate

This commit is contained in:
Andrzej Rybczak
2014-10-31 15:30:54 +01:00
parent fa1cd965fa
commit b265d56cdf
3 changed files with 31 additions and 9 deletions

View File

@@ -842,10 +842,11 @@ void ExecuteCommand::run()
std::string cmd_name;
{
Statusbar::ScopedLock lock;
NC::Window::ScopedStringHelper helper(*wFooter,
Statusbar::Helpers::TryExecuteImmediateCommand()
);
Statusbar::put() << NC::Format::Bold << ":" << NC::Format::NoBold;
wFooter->setGetStringHelper(Statusbar::Helpers::TryExecuteImmediateCommand());
cmd_name = wFooter->getString();
wFooter->setGetStringHelper(Statusbar::Helpers::getString);
}
auto cmd = Bindings.findCommand(cmd_name);
@@ -1882,10 +1883,11 @@ void ApplyFilter::run()
try
{
Statusbar::ScopedLock lock;
NC::Window::ScopedStringHelper helper(*wFooter,
Statusbar::Helpers::ApplyFilterImmediately(f, filter)
);
Statusbar::put() << NC::Format::Bold << "Apply filter: " << NC::Format::NoBold;
wFooter->setGetStringHelper(Statusbar::Helpers::ApplyFilterImmediately(f, filter));
wFooter->getString(filter);
wFooter->setGetStringHelper(Statusbar::Helpers::getString);
}
catch (NC::PromptAborted &)
{

View File

@@ -167,7 +167,7 @@ void Status::handleServerError(MPD::ServerError &e)
Statusbar::printf("MPD: %1%", e.what());
if (e.code() == MPD_SERVER_ERROR_PERMISSION)
{
wFooter->setGetStringHelper(nullptr);
NC::Window::ScopedStringHelper helper(*wFooter, nullptr);
Statusbar::put() << "Password: ";
Mpd.SetPassword(wFooter->getString(0, true));
try {
@@ -176,7 +176,6 @@ void Status::handleServerError(MPD::ServerError &e)
} catch (MPD::ServerError &e_prim) {
handleServerError(e_prim);
}
wFooter->setGetStringHelper(Statusbar::Helpers::getString);
}
else if (e.code() == MPD_SERVER_ERROR_NO_EXIST && myScreen == myBrowser)
{

View File

@@ -188,6 +188,23 @@ struct XY
/// Main class of NCurses namespace, used as base for other specialized windows
struct Window
{
/// Sets helper to a specific value for the current scope
struct ScopedStringHelper
{
template <typename HelperT>
ScopedStringHelper(Window &w, HelperT &&helper) noexcept
: m_w(w), m_helper(std::move(w.m_get_string_helper)) {
m_w.m_get_string_helper = std::forward<HelperT>(helper);
}
~ScopedStringHelper() noexcept {
m_w.m_get_string_helper = std::move(m_helper);
}
private:
Window &m_w;
GetStringHelper m_helper;
};
Window() : m_window(0), m_border_window(0) { }
/// Constructs an empty window with given parameters
@@ -254,11 +271,11 @@ struct Window
/// @see setGetStringHelper()
/// @see SetTimeout()
/// @see CreateHistory()
std::string getString(const std::string &base, size_t width = 0, bool encrypted = 0);
std::string getString(const std::string &base, size_t width = 0, bool encrypted = false);
/// Wrapper for above function that doesn't take base string (it will be empty).
/// Taken parameters are the same as for above.
std::string getString(size_t width = 0, bool encrypted = 0)
std::string getString(size_t width = 0, bool encrypted = false)
{
return getString("", width, encrypted);
}
@@ -285,7 +302,11 @@ struct Window
/// Sets helper function used in getString()
/// @param helper pointer to function that matches getStringHelper prototype
/// @see getString()
void setGetStringHelper(GetStringHelper helper) { m_get_string_helper = helper; }
template <typename HelperT>
void setGetStringHelper(HelperT &&helper)
{
m_get_string_helper = std::forward<HelperT>(helper);
}
/// Run current GetString helper function (if defined).
/// @see getString()