18#if defined(__unix__) || defined(__APPLE__)
26bool g_assume_yes =
false;
27std::optional<bool> g_interactive_override;
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;
46 std::optional<bool> is_interactive) {
47 g_interactive_override = is_interactive;
58void error(std::string_view message) {
62void step(std::string_view message) {
66void info(std::string_view message) { std::cout << message <<
"\n"; }
68bool confirm(std::string_view prompt,
bool default_answer) {
69 if (g_assume_yes)
return true;
70 if (!stdin_is_interactive())
return default_answer;
72 std::cout << prompt <<
" [" << (default_answer ?
"Y/n" :
"y/N") <<
"] ";
74 std::getline(std::cin, answer);
75 if (answer.empty())
return default_answer;
76 return answer.front() ==
'y' || answer.front() ==
'Y';
constexpr std::string_view cyan
Text foreground color: cyan.
constexpr std::string_view yellow
Text foreground color: yellow.
constexpr std::string_view red
Text foreground color: red.
constexpr std::string_view green
Text foreground color: green.
constexpr std::string_view reset
Reset all text formatting and colors to terminal default.
void set_stdin_interactive_override_for_testing(std::optional< bool > is_interactive)
Testing seam: overrides whether confirm() treats standard input as an interactive terminal.
void error(std::string_view message)
Prints an error message, prefixed with a red cross, to stderr.
void info(std::string_view message)
Prints a plain informational message to stdout.
void success(std::string_view message)
Prints a success message, prefixed with a green checkmark, to stdout.
void warning(std::string_view message)
void set_assume_yes(bool assume_yes)
Sets whether confirm() should answer "yes" without prompting.
void step(std::string_view message)
Prints a step or progress message, prefixed with an arrow, to stdout.
bool confirm(std::string_view prompt, bool default_answer=false)
Asks the user to confirm an action.
Styled terminal output and interactive confirmation prompts.