Skip to content

Commit

Permalink
?cxx/ClassSys.*xx: +classSysConsoleHasAnsiColors()
Browse files Browse the repository at this point in the history
	`SUSUWU_SH_RUNTIME_COLORS` will use this.
?`cxx/ClassSys.cxx`: ?`classSysInit`: if not `SUSUWU_SH_SKIP_COLORS`, warn unless `classSysConsoleHasAnsiColors()` is true.

?`README.md`: howto use `SUSUWU_SH_RUNTIME_COLORS`

Has to do with #17 (runtime detection of Command Sequence Introducer support for color/format codes)

Is followup to c97fa94 (?cxx/Macros.hxx +`SUSUWU_SH_RUNTIME_COLORS`)

TODO: posts/
  • Loading branch information
SwuduSusuwu committed Nov 28, 2024
1 parent a657e6f commit 6a8a3d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cxx/ClassSys.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const bool classSysInit(int argc, const char **args) {
assert(SUSUWU_NULLPTR != args[0]); /* `clangtidy` off: NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) */
return true;
}
#ifndef SUSUWU_SH_SKIP_COLORS
if(!classSysConsoleHasAnsiColors()) {
SUSUWU_WARNING("classSysInit() {(!classSysConsoleHasAnsiColors()) /* Command Sequence Introducers disabled */}")
}
#endif /* ndef SUSUWU_SH_SKIP_COLORS */
return false;
}

Expand Down Expand Up @@ -221,6 +226,37 @@ const unsigned char classSysGetConsoleAttributes() {
errno = ENOTTY;
return 0;
}
const bool classSysConsoleHasAnsiColors() {
#ifdef __WIN32__
# include <windows.h> /* CONSOLE_SCREEN_BUFFER_INFO DWORD GetConsoleMode GetStdHandle SetConsoleMode STD_OUTPUT_HANDLE */
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if(INVALID_HANDLE_VALUE == hConsole) {
SUSUWU_PRINT(WARNING, "classSysConsoleHasAnsiColors() {!GetConsoleScreenBufferInfo()}, GetLastError(): " SUSUWU_SH_PURPLE + std::to_string(GetLastError()));
return false;
}
DWORD mode;
if(!GetConsoleMode(hConsole, &mode)) {
return false;
}
SetConsoleMode(hConsole, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); /* virtual mode allows CSI colors */
return true;
#elif defined _POSIX_VERSION
return true;
#else
const char *term = getenv("TERM");
switch(std::string(term)) {
case "screen":
case "screen-256color":
case "vt100":
case "xterm":
case "xterm-256color":
return true;
/* case "dumb": */
default:
return false
}
#endif
}

static void classSysHexTests(const std::string &value) {
const size_t ss = classSysHexStr(value).size();
Expand Down
1 change: 1 addition & 0 deletions cxx/ClassSys.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const FILE *classSysFopenOwnPath() /* TODO: SUSUWU_NOEXCEPT(std::is_nothrow_invo
static const bool classSysGetConsoleInput() { return std::cin.good() && !std::cin.eof(); }
const bool classSysSetConsoleInput(bool input); /* Set to `false` for unit tests/background tasks (acts as if user pressed `<ctrl>+d`, thus input prompts will use default choices.) Returns `classSysGetConsoleInput();` */
const unsigned char classSysGetConsoleAttributes(); /* if(_WIN32 || ) { return (background * 16) + foreground color; } else if(_POSIX_SOURCE) { return "\033[%1;%2m" -> (%1 * 16) + %2 ; } else { return 0; } */
const bool classSysConsoleHasAnsiColors();

template<class Os, class Str>
inline Os &classSysHexOs(Os &os, const Str &value) {
Expand Down

0 comments on commit 6a8a3d6

Please sign in to comment.