Display correct(binary) unit prefixes in memory module (#2211)

This commit is contained in:
Michał Drozd 2020-11-27 23:30:09 +01:00 committed by GitHub
parent 5007dae35a
commit 0416093edc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 27 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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";
}
/**

View File

@ -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));