Skip to content

Commit

Permalink
prependtime: rename to prefix, add line number, make prefixes configu…
Browse files Browse the repository at this point in the history
…rable
  • Loading branch information
XPhyro committed Aug 18, 2024
1 parent e79ae13 commit 387c765
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 32 deletions.
121 changes: 121 additions & 0 deletions src/cpp/util/core/prefix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <chrono>
#include <iostream>
#include <string>

#include <cstdlib>

#include <lyra/lyra.hpp>

#include <xph/exec_info.hpp>
#include <xph/units.hpp>

#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;
}
32 changes: 0 additions & 32 deletions src/cpp/util/core/prependtime.cpp

This file was deleted.

0 comments on commit 387c765

Please sign in to comment.