Low-level terminal I/O abstraction. More...
#include <terminal.h>
Public Types | |
| enum | ColorSupport { NoColor , Grayscale16 , Grayscale256 , GrayscaleTrue , Basic , Color256 , TrueColor } |
| Describes the color capability level of the terminal. More... | |
| using | ResizeCallback = std::function< void(int cols, int rows)> |
| Callback type for window resize notifications. | |
Public Member Functions | |
| Terminal () | |
| Constructs a Terminal and saves the current terminal state. | |
| ~Terminal () | |
| Destructor. Restores terminal state and cleans up. | |
| Terminal (const Terminal &)=delete | |
| Terminal & | operator= (const Terminal &)=delete |
| Error | enableRawMode () |
| Enables raw terminal mode (disables line buffering, echo, etc). | |
| Error | disableRawMode () |
| Restores the terminal to its original mode. | |
| bool | isRawMode () const |
| Returns true if raw mode is currently enabled. | |
| Result< int > | readInput (char *buf, int maxLen) |
| Reads available input bytes without blocking. | |
| Error | windowSize (int &cols, int &rows) const |
| Queries the current terminal window size. | |
| Size2Di32 | size () const |
| Returns the terminal size as a Size2Du32 (width x height). | |
| Error | enableMouseTracking () |
| Enables xterm SGR mouse tracking. | |
| Error | disableMouseTracking () |
| Disables mouse tracking. | |
| bool | isMouseTrackingEnabled () const |
| Returns true if mouse tracking is enabled. | |
| Error | enableBracketedPaste () |
| Enables bracketed paste mode. | |
| Error | disableBracketedPaste () |
| Disables bracketed paste mode. | |
| Error | enableAlternateScreen () |
| Switches to the alternate screen buffer. | |
| Error | disableAlternateScreen () |
| Switches back to the main screen buffer. | |
| void | setResizeCallback (ResizeCallback cb) |
| Sets a callback for window resize events (SIGWINCH on POSIX). | |
| void | installSignalHandlers () |
| Installs signal handlers for clean terminal restoration. | |
| Result< int > | writeOutput (const char *data, int len) |
| Writes raw bytes to stdout. | |
Static Public Member Functions | |
| static ColorSupport | colorSupport () |
| Detects the color support level of the terminal. | |
Low-level terminal I/O abstraction.
Provides raw terminal mode, non-blocking input, window size queries, mouse tracking, alternate screen buffer management, and color capability detection. Platform-specific implementations use termios on POSIX and Console API on Windows.
Describes the color capability level of the terminal.
The TUI rendering pipeline always works with full RGB Color values internally. During TuiScreen::flush(), each cell's foreground and background are converted to the nearest representable color for the active ColorSupport level. This means the UI degrades gracefully to any color mode, but for the best visual results, applications should provide a TuiPalette whose colors are chosen with the target color mode in mind.
When the NO_COLOR environment variable is set, the detected color capability is mapped to its grayscale equivalent (e.g. TrueColor becomes GrayscaleTrue). Grayscale modes convert each color to its perceptual luminance (Rec. 709) before emitting it.
|
static |
Detects the color support level of the terminal.
Examines environment variables to determine the level of color support. The PROMEKI_COLOR variable, if set, overrides auto-detection (accepted values: "truecolor", "24bit", "256", "basic", "ansi", "16", "none"). Otherwise, COLORTERM, TERM, and TERM_PROGRAM are examined.
When NO_COLOR is set (see https://no-color.org/), the detected capability is mapped to its grayscale equivalent. The result is cached after the first call.
| Error Terminal::disableAlternateScreen | ( | ) |
Switches back to the main screen buffer.
| Error Terminal::disableBracketedPaste | ( | ) |
Disables bracketed paste mode.
| Error Terminal::disableMouseTracking | ( | ) |
Disables mouse tracking.
| Error Terminal::disableRawMode | ( | ) |
Restores the terminal to its original mode.
| Error Terminal::enableAlternateScreen | ( | ) |
Switches to the alternate screen buffer.
| Error Terminal::enableBracketedPaste | ( | ) |
Enables bracketed paste mode.
| Error Terminal::enableMouseTracking | ( | ) |
Enables xterm SGR mouse tracking.
| Error Terminal::enableRawMode | ( | ) |
Enables raw terminal mode (disables line buffering, echo, etc).
| void Terminal::installSignalHandlers | ( | ) |
Installs signal handlers for clean terminal restoration.
Ensures raw mode and alternate screen are restored on SIGTERM, SIGINT, and other termination signals.
Reads available input bytes without blocking.
| buf | Buffer to read into. |
| maxLen | Maximum bytes to read. |
| void Terminal::setResizeCallback | ( | ResizeCallback | cb | ) |
Sets a callback for window resize events (SIGWINCH on POSIX).
| cb | The callback to invoke on resize. |
| Error Terminal::windowSize | ( | int & | cols, |
| int & | rows | ||
| ) | const |
Queries the current terminal window size.
| cols | Output: number of columns. |
| rows | Output: number of rows. |
Writes raw bytes to stdout.
| data | The data to write. |
| len | Number of bytes to write. |