ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Output.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include "io/Color.hpp"
12
13#include <fmt/format.h>
14#include <cstdio>
15#include <string>
16
17namespace ppforest2::io {
25 struct Output {
26 bool quiet;
27 int indent_level = 0;
28
29 explicit Output(bool quiet)
30 : quiet(quiet) {}
31
32 // -- stdout (quiet-aware, indented) ----------------------------------
33
37 template<typename... Args> void println(fmt::format_string<Args...> fmt_str, Args&&... args) const {
38 if (!quiet) {
39 print_indent();
40 fmt::print(fmt_str, std::forward<Args>(args)...);
41 fmt::print("\n");
42 }
43 }
44
50 template<typename... Args> void print(fmt::format_string<Args...> fmt_str, Args&&... args) const {
51 if (!quiet) {
52 print_indent();
53 fmt::print(fmt_str, std::forward<Args>(args)...);
54 }
55 }
56
60 void newline() const {
61 if (!quiet) {
62 fmt::print("\n");
63 }
64 }
65
66 // -- indentation -----------------------------------------------------
67
68 void indent() { ++indent_level; }
69
70 void dedent() {
71 if (indent_level > 0) {
73 }
74 }
75
76 // -- high-level patterns ---------------------------------------------
77
81 void saved(std::string const& label, std::string const& path) const { println("{} saved to {}", label, path); }
82
89 void progress(int current, int total, int bar_width = 50) const {
90 if (quiet) {
91 return;
92 }
93
94 float pct = static_cast<float>(current) / static_cast<float>(total);
95 int pos = static_cast<int>(static_cast<float>(bar_width) * pct);
96
97 std::string bar_tpl = style::emphasis("{} |");
98 std::string bar = std::string(pos, '-') + std::string(bar_width - pos, ' ');
99
100 if (current == total) {
101 bar_tpl = style::success(bar_tpl);
102 } else {
103 bar_tpl = style::info(bar_tpl);
104 }
105
106 fmt::print(
107 "\r{:{}}" + bar_tpl + " {}/{} ({}%) ",
108 "",
109 indent_level * 2,
110 bar,
111 current,
112 total,
113 static_cast<int>(pct * 100.0)
114 );
115 std::fflush(stdout);
116
117 if (current == total) {
118 fmt::print("\n");
119 }
120 }
121
122 private:
123 void print_indent() const {
124 if (indent_level > 0) {
125 fmt::print("{:{}}", "", indent_level * 2);
126 }
127 }
128 };
129}
TTY-aware colored terminal output utilities.
std::string info(std::string const &s)
Format text in cyan (for informational highlights like progress bars).
Definition Color.hpp:124
std::string emphasis(std::string const &s)
Format text in bold (for emphasis / labels).
Definition Color.hpp:106
std::string success(std::string const &s)
Format text in green (for success messages).
Definition Color.hpp:96
Definition Color.hpp:31
void progress(int current, int total, int bar_width=50) const
Display a carriage-return progress bar.
Definition Output.hpp:89
Output(bool quiet)
Definition Output.hpp:29
void print(fmt::format_string< Args... > fmt_str, Args &&... args) const
Print indent + formatted content, no newline.
Definition Output.hpp:50
void saved(std::string const &label, std::string const &path) const
Print a file-save confirmation: "label saved to path".
Definition Output.hpp:81
int indent_level
Definition Output.hpp:27
bool quiet
Definition Output.hpp:26
void newline() const
Print a blank line. For visual separation between sections.
Definition Output.hpp:60
void println(fmt::format_string< Args... > fmt_str, Args &&... args) const
Print indent + formatted content + newline. The workhorse.
Definition Output.hpp:37
void indent()
Definition Output.hpp:68
void dedent()
Definition Output.hpp:70