feat: Warn state for cpu, memory, fs, battery modules (#2199)

* [Temperature, Ramp] fix wrong icon for temperatures near base and warn temps

* [Temperature, Ramp] fix wrong icon for temperatures near base and warn temps

* Fix minor error

* Added WARN state for cpu module

* Implement WARN state for CPU, Memory modules, working on fs module

* Implement WARN state for fs module

* Simplify WARN state implementation for cpu and memory

* explicitly check percentage in get_by_percentage_with_borders

* Fixed silly error

* implement warn state on battery module, standardize the implementation on other modules

* minor fixes

* fix annoying error

* use more intuitive param name

* Fix percentage with borders bug

* Make requested changes
Hide the effect of warn states when unused

* Backward Compat: use no format instead of fallback label

* Reformat

* Refactor

* Reformat

* Reformat: convert tabs to spaces

* Reformat
This commit is contained in:
Quan
2020-12-02 21:55:13 +07:00
committed by GitHub
parent 078aa3c8cb
commit 39c73a8234
17 changed files with 245 additions and 88 deletions

View File

@ -28,6 +28,7 @@ namespace modules {
fs_module::fs_module(const bar_settings& bar, string name_) : timer_module<fs_module>(bar, move(name_)) {
m_mountpoints = m_conf.get_list(name(), "mount");
m_remove_unmounted = m_conf.get(name(), "remove-unmounted", m_remove_unmounted);
m_perc_used_warn = m_conf.get(name(), "warn-percentage", 90);
m_fixed = m_conf.get(name(), "fixed-values", m_fixed);
m_spacing = m_conf.get(name(), "spacing", m_spacing);
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 30s);
@ -35,11 +36,15 @@ namespace modules {
// Add formats and elements
m_formatter->add(
FORMAT_MOUNTED, TAG_LABEL_MOUNTED, {TAG_LABEL_MOUNTED, TAG_BAR_FREE, TAG_BAR_USED, TAG_RAMP_CAPACITY});
m_formatter->add_optional(FORMAT_WARN, {TAG_LABEL_WARN, TAG_BAR_FREE, TAG_BAR_USED, TAG_RAMP_CAPACITY});
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%%");
}
if (m_formatter->has(TAG_LABEL_WARN)) {
m_labelwarn = load_optional_label(m_conf, name(), TAG_LABEL_WARN, "%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");
}
@ -104,6 +109,31 @@ namespace modules {
mount->percentage_free = math_util::percentage<double>(mount->bytes_avail, mount->bytes_used + mount->bytes_avail);
mount->percentage_used = math_util::percentage<double>(mount->bytes_used, mount->bytes_used + mount->bytes_avail);
const auto replace_tokens = [&](label_t& label) {
label->reset_tokens();
label->replace_token("%mountpoint%", mount->mountpoint);
label->replace_token("%type%", mount->type);
label->replace_token("%fsname%", mount->fsname);
label->replace_token("%percentage_free%", to_string(mount->percentage_free));
label->replace_token("%percentage_used%", to_string(mount->percentage_used));
label->replace_token(
"%total%", string_util::filesize(mount->bytes_total, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token(
"%free%", string_util::filesize(mount->bytes_avail, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token(
"%used%", string_util::filesize(mount->bytes_used, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
};
if (m_labelmounted) {
replace_tokens(m_labelmounted);
}
if (m_labelwarn) {
replace_tokens(m_labelwarn);
}
if (m_labelunmounted) {
m_labelunmounted->reset_tokens();
m_labelunmounted->replace_token("%mountpoint%", mount->mountpoint);
}
}
}
@ -141,7 +171,13 @@ namespace modules {
* Select format based on fs state
*/
string fs_module::get_format() const {
return m_mounts[m_index]->mounted ? FORMAT_MOUNTED : FORMAT_UNMOUNTED;
if (!m_mounts[m_index]->mounted) {
return FORMAT_UNMOUNTED;
}
if (m_mounts[m_index]->percentage_used >= m_perc_used_warn && m_formatter->has_format(FORMAT_WARN)) {
return FORMAT_WARN;
}
return FORMAT_MOUNTED;
}
/**
@ -155,24 +191,12 @@ namespace modules {
} else if (tag == TAG_BAR_USED) {
builder->node(m_barused->output(mount->percentage_used));
} else if (tag == TAG_RAMP_CAPACITY) {
builder->node(m_rampcapacity->get_by_percentage(mount->percentage_free));
builder->node(m_rampcapacity->get_by_percentage_with_borders(mount->percentage_free, 0, m_perc_used_warn));
} else if (tag == TAG_LABEL_MOUNTED) {
m_labelmounted->reset_tokens();
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(
"%total%", string_util::filesize(mount->bytes_total, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
m_labelmounted->replace_token(
"%free%", string_util::filesize(mount->bytes_avail, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
m_labelmounted->replace_token(
"%used%", string_util::filesize(mount->bytes_used, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
builder->node(m_labelmounted);
} else if (tag == TAG_LABEL_WARN) {
builder->node(m_labelwarn);
} else if (tag == TAG_LABEL_UNMOUNTED) {
m_labelunmounted->reset_tokens();
m_labelunmounted->replace_token("%mountpoint%", mount->mountpoint);
builder->node(m_labelunmounted);
} else {
return false;