From 0416093edc93e5fc686f5bb6bf9c6787d135238a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drozd?= Date: Fri, 27 Nov 2020 23:30:09 +0100 Subject: [PATCH] Display correct(binary) unit prefixes in memory module (#2211) --- include/utils/string.hpp | 4 ++-- src/modules/memory.cpp | 24 ++++++++++++------------ src/utils/string.cpp | 12 ++++++------ tests/unit_tests/utils/string.cpp | 14 +++++++------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/utils/string.hpp b/include/utils/string.hpp index 40130c44..270fc970 100644 --- a/include/utils/string.hpp +++ b/include/utils/string.hpp @@ -94,8 +94,8 @@ namespace string_util { size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth); string floating_point(double value, size_t precision, bool fixed = false, const string& locale = ""); - string filesize_mb(unsigned long long kbytes, size_t precision = 0, const string& locale = ""); - string filesize_gb(unsigned long long kbytes, size_t precision = 0, 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(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 8e445883..c07c06fc 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -98,22 +98,22 @@ namespace modules { // replace tokens if (m_label) { m_label->reset_tokens(); - m_label->replace_token("%gb_used%", string_util::filesize_gb(kb_total - kb_avail, 2, m_bar.locale)); - m_label->replace_token("%gb_free%", string_util::filesize_gb(kb_avail, 2, m_bar.locale)); - m_label->replace_token("%gb_total%", string_util::filesize_gb(kb_total, 2, m_bar.locale)); - m_label->replace_token("%mb_used%", string_util::filesize_mb(kb_total - kb_avail, 0, m_bar.locale)); - m_label->replace_token("%mb_free%", string_util::filesize_mb(kb_avail, 0, m_bar.locale)); - m_label->replace_token("%mb_total%", string_util::filesize_mb(kb_total, 0, m_bar.locale)); + m_label->replace_token("%gb_used%", string_util::filesize_gib(kb_total - kb_avail, 2, m_bar.locale)); + m_label->replace_token("%gb_free%", string_util::filesize_gib(kb_avail, 2, m_bar.locale)); + m_label->replace_token("%gb_total%", string_util::filesize_gib(kb_total, 2, m_bar.locale)); + m_label->replace_token("%mb_used%", string_util::filesize_mib(kb_total - kb_avail, 0, m_bar.locale)); + m_label->replace_token("%mb_free%", string_util::filesize_mib(kb_avail, 0, m_bar.locale)); + m_label->replace_token("%mb_total%", string_util::filesize_mib(kb_total, 0, 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_swap_used%", to_string(m_perc_swap_used)); m_label->replace_token("%percentage_swap_free%", to_string(m_perc_swap_free)); - m_label->replace_token("%mb_swap_total%", string_util::filesize_mb(kb_swap_total, 0, m_bar.locale)); - m_label->replace_token("%mb_swap_free%", string_util::filesize_mb(kb_swap_free, 0, m_bar.locale)); - m_label->replace_token("%mb_swap_used%", string_util::filesize_mb(kb_swap_total - kb_swap_free, 0, m_bar.locale)); - m_label->replace_token("%gb_swap_total%", string_util::filesize_gb(kb_swap_total, 2, m_bar.locale)); - m_label->replace_token("%gb_swap_free%", string_util::filesize_gb(kb_swap_free, 2, m_bar.locale)); - m_label->replace_token("%gb_swap_used%", string_util::filesize_gb(kb_swap_total - kb_swap_free, 2, m_bar.locale)); + m_label->replace_token("%mb_swap_total%", string_util::filesize_mib(kb_swap_total, 0, m_bar.locale)); + m_label->replace_token("%mb_swap_free%", string_util::filesize_mib(kb_swap_free, 0, m_bar.locale)); + m_label->replace_token("%mb_swap_used%", string_util::filesize_mib(kb_swap_total - kb_swap_free, 0, m_bar.locale)); + m_label->replace_token("%gb_swap_total%", string_util::filesize_gib(kb_swap_total, 2, m_bar.locale)); + m_label->replace_token("%gb_swap_free%", string_util::filesize_gib(kb_swap_free, 2, m_bar.locale)); + m_label->replace_token("%gb_swap_used%", string_util::filesize_gib(kb_swap_total - kb_swap_free, 2, m_bar.locale)); } return true; diff --git a/src/utils/string.cpp b/src/utils/string.cpp index 27fad0ee..9df04b88 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -278,17 +278,17 @@ namespace string_util { } /** - * Create a MB filesize string + * Create a MiB filesize string */ - string filesize_mb(unsigned long long kbytes, size_t precision, const string& locale) { - return floating_point(kbytes / 1024.0, precision, true, locale) + " MB"; + string filesize_mib(unsigned long long kibibytes, size_t precision, const string& locale) { + return floating_point(kibibytes / 1024.0, precision, true, locale) + " MiB"; } /** - * Create a GB filesize string + * Create a GiB filesize string */ - string filesize_gb(unsigned long long kbytes, size_t precision, const string& locale) { - return floating_point(kbytes / 1024.0 / 1024.0, precision, true, locale) + " GB"; + string filesize_gib(unsigned long long kibibytes, size_t precision, const string& locale) { + return floating_point(kibibytes / 1024.0 / 1024.0, precision, true, locale) + " GiB"; } /** diff --git a/tests/unit_tests/utils/string.cpp b/tests/unit_tests/utils/string.cpp index 09f48f8f..84c16d1e 100644 --- a/tests/unit_tests/utils/string.cpp +++ b/tests/unit_tests/utils/string.cpp @@ -124,13 +124,13 @@ TEST(String, floatingPoint) { } TEST(String, filesize) { - EXPECT_EQ("3.000 MB", string_util::filesize_mb(3 * 1024, 3)); - EXPECT_EQ("3.195 MB", string_util::filesize_mb(3 * 1024 + 200, 3)); - EXPECT_EQ("3 MB", string_util::filesize_mb(3 * 1024 + 400)); - EXPECT_EQ("4 MB", string_util::filesize_mb(3 * 1024 + 800)); - EXPECT_EQ("3.195 GB", string_util::filesize_gb(3 * 1024 * 1024 + 200 * 1024, 3)); - EXPECT_EQ("3 GB", string_util::filesize_gb(3 * 1024 * 1024 + 400 * 1024)); - EXPECT_EQ("4 GB", string_util::filesize_gb(3 * 1024 * 1024 + 800 * 1024)); + EXPECT_EQ("3.000 MiB", string_util::filesize_mib(3 * 1024, 3)); + EXPECT_EQ("3.195 MiB", string_util::filesize_mib(3 * 1024 + 200, 3)); + EXPECT_EQ("3 MiB", string_util::filesize_mib(3 * 1024 + 400)); + EXPECT_EQ("4 MiB", string_util::filesize_mib(3 * 1024 + 800)); + EXPECT_EQ("3.195 GiB", string_util::filesize_gib(3 * 1024 * 1024 + 200 * 1024, 3)); + EXPECT_EQ("3 GiB", string_util::filesize_gib(3 * 1024 * 1024 + 400 * 1024)); + EXPECT_EQ("4 GiB", string_util::filesize_gib(3 * 1024 * 1024 + 800 * 1024)); EXPECT_EQ("3 B", string_util::filesize(3)); EXPECT_EQ("3 KB", string_util::filesize(3 * 1024)); EXPECT_EQ("3 MB", string_util::filesize(3 * 1024 * 1024));