Skip to content

Commit

Permalink
Merge pull request #364 from poacpm/improve/pretty
Browse files Browse the repository at this point in the history
Improve util::pretty
  • Loading branch information
ken-matsui authored Jan 6, 2021
2 parents 324351a + 9fcc850 commit 5020c5e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 74 deletions.
8 changes: 3 additions & 5 deletions include/poac/util/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ namespace poac::util::net {
if (now_count > max_count) {
now_count = max_count;
}
const auto [ parsed_max_byte, max_byte_unit ] = util::pretty::to_byte(max_count);
const auto [ parsed_now_byte, now_byte_unit ] = util::pretty::to_byte(now_count);
return fmt::format(
FMT_STRING("{} {:.2f}{}/{:.2f}{}"),
FMT_STRING("{} {}/{}"),
to_progress(max_count, now_count),
parsed_now_byte, now_byte_unit,
parsed_max_byte, max_byte_unit
util::pretty::to_byte(now_count),
util::pretty::to_byte(max_count)
);
}

Expand Down
76 changes: 30 additions & 46 deletions include/poac/util/pretty.hpp
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
34 changes: 11 additions & 23 deletions tests/pretty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,17 @@ BOOST_AUTO_TEST_CASE( poac_util_pretty_to_byte_test1 )
using poac::util::pretty::to_byte;

// Implicit conversion, float to int
std::pair<int, std::string> temp = to_byte(12);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "12B" );
temp = to_byte(1'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "1KB" );
temp = to_byte(12'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "12KB" );
temp = to_byte(1'000'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "1MB" );
temp = to_byte(12'000'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "12MB" );
temp = to_byte(1'000'000'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "1GB" );
temp = to_byte(12'000'000'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "12GB" );
temp = to_byte(1'000'000'000'000);
std::cout << temp.second << std::endl;
BOOST_CHECK( std::to_string(temp.first) + temp.second == "1TB" );
temp = to_byte(12'000'000'000'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "12TB" );
temp = to_byte(1'000'000'000'000'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "1000TB" );
temp = to_byte(1'000'000'000'000'000'000);
BOOST_CHECK( std::to_string(temp.first) + temp.second == "1000000TB" );
BOOST_CHECK( to_byte(12) == "12.00B" );
BOOST_CHECK( to_byte(1'024) == "1.00KB" );
BOOST_CHECK( to_byte(12'000) == "11.72KB" );
BOOST_CHECK( to_byte(1'000'000) == "976.56KB" );
BOOST_CHECK( to_byte(12'000'000) == "11.44MB" );
BOOST_CHECK( to_byte(1'000'000'000) == "953.67MB" );
BOOST_CHECK( to_byte(12'000'000'000) == "11.18GB" );
BOOST_CHECK( to_byte(1'000'000'000'000) == "931.32GB" );
BOOST_CHECK( to_byte(12'000'000'000'000) == "10.91TB" );
BOOST_CHECK( to_byte(1'000'000'000'000'000) == "909.49TB" );
BOOST_CHECK( to_byte(1'000'000'000'000'000'000) == "888.18PB" );
}

// std::string clip_string(const std::string& s, const unsigned long& n)
Expand Down

0 comments on commit 5020c5e

Please sign in to comment.