refactor(tokens): Move token suffix to configuration

This commit is contained in:
Michael Carlberg 2017-01-13 20:03:08 +01:00
parent b395285a68
commit 3292cea786
14 changed files with 79 additions and 46 deletions

View File

@ -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

View File

@ -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
*/

View File

@ -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);

View File

@ -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<string>(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) {
}
}

View File

@ -1,10 +1,10 @@
#include <cassert>
#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"

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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());

View File

@ -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<string>(name(), key, "%percentage%%");
vector<string> 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<config&>(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<int>(load + 0.5f)) + "%");
percentage_cores.emplace_back(to_string(static_cast<int>(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<int>(m_total + 0.5f)) + "%");
m_label->replace_token("%percentage-cores%", string_util::join(percentage_cores, " "));
m_label->replace_token("%percentage%", to_string(static_cast<int>(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]);
}
}

View File

@ -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(

View File

@ -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;

View File

@ -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));
}
};

View File

@ -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;

View File

@ -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