Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[module/memory] Display MiB instead of GiB when GiB value is less than 1GiB #2488

Merged
merged 7 commits into from Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/utils/string.hpp
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/modules/memory.cpp
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/string.cpp
Expand Up @@ -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
*/
Expand Down