-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from poacpm/improve/pretty
Improve util::pretty
- Loading branch information
Showing
3 changed files
with
44 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,53 @@ | ||
#ifndef POAC_UTIL_PRETTY_HPP | ||
#define POAC_UTIL_PRETTY_HPP | ||
|
||
// std | ||
#include <cstdint> | ||
#include <string> | ||
#include <utility> | ||
|
||
// external | ||
#include <fmt/core.h> | ||
|
||
namespace poac::util::pretty { | ||
std::string to_time(const std::string& s) { | ||
double total_seconds = std::stod(s); | ||
if (total_seconds > 1.0) { | ||
std::string res; | ||
|
||
const auto total_secs = static_cast<std::uint_fast64_t>(total_seconds); | ||
const auto days = static_cast<std::uint_fast8_t>(total_secs / 60 / 60 / 24); | ||
if (days > 0) { | ||
res += std::to_string(days) + "d "; | ||
} | ||
const auto hours = static_cast<std::uint_fast8_t>((total_secs / 60 / 60) % 24); | ||
if (hours > 0) { | ||
res += std::to_string(hours) + "h "; | ||
} | ||
const auto minutes = static_cast<std::uint_fast8_t>((total_secs / 60) % 60); | ||
if (minutes > 0) { | ||
res += std::to_string(minutes) + "m "; | ||
} | ||
const auto seconds = static_cast<std::uint_fast8_t>(total_secs % 60); | ||
res += std::to_string(seconds) + "s"; | ||
|
||
return res; | ||
} | ||
else { | ||
if (total_seconds <= 1.0) { | ||
return s + "s"; | ||
} | ||
} | ||
|
||
std::pair<float, std::string> | ||
to_byte(const double b) { | ||
// 1024 | ||
const double kb = b / 1000.0; | ||
if (kb < 1) { | ||
return { b, "B" }; | ||
std::string res; | ||
const auto total_secs = static_cast<std::uint_fast64_t>(total_seconds); | ||
if (const auto days = total_secs / 60 / 60 / 24; days > 0) { | ||
res += std::to_string(days) + "d "; | ||
} | ||
const double mb = kb / 1000.0; | ||
if (mb < 1) { | ||
return { kb, "KB" }; | ||
if (const auto hours = (total_secs / 60 / 60) % 24; hours > 0) { | ||
res += std::to_string(hours) + "h "; | ||
} | ||
const double gb = mb / 1000.0; | ||
if (gb < 1) { | ||
return { mb, "MB" }; | ||
if (const auto minutes = (total_secs / 60) % 60; minutes > 0) { | ||
res += std::to_string(minutes) + "m "; | ||
} | ||
const double tb = gb / 1000.0; | ||
if (tb < 1) { | ||
return { gb, "GB" }; | ||
} | ||
return { tb, "TB" }; | ||
const auto seconds = total_secs % 60; | ||
res += std::to_string(seconds) + "s"; | ||
return res; | ||
} | ||
|
||
inline const std::vector<std::string> size_suffixes = { | ||
"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" | ||
}; | ||
|
||
std::string | ||
to_byte(double bytes) { | ||
int index = 0; | ||
for (; bytes >= 1000.0; bytes /= 1024.0, ++index); | ||
return fmt::format("{:.2f}{}", bytes, size_suffixes.at(index)); | ||
} | ||
|
||
// If string size is over specified number of characters and it can be clipped, | ||
// display an ellipsis (...). | ||
std::string clip_string(const std::string& s, const std::size_t& n) { | ||
if (s.size() <= n) { | ||
return s; | ||
} else { | ||
return s.substr(0, n) + "..."; | ||
} | ||
inline std::string | ||
clip_string(const std::string& s, const std::size_t& n) { | ||
return s.size() <= n ? s : s.substr(0, n) + "..."; | ||
} | ||
} // end namespace | ||
#endif // !POAC_UTIL_PRETTY_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters