cpp-terminal
cpp-terminal copied to clipboard
Find background color
I am trying to write a small helper to find the background / foreground colors of a terminal.
I can get hexadecimal values by asking echo "\e]11;?\a"
I tried to implement this:
void Term::get_background_color() {
char buf[32];
write("\e]11;?\a");
for (unsigned int i = 0; i < sizeof(buf) - 1; i++) {
while (!Private::read_raw(&buf[i]))
;
std::cout << "read [" << i << "] " << buf[i] << std::endl;
}
}
But it seems to block. Any ideas? @certik this is for mamba/micromamba progress bars :)
There is a way to do it, let me investigate.
Actually, if I use a Terminal with enable_keyboard = true
this code seems to work decently well!
void Term::get_background_color() {
char buf[24];
write("\e]11;?\a");
for (unsigned int i = 0; i < sizeof(buf) - 1; i++) {
while (!Private::read_raw(&buf[i]))
;
}
std::string color(buf, 23);
std::cout << "Received background color of : " << color.substr(1, color.size()) << std::endl;
if (color.substr(0, 9) != "\x1B]11;rgb:") {
throw std::runtime_error("Did not receive a color");
}
color = color.substr(9, color.size());
std::string r, g, b;
std::size_t split1, split2;
split1 = color.find_first_of("/");
r = color.substr(0, split1);
split1++;
split2 = color.find_first_of("/", split1);
g = color.substr(split1, split2 - split1);
b = color.substr(split2 + 1, color.size() - split2 + 1);
std::cout << "R: " << r << std::endl;
std::cout << "G: " << g << std::endl;
std::cout << "B: " << b << std::endl;
}
Yes, I think that was it. Sorry I didn't have time yesterday to look deeper.
I think we should add this to the library as extra feature. I'm adding it to the roadmap. Would be nice if we merge the open PR first.