help: show defined action chains

This commit is contained in:
Andrzej Rybczak
2015-09-27 03:31:02 +02:00
parent 43a6b81820
commit 6ad3de7366
8 changed files with 157 additions and 28 deletions

1
NEWS
View File

@@ -31,6 +31,7 @@ ncmpcpp-0.7 (????-??-??)
* Value of 'visualizer_sync_interval' is now restricted to be greater than 9.
* Output of the visualizer now scales automatically as long as 'visualizer_sample_multiplier' is set to 1.
* Command line switch that prints current song to the standard output is available once again.
* Defined action chains are now shown in help screen.
ncmpcpp-0.6.7 (2015-09-12)
* Fetching artist info from last.fm was fixed.

View File

@@ -88,9 +88,9 @@ extern size_t FooterStartY;
struct BaseAction
{
BaseAction(Type type_, const char *name_): m_type(type_), m_name(name_) { }
BaseAction(Type type_, const char *name_): m_name(name_), m_type(type_) { }
const char *name() const { return m_name; }
const std::string &name() const { return m_name; }
Type type() const { return m_type; }
virtual bool canBeRun() { return true; }
@@ -105,11 +105,13 @@ struct BaseAction
return false;
}
protected:
std::string m_name;
private:
virtual void run() = 0;
Type m_type;
const char *m_name;
};
BaseAction &get(Type at);

View File

@@ -36,8 +36,8 @@ struct Binding
typedef std::vector<Actions::BaseAction *> ActionChain;
template <typename ArgT>
Binding(ArgT &&actions)
: m_actions(std::forward<ArgT>(actions)) {
Binding(ArgT &&actions_)
: m_actions(std::forward<ArgT>(actions_)) {
assert(!m_actions.empty());
}
Binding(Actions::Type at)
@@ -58,6 +58,10 @@ struct Binding
return m_actions[0];
}
const ActionChain &actions() const {
return m_actions;
}
private:
ActionChain m_actions;
};

View File

@@ -25,6 +25,7 @@
#include "help.h"
#include "settings.h"
#include "status.h"
#include "utility/string.h"
#include "utility/wide_string.h"
#include "title.h"
#include "screen_switcher.h"
@@ -36,6 +37,22 @@ Help *myHelp;
namespace {
std::string align_key_rep(std::wstring keys)
{
size_t i = 0, len = 0;
const size_t max_len = 20;
for (; i < keys.size(); ++i)
{
int width = std::max(1, wcwidth(keys[i]));
if (len+width > max_len)
break;
else
len += width;
}
keys.resize(i + max_len - len, ' ');
return ToString(keys);
}
std::string display_keys(const Actions::Type at)
{
std::wstring result, skey;
@@ -54,18 +71,7 @@ std::string display_keys(const Actions::Type at)
}
}
}
size_t i = 0, len = 0;
const size_t max_len = 20;
for (; i < result.size(); ++i)
{
int width = std::max(1, wcwidth(result[i]));
if (len+width > max_len)
break;
else
len += width;
}
result.resize(i + max_len - len, ' ');
return ToString(result);
return align_key_rep(std::move(result));
}
void section(NC::Scrollpad &w, const char *type_, const char *title_)
@@ -93,6 +99,11 @@ void key(NC::Scrollpad &w, const Actions::Type at, const boost::format &desc)
w << " " << display_keys(at) << " : " << desc.str() << '\n';
}
void key(NC::Scrollpad &w, NC::Key::Type k, const std::string &desc)
{
w << " " << align_key_rep(keyToWString(k)) << " : " << desc << '\n';
}
/**********************************************************************/
void mouse_section(NC::Scrollpad &w, const char *title_)
@@ -393,6 +404,21 @@ void write_bindings(NC::Scrollpad &w)
mouse(w, "Right click", "Toggle output");
# endif // ENABLE_OUTPUTS
section(w, "", "Action chains");
for (const auto &k : Bindings)
{
for (const auto &binding : k.second)
{
if (!binding.isSingle())
{
std::vector<std::string> commands;
for (const auto &action : binding.actions())
commands.push_back(action->name());
key(w, k.first, join<std::string>(commands, ", "));
}
}
}
section(w, "", "List of available colors");
for (int i = 0; i < COLORS; ++i)
w << NC::Color(i, NC::Color::transparent) << i+1 << NC::Color::End << " ";

View File

@@ -18,27 +18,72 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "bindings.h"
#include "global.h"
#include "macro_utilities.h"
#include "utility/string.h"
#include "utility/wide_string.h"
namespace Actions {
PushCharacters::PushCharacters(NC::Window **w, std::vector<NC::Key::Type> &&queue)
: BaseAction(Type::MacroUtility, "push_characters")
, m_window(w)
, m_queue(queue)
{
assert(w != nullptr);
std::vector<std::string> keys;
for (const auto &key : queue)
keys.push_back(ToString(keyToWString(key)));
m_name += " \"";
m_name += join<std::string>(keys, ", ");
m_name += "\"";
}
void PushCharacters::run()
{
for (auto it = m_queue.begin(); it != m_queue.end(); ++it)
(*m_window)->pushChar(*it);
}
RequireRunnable::RequireRunnable(BaseAction *action)
: BaseAction(Type::MacroUtility, "require_runnable")
, m_action(action)
{
assert(m_action != nullptr);
m_name += " \"";
m_name += m_action->name();
m_name += "\"";
}
bool RequireRunnable::canBeRun()
{
return m_action->canBeRun();
}
RequireScreen::RequireScreen(ScreenType screen_type)
: BaseAction(Type::MacroUtility, "require_screen")
, m_screen_type(screen_type)
{
m_name += " \"";
m_name += screenTypeToString(m_screen_type);
m_name += "\"";
}
bool RequireScreen::canBeRun()
{
return Global::myScreen->type() == m_screen_type;
}
RunExternalCommand::RunExternalCommand(std::string command)
: BaseAction(Type::MacroUtility, "run_external_command")
, m_command(std::move(command))
{
m_name += " \"";
m_name += m_command;
m_name += "\"";
}
void RunExternalCommand::run()
{
GNUC_UNUSED int res;

View File

@@ -29,8 +29,7 @@ namespace Actions {
struct PushCharacters: BaseAction
{
PushCharacters(NC::Window **w, std::vector<NC::Key::Type> &&queue)
: BaseAction(Type::MacroUtility, ""), m_window(w), m_queue(queue) { }
PushCharacters(NC::Window **w, std::vector<NC::Key::Type> &&queue);
private:
virtual void run() OVERRIDE;
@@ -41,8 +40,7 @@ private:
struct RequireRunnable: BaseAction
{
RequireRunnable(BaseAction *action)
: BaseAction(Type::MacroUtility, ""), m_action(action) { assert(action); }
RequireRunnable(BaseAction *action);
private:
virtual bool canBeRun() OVERRIDE;
@@ -53,8 +51,7 @@ private:
struct RequireScreen: BaseAction
{
RequireScreen(ScreenType screen_type)
: BaseAction(Type::MacroUtility, ""), m_screen_type(screen_type) { }
RequireScreen(ScreenType screen_type);
private:
virtual bool canBeRun() OVERRIDE;
@@ -65,8 +62,7 @@ private:
struct RunExternalCommand: BaseAction
{
RunExternalCommand(std::string command)
: BaseAction(Type::MacroUtility, ""), m_command(command) { }
RunExternalCommand(std::string command);
private:
virtual void run() OVERRIDE;

View File

@@ -39,6 +39,59 @@
#include "tiny_tag_editor.h"
#include "visualizer.h"
std::string screenTypeToString(ScreenType st)
{
switch (st)
{
case ScreenType::Browser:
return "browser";
#ifdef ENABLE_CLOCK
case ScreenType::Clock:
return "clock";
#endif // ENABLE_CLOCK
case ScreenType::Help:
return "help";
#ifdef HAVE_CURL_CURL_H
case ScreenType::Lastfm:
return "last_fm";
#endif // HAVE_CURL_CURL_H
case ScreenType::Lyrics:
return "lyrics";
case ScreenType::MediaLibrary:
return "media_library";
#ifdef ENABLE_OUTPUTS
case ScreenType::Outputs:
return "outputs";
#endif // ENABLE_OUTPUTS
case ScreenType::Playlist:
return "playlist";
case ScreenType::PlaylistEditor:
return "playlist_editor";
case ScreenType::SearchEngine:
return "search_engine";
case ScreenType::SelectedItemsAdder:
return "selected_items_adder";
case ScreenType::ServerInfo:
return "server_info";
case ScreenType::SongInfo:
return "song_info";
case ScreenType::SortPlaylistDialog:
return "sort_playlist_dialog";
#ifdef HAVE_TAGLIB_H
case ScreenType::TagEditor:
return "tag_editor";
case ScreenType::TinyTagEditor:
return "tiny_tag_editor";
#endif // HAVE_TAGLIB_H
case ScreenType::Unknown:
return "unknown";
#ifdef ENABLE_VISUALIZER
case ScreenType::Visualizer:
return "visualizer";
#endif // ENABLE_VISUALIZER
}
}
ScreenType stringtoStartupScreenType(const std::string &s)
{
ScreenType result = ScreenType::Unknown;

View File

@@ -58,6 +58,8 @@ enum class ScreenType {
# endif // ENABLE_VISUALIZER
};
std::string screenTypeToString(ScreenType st);
ScreenType stringtoStartupScreenType(const std::string &s);
ScreenType stringToScreenType(const std::string &s);