Modernized code and fancier code printing
This commit is contained in:
79
toolbox.hpp
79
toolbox.hpp
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <array>
|
||||
|
||||
#define W_NAME 49
|
||||
#define W_TIME 17
|
||||
@ -19,28 +20,88 @@ namespace asp {
|
||||
void toolbox_unit_test(void) noexcept;
|
||||
std::string thousand_sep(const uint64_t&, const char&) noexcept;
|
||||
std::string thousand_sep(const uint64_t&) noexcept;
|
||||
void print_separator(const char* const) noexcept;
|
||||
|
||||
template <typename F, typename... Args>
|
||||
inline constexpr void measure_time_void(const char* step_name, const F& fnc, Args &&...args) noexcept {
|
||||
/**
|
||||
* @brief Print a formatted row of titles with of gaps seperated by a separator.
|
||||
*
|
||||
* @param gaps List of size gaps
|
||||
* @param titles List of titles
|
||||
* @param separator Separator character between each gap
|
||||
*/
|
||||
template<size_t N>
|
||||
constexpr void formatted_row(const std::array<int32_t, N>& gaps, const std::array<const char* const, N>& titles,
|
||||
const char* const separator = "│", const char line_ending = '\n') noexcept {
|
||||
for(size_t i = 0; i < N; ++i)
|
||||
printf("%s %*s ", separator, -gaps[i], titles[i]);
|
||||
printf("%s%c", separator, line_ending);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a formatted line of repeated characters.
|
||||
*
|
||||
* @param gaps List of size gaps
|
||||
* @param right Character on the left
|
||||
* @param middle Character between each separator
|
||||
* @param separator Separator character between each gap
|
||||
* @param left Character on the right
|
||||
*/
|
||||
template<size_t N>
|
||||
constexpr void formatted_line(const std::array<int32_t, N>& gaps, const char* const left, const char* const middle,
|
||||
const char* const separator, const char* const right, const char line_ending = '\n') noexcept {
|
||||
printf("%s", left);
|
||||
for(size_t i = 0; i < N; ++i){
|
||||
for(int32_t j = std::abs(gaps[i]) + 2; j > 0; --j)
|
||||
printf("%s", separator);
|
||||
if(i != N - 1)
|
||||
printf("%s", middle);
|
||||
}
|
||||
|
||||
printf("%s%c", right, line_ending);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a formatted header with the given titles and sizes.
|
||||
*
|
||||
* @param gaps List of size gaps
|
||||
* @param titles List of titles
|
||||
*/
|
||||
template<size_t N>
|
||||
constexpr void header(const std::array<int32_t, N>& gaps, const std::array<const char* const, N>& titles) noexcept {
|
||||
formatted_line(gaps, "┌", "┬", "─", "┐");
|
||||
formatted_row(gaps, titles);
|
||||
formatted_line(gaps, "├", "┼", "─", "┤");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a formatted footer with the given sizes.
|
||||
*
|
||||
* @param gaps List of size gaps
|
||||
*/
|
||||
template<size_t N>
|
||||
constexpr void footer(const std::array<int32_t, N>& gaps) noexcept {
|
||||
formatted_line(gaps, "└", "┴", "─", "┘");
|
||||
}
|
||||
|
||||
template <typename F, size_t N, typename... Args>
|
||||
constexpr void measure_time_void(const std::array<int32_t, N>& gaps, const char* step_name, const F& fnc, Args &&...args) noexcept {
|
||||
#ifndef __DEBUG
|
||||
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
|
||||
formatted_row(gaps, { "Testing summary", "In progress", "In progress" }, "│", '\r');
|
||||
#endif
|
||||
const auto start = time();
|
||||
fnc(std::forward<Args>(args)...);
|
||||
const long long timespent = duration_ns(time() - start);
|
||||
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str());
|
||||
formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str() });
|
||||
}
|
||||
|
||||
template <typename T, typename F, typename... Args>
|
||||
inline constexpr T measure_time(const char* step_name, const F& fnc, Args &&...args) noexcept {
|
||||
template <typename T, typename F, size_t N, typename... Args>
|
||||
constexpr T measure_time(const std::array<int32_t, N>& gaps, const char* step_name, const F& fnc, Args &&...args) noexcept {
|
||||
#ifndef __DEBUG
|
||||
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
|
||||
formatted_row(gaps, { "Testing summary", "In progress", "In progress" }, "│", '\r');
|
||||
#endif
|
||||
const auto start = time();
|
||||
const T res = fnc(std::forward<Args>(args)...);
|
||||
const long long timespent = duration_ns(time() - start);
|
||||
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str());
|
||||
formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str() });
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user