6#include <sys/utsname.h>
11 unsigned long value = std::stoul(hex16,
nullptr, 16);
12 return static_cast<int>(value / 256);
18std::tuple<int, int, int> UtilsOSDependable::parse_terminal_color_response(
const std::string& response) {
19 const size_t start_pos = response.find(
"rgb:");
20 if (start_pos == std::string::npos) {
24 const size_t end_pos = response.find(
"\033\\", start_pos);
25 if (end_pos == std::string::npos) {
29 std::string rgb_str = response.substr(start_pos + 4, end_pos - (start_pos + 4));
31 replace(rgb_str.begin(), rgb_str.end(),
'/',
' ');
33 std::istringstream iss(rgb_str);
34 std::string r_hex, g_hex, b_hex;
35 iss >> r_hex >> g_hex >> b_hex;
44std::string UtilsOSDependable::capture_terminal_response() {
46 tcgetattr(STDIN_FILENO, &oldt);
48 newt.c_lflag &= ~(ICANON | ECHO);
49 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
51 int oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
52 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
54 std::cout <<
"\033]11;?\033\\";
65 FD_SET(STDIN_FILENO, &readfds);
67 while (select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv) > 0) {
68 if (read(STDIN_FILENO, &ch, 1) > 0) {
75 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
76 fcntl(STDIN_FILENO, F_SETFL, oldf);
81float UtilsOSDependable::get_terminal_bg_color_luminance() {
82 if (!isatty(fileno(stdout)) || !isatty(fileno(stdin))) {
86 std::string color_str = capture_terminal_response();
87 auto [r, g, b] = parse_terminal_color_response(color_str);
88 float luminance = 0.2126 * (r / 255.0) + 0.7152 * (g / 255.0) + 0.0722 * (b / 255.0);
94 return get_terminal_bg_color_luminance() <= 0.5;
static int run_process(const char *cmd, char *const argv[])
Executes an external program, replacing the current process (exec-style).
static bool is_bg_color_dark()
Determines if terminal background color is dark.
int convert_16_bit_to_8_bit(const std::string &hex16)