Skip to content

How It Works

vyeche edited this page Nov 30, 2026 · 13 revisions

How it works

Everything kitty-side is in config/tab_bar.py, which kitty loads for {custom} in tab_title_template. It does four jobs.

1. Drawing each tab

draw_title(data) returns two lines per tab (number, status slot, name; then folder and time). Widths come from kitty's wcswidth, so wide characters line up. Colours are palette colours (fmt.fg.colorN), so both themes work without code changes. The quiet parts (the number, the agent mark, the "new output" dot) and the search field and buttons take the theme's own colours from kitty's options as they are drawn (_theme), so they follow a light/dark switch too: the quiet shades are the tab's text colour moved toward its background until just above 4.5:1 (text) or 3:1 (marks), and every pair in the search row is checked to stay above 4.5:1. If drawing ever fails, it returns a plain line instead of raising, because kitty drops the whole title template on an exception.

2. A timer for status, sound and animation

kitty only redraws the tab bar when something changes, and templates have no clock. A timer (TICK, 8 times a second) does three things:

  • Watches states. It reads each window's program status (OSC 7501, sent by Claude Code), plus "is a command running" for plain shells (the terminal's foreground process: from /proc on Linux, from kitty's own process helpers in kitty.child on macOS, which has no /proc).
  • Plays sounds. It does this on changes (working → done, → blocked, → error), and fires the 3-minute reminder.
  • Redraws. Only when a visible frame changes: spinner and pulse on every other tick, sparkles and waves on every tick, nothing when idle. It also wakes kitty's main loop, which otherwise may not paint (seen on X11 once the cursor stops blinking).

Animations count ticks (_frame), not the clock, so no frame is skipped or shown twice.

Agent marks (✳, π, …) name the program in each tab's foreground. When a shell or kitten leads the foreground process group (a tab the restart reopened), the agent below it in the same group counts: on Linux by reading the leader's children in /proc, on macOS from kitty's process-group map (one scan of all processes, shared by every tab for a second). Marks are kept 3 seconds. A window the timer first saw under 10 seconds ago that has no mark yet (a restart reopens every tab at once, and an agent can still be starting) is looked at again twice a second, and the sidebar redraws only if a mark turns up.

Overlays. While an overlay is open in a tab (the search box, the F1 sheet), kitty describes the tab by the overlay: its folder is kitty's start folder and its program is no agent. The row, its mark and the plain-command spinner use the window under it instead (Tab.active_window_for_cwd, the main window of the overlay's group). The overlays aren't started with --cwd=current: from a window running the ssh kitten, kitty would run them on the remote host.

macOS (new, untested on a real Mac): IS_MACOS picks afplay and the .wav sounds, osascript notifications (the text as arguments, never in the script) and the process lookups above. The Linux code paths are unchanged.

The search field. A click on the field turns it into an input (_field_start); the key patch below hands keys to _field_key, which keeps every key while the field is on. Enter runs the search box's own launch with --env=NINELIVES_FINDER_QUERY=… (the words %-encoded, since kitty's --env strips values and expands $VARS); tab-finder.py search starts with that query and, opened this way, replaces a box already open instead of closing it. The box's command line is unchanged, so the toggle and the quest still recognise it. Without the patch (kitty renamed what it relies on) or without tab-finder.py, a click opens the search box as before. Text from an input method (IME: Chinese, Japanese, compose sequences) is written to the window by kitty before Python sees it, so the field can't catch it: type plain keys there.

Tooltips. kitty 0.51 hands Python no pointer moves over the sidebar (mouse.c, handle_tab_bar_mouse reports motion only during a tab drag or a text selection), has no call for the pointer's position over it, and redirect_mouse_handling would take every click from kitty. So a tab's tooltip comes on a right-click (kitty ignores right-clicks on tabs): _tip_draw writes it over the rows below the tab after each layout, from what the tab's row last showed (_shown_marks). kitty draws the cat's pictures over the sidebar's cells, so the tooltip keeps off the cat's rows (_tip_cat_rows: the whole floor, which she walks along without a redraw, plus her rows when she's up at a tab, or her rows at the end of + New tab): _tip_spot puts it below the tab, else above it, else in the clear spot next to her that's nearest the tab.

The cat's menu. A right-click on the cat (_region says ("cat",), checked before the tab under a visiting cat) opens _menu; _menu_draw writes it into the sidebar's cells after each layout with the tooltip's _box_draw and colours, next to the cat (_cat_anchor: above it on the floor, else below it or wherever it fits), never on the cat's rows, since its pictures would cover the words. Its values are read once when it opens (no file reads while kitty draws) and written with settings.py's own functions (set_talk, set_cat_size, set_on…), so it changes exactly what kitty-cat and the settings list change; then the cat looks at its files at once instead of a second later. A click on an option is done on the release (_sidebar_click), like the buttons. While it's open the field's key hook hands it the keys (_menu_key): ↑/↓/←/→/Enter/Esc are its own, a kitty shortcut still runs and closes it (F1 too: any key kitty maps), and any other key closes it and is dropped, its auto-repeats too while it's held (that one key never reaches the terminal; the next ones do, as usual), so the keys are never held: it also closes when another kitty window has the focus, on a click in a terminal and after 20 s (_menu_tick, from the sidebar's timer, which also writes menu.txt for check.sh). Meanwhile the cat stays put, mid-walk or mid-hop too (_cat_walk_tick waits, and goes on afterwards as if the menu's time never passed; the menu is placed by where the cat is now), and keeps its news (st["seen"] isn't moved on) until the menu closes. kitty hands the key hook a held key's auto-repeats like presses, so after the menu or the search field closes on a key (Enter, Esc, a dropped key), that key's repeats are dropped until a fresh press (_held_repeat): a held Enter can't go on into a prompt.

3. Patches to kitty

These are installed once per kitty process. Each checks that what it relies on exists, and skips itself if not.

Patch Why
TabManager.handle_tab_bar_mouse drag tabs inside the sidebar on Wayland (COSMIC ignores kitty's compositor drag); clicks on our buttons, the cat and its bubble; carrying the cat (a press on it tells kitty a drag started, so the mouse's moves come)
TabBar.update_vertical the gap row under a tab belongs to that tab, for clicks and drags; after each sidebar layout, the drawn cat's place (the floor, small by "+ New tab", or its face) and its pictures put back; when kitty dropped its blank rows between tabs, _space_tabs clears each other tab's folder line to the sidebar's colour in kitty's cells (Line.apply_cursor, every column), and puts the search row on that colour
TabManager.tab_bar_data the search field and buttons on top; "+ New tab" label, the cat (a click on it opens the helper cat), the quest line
Boss.dispatch_action notice shortcuts for quests
Boss.dispatch_possible_special_key typing in the search field: kitty offers every key press here before the window gets it (keys.c, on_key_input; a True answer keeps the key, and its release, from the window). While the field is on, the keys go into it; a kitty shortcut still runs (and puts the button back), anything else is dropped. Off at once when the tab, window or focus changes, after 30 s without a key, or if the hook fails
Window.on_mouse_event a click in a terminal window puts the search field's button back (and the tab tooltip away)
drag image functions the high-contrast drag card (X11)

Reload safety. Ctrl+Shift+F5 runs the file again, but a patch stays the version first installed. So patches call the current load's code through attributes on kitty.tab_bar (_vye_quest_action, _vye_new_tab_label, _vye_field_key, _vye_field_mouse, _vye_hooks). The timer is replaced on every reload, so sounds never double.

4. Quests

dispatch_action reports every shortcut. ACTION_QUEST maps action names to quests. Progress is written atomically to ~/.local/state/ninelives/.

Other files

File Does
config/kitty.conf settings, key maps, theme include; at the end globinclude ninelives-${KITTY_OS}.conf
config/ninelives-macos.conf read only by kitty on macOS: the socket in $TMPDIR, Ctrl+Alt+M with afplay
config/cosmic-*.conf, *-theme.auto.conf colours; kitty picks an auto file by the desktop's light/dark setting
config/shortcuts.sh the F1 sheet (wraps to the window)
config/command_title.py which titles are a running command's command line (compared with the command the shell reported to kitty, last_cmd_cmdline) and their short form (git push); tab_bar.py, tab-finder.py and scripts/kitty-tabs.py all use it, and full-commands turns it off
config/tab-finder.py the tab search box (Ctrl+Shift+Space) and the jump to the waiting tab (Ctrl+Alt+U)
config/settings.py the settings list (Ctrl+Shift+F2, the sidebar's ⚙, ninelives settings): one row per setting, written to the same switch files the kitty-* commands use
config/nine.py the cat's parts that don't need kitty: its moves and which picture shows, walking, talking at a steady pace, its speech bubble as a picture (text drawn by a function it's handed: kitty's own renderer in the sidebar), and its memory of what it said (cat-said in the state folder: keys and short hashes of the lines, never their words, which can name a tab after a whole command line)
config/cat-helper.py, config/cat/ the helper cat (Ctrl+Alt+C, a click on the sidebar's cat, ninelives cat): an overlay that shows the drawn cat with kitty's graphics protocol (each pose sent once as a PNG, then swapped in place; never \e[2J, which would free the poses kitty keeps) and a bubble drawn in cells. It reads the tabs as the search box does (tab-finder.py's rows, kitty @ ls once a second while open) and quests from quests.txt
config/tips.sh prompt tips and the kitty-* commands
config/sounds/*.oga sounds, built by scripts/make-sounds.py
scripts/install.sh install with backups and a config check
scripts/uninstall.sh remove NineLives' files again and put your own config back
scripts/kitty-relaunch.sh restart kitty and reopen every tab (finds kitty by its remote-control socket: the one it runs in, else the only one)
scripts/kitty-tabs.py reads kitty @ ls for the relaunch: how to bring each tab back, which tabs are busy; and which kitty sockets there are
scripts/make-sounds.py builds the sounds from scratch
scripts/make-cat.py draws the cat's poses, its walk, its mouths and its eye patches (config/cat/<size>/<pose>.png) with pycairo, as small palette PNGs; --sheet FILE makes a contact sheet
tests/ unit tests, no kitty needed (see Testing)
scripts/check.sh every check in one command, see Testing
scripts/check-macos.sh, tests/live_agent_lookup.py the macOS checks (the Gauntlet's macOS lane), and the agent lookup against real processes with kitty's own Python
scripts/dev-kitty.sh a visible dev kitty with its own config folder, see Testing
scripts/test-kitty.sh, scripts/vmouse.py testing, see Testing
scripts/publish-wiki.sh publish docs/wiki/ to the repository's wiki (GitHub, Gitea, …)

The drawn cat in the sidebar

kitty draws the sidebar from a Screen of its own (TabBar.screen) with the same code as a terminal window, pictures included, but makes that Screen without a window id, and a picture reaches the GPU only for a Screen that has one. So the sidebar gets a new Screen tied to one of its kitty window's windows (picked again as soon as that window closes, before any picture is sent: a picture sent through a closed window is never uploaded and kitty draws it solid black), and the cat's frames go in as graphics-protocol commands fed to its parser (Screen.restore_scrollback) from inside kitty. Only our own quiet (q=2) graphics commands may go through: tab_bar.py checks every byte (a reply would be typed into the borrowed window's program, a BEL would ask the desktop for attention). Each sidebar redraw erases the screen, so every picture also has a placement that is never drawn (it keeps the picture), and the cat and its bubble are put back at the end of each redraw. It moves a pixel at a time (a cell plus a pixel offset); a timer runs 30 times a second only while it walks (120 on the way to a tab and back, so no frame on screen shows it standing still mid-hop) and 30 times while it talks, and neither redraws the sidebar's text. kitty draws a picture placed by rows that starts part way down a row that much shorter (its bottom stays on a row's edge), so the frames go in with see-through rows below them and each placement asks for the source rows that fill exactly what kitty draws: the cat is the same size at every pixel of a hop. Its bubble is a picture too, drawn with kitty's own text renderer, and the words are shown a little more at a time by cropping their picture. While the cat is on its way up to a tab (or back down) the bubble is placed for where it will sit (_bubble_rest), once, without its tail: the hops move only the cat, and the bubble's tail is placed once it lands. nine.py's bubble_spot picks the side: touching the cat, off the tab it points at and the active tab's first row (its ×), then off the rest of the active tab, then over as few other tabs' name rows as it can (bubble_cost). If the active tab's first row can't be kept clear, _bubble_render makes the bubble narrower, left of that ×. On a kitty without these internals the cat face shows instead.

Clone this wiki locally