This commit is contained in:
SWarrener 2024-01-17 10:25:37 -08:00 committed by GitHub
commit 77513244a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 1 deletions

View File

@ -98,7 +98,9 @@ string filesize_mib(unsigned long long kibibytes, size_t precision = 0, const st
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 = "");
string filesize(unsigned long long bytes, size_t precision = 0, bool fixed = false, const string& locale = "");
string filesize_specific(
unsigned long long bytes, char target, size_t precision = 0, bool fixed = false, const string& locale = "");
hash_type hash(const string& src);
} // namespace string_util

View File

@ -188,6 +188,14 @@ namespace modules {
"%total%", string_util::filesize(mount->bytes_total, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%free%", string_util::filesize(mount->bytes_avail, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%used%", string_util::filesize(mount->bytes_used, m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%kb_free%", string_util::filesize_specific(mount->bytes_avail, 'k', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%kb_used%", string_util::filesize_specific(mount->bytes_used, 'k', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%mb_free%", string_util::filesize_specific(mount->bytes_avail, 'm', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%mb_used%", string_util::filesize_specific(mount->bytes_used, 'm', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%gb_free%", string_util::filesize_specific(mount->bytes_avail, 'g', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%gb_used%", string_util::filesize_specific(mount->bytes_used, 'g', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%tb_free%", string_util::filesize_specific(mount->bytes_avail, 't', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
label->replace_token("%tb_used%", string_util::filesize_specific(mount->bytes_used, 't', m_fixed ? 2 : 0, m_fixed, m_bar.locale));
};
if (tag == TAG_BAR_FREE) {

View File

@ -5,6 +5,7 @@
#include <iomanip>
#include <sstream>
#include <utility>
#include <math.h>
POLYBAR_NS
@ -452,6 +453,33 @@ string filesize(unsigned long long bytes, size_t precision, bool fixed, const st
return floating_point(value, precision, fixed, locale) + " " + suffix;
}
/**
* Create a filesize string with a specific target unit
*/
string filesize_specific(unsigned long long bytes, char target, size_t precision, bool fixed, const string& locale) {
double value = bytes;
switch(target) {
case 'k':
value /= 1024.0;
return floating_point(value, precision, fixed, locale) + " KB";
break;
case 'm':
value /= pow(1024.0, 2);
return floating_point(value, precision, fixed, locale) + " MB";
break;
case 'g':
value /= pow(1024.0, 3);
return floating_point(value, precision, fixed, locale) + " GB";
break;
case 't':
value /= pow(1024.0, 4);
return floating_point(value, precision, fixed, locale) + " TB";
break;
default:
return floating_point(value, precision, fixed, locale) + " B";
}
}
/**
* Compute string hash
*/

View File

@ -172,6 +172,8 @@ TEST(String, filesize) {
EXPECT_EQ("3 MB", string_util::filesize(3 * 1024 * 1024));
EXPECT_EQ("3 GB", string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024));
EXPECT_EQ("3 TB", string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024 * 1024));
EXPECT_EQ("3 TB", string_util::filesize_specific((unsigned long long)3 * 1024 * 1024 * 1024 * 1024, 't'));
EXPECT_EQ("3 GB", string_util::filesize_specific((unsigned long long)3 * 1024 * 1024 * 1024, 'g'));
}
// utf8_to_ucs4 {{{