Skip to content

Commit

Permalink
parse: add SI and IEC byte units
Browse files Browse the repository at this point in the history
also there was an oopsie from parent commit: added theme.cursor and renamed it to theme.cursor_name*
  • Loading branch information
Toni500github committed Oct 10, 2024
1 parent 69625a1 commit f77e8be
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 81 deletions.
11 changes: 9 additions & 2 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Config
bool gui = false;
bool sep_reset_after = false;
bool slow_query_warnings= false;
bool use_SI_unit = false;

// modules specific config
std::string uptime_d_fmt;
Expand Down Expand Up @@ -243,12 +244,18 @@ magenta = "\e[1;35m"
cyan = "\e[1;36m"
white = "\e[1;37m"
# Alias colors. Basically more color variables, but config depending.
# Alias colors. Basically more color variables, but config depending (no shot).
# They can be used as like as the color tag.
# This is as like as using the --color argument
# Syntax must be "name=value", e.g "purple=magenta" or "orange=!#F08000"
alias-colors = ["purple=magenta"]
# Used in disk, ram and swap modules.
# If true, we're going to use the SI standard byte unit (1kb == 1000 bytes)
# Else if false, we using the IEC byte unit (1kb == 1024 bytes)
# Really nerdy stuff
use-SI-byte-unit = false
# Colors to be used in percentage tag and modules members.
# They are used as if you're using the color tag.
# It's an array just for "convinience"
Expand Down Expand Up @@ -316,4 +323,4 @@ bg-image = "disable"
)#";

#endif
#endif // _CONFIG_HPP
17 changes: 9 additions & 8 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ constexpr std::size_t operator""_len(const char*, std::size_t ln) noexcept
struct byte_units_t
{
std::string unit;
float num_bytes;
double num_bytes;
};

constexpr const char NOCOLOR[] = "\033[0m";
Expand Down Expand Up @@ -58,7 +58,8 @@ std::string binarySearchPCIArray(const std::string_view vendor_id, const std::s
std::string binarySearchPCIArray(const std::string_view vendor_id);
std::string read_shell_exec(const std::string_view cmd);
void getFileValue(u_short& iterIndex, const std::string_view line, std::string& str, const size_t& amount);
byte_units_t auto_devide_bytes(const size_t num);
byte_units_t auto_devide_bytes(const double num, const std::uint16_t base, const std::string_view maxprefix = "");
byte_units_t devide_bytes(const double num, const std::string_view prefix);
bool is_file_image(const unsigned char* bytes);
void ctrl_d_handler(const std::istream& cin);
std::string expandVar(std::string ret);
Expand Down Expand Up @@ -87,7 +88,7 @@ void error(const std::string_view fmt, Args&&... args) noexcept
template <typename... Args>
void die(const std::string_view fmt, Args&&... args) noexcept
{
fmt::println(stderr, BOLD_COLOR(fmt::rgb(fmt::color::red)), "ERROR: {}",
fmt::println(stderr, BOLD_COLOR(fmt::rgb(fmt::color::red)), "FATAL: {}",
fmt::format(fmt::runtime(fmt), std::forward<Args>(args)...));
std::exit(1);
}
Expand Down Expand Up @@ -119,25 +120,25 @@ void info(const std::string_view fmt, Args&&... args) noexcept
* @param def The default result
* @param fmt The format string
* @param args Arguments in the format
* @returns the result, y = true, f = false, only returns def if the result is def
* @returns the result, y = true, n = false, only returns def if the result is def
*/
template <typename... Args>
bool askUserYorN(bool def, const std::string_view fmt, Args&&... args)
{
const std::string& inputs_str = fmt::format("[{}/{}]: ", (def ? 'Y' : 'y'), (!def ? 'N' : 'n'));
const std::string& inputs_str = fmt::format(" [{}]: ", def ? "Y/n" : "y/N");
std::string result;
fmt::print(fmt::runtime(fmt), std::forward<Args>(args)...);
fmt::print(" {}", inputs_str);
fmt::print("{}", inputs_str);

while (std::getline(std::cin, result) && (result.length() > 1))
fmt::print(BOLD_COLOR(fmt::rgb(fmt::color::yellow)), "Please answear y or n, {}", inputs_str);
fmt::print(BOLD_COLOR(fmt::rgb(fmt::color::yellow)), "Please answear y or n,{}", inputs_str);

ctrl_d_handler(std::cin);

if (result.empty())
return def;

if (def ? tolower(result[0]) != 'n' : tolower(result[0]) != 'y')
if (def ? std::tolower(result[0]) != 'n' : std::tolower(result[0]) != 'y')
return def;

return !def;
Expand Down
3 changes: 2 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void Config::loadConfigFile(const std::string_view filename, colors_t& colors)
this->gui = this->getValue<bool>("gui.enable", false);
this->slow_query_warnings= this->getValue<bool>("config.slow-query-warnings", false);
this->sep_reset_after = this->getValue<bool>("config.sep-reset-after", false);
this->use_SI_unit = this->getValue<bool>("config.use-SI-byte-unit", false);
this->ascii_logo_type = this->getValue<std::string>("config.ascii-logo-type", "");
this->source_path = this->getValue<std::string>("config.source-path", "os");
this->data_dir = this->getValue<std::string>("config.data-dir", "/usr/share/customfetch");
Expand Down Expand Up @@ -96,7 +97,7 @@ void Config::loadConfigFile(const std::string_view filename, colors_t& colors)
this->addAliasColors(str);
}

// Config::getValue but don't want to specify the template
// Config::getValue() but don't want to specify the template
std::string Config::getThemeValue(const std::string_view value, const std::string_view fallback) const
{
return this->tbl.at_path(value).value<std::string>().value_or(fallback.data());
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ theme-gtk-all
icons : gtk icons theme name [Papirus-Dark [GTK2/3], Qogir [GTK4]]
font : gtk font theme name [Cantarell 10 [GTK2], Noto Sans, 10 [GTK3], Noto Sans 10 [GTK4]]
# note: these members are auto displayed in KiB, MiB, GiB and TiB.
# they all (except those who has the same name as the module or that ends with "_perc")
# have a -KiB, -GiB and -MiB variant.
# note: these members are auto displayed in from B to YB (depending if using SI byte unit or not(IEC)).
# they all (except those that has the same name as the module or that ends with "_perc")
# have variants from -B to -YB and -B to -YiB
# example: if you want to show your 512MiB of used RAM in GiB
# use the `used-GiB` variant (they don't print the unit tho)
ram
Expand Down
88 changes: 46 additions & 42 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "switch_fnv1a.hpp"
#include "util.hpp"

// declarations of static members in query.hpp
Query::System::System_t Query::System::m_system_infos;
Query::Theme::Theme_t Query::Theme::m_theme_infos;
Query::User::User_t Query::User::m_users_infos;
Expand Down Expand Up @@ -118,6 +119,7 @@ static std::string convert_hex_ansi_escape(const std::string_view hexstr, const
}
#endif

// parse() for parse_args_t& arguments
// some times we don't want to use the original pureOutput,
// so we have to create a tmp string just for the sake of the function arguments
static std::string parse(const std::string_view input, std::string& _, parse_args_t& parse_args)
Expand Down Expand Up @@ -868,6 +870,18 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
static std::vector<std::string> queried_themes_names;
static systemInfo_t queried_themes;

const std::uint16_t byte_unit = config.use_SI_unit ? 1000 : 1024;
constexpr std::array<std::string_view, 32> sorted_valid_prefixes = {"B", "EB", "EiB", "GB", "GiB", "kB", "KiB", "MB", "MiB", "PB", "PiB", "TB", "TiB", "YB", "YiB", "ZB", "ZiB"};

const auto& return_devided_bytes = [&sorted_valid_prefixes, &moduleMemberName](const float& amount) -> float
{
const std::string& prefix = moduleMemberName.substr(moduleMemberName.find('-')+1);
if (std::binary_search(sorted_valid_prefixes.begin(), sorted_valid_prefixes.end(), prefix))
return static_cast<float>(devide_bytes(amount, prefix).num_bytes);

return 0;
};

if (moduleName == "os")
{
Query::System query_system;
Expand Down Expand Up @@ -1228,9 +1242,9 @@ void addValueFromModule(const std::string& moduleName, const std::string& module

if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
{
byte_units.at(TOTAL) = auto_devide_bytes(query_disk.total_amount());
byte_units.at(USED) = auto_devide_bytes(query_disk.used_amount());
byte_units.at(FREE) = auto_devide_bytes(query_disk.free_amount());
byte_units.at(TOTAL) = auto_devide_bytes(query_disk.total_amount(), byte_unit);
byte_units.at(USED) = auto_devide_bytes(query_disk.used_amount(), byte_unit);
byte_units.at(FREE) = auto_devide_bytes(query_disk.free_amount(), byte_unit);

switch (moduleMember_hash)
{
Expand Down Expand Up @@ -1274,17 +1288,13 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
parse_args));
break;

case "used-GiB"_fnv1a16: SYSINFO_INSERT(query_disk.used_amount() / 1073741824); break;
case "used-MiB"_fnv1a16: SYSINFO_INSERT(query_disk.used_amount() / 1048576); break;
case "used-KiB"_fnv1a16: SYSINFO_INSERT(query_disk.used_amount() / 1024); break;

case "total-GiB"_fnv1a16: SYSINFO_INSERT(query_disk.total_amount() / 1073741824); break;
case "total-MiB"_fnv1a16: SYSINFO_INSERT(query_disk.total_amount() / 1048576); break;
case "total-KiB"_fnv1a16: SYSINFO_INSERT(query_disk.total_amount() / 1024); break;

case "free-GiB"_fnv1a16: SYSINFO_INSERT(query_disk.free_amount() / 1073741824); break;
case "free-MiB"_fnv1a16: SYSINFO_INSERT(query_disk.free_amount() / 1048576); break;
case "free-KiB"_fnv1a16: SYSINFO_INSERT(query_disk.free_amount() / 1024); break;
default:
if (hasStart(moduleMemberName, "free-"))
SYSINFO_INSERT(return_devided_bytes(query_disk.free_amount()));
else if (hasStart(moduleMemberName, "used-"))
SYSINFO_INSERT(return_devided_bytes(query_disk.used_amount()));
else if (hasStart(moduleMemberName, "total-"))
SYSINFO_INSERT(return_devided_bytes(query_disk.total_amount()));
}
}
}
Expand All @@ -1305,9 +1315,10 @@ void addValueFromModule(const std::string& moduleName, const std::string& module

if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
{
byte_units.at(FREE) = auto_devide_bytes(query_ram.swap_free_amount() * 1024);
byte_units.at(USED) = auto_devide_bytes(query_ram.swap_used_amount() * 1024);
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.swap_total_amount() * 1024);
// idk, trick the diviser
byte_units.at(FREE) = auto_devide_bytes(query_ram.swap_free_amount() * byte_unit, byte_unit);
byte_units.at(USED) = auto_devide_bytes(query_ram.swap_used_amount() * byte_unit, byte_unit);
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.swap_total_amount() * byte_unit, byte_unit);

switch (moduleMember_hash)
{
Expand Down Expand Up @@ -1350,17 +1361,13 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
parse_args));
break;

case "free-GiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_free_amount() / 1048576); break;
case "free-MiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_free_amount() / 1024); break;
case "free-KiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_free_amount()); break;

case "used-GiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_used_amount() / 1048576); break;
case "used-MiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_used_amount() / 1024); break;
case "used-KiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_used_amount()); break;

case "total-GiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_total_amount() / 1048576); break;
case "total-MiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_total_amount() / 1024); break;
case "total-KiB"_fnv1a16: SYSINFO_INSERT(query_ram.swap_total_amount()); break;
default:
if (hasStart(moduleMemberName, "free-"))
SYSINFO_INSERT(return_devided_bytes(query_ram.swap_free_amount()));
else if (hasStart(moduleMemberName, "used-"))
SYSINFO_INSERT(return_devided_bytes(query_ram.swap_used_amount()));
else if (hasStart(moduleMemberName, "total-"))
SYSINFO_INSERT(return_devided_bytes(query_ram.swap_total_amount()));
}
}
}
Expand All @@ -1381,9 +1388,10 @@ void addValueFromModule(const std::string& moduleName, const std::string& module

if (sysInfo.at(moduleName).find(moduleMemberName) == sysInfo.at(moduleName).end())
{
byte_units.at(USED) = auto_devide_bytes(query_ram.used_amount() * 1024);
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.total_amount() * 1024);
byte_units.at(FREE) = auto_devide_bytes(query_ram.free_amount() * 1024);
// idk, trick the diviser
byte_units.at(USED) = auto_devide_bytes(query_ram.used_amount() * byte_unit, byte_unit);
byte_units.at(TOTAL) = auto_devide_bytes(query_ram.total_amount() * byte_unit, byte_unit);
byte_units.at(FREE) = auto_devide_bytes(query_ram.free_amount() * byte_unit, byte_unit);

switch (moduleMember_hash)
{
Expand Down Expand Up @@ -1420,17 +1428,13 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
SYSINFO_INSERT(get_and_color_percentage(query_ram.used_amount(), query_ram.total_amount(), parse_args));
break;

case "used-GiB"_fnv1a16: SYSINFO_INSERT(query_ram.used_amount() / 1048576); break;
case "used-MiB"_fnv1a16: SYSINFO_INSERT(query_ram.used_amount() / 1024); break;
case "used-KiB"_fnv1a16: SYSINFO_INSERT(query_ram.used_amount()); break;

case "total-GiB"_fnv1a16: SYSINFO_INSERT(query_ram.total_amount() / 1048576); break;
case "total-MiB"_fnv1a16: SYSINFO_INSERT(query_ram.total_amount() / 1024); break;
case "total-KiB"_fnv1a16: SYSINFO_INSERT(query_ram.total_amount()); break;

case "free-GiB"_fnv1a16: SYSINFO_INSERT(query_ram.free_amount() / 1048576); break;
case "free-MiB"_fnv1a16: SYSINFO_INSERT(query_ram.free_amount() / 1024); break;
case "free-KiB"_fnv1a16: SYSINFO_INSERT(query_ram.free_amount()); break;
default:
if (hasStart(moduleMemberName, "free-"))
SYSINFO_INSERT(return_devided_bytes(query_ram.free_amount()));
else if (hasStart(moduleMemberName, "used-"))
SYSINFO_INSERT(return_devided_bytes(query_ram.used_amount()));
else if (hasStart(moduleMemberName, "total-"))
SYSINFO_INSERT(return_devided_bytes(query_ram.total_amount()));
}
}
}
Expand Down
62 changes: 37 additions & 25 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -99,48 +100,59 @@ std::string expandVar(std::string ret)

std::string read_by_syspath(const std::string_view path)
{
std::ifstream f_drm(path.data());
if (!f_drm.is_open())
std::ifstream f(path.data());
if (!f.is_open())
{
error("Failed to open {}", path);
return UNKNOWN;
}

std::string ret;
std::getline(f_drm, ret);
std::getline(f, ret);
return ret;
}

byte_units_t auto_devide_bytes(const size_t num)
byte_units_t auto_devide_bytes(const double num, const std::uint16_t base, const std::string_view maxprefix)
{
struct byte_units_t ret;
if (num >= 1000000000000)
{
ret.num_bytes = static_cast<float>(num) / 1099511627776;
ret.unit = "TiB";
}
else if (num >= 1000000000)
{
ret.num_bytes = static_cast<float>(num) / 1073741824;
ret.unit = "GiB";
}
else if (num >= 1000000)
double size = num;

std::array<std::string_view, 10> prefixes;
if (base == 1024)
prefixes = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};
else if (base == 1000)
prefixes = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
else
prefixes = {"B"};

std::uint16_t counter = 0;
if (maxprefix.empty())
{
ret.num_bytes = static_cast<float>(num) / 1048576;
ret.unit = "MiB";
for (; counter < prefixes.size() && size >= base; ++counter)
size /= base;
}
else if (num >= 1000)
else
{
ret.num_bytes = static_cast<float>(num) / 1024;
ret.unit = "kiB";
for (; counter < prefixes.size() && size >= base && prefixes.at(counter) != maxprefix;
++counter)
size /= base;
}
else

return {prefixes.at(counter).data(), size};
}

byte_units_t devide_bytes(const double num, const std::string_view prefix)
{
if (prefix != "B")
{
ret.num_bytes = num;
ret.unit = "bytes";
// GiB
// 012
if (prefix.size() == 3 && prefix.at(1) == 'i')
return auto_devide_bytes(num, 1024, prefix);
else
return auto_devide_bytes(num, 1000, prefix);
}

return ret;
return auto_devide_bytes(num, 0);
}

bool is_file_image(const unsigned char* bytes)
Expand Down

0 comments on commit f77e8be

Please sign in to comment.