ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Color.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include <string>
13
14#include <fmt/color.h>
15#include <fmt/format.h>
16
17#ifdef _WIN32
18#include <windows.h>
19#else
20#include <unistd.h>
21#endif
22
36 inline bool& color_enabled() {
37 static bool enabled = true;
38 return enabled;
39 }
40
49 inline void init_color(bool no_color) {
50 if (no_color) {
51 color_enabled() = false;
52 return;
53 }
54 // clang-format off
55 #ifdef _WIN32
56 // clang-format on
57 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
58 DWORD dwMode = 0;
59
60 if (hOut != INVALID_HANDLE_VALUE && GetConsoleMode(hOut, &dwMode)) {
61 SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
62 color_enabled() = true;
63 } else {
64 color_enabled() = false;
65 }
66 // clang-format off
67 #else
68 color_enabled() = static_cast<bool>(isatty(fileno(stdout)));
69 #endif
70 // clang-format on
71 }
72
78 inline std::string styled(std::string const& s, fmt::terminal_color c) {
79 if (!color_enabled()) {
80 return s;
81 }
82
83 return fmt::format("\033[{}m{}\033[39m", static_cast<uint8_t>(c), s);
84 }
85
89 inline std::string error(std::string const& s) {
90 return styled(s, fmt::terminal_color::red);
91 }
92
96 inline std::string success(std::string const& s) {
97 return styled(s, fmt::terminal_color::green);
98 }
99
106 inline std::string emphasis(std::string const& s) {
107 if (!color_enabled()) {
108 return s;
109 }
110
111 return fmt::format("\033[1m{}\033[22m", s);
112 }
113
117 inline std::string muted(std::string const& s) {
118 return styled(s, fmt::terminal_color::bright_black);
119 }
120
124 inline std::string info(std::string const& s) {
125 return styled(s, fmt::terminal_color::cyan);
126 }
127
131 inline std::string warning(std::string const& s) {
132 return styled(s, fmt::terminal_color::yellow);
133 }
134
144 inline std::string link(std::string const& url, std::string const& text) {
145 if (!color_enabled()) {
146 return text;
147 }
148 // BEL terminator (rather than ST `ESC \`) — more widely accepted by
149 // terminals that implement only a subset of the OSC 8 spec.
150 return fmt::format("\x1b]8;;{}\x07{}\x1b]8;;\x07", url, text);
151 }
152}
TTY-aware ANSI colored output utilities.
Definition Color.hpp:31
std::string error(std::string const &s)
Format text in red (for error messages).
Definition Color.hpp:89
void init_color(bool no_color)
Initialize color support based on TTY detection and user preference.
Definition Color.hpp:49
bool & color_enabled()
Global toggle for colored output.
Definition Color.hpp:36
std::string info(std::string const &s)
Format text in cyan (for informational highlights like progress bars).
Definition Color.hpp:124
std::string link(std::string const &url, std::string const &text)
Wrap text in an OSC 8 hyperlink targeting url.
Definition Color.hpp:144
std::string emphasis(std::string const &s)
Format text in bold (for emphasis / labels).
Definition Color.hpp:106
std::string warning(std::string const &s)
Format text in yellow (for warnings).
Definition Color.hpp:131
std::string styled(std::string const &s, fmt::terminal_color c)
Apply a foreground color with a specific reset (\033[39m) instead of fmt's full reset,...
Definition Color.hpp:78
std::string success(std::string const &s)
Format text in green (for success messages).
Definition Color.hpp:96
std::string muted(std::string const &s)
Format text in dim gray (for hints and secondary info).
Definition Color.hpp:117
Definition IO.hpp:65