From 3292cea7864f33face7d7817c4b83cfe9a3d376d Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 13 Jan 2017 20:03:08 +0100 Subject: [PATCH] refactor(tokens): Move token suffix to configuration --- doc/config.cmake | 6 +++--- include/components/config.hpp | 18 ++++++++++++++++++ include/drawtypes/label.hpp | 3 ++- src/components/config.cpp | 3 ++- src/components/parser.cpp | 2 +- src/drawtypes/label.cpp | 22 ++++++++++++++-------- src/modules/backlight.cpp | 4 ++-- src/modules/battery.cpp | 8 ++++---- src/modules/cpu.cpp | 31 +++++++++++++++++++------------ src/modules/fs.cpp | 6 +++--- src/modules/memory.cpp | 6 +++--- src/modules/network.cpp | 4 ++-- src/modules/volume.cpp | 8 ++++---- src/modules/xbacklight.cpp | 4 ++-- 14 files changed, 79 insertions(+), 46 deletions(-) diff --git a/doc/config.cmake b/doc/config.cmake index 136a3bfd..6e93dadb 100644 --- a/doc/config.cmake +++ b/doc/config.cmake @@ -91,7 +91,7 @@ mount-0 = / mount-1 = /home mount-2 = /invalid/mountpoint -label-mounted = %mountpoint%: %percentage_free% +label-mounted = %mountpoint%: %percentage_free%% label-unmounted = %mountpoint%: not mounted label-unmounted-foreground = ${colors.foreground-alt} @@ -192,7 +192,7 @@ interval = 2 format-prefix = " " format-prefix-foreground = ${colors.foreground-alt} format-underline = #f90000 -label = %percentage% +label = %percentage%% [module/memory] type = internal/memory @@ -200,7 +200,7 @@ interval = 2 format-prefix = " " format-prefix-foreground = ${colors.foreground-alt} format-underline = #4bffdc -label = %percentage_used% +label = %percentage_used%% [module/wlan] type = internal/network diff --git a/include/components/config.hpp b/include/components/config.hpp index 6ffa0d2c..6beb7a5b 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -38,6 +38,24 @@ class config { return it != m_sections.end() && it->second.find(key) != it->second.end(); } + /** + * Set parameter value + */ + void set(const string& section, const string& key, string&& value) { + auto it = m_sections.find(section); + if (it == m_sections.end()) { + valuemap_t values; + values[key] = value; + m_sections[section] = move(values); + } + auto it2 = it->second.find(key); + if ((it2 = it->second.find(key)) == it->second.end()) { + it2 = it->second.emplace_hint(it2, key, value); + } else { + it2->second = value; + } + } + /** * Get parameter for the current bar by name */ diff --git a/include/drawtypes/label.hpp b/include/drawtypes/label.hpp index 3adf884b..d2ec7bb4 100644 --- a/include/drawtypes/label.hpp +++ b/include/drawtypes/label.hpp @@ -62,7 +62,8 @@ namespace drawtypes { label_t clone(); void clear(); void reset_tokens(); - bool has_token(const string& token); + void reset_tokens(const string& tokenized); + bool has_token(const string& token) const; void replace_token(const string& token, string replacement); void replace_defined_values(const label_t& label); void copy_undefined(const label_t& label); diff --git a/src/components/config.cpp b/src/components/config.cpp index b1e07ea5..9f802227 100644 --- a/src/components/config.cpp +++ b/src/components/config.cpp @@ -67,7 +67,8 @@ string config::section() const { void config::warn_deprecated(const string& section, const string& key, string replacement) const { try { auto value = get(section, key); - m_log.warn("The config parameter `%s.%s` is deprecated, use `%s.%s` instead.", section, key, section, move(replacement)); + m_log.warn( + "The config parameter `%s.%s` is deprecated, use `%s.%s` instead.", section, key, section, move(replacement)); } catch (const key_error& err) { } } diff --git a/src/components/parser.cpp b/src/components/parser.cpp index 4e90272b..d78b8301 100644 --- a/src/components/parser.cpp +++ b/src/components/parser.cpp @@ -1,10 +1,10 @@ #include -#include "settings.hpp" #include "components/parser.hpp" #include "components/types.hpp" #include "events/signal.hpp" #include "events/signal_emitter.hpp" +#include "settings.hpp" #include "utils/color.hpp" #include "utils/factory.hpp" #include "utils/file.hpp" diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp index fd7e120a..f4348d5e 100644 --- a/src/drawtypes/label.cpp +++ b/src/drawtypes/label.cpp @@ -2,6 +2,7 @@ #include "drawtypes/label.hpp" #include "utils/factory.hpp" +#include "utils/string.hpp" POLYBAR_NS @@ -32,8 +33,12 @@ namespace drawtypes { m_tokenized = m_text; } - bool label::has_token(const string& token) { - return m_text.find(token) != string::npos; + void label::reset_tokens(const string& tokenized) { + m_tokenized = tokenized; + } + + bool label::has_token(const string& token) const { + return m_tokenized.find(token) != string::npos; } void label::replace_token(const string& token, string replacement) { @@ -49,6 +54,7 @@ namespace drawtypes { replacement.insert(0_z, tok.min - replacement.length(), ' '); } m_tokenized = string_util::replace_all(m_tokenized, token, move(replacement)); + m_tokenized = string_util::replace_all(m_tokenized, token, move(replacement)); } } } @@ -162,9 +168,11 @@ namespace drawtypes { while ((start = line.find('%')) != string::npos && (end = line.find('%', start + 1)) != string::npos) { auto token_str = line.substr(start, end - start + 1); - // ignore false positives (lemonbar-style declarations) - if (token_str[1] == '{') { - line.erase(0, start + 1); + // ignore false positives + // lemonbar tags %{...} + // trailing percentage signs %token%% + if (token_str[1] == '{' || token_str[1] == ' ') { + line.erase(0, end); continue; } @@ -205,9 +213,7 @@ namespace drawtypes { // find suffix delimiter if ((pos = token_str.find(':', pos + 1)) != string::npos) { - token.suffix = token_str.substr(pos + 1); - // remove closing token % - token.suffix.erase(token.suffix.size() - 1); + token.suffix = token_str.substr(pos + 1, token_str.size() - pos - 2); } } diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index 076a020c..3562fad5 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -31,7 +31,7 @@ namespace modules { m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR, TAG_RAMP}); if (m_formatter->has(TAG_LABEL)) { - m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%"); + m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%%"); } if (m_formatter->has(TAG_BAR)) { m_progressbar = load_progressbar(m_bar, m_conf, name(), TAG_BAR); @@ -61,7 +61,7 @@ namespace modules { if (m_label) { m_label->reset_tokens(); - m_label->replace_token("%percentage%", to_string(m_percentage) + "%"); + m_label->replace_token("%percentage%", to_string(m_percentage)); } return true; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index a38cba49..cc3974a2 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -102,13 +102,13 @@ namespace modules { m_ramp_capacity = load_ramp(m_conf, name(), TAG_RAMP_CAPACITY); } if (m_formatter->has(TAG_LABEL_CHARGING, FORMAT_CHARGING)) { - m_label_charging = load_optional_label(m_conf, name(), TAG_LABEL_CHARGING, "%percentage%"); + m_label_charging = load_optional_label(m_conf, name(), TAG_LABEL_CHARGING, "%percentage%%"); } if (m_formatter->has(TAG_LABEL_DISCHARGING, FORMAT_DISCHARGING)) { - m_label_discharging = load_optional_label(m_conf, name(), TAG_LABEL_DISCHARGING, "%percentage%"); + m_label_discharging = load_optional_label(m_conf, name(), TAG_LABEL_DISCHARGING, "%percentage%%"); } if (m_formatter->has(TAG_LABEL_FULL, FORMAT_FULL)) { - m_label_full = load_optional_label(m_conf, name(), TAG_LABEL_FULL, "%percentage%"); + m_label_full = load_optional_label(m_conf, name(), TAG_LABEL_FULL, "%percentage%%"); } // Create inotify watches @@ -200,7 +200,7 @@ namespace modules { if (label) { label->reset_tokens(); - label->replace_token("%percentage%", to_string(m_percentage) + "%"); + label->replace_token("%percentage%", to_string(m_percentage)); if (m_state != battery_module::state::FULL && !m_timeformat.empty()) { label->replace_token("%time%", current_time()); diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 7780430c..829adfca 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -20,6 +20,10 @@ namespace modules { m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR_LOAD, TAG_RAMP_LOAD, TAG_RAMP_LOAD_PER_CORE}); + // warmup cpu times + read_values(); + read_values(); + if (m_formatter->has(TAG_BAR_LOAD)) { m_barload = load_progressbar(m_bar, m_conf, name(), TAG_BAR_LOAD); } @@ -30,12 +34,18 @@ namespace modules { m_rampload_core = load_ramp(m_conf, name(), TAG_RAMP_LOAD_PER_CORE); } if (m_formatter->has(TAG_LABEL)) { - m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%"); - } + // Update the label parameter and replace the %percentag-cores% token with the individual core tokens + string key{&TAG_LABEL[1], strlen(TAG_LABEL) - 2}; + auto label = m_conf.get(name(), key, "%percentage%%"); + vector cores; + for (size_t i = 1; i <= m_cputimes.size(); i++) { + cores.emplace_back("%percentage-core" + to_string(i) + "%%"); + } + label = string_util::replace_all(label, "%percentage-cores%", string_util::join(cores, " ")); + const_cast(m_conf).set(name(), key, move(label)); - // warmup - read_values(); - read_values(); + m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%%"); + } } bool cpu_module::update() { @@ -47,7 +57,6 @@ namespace modules { m_load.clear(); auto cores_n = m_cputimes.size(); - if (!cores_n) { return false; } @@ -59,7 +68,7 @@ namespace modules { m_load.emplace_back(load); if (m_label) { - percentage_cores.emplace_back(to_string(static_cast(load + 0.5f)) + "%"); + percentage_cores.emplace_back(to_string(static_cast(load + 0.5))); } } @@ -67,12 +76,10 @@ namespace modules { if (m_label) { m_label->reset_tokens(); - m_label->replace_token("%percentage%", to_string(static_cast(m_total + 0.5f)) + "%"); - m_label->replace_token("%percentage-cores%", string_util::join(percentage_cores, " ")); + m_label->replace_token("%percentage%", to_string(static_cast(m_total + 0.5))); - size_t i{0}; - for (auto&& p : percentage_cores) { - m_label->replace_token("%percentage-core" + to_string(++i) + "%", p); + for (size_t i = 0; i < percentage_cores.size(); i++) { + m_label->replace_token("%percentage-core" + to_string(i + 1) + "%", percentage_cores[i]); } } diff --git a/src/modules/fs.cpp b/src/modules/fs.cpp index b8399fec..5a87cd8b 100644 --- a/src/modules/fs.cpp +++ b/src/modules/fs.cpp @@ -38,7 +38,7 @@ namespace modules { m_formatter->add(FORMAT_UNMOUNTED, TAG_LABEL_UNMOUNTED, {TAG_LABEL_UNMOUNTED}); if (m_formatter->has(TAG_LABEL_MOUNTED)) { - m_labelmounted = load_optional_label(m_conf, name(), TAG_LABEL_MOUNTED, "%mountpoint% %percentage_free%"); + m_labelmounted = load_optional_label(m_conf, name(), TAG_LABEL_MOUNTED, "%mountpoint% %percentage_free%%"); } if (m_formatter->has(TAG_LABEL_UNMOUNTED)) { m_labelunmounted = load_optional_label(m_conf, name(), TAG_LABEL_UNMOUNTED, "%mountpoint% is not mounted"); @@ -159,8 +159,8 @@ namespace modules { m_labelmounted->replace_token("%mountpoint%", mount->mountpoint); m_labelmounted->replace_token("%type%", mount->type); m_labelmounted->replace_token("%fsname%", mount->fsname); - m_labelmounted->replace_token("%percentage_free%", to_string(mount->percentage_free) + "%"); - m_labelmounted->replace_token("%percentage_used%", to_string(mount->percentage_used) + "%"); + m_labelmounted->replace_token("%percentage_free%", to_string(mount->percentage_free)); + m_labelmounted->replace_token("%percentage_used%", to_string(mount->percentage_used)); m_labelmounted->replace_token( "%total%", string_util::filesize(mount->bytes_total, m_fixed ? 2 : 0, m_fixed, m_bar.locale)); m_labelmounted->replace_token( diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 82fe5b36..9575bb02 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -26,7 +26,7 @@ namespace modules { m_bar_memfree = load_progressbar(m_bar, m_conf, name(), TAG_BAR_FREE); } if (m_formatter->has(TAG_LABEL)) { - m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage_used%"); + m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage_used%%"); } } @@ -68,8 +68,8 @@ namespace modules { m_label->replace_token("%mb_used%", string_util::filesize_mb(kb_total - kb_avail, 2, m_bar.locale)); m_label->replace_token("%mb_free%", string_util::filesize_mb(kb_avail, 2, m_bar.locale)); m_label->replace_token("%mb_total%", string_util::filesize_mb(kb_total, 2, m_bar.locale)); - m_label->replace_token("%percentage_used%", to_string(m_perc_memused) + "%"); - m_label->replace_token("%percentage_free%", to_string(m_perc_memfree) + "%"); + m_label->replace_token("%percentage_used%", to_string(m_perc_memused)); + m_label->replace_token("%percentage_free%", to_string(m_perc_memfree)); } return true; diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 2fe85e98..c46bb29e 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -120,8 +120,8 @@ namespace modules { label->replace_token("%linkspeed%", m_wired->linkspeed()); } else if (m_wireless) { label->replace_token("%essid%", m_wireless->essid()); - label->replace_token("%signal%", to_string(m_signal) + "%"); - label->replace_token("%quality%", to_string(m_quality) + "%"); + label->replace_token("%signal%", to_string(m_signal)); + label->replace_token("%quality%", to_string(m_quality)); } }; diff --git a/src/modules/volume.cpp b/src/modules/volume.cpp index 65dd2ea1..d2c84d9c 100644 --- a/src/modules/volume.cpp +++ b/src/modules/volume.cpp @@ -75,10 +75,10 @@ namespace modules { m_bar_volume = load_progressbar(m_bar, m_conf, name(), TAG_BAR_VOLUME); } if (m_formatter->has(TAG_LABEL_VOLUME, FORMAT_VOLUME)) { - m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%"); + m_label_volume = load_optional_label(m_conf, name(), TAG_LABEL_VOLUME, "%percentage%%"); } if (m_formatter->has(TAG_LABEL_MUTED, FORMAT_MUTED)) { - m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%"); + m_label_muted = load_optional_label(m_conf, name(), TAG_LABEL_MUTED, "%percentage%%"); } if (m_formatter->has(TAG_RAMP_VOLUME)) { m_ramp_volume = load_ramp(m_conf, name(), TAG_RAMP_VOLUME); @@ -168,12 +168,12 @@ namespace modules { // Replace label tokens if (m_label_volume) { m_label_volume->reset_tokens(); - m_label_volume->replace_token("%percentage%", to_string(m_volume) + "%"); + m_label_volume->replace_token("%percentage%", to_string(m_volume)); } if (m_label_muted) { m_label_muted->reset_tokens(); - m_label_muted->replace_token("%percentage%", to_string(m_volume) + "%"); + m_label_muted->replace_token("%percentage%", to_string(m_volume)); } return true; diff --git a/src/modules/xbacklight.cpp b/src/modules/xbacklight.cpp index 06e9f267..13e06749 100644 --- a/src/modules/xbacklight.cpp +++ b/src/modules/xbacklight.cpp @@ -60,7 +60,7 @@ namespace modules { m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR, TAG_RAMP}); if (m_formatter->has(TAG_LABEL)) { - m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%"); + m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage%%"); } if (m_formatter->has(TAG_BAR)) { m_progressbar = load_progressbar(m_bar, m_conf, name(), TAG_BAR); @@ -107,7 +107,7 @@ namespace modules { // Update label tokens if (m_label) { m_label->reset_tokens(); - m_label->replace_token("%percentage%", to_string(m_percentage) + "%"); + m_label->replace_token("%percentage%", to_string(m_percentage)); } // Emit a broadcast notification so that