From 22140f7db9ce55e129c757581fa4abfabc890a04 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 13 Jan 2017 11:22:23 +0100 Subject: [PATCH] refactor(builder): Action wrapped labels --- include/components/builder.hpp | 1 + src/components/builder.cpp | 25 ++++++++++++----- src/modules/bspwm.cpp | 11 +++----- src/modules/mpd.cpp | 50 ++++++++++------------------------ 4 files changed, 38 insertions(+), 49 deletions(-) diff --git a/include/components/builder.hpp b/include/components/builder.hpp index eac5afac..646c71f1 100644 --- a/include/components/builder.hpp +++ b/include/components/builder.hpp @@ -52,6 +52,7 @@ class builder { void underline(const string& color = ""); void underline_close(); void cmd(mousebtn index, string action, bool condition = true); + void cmd(mousebtn index, string action, const label_t& label); void cmd_close(bool condition = true); protected: diff --git a/src/components/builder.cpp b/src/components/builder.cpp index 47607d09..c4e8fdca 100644 --- a/src/components/builder.cpp +++ b/src/components/builder.cpp @@ -83,6 +83,10 @@ void builder::append(string text) { * This will also parse raw syntax tags */ void builder::node(string str, bool add_space) { + if (str.empty()) { + return; + } + string::size_type n, m; string s(move(str)); @@ -506,15 +510,22 @@ void builder::underline_close() { * Open command tag */ void builder::cmd(mousebtn index, string action, bool condition) { - int button = static_cast(index); - - if (!condition || action.empty()) { - return; + if (condition && !action.empty()) { + action = string_util::replace_all(action, ":", "\\:"); + tag_open(syntaxtag::A, to_string(static_cast(index)) + ":" + action + ":"); } +} - action = string_util::replace_all(action, ":", "\\:"); - - tag_open(syntaxtag::A, to_string(button) + ":" + action + ":"); +/** + * Wrap label in command block + */ +void builder::cmd(mousebtn index, string action, const label_t& label) { + if (!action.empty() && label && *label) { + action = string_util::replace_all(action, ":", "\\:"); + tag_open(syntaxtag::A, to_string(static_cast(index)) + ":" + action + ":"); + node(label); + tag_close(syntaxtag::A); + } } /** diff --git a/src/modules/bspwm.cpp b/src/modules/bspwm.cpp index 8dfae1d4..21da9f18 100644 --- a/src/modules/bspwm.cpp +++ b/src/modules/bspwm.cpp @@ -5,6 +5,7 @@ #include "modules/bspwm.hpp" #include "utils/factory.hpp" #include "utils/file.hpp" +#include "utils/string.hpp" #include "modules/meta/base.inl" @@ -393,18 +394,14 @@ namespace modules { workspace_n++; if (m_click) { - builder->cmd(mousebtn::LEFT, EVENT_CLICK + to_string(m_index) + "+" + to_string(workspace_n)); - builder->node(ws.second); - builder->cmd_close(); + builder->cmd(mousebtn::LEFT, stringstream() << EVENT_CLICK << m_index << "+" << workspace_n, ws.second); } else { builder->node(ws.second); } if (m_inlinemode && m_monitors[m_index]->focused && check_mask(ws.first, bspwm_state::FOCUSED)) { for (auto&& mode : m_monitors[m_index]->modes) { - if (*mode.get()) { - builder->node(mode); - } + builder->node(mode); } } } @@ -421,7 +418,7 @@ namespace modules { int modes_n = 0; for (auto&& mode : m_monitors[m_index]->modes) { - if (*mode.get()) { + if (mode && *mode) { builder->node(mode); modes_n++; } diff --git a/src/modules/mpd.cpp b/src/modules/mpd.cpp index 39cd53bc..0addecdf 100644 --- a/src/modules/mpd.cpp +++ b/src/modules/mpd.cpp @@ -257,62 +257,42 @@ namespace modules { } bool mpd_module::build(builder* builder, const string& tag) const { - bool is_playing = false; - bool is_paused = false; - bool is_stopped = true; - int elapsed_percentage = 0; - - if (m_status) { - elapsed_percentage = m_status->get_elapsed_percentage(); - - if (m_status->match_state(mpdstate::PLAYING)) { - is_playing = true; - } - if (m_status->match_state(mpdstate::PAUSED)) { - is_paused = true; - } - if (!(m_status->match_state(mpdstate::STOPPED))) { - is_stopped = false; - } - } - - auto icon_cmd = [&builder](string cmd, icon_t icon) { - builder->cmd(mousebtn::LEFT, cmd); - builder->node(icon); - builder->cmd_close(); - }; + bool is_playing = m_status && m_status->match_state(mpdstate::PLAYING); + bool is_paused = m_status && m_status->match_state(mpdstate::PAUSED); + bool is_stopped = m_status && m_status->match_state(mpdstate::STOPPED); if (tag == TAG_LABEL_SONG && !is_stopped) { builder->node(m_label_song); } else if (tag == TAG_LABEL_TIME && !is_stopped) { builder->node(m_label_time); } else if (tag == TAG_BAR_PROGRESS && !is_stopped) { - builder->node(m_bar_progress->output(elapsed_percentage)); + builder->node(m_bar_progress->output(!m_status ? 0 : m_status->get_elapsed_percentage())); } else if (tag == TAG_LABEL_OFFLINE) { builder->node(m_label_offline); } else if (tag == TAG_ICON_RANDOM) { - icon_cmd(EVENT_RANDOM, m_icons->get("random")); + builder->cmd(mousebtn::LEFT, EVENT_RANDOM, m_icons->get("random")); } else if (tag == TAG_ICON_REPEAT) { - icon_cmd(EVENT_REPEAT, m_icons->get("repeat")); + builder->cmd(mousebtn::LEFT, EVENT_REPEAT, m_icons->get("repeat")); } else if (tag == TAG_ICON_REPEAT_ONE) { - icon_cmd(EVENT_REPEAT_ONE, m_icons->get("repeat_one")); + builder->cmd(mousebtn::LEFT, EVENT_REPEAT_ONE, m_icons->get("repeat_one")); } else if (tag == TAG_ICON_PREV) { - icon_cmd(EVENT_PREV, m_icons->get("prev")); + builder->cmd(mousebtn::LEFT, EVENT_PREV, m_icons->get("prev")); } else if ((tag == TAG_ICON_STOP || tag == TAG_TOGGLE_STOP) && (is_playing || is_paused)) { - icon_cmd(EVENT_STOP, m_icons->get("stop")); + builder->cmd(mousebtn::LEFT, EVENT_STOP, m_icons->get("stop")); } else if ((tag == TAG_ICON_PAUSE || tag == TAG_TOGGLE) && is_playing) { - icon_cmd(EVENT_PAUSE, m_icons->get("pause")); + builder->cmd(mousebtn::LEFT, EVENT_PAUSE, m_icons->get("pause")); } else if ((tag == TAG_ICON_PLAY || tag == TAG_TOGGLE || tag == TAG_TOGGLE_STOP) && !is_playing) { - icon_cmd(EVENT_PLAY, m_icons->get("play")); + builder->cmd(mousebtn::LEFT, EVENT_PLAY, m_icons->get("play")); } else if (tag == TAG_ICON_NEXT) { - icon_cmd(EVENT_NEXT, m_icons->get("next")); + builder->cmd(mousebtn::LEFT, EVENT_NEXT, m_icons->get("next")); } else if (tag == TAG_ICON_SEEKB) { - icon_cmd(string(EVENT_SEEK).append("-5"), m_icons->get("seekb")); + builder->cmd(mousebtn::LEFT, EVENT_SEEK + "-5"s, m_icons->get("seekb")); } else if (tag == TAG_ICON_SEEKF) { - icon_cmd(string(EVENT_SEEK).append("+5"), m_icons->get("seekf")); + builder->cmd(mousebtn::LEFT, EVENT_SEEK + "+5"s, m_icons->get("seekf")); } else { return false; } + return true; }