From 008360df3639de74bc90c4b02e2acd18856fd214 Mon Sep 17 00:00:00 2001 From: Anton Anikin Date: Wed, 9 Jul 2025 22:01:45 +0800 Subject: [PATCH] option to enable/disable color output --- CMakeLists.txt | 3 +- src/array.cpp | 49 ++++++++++++++++++++++-- src/array.h | 3 ++ src/array_utils.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 src/array_utils.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b18080a..e03339f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.11) # FetchContent project(keycode) enable_language(CXX) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) include(FetchContent) include("cmake/Check-from_chars.cmake") @@ -44,6 +44,7 @@ endif() add_library(keycode STATIC src/array.cpp + src/array_utils.cpp src/keycode.c ) diff --git a/src/array.cpp b/src/array.cpp index c2035dc..9559e68 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -4,7 +4,7 @@ #include #include -#if USE_FAST_FLOAT +#if defined(USE_FAST_FLOAT) #include using fast_float::from_chars; using fast_float::from_chars_result; @@ -18,6 +18,39 @@ using std::from_chars_result; constexpr int PRINT_OFFSET = 30; +static bool colors_enabled = false; + +#if defined(_WIN32) +bool +terminal_is_cmd(); +#endif + +void +tak_setup(int _colors_enabled) +{ + if (_colors_enabled == 0) { + colors_enabled = false; + } + else if (_colors_enabled == 1) { + colors_enabled = true; + } + else { +#if defined(_WIN32) + colors_enabled = !terminal_is_cmd(); +#else + colors_enabled = true; +#endif + } + + fmt::print("# colors are "); + if (colors_enabled) { + fmt::print(fg(fmt::color::green), "enabled\n"); + } + else { + fmt::print("disabled\n"); + } +} + inline void putchar_n(int c, int count) { @@ -104,7 +137,12 @@ print_value(T value, T min, T max) str = fmt::format("{:.>{}}", str, PRINT_OFFSET); str = fmt::format("{} [{:g}, {:g}]", str, (double)min, (double)max); - fmt::print(fg(fmt::color::gray), str); + if (colors_enabled) { + fmt::print(fg(fmt::color::gray), "{}", str); + } + else { + fmt::print("{}", str); + } goback(str); } @@ -142,7 +180,12 @@ edit_value(T& value, T min, T max, int exist_key = -1) fmt::print("{} ", str); } else { - fmt::print(fg(fmt::color::red), "{} ", str); + if (colors_enabled) { + fmt::print(fg(fmt::color::red), "{} ", str); + } + else { + fmt::print("{} ", str); + } } goback(1); diff --git a/src/array.h b/src/array.h index a51c01c..a685b84 100644 --- a/src/array.h +++ b/src/array.h @@ -4,6 +4,9 @@ extern "C" { #endif +void +tak_setup(int colors_enabled); + void taklong(long*); diff --git a/src/array_utils.cpp b/src/array_utils.cpp new file mode 100644 index 0000000..2d0fdf5 --- /dev/null +++ b/src/array_utils.cpp @@ -0,0 +1,91 @@ +#if defined(_WIN32) + +#include +#include + +#include +// wtf ? include must be below +// tested on MSVC 19.44.35211 +#include + +// https://stackoverflow.com/questions/29939893/get-parent-process-name-windows +// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-queryfullprocessimagenamea + +static DWORD +get_parent_pid(DWORD pid) +{ + DWORD ppid = 0; + HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + + PROCESSENTRY32 pe; + pe.dwSize = sizeof(PROCESSENTRY32); + + if (Process32First(h, &pe)) { + do { + if (pe.th32ProcessID == pid) { + ppid = pe.th32ParentProcessID; + break; + } + } while (Process32Next(h, &pe)); + } + CloseHandle(h); + + return ppid; +} + +static DWORD +get_process_name(DWORD pid, LPSTR exe_path, PDWORD exe_path_size) +{ + HANDLE h = NULL; + DWORD e = 0; + + h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); + if (h) { + if (QueryFullProcessImageNameA(h, 0, exe_path, exe_path_size) == 0) { + e = GetLastError(); + } + CloseHandle(h); + } + else { + e = GetLastError(); + } + + return e; +} + +bool +terminal_is_cmd() +{ + char* exe_path = new char[MAX_PATH]; + DWORD exe_path_size = MAX_PATH; + + DWORD ppid = GetCurrentProcessId(); + DWORD error = 0; + + while (ppid != 0) { + DWORD pid = ppid; + ppid = get_parent_pid(pid); + + exe_path_size = MAX_PATH; + error = get_process_name(ppid, exe_path, &exe_path_size); + + // fmt::print( + // "PID={}; PPID={}; Err={}; EXE='{}'\n", + // pid, + // ppid, + // error, + // exe_path); + + std::string_view path(exe_path); + if (path.ends_with("\\WindowsTerminal.exe") + || path.ends_with("\\ConEmuC64.exe") + || path.ends_with("\\ConEmuC.exe")) + { + return false; + } + } + + return true; +} + +#endif