Molecular Dynamics Simulation  1.0
Functions
ProgressBarInterceptor.cpp File Reference
#include "ProgressBarInterceptor.h"
#include <spdlog/fmt/chrono.h>
#include <sys/ioctl.h>
#include <filesystem>
#include <iostream>
#include "io/logger/Logger.h"
#include "simulation/SimulationParams.h"
#include "utils/FormatTime.h"
Include dependency graph for ProgressBarInterceptor.cpp:

Go to the source code of this file.

Functions

int displayed_width (const char *p)
 
void printProgress (const std::filesystem::path &input_file_path, size_t percentage, size_t iteration, size_t expected_iterations, int estimated_remaining_seconds=-1, double particle_updates_per_second=-1, bool finished=false)
 

Function Documentation

◆ displayed_width()

int displayed_width ( const char *  p)

Definition at line 13 of file ProgressBarInterceptor.cpp.

13  {
14  int result = 0;
15  for (; *p; ++p) {
16  if (p[0] == '\e' && p[1] == '[')
17  while (*p != 'm')
18  if (*p)
19  ++p;
20  else
21  throw std::runtime_error("string terminates inside ANSI colour sequence");
22  else
23  ++result;
24  }
25  return result;
26 }

◆ printProgress()

void printProgress ( const std::filesystem::path &  input_file_path,
size_t  percentage,
size_t  iteration,
size_t  expected_iterations,
int  estimated_remaining_seconds = -1,
double  particle_updates_per_second = -1,
bool  finished = false 
)

Definition at line 28 of file ProgressBarInterceptor.cpp.

29  {
30  struct winsize size {};
31  ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
32 
33  auto should_progress_bar_length = std::min(std::max(size.ws_col - 90, 0), 100);
34  auto length_full_progress_bar = static_cast<size_t>(percentage * static_cast<double>(should_progress_bar_length) / 100.0);
35 
36  auto progress_bar = fmt::format("[{}{}]", ansi_blue_bold + std::string(length_full_progress_bar, '#'),
37  std::string(should_progress_bar_length - length_full_progress_bar, ' ') + ansi_end);
38 
39  std::string line = fmt::format("{} {}/{} {:>4} ETA: {} MUP/s: {} [{}]", progress_bar, iteration, expected_iterations,
40  ansi_bright_white_bold + std::to_string(percentage) + "%" + ansi_end,
41  ansi_bright_white_bold + format_seconds_eta(estimated_remaining_seconds) + ansi_end,
42  ansi_bright_white_bold + format_mup_s(particle_updates_per_second) + ansi_end,
43  ansi_bright_white_bold + std::filesystem::path(input_file_path).stem().string() + ansi_end);
44 
45  if (displayed_width(line.c_str()) > size.ws_col) {
46  line = line.substr(0, size.ws_col - 3) + "...";
47  }
48 
49  std::cout << "\33[2K\r" << line << std::flush;
50 
51  if (finished) {
52  std::cout << std::endl;
53  }
54 }
std::string format_seconds_eta(int total_seconds)
Formats the given seconds into a string of the form "HH:MM:SS".
Definition: FormatTime.cpp:5
std::string format_mup_s(double mup_s)
Formats the given mup/s into a string.
Definition: FormatTime.cpp:17
const std::string ansi_bright_white_bold
Definition: Logger.h:12
const std::string ansi_end
Definition: Logger.h:13
const std::string ansi_blue_bold
Definition: Logger.h:9
int displayed_width(const char *p)
std::string to_string(const Container &container, const std::string &delimiter=", ", const std::array< std::string, 2 > &surround={"[", "]"})
Definition: ArrayUtils.h:97