1#ifndef KOUTIL_TERM_TERMINAL_H
2#define KOUTIL_TERM_TERMINAL_H
17 #include <sys/ioctl.h>
18 #include <sys/select.h>
21#elif defined(OS_WINDOWS)
22 #define WIN32_LEAN_AND_MEAN
129 using namespace std::string_view_literals;
138#if defined(OS_WINDOWS)
142 if (colorterm.find(
"24bit") != std::string_view::npos || colorterm.find(
"truecolor") != std::string_view::npos) {
147 if (colorterm.find(
"256") != std::string_view::npos || term.find(
"256") != std::string_view::npos) {
152#if defined(OS_WINDOWS)
153 HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
154 HANDLE console_in = GetStdHandle(STD_INPUT_HANDLE);
159 if (SetConsoleOutputCP(CP_UTF8) ==
false) {
164 if (console_in == INVALID_HANDLE_VALUE || !GetConsoleMode(console_in, &mode_in)) {
169 if (console_out == INVALID_HANDLE_VALUE || !GetConsoleMode(console_out, &mode_out)) {
174 s_instance->m_on_exit.push([=] { SetConsoleMode(console_in, mode_in); });
175 s_instance->m_on_exit.push([=] { SetConsoleMode(console_out, mode_out); });
177 mode_out |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
178 mode_in |= ENABLE_VIRTUAL_TERMINAL_INPUT | ENABLE_WINDOW_INPUT;
180 if (!SetConsoleMode(console_in, mode_in) || !SetConsoleMode(console_out, mode_out)) {
205 auto add_signal_handler = [](
int sig) {
207 s_instance->m_on_exit.emplace([=]() { std::signal(sig, old_handler); });
210 for (
auto&& sig : { SIGTERM, SIGSEGV, SIGINT, SIGILL, SIGABRT, SIGFPE }) {
211 add_signal_handler(sig);
215 add_signal_handler(SIGQUIT);
242 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
246 return { .
width = ws.ws_col, .height = ws.ws_row };
248#elif defined(OS_WINDOWS)
249 CONSOLE_SCREEN_BUFFER_INFO csbi;
250 HANDLE console_out = GetStdHandle(STD_OUTPUT_HANDLE);
252 if (console_out == INVALID_HANDLE_VALUE || !GetConsoleScreenBufferInfo(console_out, &csbi)) {
256 return { .width = csbi.srWindow.Right - csbi.srWindow.Left + 1,
257 .height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1 };
static std::unique_ptr< terminal > s_instance
Definition terminal.h:123
std::stack< std::function< void()> > m_on_exit
Definition terminal.h:105
static Dimensions query_dimensions()
Queries the dimensions of the terminal window.
Definition terminal.h:237
static Error error()
Retrieves the current error state of the terminal.
Definition terminal.h:227
static bool has_error()
Checks if the terminal has encountered an error.
Definition terminal.h:232
bool m_has_signals
Definition terminal.h:107
static void handle_exit_signal(int)
Definition terminal.h:121
ColorSupport m_color_support
Definition terminal.h:108
void exit()
Definition terminal.h:112
~terminal()
Definition terminal.h:102
static void rollback()
Rolls back changes made to the terminal.
Definition terminal.h:220
static bool init()
Initializes the terminal.
Definition terminal.h:128
static ColorSupport color_support()
Retrieves the level of color support for the terminal.
Definition terminal.h:222
static void register_signals()
Registers signal handlers for the terminal.
Definition terminal.h:197
Error
Definition terminal.h:53
Error m_error
Definition terminal.h:106
std::ostream & reset_all(std::ostream &stream)
Resets all text styles and colors to default.
Definition style.h:109
ColorSupport
Enumerates the levels of color support for the terminal.
Definition terminal.h:37
std::string_view safe_getenv(const char *name)
Definition utils.h:22
Represents the dimensions of the terminal buffer.
Definition terminal.h:46
std::size_t height
Definition terminal.h:48
std::size_t width
Definition terminal.h:47