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') {
32 return static_cast<char>(c -
'A' +
'a');
38static 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') {
38static std::string
getenv(
const char *name) {
…}
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);
109 if (iStart < 8) base[1] =
static_cast<char>(base[1] + iStart);
110 ret += std::string((!bFirst) ?
";" :
"") + base;
117 std::string base =
"40";
119 for (; iStart < 8 && !(iFlag & (1 << iStart)); ++iStart);
120 if (iStart < 8) base[1] =
static_cast<char>(base[1] + iStart);
121 ret += std::string((!bFirst) ?
";" :
"") + base;
135 std::unordered_set<std::string> color_term;
136 color_term.insert(
"eterm");
137 color_term.insert(
"ansi");
138 color_term.insert(
"color-xterm");
139 color_term.insert(
"con132x25");
140 color_term.insert(
"con132x30");
141 color_term.insert(
"con132x43");
142 color_term.insert(
"con132x60");
143 color_term.insert(
"con80x25");
144 color_term.insert(
"con80x28");
145 color_term.insert(
"con80x30");
146 color_term.insert(
"con80x43");
147 color_term.insert(
"con80x50");
148 color_term.insert(
"con80x60");
149 color_term.insert(
"cons25");
150 color_term.insert(
"console");
151 color_term.insert(
"cygwin");
152 color_term.insert(
"dtterm");
153 color_term.insert(
"eterm-color");
154 color_term.insert(
"gnome");
155 color_term.insert(
"jfbterm");
156 color_term.insert(
"konsole");
157 color_term.insert(
"kterm");
158 color_term.insert(
"linux");
159 color_term.insert(
"linux-c");
160 color_term.insert(
"mach-color");
161 color_term.insert(
"mlterm");
162 color_term.insert(
"putty");
163 std::unordered_set<std::string> color_term_prefix;
164 color_term_prefix.insert(
"xterm");
165 color_term_prefix.insert(
"screen");
166 color_term_prefix.insert(
"vt100");
167 color_term_prefix.insert(
"vt220");
168 color_term_prefix.insert(
"rxvt");
172 if (term_name.empty()) {
176 std::transform(term_name.begin(), term_name.end(), term_name.begin(),
detail::tolower);
178 if (color_term.end() != color_term.find(term_name)) {
181 for (
auto &prefix : color_term_prefix) {
188 if (term_name.size() >= 4 && 0 ==
UTIL_STRFUNC_STRNCMP(term_name.c_str() + term_name.size() - 4,
"-256", 4)) {
192 if (term_name.size() >= 9 && 0 ==
UTIL_STRFUNC_STRNCMP(term_name.c_str() + term_name.size() - 9,
"-256color", 9)) {
200 if (!tf_build.empty() && !agent_name.empty()) {
211 if (term_program.empty()) {
214 std::transform(term_program.begin(), term_program.end(), term_program.begin(),
detail::tolower);
215 if (term_program.find(
"apple_terminal") != std::string::npos ||
216 term_program.find(
"iterm.app") != std::string::npos) {
227 const char *known_ci_names[] = {
"travis",
"circleci",
"appveyor",
"gitlab_ci",
228 "github_actions",
"buildkite",
"drone"};
229 for (
auto known_ci_name : known_ci_names) {
236 if (ci_name.empty()) {
248 static int status_ = 0;
254 if (status_ < 0 || iFlag == 0)
return strInput;
260#ifdef SHELL_FONT_USING_WIN32_CONSOLE
262static std::map<int, WORD> &_get_flag_mapping() {
263 static std::map<int, WORD> ret;
279 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
294static WORD _get_default_color() {
return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; }
299#ifdef SHELL_FONT_USING_WIN32_CONSOLE
300 if (os == &std::cout) {
301 hOsHandle = GetStdHandle(STD_OUTPUT_HANDLE);
302 }
else if (os == &std::cerr) {
303 hOsHandle = GetStdHandle(STD_ERROR_HANDLE);
311 if (
nullptr == pOs) {
323#ifdef SHELL_FONT_USING_WIN32_CONSOLE
324 hOsHandle = other.hOsHandle;
372 if (
nullptr == pOs) {
380#ifdef SHELL_FONT_USING_WIN32_CONSOLE
381 if (
nullptr != hOsHandle) {
382 std::map<int, WORD> &color_map = _get_flag_mapping();
384 int left_flag = flag;
387 int f = left_flag & (left_flag ^ (left_flag - 1));
388 std::map<int, WORD>::iterator iter = color_map.find(f);
389 if (iter != color_map.end()) {
390 style |= iter->second;
393 left_flag = left_flag & (left_flag - 1);
396 SetConsoleTextAttribute(hOsHandle, style);
406 if (
nullptr == pOs) {
412#ifdef SHELL_FONT_USING_WIN32_CONSOLE
413 if (
nullptr != hOsHandle) {
414 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 & open(int flag) const
const shell_stream_opr & operator<<(const Ty &v) 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