Added: Option for base 10 bytes/bits

This commit is contained in:
aristocratos 2022-01-16 13:39:50 +01:00
parent f46989ad35
commit 989427ff3a
3 changed files with 40 additions and 10 deletions

View File

@ -123,6 +123,8 @@ namespace Config {
{"temp_scale", "#* Which temperature scale to use, available values: \"celsius\", \"fahrenheit\", \"kelvin\" and \"rankine\"."},
{"base_10_sizes", "#* Use base 10 for bits/bytes sizes, KB = 1000 instead of KiB = 1024."},
{"show_cpu_freq", "#* Show CPU frequency."},
{"clock_format", "#* Draw a clock at top of screen, formatting according to strftime, empty string to disable.\n"
@ -233,6 +235,7 @@ namespace Config {
{"use_fstab", true},
{"show_io_stat", true},
{"io_mode", false},
{"base_10_sizes", false},
{"io_graph_combined", false},
{"net_auto", true},
{"net_sync", false},

View File

@ -257,6 +257,14 @@ namespace Menu {
"\"%H\" = 24h hour, \"%I\" = 12h hour",
"\"%M\" = minute, \"%S\" = second",
"\"%d\" = day, \"%m\" = month, \"%y\" = year"},
{"base_10_sizes",
"Use base 10 for bits and bytes sizes.",
"",
"Uses KB = 1000 instead of KiB = 1024,",
"MB = 1000KB instead of MiB = 1024KiB,",
"and so on.",
"",
"True or False."},
{"background_update",
"Update main ui when menus are showing.",
"",
@ -1134,6 +1142,9 @@ namespace Menu {
else if (option == "background_update") {
Runner::pause_output = false;
}
else if (option == "base_10_sizes") {
recollect = true;
}
}
else if (selPred.test(isBrowseable)) {
auto& optList = optionsList.at(option).get();

View File

@ -32,6 +32,7 @@ tab-size = 4
#include <btop_shared.hpp>
#include <btop_tools.hpp>
#include <btop_config.hpp>
using std::string_view, std::max, std::floor, std::to_string, std::cin, std::cout, std::flush, robin_hood::unordered_flat_map;
namespace fs = std::filesystem;
@ -288,23 +289,38 @@ namespace Tools {
string floating_humanizer(uint64_t value, const bool shorten, size_t start, const bool bit, const bool per_second) {
string out;
const size_t mult = (bit) ? 8 : 1;
static const array<string, 11> Units_bit = {"bit", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib", "Bib", "GEb"};
static const array<string, 11> Units_byte = {"Byte", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "BiB", "GEB"};
const auto& units = (bit) ? Units_bit : Units_byte;
const bool mega = Config::getB("base_10_sizes");
static const array<string, 11> mebiUnits_bit = {"bit", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib", "Bib", "GEb"};
static const array<string, 11> mebiUnits_byte = {"Byte", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", "BiB", "GEB"};
static const array<string, 11> megaUnits_bit = {"bit", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb", "Bb", "Gb"};
static const array<string, 11> megaUnits_byte = {"Byte", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB", "GB"};
const auto& units = (bit) ? ( mega ? megaUnits_bit : mebiUnits_bit) : ( mega ? megaUnits_byte : mebiUnits_byte);
value *= 100 * mult;
while (value >= 102400) {
value >>= 10;
if (value < 100) {
out = to_string(value);
break;
if (mega) {
while (value >= 100000) {
value /= 1000;
if (value < 100) {
out = to_string(value);
break;
}
start++;
}
}
else {
while (value >= 102400) {
value >>= 10;
if (value < 100) {
out = to_string(value);
break;
}
start++;
}
start++;
}
if (out.empty()) {
out = to_string(value);
if (out.size() == 4 and start > 0) { out.pop_back(); out.insert(2, ".");}
if (not mega and out.size() == 4 and start > 0) { out.pop_back(); out.insert(2, ".");}
else if (out.size() == 3 and start > 0) out.insert(1, ".");
else if (out.size() >= 2) out.resize(out.size() - 2);
}