This commit is contained in:
2026-01-02 15:47:33 +01:00
parent 1c7dd10089
commit 9d1b50882b
2 changed files with 22 additions and 25 deletions

View File

@@ -6,6 +6,7 @@
#include <array>
#include <charconv>
#include <cstdint>
#include <log.hpp>
#include <filesystem>
#include <string>
@@ -16,9 +17,29 @@ enum just { right, left };
std::string pad(std::string s, uint8_t len, just j = right);
std::string center(std::string str, uint8_t len);
std::string pBytes(double bytes, bool isKbytes = false);
std::string getFirstDir(const std::string_view& path);
inline std::string pNum(const auto num, const uint8_t decs = 0) {
static std::array<char, 20> buf;
auto [ptr, ec] = std::to_chars(buf.data(), buf.data() + buf.size(), static_cast<double>(num), std::chars_format::fixed, decs);
return {buf.data(), ptr};
}
/* converts large number in (k)bytes to human-readable numer with suffix and sane decimal */
std::string pBytes(const auto bytes, const bool isKbytes = false) {
double bytes_d = bytes;
for (constexpr std::array suffix_list = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'}; const auto& suffix : suffix_list) {
if (suffix == 'K' && isKbytes) continue; // skip first loop if already in Kbytes
bytes_d /= 1024.0;
if (bytes_d < 1024) {
if (bytes_d > 100) return pNum(bytes_d) + suffix;
if (bytes_d > 10) return pNum(bytes_d, 1) + suffix;
return pNum(bytes_d, 2) + suffix;
}
}
return "++++";
}
/* reads file into a std::array buffer */
template <size_t N>
inline ssize_t readFile(const std::string& path, std::array<char, N>& buffer) {
@@ -50,13 +71,6 @@ inline void fastReadNum(const std::string& path, std::array<char, N>& buf, T& re
result = static_cast<T>(temp / Divider);
}
inline std::string pNum(const auto num, const uint8_t decs = 0) {
static std::array<char, 20> buf;
auto [ptr, ec] = std::to_chars(buf.data(), buf.data() + buf.size(), num, std::chars_format::fixed, decs);
return std::string(buf.data(), ptr);
}
inline bool fileExists(const std::string_view& path) {
return std::filesystem::exists(path);
}

View File

@@ -1,7 +1,5 @@
#include <array>
#include <cstdint>
#include <filesystem>
#include <log.hpp>
#include <string>
#include <string_view>
#include <utils.hpp>
@@ -23,21 +21,6 @@ std::string center(std::string str, const uint8_t len) {
return str;
}
/* converts large number in (k)bytes to human-readable numer with suffix and sane decimal */
std::string pBytes(double bytes, const bool isKbytes) {
for (constexpr std::array suffix_list = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'}; const auto& suffix : suffix_list) {
if (suffix == 'K' && isKbytes) continue; // skip first loop if already in Kbytes
bytes /= 1024.0;
if (bytes < 1024) {
if (bytes > 100) return pNum(bytes) + suffix;
if (bytes > 10) return pNum(bytes, 1) + suffix;
return pNum(bytes, 2) + suffix;
}
}
return "++++";
}
/* finds first dir in path and returns it. don't refactor, you don't understand it, its fast enough, and you only use it on init */
std::string getFirstDir(const std::string_view& path) {
for (const auto& entry : std::filesystem::directory_iterator(path))