diff --git a/src/cpp/util/core/prefix.cpp b/src/cpp/util/core/prefix.cpp new file mode 100644 index 00000000..b99033d6 --- /dev/null +++ b/src/cpp/util/core/prefix.cpp @@ -0,0 +1,121 @@ +#include +#include +#include + +#include + +#include + +#include +#include + +#define DECLARE_FIELD(TYPE, NAME, DEFAULT) \ +public: \ + static const constexpr TYPE km_default_##NAME = DEFAULT; \ + \ +private: \ + TYPE m_##NAME = km_default_##NAME; \ + \ +public: \ + [[nodiscard]] TYPE NAME() const noexcept \ + { \ + return m_##NAME; \ + } + +#define OPT(NAME) lyra::opt(m_##NAME) + +DEFINE_EXEC_INFO() + +class Options { + DECLARE_FIELD(bool, prefix_time, false); + DECLARE_FIELD(bool, prefix_delta, false); + DECLARE_FIELD(bool, prefix_line, false); + +public: + Options() = delete; + + Options(int argc, char** argv) + { + bool help = false; + auto cli = lyra::cli() | lyra::help(help) | + OPT(prefix_time)["-t"]["--time"]("prefix lines with current time") | + OPT(prefix_delta)["-d"]["--delta"]("prefix lines with delta time") | + OPT(prefix_line)["-l"]["--line"]("prefix lines with line number"); + auto args = cli.parse({ argc, argv }); + + if (!args) { + std::cerr << args.message() << '\n'; + std::cerr << "Try '" << xph::exec_path << " -h' for more information.\n"; + std::exit(EXIT_FAILURE); + } + + if (help) { + std::cout << cli << '\n'; + std::exit(EXIT_SUCCESS); + } + } +}; + +int main(int argc, char* argv[]) +{ + xph::gather_exec_info(argc, argv); + + Options options(argc, argv); + + std::chrono::steady_clock clock; + + auto start_time = clock.now(); + auto last_time = start_time; + + for (std::string line; std::getline(std::cin, line);) { + auto current_time = clock.now(); + + bool prefixed = false; + if (options.prefix_line()) { + static int line_number = 0; + + std::cout << "[#" << line_number++; + + prefixed = true; + } + + if (options.prefix_time()) { + auto elapsed_time = current_time - start_time; + auto elapsed_ticks = elapsed_time.count(); + auto elapsed_human_ticks = xph::unit::tick_to_human(elapsed_ticks); + + if (prefixed) + std::cout << " | "; + else + std::cout << '['; + + std::cout << '@' << elapsed_human_ticks; + + prefixed = true; + } + + if (options.prefix_delta()) { + auto differential_time = current_time - last_time; + auto differential_ticks = differential_time.count(); + auto differential_human_ticks = xph::unit::tick_to_human(differential_ticks); + + if (prefixed) + std::cout << " | "; + else + std::cout << '['; + + std::cout << '+' << differential_human_ticks; + + prefixed = true; + } + + if (prefixed) + std::cout << "]: "; + + std::cout << line << '\n'; + + last_time = current_time; + } + + return EXIT_SUCCESS; +} diff --git a/src/cpp/util/core/prependtime.cpp b/src/cpp/util/core/prependtime.cpp deleted file mode 100644 index 131b3bbb..00000000 --- a/src/cpp/util/core/prependtime.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include - -#include - -int main() -{ - std::chrono::steady_clock clock; - - auto start_time = clock.now(); - auto last_time = start_time; - - for (std::string line; std::getline(std::cin, line);) { - auto current_time = clock.now(); - - auto elapsed_time = current_time - start_time; - auto elapsed_ticks = elapsed_time.count(); - auto elapsed_human_ticks = xph::unit::tick_to_human(elapsed_ticks); - - auto differential_time = current_time - last_time; - auto differential_ticks = differential_time.count(); - auto differential_human_ticks = xph::unit::tick_to_human(differential_ticks); - - std::cout << '[' << elapsed_human_ticks << " (+" << differential_human_ticks - << ")]: " << line << '\n'; - - last_time = current_time; - } - - return 0; -}