20std::pair<int, int>
size();
25std::pair<int, int>
size(std::FILE* stream);
34int rows(std::FILE* stream);
54 if (
char const* no_color = std::getenv(
"NO_COLOR"))
56 return no_color[0] !=
'\0';
64 constexpr std::string_view safe_chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@%+=:,./-";
67 s.empty() || std::ranges::any_of(s, [&](
char c) {
return safe_chars.find(
c) == std::string_view::npos; });
73 result.reserve(s.size() * 2);
78 result.append(
"\\\\");
80 else if (
c ==
'"' ||
c ==
'$' ||
c ==
'`')
82 result.push_back(
'\\');
90 return "\"" + result +
"\"";
92 return std::string(s);
96inline std::string
cmdline(
int argc,
char** argv)
100 for (
int i = 1; i < argc; ++i)
108inline bool env_exists(std::string
const& str) {
return getenv(str.c_str()) !=
nullptr; }
128template <
typename T,
typename U> T
getenv_or_default(
const std::string& var,
const U& default_value)
130 if (
const char* env = std::getenv(var.c_str()))
134 return from_string<T>(env);
141 return T(default_value);
146 char const* Str = getenv(str.c_str());
199using FGColor = std::variant<ForegroundColor, RGBColor>;
200using BGColor = std::variant<BackgroundColor, RGBColor>;
214 return static_cast<ColorAttribute>(
static_cast<unsigned>(lhs) |
static_cast<unsigned>(rhs));
238 toggle(std::string_view str,
bool default_value =
true)
242 value = default_value;
254 value = default_value;
259 operator bool()
const {
return value; }
265 std::optional<FGColor>
fg;
266 std::optional<BGColor>
bg;
296 if (other.
fg.has_value()) result.
fg = other.
fg;
297 if (other.
bg.has_value()) result.
bg = other.
bg;
311 return "38;2;" + std::to_string(rgb.
r) +
";" + std::to_string(rgb.
g) +
";" + std::to_string(rgb.
b);
315 return "48;2;" + std::to_string(rgb.
r) +
";" + std::to_string(rgb.
g) +
";" + std::to_string(rgb.
b);
319 [[nodiscard]] std::string
to_string()
const;
347 return ts.
to_string() + std::string(s) +
"\033[0m";
352 return b ? (ts.
to_string() + s +
"\033[0m") : std::string(s);
Definition terminal.hpp:262
TerminalStyle(char const *s)
Definition terminal.hpp:288
std::optional< FGColor > fg
Definition terminal.hpp:265
TerminalStyle(ForegroundColor f)
Definition terminal.hpp:272
static std::string rgb_bg_code(const RGBColor &rgb)
Definition terminal.hpp:313
friend TerminalStyle operator|(ForegroundColor f, const TerminalStyle &ts)
Definition terminal.hpp:303
TerminalStyle(const RGBColor &rgb, bool foreground=true)
Definition terminal.hpp:277
friend TerminalStyle operator|(const RGBColor &rgb, const TerminalStyle &ts)
Definition terminal.hpp:306
TerminalStyle(std::string_view sv)
Definition terminal.hpp:290
TerminalStyle operator|(const TerminalStyle &other) const
Definition terminal.hpp:293
friend TerminalStyle operator|(ColorAttribute a, const TerminalStyle &ts)
Definition terminal.hpp:305
TerminalStyle(BackgroundColor b)
Definition terminal.hpp:273
TerminalStyle(ColorAttribute a)
Definition terminal.hpp:274
std::optional< BGColor > bg
Definition terminal.hpp:266
ColorAttribute attrs
Definition terminal.hpp:267
std::string to_string() const
Definition terminal.cpp:34
friend TerminalStyle operator|(BackgroundColor b, const TerminalStyle &ts)
Definition terminal.hpp:304
static std::string rgb_fg_code(const RGBColor &rgb)
Definition terminal.hpp:309
Definition terminal.cpp:32
bool is_a_terminal(std::FILE *stream)
Test whether a C output stream refers to a terminal.
Definition terminal.cpp:508
T getenv_or_default(const std::string &var, const U &default_value)
Definition terminal.hpp:128
int columns()
Return the output terminal column count.
Definition terminal.cpp:502
constexpr ColorAttribute operator|(ColorAttribute lhs, ColorAttribute rhs)
Definition terminal.hpp:212
ForegroundColor
Definition terminal.hpp:152
std::string color_if(std::string_view s, bool b, const TerminalStyle &ts)
Definition terminal.hpp:350
TerminalStyle parseTerminalStyle(const std::string &styleStr)
Definition terminal.cpp:346
std::string quote_shell(std::string_view s)
Definition terminal.hpp:61
std::variant< ForegroundColor, RGBColor > FGColor
Definition terminal.hpp:199
ColorAttribute
Definition terminal.hpp:204
bool env_exists(std::string const &str)
Definition terminal.hpp:108
std::string color_text(std::string_view s, const TerminalStyle &ts)
Definition terminal.hpp:345
std::string cmdline(int argc, char **argv)
Definition terminal.hpp:96
BackgroundColor
Definition terminal.hpp:173
std::string expand_environment(std::string const &s)
Definition terminal.cpp:515
bool no_color_requested()
Return whether NO_COLOR requests suppressing default ANSI color output.
Definition terminal.hpp:52
std::pair< int, int > size()
Return the output terminal size as (rows, columns).
Definition terminal.cpp:506
std::variant< BackgroundColor, RGBColor > BGColor
Definition terminal.hpp:200
constexpr ColorAttribute & operator|=(ColorAttribute &lhs, ColorAttribute rhs)
Definition terminal.hpp:218
int rows()
Return the output terminal row count.
Definition terminal.cpp:491
bool iequals(std::string_view a, std::string_view b)
Definition string_util.hpp:52
Definition terminal.hpp:195
int b
Definition terminal.hpp:196
int g
Definition terminal.hpp:196
int r
Definition terminal.hpp:196
A simple yes/no toggle that parses from a string.
Definition terminal.hpp:230
toggle(bool value_)
Definition terminal.hpp:233
toggle(std::string_view str, bool default_value=true)
Construct from a string (e.g.
Definition terminal.hpp:238
bool value
Definition terminal.hpp:231