feat(memory): Tokens that switch between MiB and GiB (#2488)

* feat:add tokens to display memory in MiB when GiB val <= 1.0 (#2472)

* fix: correct swap_used calculation

* fix: pass variable by reference rather than by value

* fix: add precision arguments to filesize_gib_mib(); better condition

* doc: add #2472 to CHANGELOG

* fix: missing default argument values

* Apply suggestions from code review

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
This commit is contained in:
Luca Maltagliati 2021-09-04 13:14:13 +02:00 committed by GitHub
parent a520fead94
commit ddabe3f0f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 1 deletions

View File

@ -52,6 +52,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `POLYBAR_FLAGS` cmake variable can be used to pass extra C++ compiler flags.
### Added
- `internal/memory`: New tokens `%used%`, `%free%`, `%total%`, `%swap_total%`,
`%swap_free%`, and `%swap_used%` that automatically switch between MiB and GiB
when below or above 1GiB.
([`2472`](https://github.com/polybar/polybar/issues/2472))
- Option to always show urgent windows in i3 module when `pin-workspace` is active
([`2374`](https://github.com/polybar/polybar/issues/2374))
- `internal/xworkspaces`: `reverse-scroll` can be used to reverse the scroll

View File

@ -96,6 +96,7 @@ namespace string_util {
string floating_point(double value, size_t precision, bool fixed = false, const string& locale = "");
string filesize_mib(unsigned long long kibibytes, size_t precision = 0, const string& locale = "");
string filesize_gib(unsigned long long kibibytes, size_t precision = 0, const string& locale = "");
string filesize_gib_mib(unsigned long long kibibytes, size_t precision_mib = 0, size_t precision_gib = 0, const string& locale = "");
string filesize(unsigned long long kbytes, size_t precision = 0, bool fixed = false, const string& locale = "");
hash_type hash(const string& src);

View File

@ -119,6 +119,12 @@ namespace modules {
label->replace_token("%gb_swap_total%", string_util::filesize_gib(kb_swap_total, 2, m_bar.locale));
label->replace_token("%gb_swap_free%", string_util::filesize_gib(kb_swap_free, 2, m_bar.locale));
label->replace_token("%gb_swap_used%", string_util::filesize_gib(kb_swap_total - kb_swap_free, 2, m_bar.locale));
label->replace_token("%used%", string_util::filesize_gib_mib(kb_total - kb_avail, 0, 2, m_bar.locale));
label->replace_token("%free%", string_util::filesize_gib_mib(kb_avail, 0, 2, m_bar.locale));
label->replace_token("%total%", string_util::filesize_gib_mib(kb_total, 0, 2, m_bar.locale));
label->replace_token("%swap_total%", string_util::filesize_gib_mib(kb_swap_total, 0, 2, m_bar.locale));
label->replace_token("%swap_free%", string_util::filesize_gib_mib(kb_swap_free, 0, 2, m_bar.locale));
label->replace_token("%swap_used%", string_util::filesize_gib_mib(kb_swap_total - kb_swap_free, 0, 2, m_bar.locale));
};
if (m_label) {
@ -131,7 +137,7 @@ namespace modules {
return true;
}
string memory_module::get_format() const {
if (m_perc_memused>= m_perc_memused_warn && m_formatter->has_format(FORMAT_WARN)) {
return FORMAT_WARN;

View File

@ -291,6 +291,17 @@ namespace string_util {
return floating_point(kibibytes / 1024.0 / 1024.0, precision, true, locale) + " GiB";
}
/**
* Create a GiB string, if the value in GiB is >= 1.0. Otherwise, create a MiB string.
*/
string filesize_gib_mib(unsigned long long kibibytes, size_t precision_mib, size_t precision_gib, const string& locale) {
if(kibibytes < 1024 * 1024) {
return filesize_mib(kibibytes, precision_mib, locale);
} else {
return filesize_gib(kibibytes, precision_gib, locale);
}
}
/**
* Create a filesize string by converting given bytes to highest unit possible
*/