On this page
Terminal class for raw-mode input handling and screen management.
#claudewheel.terminal
#claudewheel.terminal
Raw terminal I/O: cbreak mode, escape sequence decoding, and alt screen.
#Terminal
Low-level terminal I/O: raw mode, key reading, alt screen, and size detection.
#get_size
def get_size(self) -> tuple[int, int]#enter_raw
def enter_raw(self, alt_screen: bool=True) -> None#subscribe_mode2031
def subscribe_mode2031(self) -> NoneSubscribe to Mode 2031 theme-change notifications.
#exit_raw
def exit_raw(self) -> None#cooked
def cooked(self)Temporarily leave raw mode for the duration of the with-block.
If the terminal is currently raw, exits raw mode on entry and re-enters it on exit with the same alt_screen flag it had before. If already cooked, this is a no-op passthrough (so nesting is safe). Raw mode is restored even if the body raises.
#read_key
def read_key(self) -> strRead a single keypress, decoding escape sequences for arrow keys etc.
#_write_tty
def _write_tty(self, text: str) -> NoneWrite directly to the TTY device.
#write
def write(self, text: str) -> None#flush
def flush(self) -> None#close
def close(self) -> NoneClose the /dev/tty file handle.
#detect_terminal_background
def detect_terminal_background() -> str | NoneDetect whether the terminal has a light or dark background.
Uses the OSC 11 query (background color request) with a DA1 sentinel to detect terminals that do not support OSC 11. Returns "light", "dark", or None (unsupported / timeout / error).
Must be called BEFORE entering the TUI (before raw mode, before user input can race with the response).
#detect_mode2031_support
def detect_mode2031_support() -> str | NoneDetect whether the terminal supports Mode 2031 (theme-change notifications).
Sends CSI ?996n (Mode 2031 query) + DA1 sentinel. If the terminal responds with CSI ?997;Xn (where X=1 for dark, X=2 for light), Mode 2031 is supported. Returns "dark", "light", or None.
Must be called BEFORE entering the TUI.
#_parse_mode2031_response
def _parse_mode2031_response(fd: int) -> str | NoneRead and parse the Mode 2031 query response from the terminal.
Returns "dark" (mode=1), "light" (mode=2), or None (unsupported/timeout).
#_parse_osc11_response
def _parse_osc11_response(fd: int) -> str | NoneRead and parse the OSC 11 response from the terminal.
Returns "light", "dark", or None.
#_classify_rgb
def _classify_rgb(rgb_str: str) -> str | NoneParse an rgb:RR/GG/BB (1-4 hex digits per channel) string and classify as light or dark.
Returns "light" if perceived luminance > 0.5, "dark" otherwise, or None on parse error.