From 78bb3695e6c13ff3e62e6362c61478527611aded Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 30 Dec 2016 23:32:05 +0100 Subject: [PATCH] refactor(config): Deduce return type from default value --- include/common.hpp | 2 + include/components/bar.hpp | 5 +- include/components/config.hpp | 19 +++---- src/components/bar.cpp | 103 +++++++++++++++++----------------- src/components/config.cpp | 6 ++ src/components/controller.cpp | 12 ++-- src/components/screen.cpp | 2 +- src/drawtypes/animation.cpp | 6 +- src/drawtypes/label.cpp | 24 ++++---- src/drawtypes/progressbar.cpp | 6 +- src/main.cpp | 4 +- src/modules/backlight.cpp | 2 +- src/modules/battery.cpp | 10 ++-- src/modules/bspwm.cpp | 23 +++----- src/modules/cpu.cpp | 2 +- src/modules/date.cpp | 10 ++-- src/modules/fs.cpp | 2 +- src/modules/github.cpp | 2 +- src/modules/i3.cpp | 16 +++--- src/modules/ipc.cpp | 10 ++-- src/modules/memory.cpp | 2 +- src/modules/menu.cpp | 6 +- src/modules/meta/base.cpp | 2 +- src/modules/mpd.cpp | 4 +- src/modules/network.cpp | 11 ++-- src/modules/script.cpp | 21 ++++--- src/modules/temperature.cpp | 6 +- src/modules/text.cpp | 10 ++-- src/modules/volume.cpp | 13 ++--- src/modules/xbacklight.cpp | 6 +- src/modules/xkeyboard.cpp | 2 +- src/modules/xworkspaces.cpp | 8 +-- src/x11/tray_manager.cpp | 23 ++++---- 33 files changed, 184 insertions(+), 196 deletions(-) diff --git a/include/common.hpp b/include/common.hpp index dcce0f53..3dcbda39 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -48,6 +48,8 @@ using std::array; using std::vector; using std::to_string; +using namespace std::string_literals; + constexpr size_t operator"" _z(unsigned long long n) { return n; } diff --git a/include/components/bar.hpp b/include/components/bar.hpp index 76f30b11..5ed59397 100644 --- a/include/components/bar.hpp +++ b/include/components/bar.hpp @@ -42,11 +42,10 @@ class bar : public xpp::event::sink&&, unique_ptr&&, unique_ptr&&, bool only_initialize_values); ~bar(); - void parse(string&& data) const; - void parse(const string& data, bool force = false); - const bar_settings settings() const; + void parse(string&& data); + protected: void restack_window(); void reconfigure_pos(); diff --git a/include/components/config.hpp b/include/components/config.hpp index 258f2877..e54dc96d 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -11,9 +11,6 @@ POLYBAR_NS -#define GET_CONFIG_VALUE(section, var, name) var = m_conf.get(section, name, var) -#define REQ_CONFIG_VALUE(section, var, name) var = m_conf.get(section, name) - DEFINE_ERROR(value_error); DEFINE_ERROR(key_error); @@ -43,7 +40,7 @@ class config { /** * Get parameter for the current bar by name */ - template + template T get(const string& key) const { return get(section(), key); } @@ -51,7 +48,7 @@ class config { /** * Get value of a variable by section and parameter name */ - template + template T get(const string& section, const string& key) const { auto it = m_sections.find(section); if (it == m_sections.end() || it->second.find(key) == it->second.end()) { @@ -64,7 +61,7 @@ class config { * Get value of a variable by section and parameter name * with a default value in case the parameter isn't defined */ - template + template T get(const string& section, const string& key, const T& default_value) const { try { string string_value{get(section, key)}; @@ -78,7 +75,7 @@ class config { /** * Get list of values for the current bar by name */ - template + template vector get_list(const string& key) const { return get_list(section(), key); } @@ -86,7 +83,7 @@ class config { /** * Get list of values by section and parameter name */ - template + template vector get_list(const string& section, const string& key) const { vector results; @@ -116,7 +113,7 @@ class config { * Get list of values by section and parameter name * with a default list in case the list isn't defined */ - template + template vector get_list(const string& section, const string& key, const vector& default_value) const { vector results; @@ -148,7 +145,7 @@ class config { * warning message. If it fails load the value using the new key and given * fallback value */ - template + template T deprecated(const string& section, const string& old, const string& newkey, const T& fallback) const { try { T value{get(section, old)}; @@ -162,7 +159,7 @@ class config { /** * @see deprecated */ - template + template T deprecated_list(const string& section, const string& old, const string& newkey, const vector& fallback) const { try { vector value{get_list(section, old)}; diff --git a/src/components/bar.cpp b/src/components/bar.cpp index 76c7c71f..ffba28d1 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -1,6 +1,5 @@ #include #include -#include #include "components/bar.hpp" #include "components/config.hpp" @@ -69,9 +68,9 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const string bs{m_conf.section()}; // Get available RandR outputs - auto monitor_name = m_conf.get(bs, "monitor", ""); - auto monitor_name_fallback = m_conf.get(bs, "monitor-fallback", ""); - auto monitor_strictmode = m_conf.get(bs, "monitor-strict", false); + auto monitor_name = m_conf.get(bs, "monitor", ""s); + auto monitor_name_fallback = m_conf.get(bs, "monitor-fallback", ""s); + auto monitor_strictmode = m_conf.get(bs, "monitor-strict", false); auto monitors = randr_util::get_monitors(m_connection, m_connection.screen()->root, monitor_strictmode); if (monitors.empty()) { @@ -123,46 +122,44 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const m_opts.override_redirect = m_conf.get(bs, "dock"); m_conf.warn_deprecated(bs, "dock", "override-redirect"); } catch (const key_error& err) { - m_opts.override_redirect = m_conf.get(bs, "override-redirect", m_opts.override_redirect); + m_opts.override_redirect = m_conf.get(bs, "override-redirect", m_opts.override_redirect); } - m_opts.dimvalue = m_conf.get(bs, "dim-value", 1.0); + m_opts.dimvalue = m_conf.get(bs, "dim-value", 1.0); m_opts.dimvalue = math_util::cap(m_opts.dimvalue, 0.0, 1.0); // Build WM_NAME - m_opts.wmname = m_conf.get(bs, "wm-name", "polybar-" + bs.substr(4) + "_" + m_opts.monitor->name); + m_opts.wmname = m_conf.get(bs, "wm-name", "polybar-" + bs.substr(4) + "_" + m_opts.monitor->name); m_opts.wmname = string_util::replace(m_opts.wmname, " ", "-"); // Load configuration values - m_opts.origin = m_conf.get(bs, "bottom", false) ? edge::BOTTOM : edge::TOP; - m_opts.spacing = m_conf.get(bs, "spacing", m_opts.spacing); - m_opts.padding.left = m_conf.get(bs, "padding-left", m_opts.padding.left); - m_opts.padding.right = m_conf.get(bs, "padding-right", m_opts.padding.right); - m_opts.module_margin.left = - m_conf.get(bs, "module-margin-left", m_opts.module_margin.left); - m_opts.module_margin.right = - m_conf.get(bs, "module-margin-right", m_opts.module_margin.right); - m_opts.separator = string_util::trim(m_conf.get(bs, "separator", ""), '"'); - m_opts.locale = m_conf.get(bs, "locale", ""); + m_opts.origin = m_conf.get(bs, "bottom", false) ? edge::BOTTOM : edge::TOP; + m_opts.spacing = m_conf.get(bs, "spacing", m_opts.spacing); + m_opts.padding.left = m_conf.get(bs, "padding-left", m_opts.padding.left); + m_opts.padding.right = m_conf.get(bs, "padding-right", m_opts.padding.right); + m_opts.module_margin.left = m_conf.get(bs, "module-margin-left", m_opts.module_margin.left); + m_opts.module_margin.right = m_conf.get(bs, "module-margin-right", m_opts.module_margin.right); + m_opts.separator = string_util::trim(m_conf.get(bs, "separator", ""s), '"'); + m_opts.locale = m_conf.get(bs, "locale", ""s); if (only_initialize_values) { return; } // Load values used to adjust the struts atom - m_opts.strut.top = m_conf.get("global/wm", "margin-top", 0); - m_opts.strut.bottom = m_conf.get("global/wm", "margin-bottom", 0); + m_opts.strut.top = m_conf.get("global/wm", "margin-top", 0); + m_opts.strut.bottom = m_conf.get("global/wm", "margin-bottom", 0); // Load commands used for fallback click handlers vector actions; - actions.emplace_back(action{mousebtn::LEFT, m_conf.get(bs, "click-left", "")}); - actions.emplace_back(action{mousebtn::MIDDLE, m_conf.get(bs, "click-middle", "")}); - actions.emplace_back(action{mousebtn::RIGHT, m_conf.get(bs, "click-right", "")}); - actions.emplace_back(action{mousebtn::SCROLL_UP, m_conf.get(bs, "scroll-up", "")}); - actions.emplace_back(action{mousebtn::SCROLL_DOWN, m_conf.get(bs, "scroll-down", "")}); - actions.emplace_back(action{mousebtn::DOUBLE_LEFT, m_conf.get(bs, "double-click-left", "")}); - actions.emplace_back(action{mousebtn::DOUBLE_MIDDLE, m_conf.get(bs, "double-click-middle", "")}); - actions.emplace_back(action{mousebtn::DOUBLE_RIGHT, m_conf.get(bs, "double-click-right", "")}); + actions.emplace_back(action{mousebtn::LEFT, m_conf.get(bs, "click-left", ""s)}); + actions.emplace_back(action{mousebtn::MIDDLE, m_conf.get(bs, "click-middle", ""s)}); + actions.emplace_back(action{mousebtn::RIGHT, m_conf.get(bs, "click-right", ""s)}); + actions.emplace_back(action{mousebtn::SCROLL_UP, m_conf.get(bs, "scroll-up", ""s)}); + actions.emplace_back(action{mousebtn::SCROLL_DOWN, m_conf.get(bs, "scroll-down", ""s)}); + actions.emplace_back(action{mousebtn::DOUBLE_LEFT, m_conf.get(bs, "double-click-left", ""s)}); + actions.emplace_back(action{mousebtn::DOUBLE_MIDDLE, m_conf.get(bs, "double-click-middle", ""s)}); + actions.emplace_back(action{mousebtn::DOUBLE_RIGHT, m_conf.get(bs, "double-click-right", ""s)}); for (auto&& act : actions) { if (!act.command.empty()) { @@ -171,42 +168,42 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const } // Load foreground/background - m_opts.background = color::parse(m_conf.get(bs, "background", color_util::hex(m_opts.background))); - m_opts.foreground = color::parse(m_conf.get(bs, "foreground", color_util::hex(m_opts.foreground))); + m_opts.background = color::parse(m_conf.get(bs, "background", color_util::hex(m_opts.background))); + m_opts.foreground = color::parse(m_conf.get(bs, "foreground", color_util::hex(m_opts.foreground))); // Load over-/underline color and size (warn about deprecated params if used) m_conf.warn_deprecated(bs, "linecolor", "{underline,overline}-color"); m_conf.warn_deprecated(bs, "lineheight", "{underline,overline}-size"); - auto linecolor = color::parse(m_conf.get(bs, "linecolor", "#f00")); - auto lineheight = m_conf.get(bs, "lineheight", 0); - m_opts.overline.size = m_conf.get(bs, "overline-size", lineheight); - m_opts.overline.color = color::parse(m_conf.get(bs, "overline-color", linecolor)); - m_opts.underline.size = m_conf.get(bs, "underline-size", lineheight); - m_opts.underline.color = color::parse(m_conf.get(bs, "underline-color", linecolor)); + auto linecolor = color::parse(m_conf.get(bs, "linecolor", "#f00"s)); + auto lineheight = m_conf.get(bs, "lineheight", 0); + m_opts.overline.size = m_conf.get(bs, "overline-size", lineheight); + m_opts.overline.color = color::parse(m_conf.get(bs, "overline-color", linecolor)); + m_opts.underline.size = m_conf.get(bs, "underline-size", lineheight); + m_opts.underline.color = color::parse(m_conf.get(bs, "underline-color", linecolor)); // Load border settings - auto bsize = m_conf.get(bs, "border-size", 0); - auto bcolor = m_conf.get(bs, "border-color", "#00000000"); + auto bsize = m_conf.get(bs, "border-size", 0); + auto bcolor = m_conf.get(bs, "border-color", "#00000000"s); m_opts.borders.emplace(edge::TOP, border_settings{}); - m_opts.borders[edge::TOP].size = m_conf.get(bs, "border-top", bsize); - m_opts.borders[edge::TOP].color = color::parse(m_conf.get(bs, "border-top-color", bcolor)); + m_opts.borders[edge::TOP].size = m_conf.get(bs, "border-top", bsize); + m_opts.borders[edge::TOP].color = color::parse(m_conf.get(bs, "border-top-color", bcolor)); m_opts.borders.emplace(edge::BOTTOM, border_settings{}); - m_opts.borders[edge::BOTTOM].size = m_conf.get(bs, "border-bottom", bsize); - m_opts.borders[edge::BOTTOM].color = color::parse(m_conf.get(bs, "border-bottom-color", bcolor)); + m_opts.borders[edge::BOTTOM].size = m_conf.get(bs, "border-bottom", bsize); + m_opts.borders[edge::BOTTOM].color = color::parse(m_conf.get(bs, "border-bottom-color", bcolor)); m_opts.borders.emplace(edge::LEFT, border_settings{}); - m_opts.borders[edge::LEFT].size = m_conf.get(bs, "border-left", bsize); - m_opts.borders[edge::LEFT].color = color::parse(m_conf.get(bs, "border-left-color", bcolor)); + m_opts.borders[edge::LEFT].size = m_conf.get(bs, "border-left", bsize); + m_opts.borders[edge::LEFT].color = color::parse(m_conf.get(bs, "border-left-color", bcolor)); m_opts.borders.emplace(edge::RIGHT, border_settings{}); - m_opts.borders[edge::RIGHT].size = m_conf.get(bs, "border-right", bsize); - m_opts.borders[edge::RIGHT].color = color::parse(m_conf.get(bs, "border-right-color", bcolor)); + m_opts.borders[edge::RIGHT].size = m_conf.get(bs, "border-right", bsize); + m_opts.borders[edge::RIGHT].color = color::parse(m_conf.get(bs, "border-right-color", bcolor)); // Load geometry values - auto w = m_conf.get(m_conf.section(), "width", "100%"); - auto h = m_conf.get(m_conf.section(), "height", "24"); - auto offsetx = m_conf.get(m_conf.section(), "offset-x", ""); - auto offsety = m_conf.get(m_conf.section(), "offset-y", ""); + auto w = m_conf.get(m_conf.section(), "width", "100%"s); + auto h = m_conf.get(m_conf.section(), "height", "24"s); + auto offsetx = m_conf.get(m_conf.section(), "offset-x", ""s); + auto offsety = m_conf.get(m_conf.section(), "offset-y", ""s); if ((m_opts.size.w = atoi(w.c_str())) && w.find('%') != string::npos) { m_opts.size.w = math_util::percentage_to_value(m_opts.size.w, m_opts.monitor->w); @@ -251,7 +248,7 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const m_opts.center.x += m_opts.borders[edge::LEFT].size; m_log.trace("bar: Create renderer"); - auto fonts = m_conf.get_list(m_conf.section(), "font", {}); + auto fonts = m_conf.get_list(m_conf.section(), "font", {}); m_renderer = renderer::make(m_opts, move(fonts)); m_log.trace("bar: Attaching sink to registry"); @@ -315,7 +312,7 @@ const bar_settings bar::settings() const { * @param data Input string * @param force Unless true, do not parse unchanged data */ -void bar::parse(const string& data, bool force) { +void bar::parse(string&& data) { if (!m_mutex.try_lock()) { return; } @@ -326,7 +323,7 @@ void bar::parse(const string& data, bool force) { return m_log.trace("bar: Ignoring update (shaded)"); } - if (data == m_lastinput && !force) { + if (data == m_lastinput) { return; } @@ -362,7 +359,7 @@ void bar::restack_window() { string wm_restack; try { - wm_restack = m_conf.get(m_conf.section(), "wm-restack"); + wm_restack = m_conf.get(m_conf.section(), "wm-restack"); } catch (const key_error& err) { return; } diff --git a/src/components/config.cpp b/src/components/config.cpp index a057b7c0..ceb41ffb 100644 --- a/src/components/config.cpp +++ b/src/components/config.cpp @@ -9,6 +9,7 @@ #include "utils/factory.hpp" #include "utils/file.hpp" #include "utils/string.hpp" +#include "x11/color.hpp" POLYBAR_NS @@ -268,4 +269,9 @@ chrono::duration config::convert(string&& value) const { return chrono::duration{convert(forward(value))}; } +template <> +color config::convert(string&& value) const { + return color{forward(value)}; +} + POLYBAR_NS_END diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 93e65188..9e014983 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -86,11 +86,11 @@ controller::controller(connection& conn, signal_emitter& emitter, const logger& string configured_modules; if (align == alignment::LEFT) { - configured_modules = m_conf.get(m_conf.section(), "modules-left", ""); + configured_modules = m_conf.get(m_conf.section(), "modules-left", ""s); } else if (align == alignment::CENTER) { - configured_modules = m_conf.get(m_conf.section(), "modules-center", ""); + configured_modules = m_conf.get(m_conf.section(), "modules-center", ""s); } else if (align == alignment::RIGHT) { - configured_modules = m_conf.get(m_conf.section(), "modules-right", ""); + configured_modules = m_conf.get(m_conf.section(), "modules-right", ""s); } for (auto& module_name : string_util::split(configured_modules, ' ')) { @@ -99,7 +99,7 @@ controller::controller(connection& conn, signal_emitter& emitter, const logger& } try { - auto type = m_conf.get("module/" + module_name, "type"); + auto type = m_conf.get("module/" + module_name, "type"); if (type == "custom/ipc" && !m_ipc) { throw application_error("Inter-process messaging needs to be enabled"); @@ -240,7 +240,7 @@ void controller::read_events() { vector fds; fds.emplace_back(*m_queuefd[PIPE_READ]); - fds.emplace_back(fd_connection = m_connection.get_file_descriptor()); + fds.emplace_back((fd_connection = m_connection.get_file_descriptor())); if (m_confwatch) { m_log.trace("controller: Attach config watch"); @@ -484,7 +484,7 @@ bool controller::on(const sig_ev::update&) { try { if (!m_writeback) { - m_bar->parse(move(contents), false); + m_bar->parse(move(contents)); } else { std::cout << contents << std::endl; } diff --git a/src/components/screen.cpp b/src/components/screen.cpp index 9549d1ba..7b469e39 100644 --- a/src/components/screen.cpp +++ b/src/components/screen.cpp @@ -37,7 +37,7 @@ screen::screen(connection& conn, signal_emitter& emitter, const logger& logger, , m_monitors(randr_util::get_monitors(m_connection, m_root, true)) , m_size({conn.screen()->width_in_pixels, conn.screen()->height_in_pixels}) { // Check if the reloading has been disabled by the user - if (!m_conf.get("settings", "screenchange-reload", false)) { + if (!m_conf.get("settings", "screenchange-reload", false)) { return; } diff --git a/src/drawtypes/animation.cpp b/src/drawtypes/animation.cpp index f25eacc9..60737443 100644 --- a/src/drawtypes/animation.cpp +++ b/src/drawtypes/animation.cpp @@ -50,9 +50,9 @@ namespace drawtypes { auto anim_defaults = load_optional_icon(conf, section, name); if (required) { - frames = conf.get_list(section, name); + frames = conf.get_list(section, name); } else { - frames = conf.get_list(section, name, {}); + frames = conf.get_list(section, name, {}); } for (size_t i = 0; i < frames.size(); i++) { @@ -60,7 +60,7 @@ namespace drawtypes { vec.back()->copy_undefined(anim_defaults); } - auto framerate = conf.get(section, name + "-framerate", 1000); + auto framerate = conf.get(section, name + "-framerate", 1000); return factory_util::shared(move(vec), framerate); } diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp index bf817e4c..4f6e7eea 100644 --- a/src/drawtypes/label.cpp +++ b/src/drawtypes/label.cpp @@ -132,9 +132,9 @@ namespace drawtypes { }, margin{}; if (required) { - text = conf.get(section, name); + text = conf.get(section, name); } else { - text = conf.get(section, name, move(def)); + text = conf.get(section, name, move(def)); } size_t len{text.size()}; @@ -144,9 +144,9 @@ namespace drawtypes { } const auto get_left_right = [&](string key) { - auto value = conf.get(section, key, 0); - auto left = conf.get(section, key + "-left", value); - auto right = conf.get(section, key + "-right", value); + auto value = conf.get(section, key, 0); + auto left = conf.get(section, key + "-left", value); + auto right = conf.get(section, key + "-right", value); return side_values{static_cast(left), static_cast(right)}; }; @@ -207,15 +207,15 @@ namespace drawtypes { // clang-format off return factory_util::shared