refactor: align tr_formatter_speed_KBps with `stringForSpeed: kb: mb: gb:` (#5108)

This commit is contained in:
Cœur 2023-04-14 23:36:34 +08:00 committed by GitHub
parent 97352a60ad
commit afa9f64feb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 10 deletions

View File

@ -755,25 +755,76 @@ std::string tr_formatter_speed_KBps(double kilo_per_second)
auto speed = kilo_per_second; auto speed = kilo_per_second;
if (speed <= 999.95) // 0.0 KB to 999.9 KB if (speed < 999.95) // 0.0 KB to 999.9 KB (0.0 KiB to 999.9 KiB)
{ {
return fmt::format("{:Ld} {:s}", int(speed), std::data(speed_units[TR_FMT_KB].name)); return fmt::format("{:.1Lf} {:s}", speed, std::data(speed_units[TR_FMT_KB].name));
} }
double const kilo = speed_units[TR_FMT_KB].value; double const kilo = speed_units[TR_FMT_KB].value;
speed /= kilo; speed /= kilo;
if (speed <= 99.995) // 0.98 MB to 99.99 MB if (speed < 99.995) // 0.98 MB to 99.99 MB (1.00 MiB to 99.99 MiB)
{ {
return fmt::format("{:.2Lf} {:s}", speed, std::data(speed_units[TR_FMT_MB].name)); return fmt::format("{:.2Lf} {:s}", speed, std::data(speed_units[TR_FMT_MB].name));
} }
if (speed < 999.95) // 100.0 MB to 999.9 MB (100.0 MiB to 999.9 MiB)
if (speed <= 999.95) // 100.0 MB to 999.9 MB
{ {
return fmt::format("{:.1Lf} {:s}", speed, std::data(speed_units[TR_FMT_MB].name)); return fmt::format("{:.1Lf} {:s}", speed, std::data(speed_units[TR_FMT_MB].name));
} }
return fmt::format("{:.1Lf} {:s}", speed / kilo, std::data(speed_units[TR_FMT_GB].name)); speed /= kilo;
if (speed < 99.995) // 0.98 GB to 99.99 GB (1.00 GiB to 99.99 GiB)
{
return fmt::format("{:.2Lf} {:s}", speed, std::data(speed_units[TR_FMT_GB].name));
}
// 100.0 GB and above (100.0 GiB and above)
return fmt::format("{:.1Lf} {:s}", speed, std::data(speed_units[TR_FMT_GB].name));
}
std::string tr_formatter_speed_compact_KBps(double kilo_per_second)
{
using namespace formatter_impl;
auto speed = kilo_per_second;
if (speed < 99.95) // 0.0 KB to 99.9 KB (0.0 KiB to 99.9 KiB)
{
return fmt::format("{:.1Lf} {:s}", speed, std::data(speed_units[TR_FMT_KB].name));
}
if (speed < 999.5) // 100 KB to 999 KB (100 KiB to 999 KiB)
{
return fmt::format("{:.0Lf} {:s}", speed, std::data(speed_units[TR_FMT_KB].name));
}
double const kilo = speed_units[TR_FMT_KB].value;
speed /= kilo;
if (speed < 9.995) // 0.98 MB to 9.99 MB (1.00 MiB to 9.99 MiB)
{
return fmt::format("{:.2Lf} {:s}", speed, std::data(speed_units[TR_FMT_MB].name));
}
if (speed < 99.95) // 10.0 MB to 99.9 MB (10.0 MiB to 99.9 MiB)
{
return fmt::format("{:.1Lf} {:s}", speed, std::data(speed_units[TR_FMT_MB].name));
}
if (speed < 999.5) // 100 MB to 999 MB (100 MiB to 999 MiB)
{
return fmt::format("{:.0Lf} {:s}", speed, std::data(speed_units[TR_FMT_MB].name));
}
speed /= kilo;
if (speed < 9.995) // 0.98 GB to 9.99 GB (1.00 GiB to 9.99 GiB)
{
return fmt::format("{:.2Lf} {:s}", speed, std::data(speed_units[TR_FMT_GB].name));
}
if (speed < 99.95) // 10.0 GB to 99.9 GB (10.0 GiB to 99.9 GiB)
{
return fmt::format("{:.1Lf} {:s}", speed, std::data(speed_units[TR_FMT_GB].name));
}
// 100 GB and above (100 GiB and above)
return fmt::format("{:.0Lf} {:s}", speed, std::data(speed_units[TR_FMT_GB].name));
} }
size_t tr_mem_K = 0; size_t tr_mem_K = 0;

View File

@ -311,19 +311,21 @@ extern size_t tr_speed_K;
extern size_t tr_mem_K; extern size_t tr_mem_K;
extern uint64_t tr_size_K; /* unused? */ extern uint64_t tr_size_K; /* unused? */
/* format a speed from KBps into a user-readable string. */ /** @brief Format a speed from KBps into a user-readable string of at most 4 significant digits. */
[[nodiscard]] std::string tr_formatter_speed_KBps(double kilo_per_second); [[nodiscard]] std::string tr_formatter_speed_KBps(double kilo_per_second);
/** @brief Format a speed from KBps into a user-readable string of at most 3 significant digits. */
[[nodiscard]] std::string tr_formatter_speed_compact_KBps(double kilo_per_second);
/* format a memory size from bytes into a user-readable string. */ /** @brief Format a memory size from bytes into a user-readable string. */
[[nodiscard]] std::string tr_formatter_mem_B(size_t bytes); [[nodiscard]] std::string tr_formatter_mem_B(size_t bytes);
/* format a memory size from MB into a user-readable string. */ /** @brief Format a memory size from MB into a user-readable string. */
[[nodiscard]] static inline std::string tr_formatter_mem_MB(double MBps) [[nodiscard]] static inline std::string tr_formatter_mem_MB(double MBps)
{ {
return tr_formatter_mem_B((size_t)(MBps * tr_mem_K * tr_mem_K)); return tr_formatter_mem_B((size_t)(MBps * tr_mem_K * tr_mem_K));
} }
/* format a file size from bytes into a user-readable string. */ /** @brief Format a file size from bytes into a user-readable string. */
[[nodiscard]] std::string tr_formatter_size_B(uint64_t bytes); [[nodiscard]] std::string tr_formatter_size_B(uint64_t bytes);
void tr_formatter_get_units(void* dict); void tr_formatter_get_units(void* dict);