cli20 0.1.2
A C++20 typed-schema command-line parser
Loading...
Searching...
No Matches
color.hh
Go to the documentation of this file.
1#pragma once
2
3#ifdef _WIN32
4#include <io.h>
5#else
6#include <unistd.h>
7#endif
8
9#include <string_view>
10
11namespace cli {
12
20enum class ColorMode { detect, never, always };
21
29 std::string_view usage{"\033[1m"};
30 std::string_view heading{"\033[1;4;36m"};
31 std::string_view option{"\033[1;32m"};
32 std::string_view metavar{"\033[36m"};
33 std::string_view command{"\033[1;33m"};
34 std::string_view metadata{"\033[2m"};
35 std::string_view group{"\033[1;33m"};
36 std::string_view reset{"\033[0m"};
37};
38
40
52
54inline constexpr RecurseHelpTag recurse_help{};
55
56namespace detail {
57
65struct AnsiStyle {
67 bool enabled;
68 HelpPalette palette{};
69
75 [[nodiscard]] constexpr auto bold() const noexcept -> std::string_view {
76 return enabled ? "\033[1m" : "";
77 }
78
85 [[nodiscard]] constexpr auto underline() const noexcept -> std::string_view {
86 return enabled ? "\033[4m" : "";
87 }
88
94 [[nodiscard]] constexpr auto reset() const noexcept -> std::string_view {
95 return enabled ? palette.reset : "";
96 }
97
98 [[nodiscard]] constexpr auto usage() const noexcept -> std::string_view {
99 return enabled ? palette.usage : "";
100 }
101
102 [[nodiscard]] constexpr auto heading() const noexcept -> std::string_view {
103 return enabled ? palette.heading : "";
104 }
105
106 [[nodiscard]] constexpr auto option() const noexcept -> std::string_view {
107 return enabled ? palette.option : "";
108 }
109
110 [[nodiscard]] constexpr auto metavar() const noexcept -> std::string_view {
111 return enabled ? palette.metavar : "";
112 }
113
114 [[nodiscard]] constexpr auto command() const noexcept -> std::string_view {
115 return enabled ? palette.command : "";
116 }
117
118 [[nodiscard]] constexpr auto metadata() const noexcept -> std::string_view {
119 return enabled ? palette.metadata : "";
120 }
121
122 [[nodiscard]] constexpr auto group() const noexcept -> std::string_view {
123 return enabled ? palette.group : "";
124 }
125};
126
135inline auto is_tty() noexcept -> bool {
136#ifdef _WIN32
137 return _isatty(1) != 0;
138#else
139 return ::isatty(STDOUT_FILENO) != 0;
140#endif
141}
142
153inline auto resolve_color(ColorMode mode,
154 HelpPalette palette = default_help_palette) noexcept
155 -> AnsiStyle {
156 const bool on =
157 (mode == ColorMode::always) || (mode == ColorMode::detect && is_tty());
158 return AnsiStyle{.enabled = on, .palette = palette};
159}
160
161} // namespace detail
162} // namespace cli
Definition action.hh:29
constexpr HelpPalette default_help_palette
Definition color.hh:39
constexpr RecurseHelpTag recurse_help
Convenience instance of RecurseHelpTag.
Definition color.hh:54
ColorMode
Controls whether ANSI escape codes are emitted by format_help().
Definition color.hh:20
ANSI palette used by generated help output.
Definition color.hh:28
std::string_view usage
Definition color.hh:29
std::string_view metavar
Definition color.hh:32
std::string_view option
Definition color.hh:31
std::string_view group
Definition color.hh:35
std::string_view metadata
Definition color.hh:34
std::string_view reset
Definition color.hh:36
std::string_view heading
Definition color.hh:30
std::string_view command
Definition color.hh:33
Tag type passed to format_help() to request recursive sub-command output.
Definition color.hh:51