NoTeX 1.0.0
A modern noteworthy LaTeX template
Loading...
Searching...
No Matches
output.cpp
1
9
10#include "notex/output.hpp"
11
12#include <cstdio>
13#include <iostream>
14#include <string>
15
16#include "notex/constants.hpp"
17
18#if defined(__unix__) || defined(__APPLE__)
19#include <unistd.h>
20#endif
21
22namespace notex::ui {
23
24namespace {
25
26bool g_assume_yes = false; // NOLINT: deliberate translation-unit-local state
27std::optional<bool> g_interactive_override; // NOLINT: same as above
28
30bool stdin_is_interactive() {
31 if (g_interactive_override.has_value()) return *g_interactive_override;
32#if defined(__unix__) || defined(__APPLE__)
33 return ::isatty(fileno(stdin)) != 0;
34#else
35 return false;
36#endif
37}
38
39} // namespace
40
41void set_assume_yes(bool assume_yes) { g_assume_yes = assume_yes; }
42
43bool assume_yes() { return g_assume_yes; }
44
46 std::optional<bool> is_interactive) {
47 g_interactive_override = is_interactive;
48}
49
50void success(std::string_view message) {
51 std::cout << ansi::color::green << "✓ " << ansi::reset << message << "\n";
52}
53
54void warning(std::string_view message) {
55 std::cout << ansi::color::yellow << "! " << ansi::reset << message << "\n";
56}
57
58void error(std::string_view message) {
59 std::cerr << ansi::color::red << "✗ " << ansi::reset << message << "\n";
60}
61
62void step(std::string_view message) {
63 std::cout << ansi::color::cyan << "➤ " << ansi::reset << message << "\n";
64}
65
66void info(std::string_view message) { std::cout << message << "\n"; }
67
68bool confirm(std::string_view prompt, bool default_answer) {
69 if (g_assume_yes) return true;
70 if (!stdin_is_interactive()) return default_answer;
71
72 std::cout << prompt << " [" << (default_answer ? "Y/n" : "y/N") << "] ";
73 std::string answer;
74 std::getline(std::cin, answer);
75 if (answer.empty()) return default_answer;
76 return answer.front() == 'y' || answer.front() == 'Y';
77}
78
79} // namespace notex::ui
Compile-time constants.
constexpr std::string_view cyan
Text foreground color: cyan.
Definition constants.hpp:79
constexpr std::string_view yellow
Text foreground color: yellow.
Definition constants.hpp:73
constexpr std::string_view red
Text foreground color: red.
Definition constants.hpp:69
constexpr std::string_view green
Text foreground color: green.
Definition constants.hpp:71
constexpr std::string_view reset
Reset all text formatting and colors to terminal default.
Definition constants.hpp:30
void set_stdin_interactive_override_for_testing(std::optional< bool > is_interactive)
Testing seam: overrides whether confirm() treats standard input as an interactive terminal.
Definition output.cpp:45
bool assume_yes()
Definition output.cpp:43
void error(std::string_view message)
Prints an error message, prefixed with a red cross, to stderr.
Definition output.cpp:58
void info(std::string_view message)
Prints a plain informational message to stdout.
Definition output.cpp:66
void success(std::string_view message)
Prints a success message, prefixed with a green checkmark, to stdout.
Definition output.cpp:50
void warning(std::string_view message)
Definition output.cpp:54
void set_assume_yes(bool assume_yes)
Sets whether confirm() should answer "yes" without prompting.
Definition output.cpp:41
void step(std::string_view message)
Prints a step or progress message, prefixed with an arrow, to stdout.
Definition output.cpp:62
bool confirm(std::string_view prompt, bool default_answer=false)
Asks the user to confirm an action.
Definition output.cpp:68
Styled terminal output and interactive confirmation prompts.