option to enable/disable color output
This commit is contained in:
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.11) # FetchContent
|
|||||||
project(keycode)
|
project(keycode)
|
||||||
|
|
||||||
enable_language(CXX)
|
enable_language(CXX)
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
include("cmake/Check-from_chars.cmake")
|
include("cmake/Check-from_chars.cmake")
|
||||||
@@ -44,6 +44,7 @@ endif()
|
|||||||
|
|
||||||
add_library(keycode STATIC
|
add_library(keycode STATIC
|
||||||
src/array.cpp
|
src/array.cpp
|
||||||
|
src/array_utils.cpp
|
||||||
src/keycode.c
|
src/keycode.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
#if USE_FAST_FLOAT
|
#if defined(USE_FAST_FLOAT)
|
||||||
#include <fast_float/fast_float.h>
|
#include <fast_float/fast_float.h>
|
||||||
using fast_float::from_chars;
|
using fast_float::from_chars;
|
||||||
using fast_float::from_chars_result;
|
using fast_float::from_chars_result;
|
||||||
@@ -18,6 +18,39 @@ using std::from_chars_result;
|
|||||||
|
|
||||||
constexpr int PRINT_OFFSET = 30;
|
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
|
inline void
|
||||||
putchar_n(int c, int count)
|
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("{:.>{}}", str, PRINT_OFFSET);
|
||||||
str = fmt::format("{} [{:g}, {:g}]", str, (double)min, (double)max);
|
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);
|
goback(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +180,12 @@ edit_value(T& value, T min, T max, int exist_key = -1)
|
|||||||
fmt::print("{} ", str);
|
fmt::print("{} ", str);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fmt::print(fg(fmt::color::red), "{} ", str);
|
if (colors_enabled) {
|
||||||
|
fmt::print(fg(fmt::color::red), "{} ", str);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fmt::print("{} ", str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
goback(1);
|
goback(1);
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void
|
||||||
|
tak_setup(int colors_enabled);
|
||||||
|
|
||||||
void
|
void
|
||||||
taklong(long*);
|
taklong(long*);
|
||||||
|
|
||||||
|
|||||||
91
src/array_utils.cpp
Normal file
91
src/array_utils.cpp
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
#if defined(_WIN32)
|
||||||
|
|
||||||
|
#include <fmt/base.h>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
// wtf ? <tlhelp32.h> include must be below <windows.h>
|
||||||
|
// tested on MSVC 19.44.35211
|
||||||
|
#include <tlhelp32.h>
|
||||||
|
|
||||||
|
// 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
|
||||||
Reference in New Issue
Block a user