diff --git a/CHANGELOG.md b/CHANGELOG.md index 580b8c648..3ad80fba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/include/utils/string.hpp b/include/utils/string.hpp index 270fc9701..53b9b9ae6 100644 --- a/include/utils/string.hpp +++ b/include/utils/string.hpp @@ -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); diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index f97864fb2..eb36e5dc9 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -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; diff --git a/src/utils/string.cpp b/src/utils/string.cpp index 9df04b885..9ff1be195 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -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 */