Porytiles
Loading...
Searching...
No Matches
stderr_styled_user_diagnostics.cpp
Go to the documentation of this file.
2
3#include <iostream>
4#include <ranges>
5#include <string>
6#include <vector>
7
10
11namespace porytiles2 {
12
13void StderrStyledUserDiagnostics::note(const std::string &tag, const std::vector<std::string> &lines) const
14{
15 assert_or_panic(!lines.empty(), "lines vector was empty");
16 /*
17 * TODO: instead of hardcoding AnsiStyledTextFormatter here, we should have this class detect if stderr is a TTY and
18 * select a text formatter accordingly.
19 */
20 std::cerr << format_->style("note:", Style::bold | Style::cyan) << " ";
21 std::cerr << lines.at(0);
22 std::cerr << " [" << format_->style(tag, Style::bold | Style::cyan) << "]" << std::endl;
23 for (const auto &note_line : std::ranges::views::drop(lines, 1)) {
24 std::cerr << format_->style("│", Style::bold | Style::cyan) << " " << note_line << std::endl;
25 }
26}
27
28void StderrStyledUserDiagnostics::warn_note(const std::string &tag, const std::vector<std::string> &lines) const
29{
30 assert_or_panic(!lines.empty(), "lines vector was empty");
31 std::cerr << format_->style("note:", Style::bold | Style::cyan) << " ";
32 std::cerr << lines.at(0);
33 std::cerr << " [" << format_->style(tag, Style::bold | Style::cyan) << "]" << std::endl;
34 for (const auto &note_line : std::ranges::views::drop(lines, 1)) {
35 std::cerr << format_->style("│", Style::bold | Style::cyan) << " " << note_line << std::endl;
36 }
37}
38
39void StderrStyledUserDiagnostics::warn(const std::string &tag, const std::vector<std::string> &lines) const
40{
41 assert_or_panic(!lines.empty(), "lines vector was empty");
42 std::cerr << format_->style("warning:", Style::bold | Style::magenta) << " ";
43 std::cerr << lines.at(0);
44 std::cerr << " [" << format_->style(tag, Style::bold | Style::magenta) << "]" << std::endl;
45 for (const auto &warn_line : std::ranges::views::drop(lines, 1)) {
46 std::cerr << format_->style("│", Style::bold | Style::magenta) << " " << warn_line << std::endl;
47 }
48}
49
50void StderrStyledUserDiagnostics::err(const std::string &tag, const std::vector<std::string> &lines) const
51{
52 assert_or_panic(!lines.empty(), "lines vector was empty");
53 std::cerr << format_->style("error:", Style::bold | Style::red) << " ";
54 std::cerr << lines.at(0);
55 std::cerr << " [" << format_->style(tag, Style::bold | Style::red) << "]" << std::endl;
56 for (const auto &err_line : std::ranges::views::drop(lines, 1)) {
57 std::cerr << format_->style("│", Style::bold | Style::red) << " " << err_line << std::endl;
58 }
59}
60
62{
63 // TODO: emit some kind of cool ascii art for start of fatal chain
64 // something like:
65 // |-------- FATAL ERROR CHAIN --------|
66 // |-----------------------------------|
67 // or something similar
68 auto lines = err.details(*format_);
69 if (!lines.empty()) {
70 std::cerr << format_->style("fatal:", Style::bold | Style::red) << " ";
71 std::cerr << lines.at(0) << std::endl;
72 for (const auto &line : std::ranges::views::drop(lines, 1)) {
73 std::cerr << format_->style("│", Style::bold | Style::red) << " " << line << std::endl;
74 }
75 }
76}
77
79{
80 std::cerr << format_->style("│", Style::bold | Style::red) << " " << std::endl;
81 std::cerr << format_->style("├ caused by:", Style::bold | Style::red) << std::endl;
82 std::cerr << format_->style("│", Style::bold | Style::red) << " " << std::endl;
83
84 auto lines = err.details(*format_);
85 if (!lines.empty()) {
86 std::cerr << format_->style("│", Style::bold | Style::red) << " " << lines.at(0) << std::endl;
87 for (const auto &line : std::ranges::views::drop(lines, 1)) {
88 std::cerr << format_->style("│", Style::bold | Style::red) << " " << line << std::endl;
89 }
90 }
91}
92
94{
95 std::cerr << format_->style("│", Style::bold | Style::red) << " " << std::endl;
96 std::cerr << format_->style("├ root cause:", Style::bold | Style::red) << std::endl;
97 std::cerr << format_->style("│", Style::bold | Style::red) << " " << std::endl;
98
99 auto lines = err.details(*format_);
100 if (!lines.empty()) {
101 std::cerr << format_->style("│", Style::bold | Style::red) << " " << lines.at(0) << std::endl;
102 for (const auto &line : std::ranges::views::drop(lines, 1)) {
103 std::cerr << format_->style("│", Style::bold | Style::red) << " " << line << std::endl;
104 }
105 }
106}
107
108} // namespace porytiles2
Abstract interface for all error types used in ChainableResult error chains.
Definition error.hpp:26
void emit_fatal_step(const Error &err) const override
Emit an intermediate step error in a fatal error chain to stderr.
void warn_note(const std::string &tag, const std::vector< std::string > &lines) const override
Display a multi-line tagged warning note to stderr.
void emit_fatal_root(const Error &err) const override
Emit the root cause error in a fatal error chain to stderr.
void warn(const std::string &tag, const std::vector< std::string > &lines) const override
Display a multi-line tagged warning to stderr.
void emit_fatal_proximate(const Error &err) const override
Emit the proximate (immediate) error in a fatal error chain to stderr.
void note(const std::string &tag, const std::vector< std::string > &lines) const override
Display a multi-line tagged informational note to stderr.
void err(const std::string &tag, const std::vector< std::string > &lines) const override
Display a multi-line tagged error message to stderr.
virtual std::string style(const std::string &text, Style styles) const =0
Applies styling to a text string.
@ magenta
Magenta text color.
@ cyan
Cyan text color.
@ bold
Bold text formatting.
@ red
Red text color.
void assert_or_panic(const bool condition, const StringViewSourceLoc &s)
Conditionally panics if the given condition is false.
Definition panic.hpp:70