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 <functional>
16#include <string>
17
18namespace ppforest2::io {
26 struct Output {
27 bool quiet;
28 int indent_level = 0;
29
30 explicit Output(bool quiet)
31 : quiet(quiet) {}
32
33 // -- stdout (quiet-aware, indented) ----------------------------------
34
38 template<typename... Args> void println(fmt::format_string<Args...> fmt_str, Args&&... args) const {
39 if (!quiet) {
40 print_indent();
41 fmt::print(fmt_str, std::forward<Args>(args)...);
42 fmt::print("\n");
43 }
44 }
45
51 template<typename... Args> void print(fmt::format_string<Args...> fmt_str, Args&&... args) const {
52 if (!quiet) {
53 print_indent();
54 fmt::print(fmt_str, std::forward<Args>(args)...);
55 }
56 }
57
61 void newline() const {
62 if (!quiet) {
63 fmt::print("\n");
64 }
65 }
66
70 void flush() const { std::fflush(stdout); }
71
72 // -- stderr (always prints, no indent) -------------------------------
73
77 template<typename... Args> void errorln(fmt::format_string<Args...> fmt_str, Args&&... args) const {
78 fmt::print(stderr, fmt_str, std::forward<Args>(args)...);
79 fmt::print(stderr, "\n");
80 }
81
82 // -- indentation -----------------------------------------------------
83
84 void indent() { ++indent_level; }
85
86 void dedent() {
87 if (indent_level > 0)
89 }
90
91 // -- high-level patterns ---------------------------------------------
92
96 void saved(std::string const& label, std::string const& path) const { println("{} saved to {}", label, path); }
97
104 void progress(int current, int total, int bar_width = 50) const {
105 if (quiet)
106 return;
107
108 float pct = static_cast<float>(current) / total;
109 int pos = static_cast<int>(bar_width * pct);
110
111 std::string bar_tpl = style::emphasis("{} |");
112 std::string bar = std::string(pos, '-') + std::string(bar_width - pos, ' ');
113
114 if (current == total) {
115 bar_tpl = style::success(bar_tpl);
116 } else {
117 bar_tpl = style::info(bar_tpl);
118 }
119
120 fmt::print("\r{:{}}" + bar_tpl + " {}/{} ({}%) ",
121 "",
122 indent_level * 2,
123 bar,
124 current,
125 total,
126 static_cast<int>(pct * 100.0));
127 std::fflush(stdout);
128
129 if (current == total) {
130 fmt::print("\n");
131 }
132 }
133
140 int try_or_fail(std::function<void()> const& f, std::string const& context = "") const {
141 try {
142 f();
143 return 0;
144 } catch (std::exception const& e) {
145 if (context.empty()) {
146 errorln("{} {}", style::error("Error:"), e.what());
147 } else {
148 errorln("{} {}: {}", style::error("Error:"), context, e.what());
149 }
150
151 return 1;
152 }
153 }
154
155 private:
156 void print_indent() const {
157 if (indent_level > 0) {
158 fmt::print("{:{}}", "", indent_level * 2);
159 }
160 }
161 };
162}
TTY-aware colored terminal output utilities.
std::string error(std::string const &s)
Format text in red (for error messages).
Definition Color.hpp:86
std::string info(std::string const &s)
Format text in cyan (for informational highlights like progress bars).
Definition Color.hpp:120
std::string emphasis(std::string const &s)
Format text in bold (for emphasis / labels).
Definition Color.hpp:103
std::string success(std::string const &s)
Format text in green (for success messages).
Definition Color.hpp:93
Definition Color.hpp:31
void flush() const
Flush stdout. For interactive output like progress bars.
Definition Output.hpp:70
void progress(int current, int total, int bar_width=50) const
Display a carriage-return progress bar.
Definition Output.hpp:104
Output(bool quiet)
Definition Output.hpp:30
void print(fmt::format_string< Args... > fmt_str, Args &&... args) const
Print indent + formatted content, no newline.
Definition Output.hpp:51
void saved(std::string const &label, std::string const &path) const
Print a file-save confirmation: "label saved to path".
Definition Output.hpp:96
int indent_level
Definition Output.hpp:28
bool quiet
Definition Output.hpp:27
void errorln(fmt::format_string< Args... > fmt_str, Args &&... args) const
Print formatted content + newline to stderr. Always prints.
Definition Output.hpp:77
void newline() const
Print a blank line. For visual separation between sections.
Definition Output.hpp:61
void println(fmt::format_string< Args... > fmt_str, Args &&... args) const
Print indent + formatted content + newline. The workhorse.
Definition Output.hpp:38
void indent()
Definition Output.hpp:84
int try_or_fail(std::function< void()> const &f, std::string const &context="") const
Run a callable, catch std::exception, print error, return 1.
Definition Output.hpp:140
void dedent()
Definition Output.hpp:86