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() {
45 int tty = open(
"/dev/tty", O_RDWR);
50 tcgetattr(tty, &oldt);
53 newt.c_lflag &= ~(ICANON | ECHO);
55 tcsetattr(tty, TCSANOW, &newt);
57 int oldf = fcntl(tty, F_GETFL, 0);
58 fcntl(tty, F_SETFL, oldf | O_NONBLOCK);
61 write(tty,
"\033]11;?\033\\", 8);
71 FD_SET(tty, &readfds);
76 int ret = select(tty + 1, &readfds,
nullptr,
nullptr, &tv);
81 if (read(tty, &ch, 1) > 0) {
88 tcsetattr(tty, TCSANOW, &oldt);
89 fcntl(tty, F_SETFL, oldf);
95float UtilsOSDependable::get_terminal_bg_color_luminance() {
96 if (!isatty(fileno(stdout)) || !isatty(fileno(stdin))) {
100 std::string color_str = capture_terminal_response();
101 auto [r, g, b] = parse_terminal_color_response(color_str);
102 float luminance = 0.2126 * (r / 255.0) + 0.7152 * (g / 255.0) + 0.0722 * (b / 255.0);
108 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)