8 #include <unordered_set>
10 #define SHELL_FONT_SET_OPT_END "\033[0m"
12 #ifndef UTIL_STRFUNC_STRCASE_CMP
13 # if defined(_MSC_VER) && _MSC_VER >= 1600
14 # define UTIL_STRFUNC_STRCASE_CMP(l, r) _stricmp(l, r)
15 # define UTIL_STRFUNC_STRNCASE_CMP(l, r, s) _strnicmp(l, r, s)
16 # define UTIL_STRFUNC_STRCMP(l, r) strcmp(l, r)
17 # define UTIL_STRFUNC_STRNCMP(l, r, s) strncmp(l, r, s)
19 # define UTIL_STRFUNC_STRCASE_CMP(l, r) strcasecmp(l, r)
20 # define UTIL_STRFUNC_STRNCASE_CMP(l, r, s) strncasecmp(l, r, s)
21 # define UTIL_STRFUNC_STRCMP(l, r) strcmp(l, r)
22 # define UTIL_STRFUNC_STRNCMP(l, r, s) strncmp(l, r, s)
31 if (c >=
'A' && c <=
'Z') {
38 static std::string
getenv(
const char *name) {
40 #if (defined(_MSC_VER) && _MSC_VER >= 1600) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
42 if (0 != getenv_s(&len,
nullptr, 0, name)) {
50 if (0 != getenv_s(&len, &ret[0], ret.size(), name)) {
55 while (!ret.empty() && ret[ret.size() - 1] ==
'\0') {
66 while (!ret.empty() && ret[ret.size() - 1] ==
'\0') {
91 ret += std::string((!bFirst) ?
";" :
"") +
"4";
95 ret += std::string((!bFirst) ?
";" :
"") +
"5";
99 ret += std::string((!bFirst) ?
";" :
"") +
"2";
106 std::string base =
"30";
108 for (; iStart < 8 && !(iFlag & (1 << iStart)); ++iStart)
110 if (iStart < 8) base[1] +=
static_cast<char>(iStart);
111 ret += std::string((!bFirst) ?
";" :
"") + base;
118 std::string base =
"40";
120 for (; iStart < 8 && !(iFlag & (1 << iStart)); ++iStart)
122 if (iStart < 8) base[1] +=
static_cast<char>(iStart);
123 ret += std::string((!bFirst) ?
";" :
"") + base;
137 std::unordered_set<std::string> color_term;
138 color_term.insert(
"eterm");
139 color_term.insert(
"ansi");
140 color_term.insert(
"color-xterm");
141 color_term.insert(
"con132x25");
142 color_term.insert(
"con132x30");
143 color_term.insert(
"con132x43");
144 color_term.insert(
"con132x60");
145 color_term.insert(
"con80x25");
146 color_term.insert(
"con80x28");
147 color_term.insert(
"con80x30");
148 color_term.insert(
"con80x43");
149 color_term.insert(
"con80x50");
150 color_term.insert(
"con80x60");
151 color_term.insert(
"cons25");
152 color_term.insert(
"console");
153 color_term.insert(
"cygwin");
154 color_term.insert(
"dtterm");
155 color_term.insert(
"eterm-color");
156 color_term.insert(
"gnome");
157 color_term.insert(
"jfbterm");
158 color_term.insert(
"konsole");
159 color_term.insert(
"kterm");
160 color_term.insert(
"linux");
161 color_term.insert(
"linux-c");
162 color_term.insert(
"mach-color");
163 color_term.insert(
"mlterm");
164 color_term.insert(
"putty");
165 std::unordered_set<std::string> color_term_prefix;
166 color_term_prefix.insert(
"xterm");
167 color_term_prefix.insert(
"screen");
168 color_term_prefix.insert(
"vt100");
169 color_term_prefix.insert(
"vt220");
170 color_term_prefix.insert(
"rxvt");
174 if (term_name.empty()) {
178 std::transform(term_name.begin(), term_name.end(), term_name.begin(),
detail::tolower);
180 if (color_term.end() != color_term.find(term_name)) {
183 for (
auto &prefix : color_term_prefix) {
190 if (term_name.size() >= 4 && 0 ==
UTIL_STRFUNC_STRNCMP(term_name.c_str() + term_name.size() - 4,
"-256", 4)) {
194 if (term_name.size() >= 9 && 0 ==
UTIL_STRFUNC_STRNCMP(term_name.c_str() + term_name.size() - 9,
"-256color", 9)) {
202 if (!tf_build.empty() && !agent_name.empty()) {
213 if (term_program.empty()) {
216 std::transform(term_program.begin(), term_program.end(), term_program.begin(),
detail::tolower);
217 if (term_program.find(
"apple_terminal") != std::string::npos ||
218 term_program.find(
"iterm.app") != std::string::npos) {
229 const char *known_ci_names[] = {
"travis",
"circleci",
"appveyor",
"gitlab_ci",
230 "github_actions",
"buildkite",
"drone"};
231 for (
auto known_ci_name : known_ci_names) {
238 if (ci_name.empty()) {
250 static int status_ = 0;
256 if (status_ < 0 || iFlag == 0)
return strInput;
262 #ifdef SHELL_FONT_USING_WIN32_CONSOLE
264 static std::map<int, WORD> &_get_flag_mapping() {
265 static std::map<int, WORD> ret;
281 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
296 static WORD _get_default_color() {
return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; }
301 #ifdef SHELL_FONT_USING_WIN32_CONSOLE
302 if (os == &std::cout) {
303 hOsHandle = GetStdHandle(STD_OUTPUT_HANDLE);
304 }
else if (os == &std::cerr) {
305 hOsHandle = GetStdHandle(STD_ERROR_HANDLE);
313 if (
nullptr == pOs) {
325 #ifdef SHELL_FONT_USING_WIN32_CONSOLE
326 hOsHandle = other.hOsHandle;
374 if (
nullptr == pOs) {
382 #ifdef SHELL_FONT_USING_WIN32_CONSOLE
383 if (
nullptr != hOsHandle) {
384 std::map<int, WORD> &color_map = _get_flag_mapping();
386 int left_flag = flag;
389 int f = left_flag & (left_flag ^ (left_flag - 1));
390 std::map<int, WORD>::iterator iter = color_map.find(f);
391 if (iter != color_map.end()) {
392 style |= iter->second;
395 left_flag = left_flag & (left_flag - 1);
398 SetConsoleTextAttribute(hOsHandle, style);
408 if (
nullptr == pOs) {
414 #ifdef SHELL_FONT_USING_WIN32_CONSOLE
415 if (
nullptr != hOsHandle) {
416 SetConsoleTextAttribute(hOsHandle, _get_default_color());
static std::string GetStyleCloseCode()
std::string GenerateString(const std::string &strInput)
std::string GetStyleCode()
shell_stream_opr & operator=(const shell_stream_opr &)
shell_stream_opr(const shell_stream_opr &)
const shell_stream_opr & operator<<(const Ty &v) const
const shell_stream_opr & open(int flag) const
shell_stream_opr operator()() const
shell_stream(stream_t &stream=std::cout)
static char tolower(char c)
static std::string getenv(const char *name)
static int _check_term_color_status()
#define UTIL_STRFUNC_STRNCMP(l, r, s)
#define SHELL_FONT_SET_OPT_END
#define UTIL_STRFUNC_STRCASE_CMP(l, r)
@ SHELL_FONT_SPEC_UNDERLINE
@ SHELL_FONT_COLOR_YELLOW
@ SHELL_FONT_COLOR_MAGENTA
shell_font_background_color
@ SHELL_FONT_BACKGROUND_COLOR_BLACK
@ SHELL_FONT_BACKGROUND_COLOR_BLUE
@ SHELL_FONT_BACKGROUND_COLOR_MAGENTA
@ SHELL_FONT_BACKGROUND_COLOR_YELLOW
@ SHELL_FONT_BACKGROUND_COLOR_RED
@ SHELL_FONT_BACKGROUND_COLOR_GREEN
@ SHELL_FONT_BACKGROUND_COLOR_CYAN
@ SHELL_FONT_BACKGROUND_COLOR_WHITE