Skip to content

Architecture

Saumyadip edited this page Nov 6, 2026 · 4 revisions

Architecture

A friendly tour of how ananta is put together. ananta is about 6,800 lines of Go with just a handful of external dependencies (creack/pty, BurntSushi/toml, golang.org/x/term).

📸 Screenshot coming here: A diagram of client and server talking over a unix socket. 🎬 Video coming here: A visual walkthrough of the packages and how data flows.

Two shapes: attached and local

Attached (default): a thin client inside your terminal talks to a background server over a unix socket.

┌────────────────────┐   unix socket, framed messages   ┌─────────────────────┐
│  ananta client     │ <───────────────────────────────>│ ananta server       │
│  relays keystrokes │  MsgInput  ->   <- MsgOutput     │ owns every PTY,     │
│  draws frames      │  MsgResize ->   <- MsgSetup      │ mux, and emulator   │
└────────────────────┘   <- MsgEnded                    └─────────────────────┘

Local (ananta local): one process does everything in your terminal. Nothing survives when the terminal closes.

Package map

Package What it does
cmd/ananta Command-line entry: flags, new/attach/ls/kill/local/init
internal/session Persistent sessions: the unix-socket protocol, the daemon, the attaching client
internal/app "Local" mode: a raw input loop around the mux
internal/config TOML loading, keymap compilation, action table, themes
internal/input Turns raw terminal bytes into named key/mouse chords
internal/screen Cell grid and diffing renderer (no tears mid-frame)
internal/vt Terminal emulator with scrollback (vendored vt10x fork)
internal/pane One PTY, one emulator, one reader goroutine
internal/mux The multiplexer — all user-visible behaviour
internal/stats /proc and /sys sampling plus custom command widgets
internal/clipboard OSC 52 packing plus native clipboard tool drivers
internal/width Display width of text (CJK / emoji take 2 cells)

The mux sub-packages, in a good reading order for new contributors:

mux.go → layout.go → actions.go → keyboard.go → mouse.go → render.go →
bar.go → help.go → selection.go → copymode.go → scrollback.go →
wallpaper.go → control.go

The main loop (mux)

Mux.Loop owns all mutable layout and selection state. It waits on:

  • keys — decoded key and mouse batches
  • post — closures other goroutines run here (attach, resize, stats)
  • dirty — nudges (I/O in a pane, a stat change)
  • exits — dead panes to remove
  • a ticker at fps

A render frame is emitted only when something changed or an animation is still running. If nothing looks different, the tick costs almost nothing.

Thread safety in one sentence

Everything that changes layout or selection lives on the loop goroutine. The only mutex of note guards each pane's VT state while the PTY reader parses chunks and the render briefly reads it. The session server hands state changes in through post, never by writing fields directly.

The wire protocol (client ↔ server)

Every frame is 1 byte type + uint32 BE length + payload (16 MiB payload limit):

Type Direction Meaning
H client → server Hello: JSON {Cols, Rows}
I client → server Raw terminal bytes
R client → server Resize: JSON size
K client → server Kill session
P / p both Info request / response (used by ananta ls)
S server → client Setup: escape sequences to paint and tear down
O server → client Bytes to paint on screen
X server → client "You were detached" (payload = reason)
Q server → client Session ended

Attach sequence: H → S → (input/output loop) → X or Q. One client per session; a second attach displaces the first.

Terminal emulator (internal/vt)

A vendored fork of vt10x (MIT) — a Go port of suckless st's state table — with glyph grids, attributes, cursor management, DEC private modes, and OSC/D scanners. On top:

  • pushHistory — records scroll-up of the primary screen
  • SetScrollbackLimit, ScrollbackLen, Row, ClearScrollback — the ring used by the mux
  • Logical-row addressing: history first, then the live screen, unifying screen and scrollback for selections and copy mode

Renderer (internal/screen)

A double-buffered cell grid. Each frame is composed into cur, diffed against prev, and only changed cells are written with absolute cursor positioning plus attribute runs. Frames are bounded by synchronized-update markers (CSI ? 2026 h/l) and blinking-cursor suppression, so no tearing appears mid-frame.

Extending ananta

  • New action: add to ActionInfo (config/actions.go), add a case in run (mux/actions.go), bind it in default.toml. The help overlay picks it up automatically.
  • New bar widget: add a case in bar.go:widget and name it in left/center/right — or just point a custom widget at a shell command (see Status Bar).
  • New key events: internal/input/keys.go is where raw terminal bytes become named chords.

Testing

make test   # go test ./...
make vet    # go vet ./...

Test coverage is thin today, focused on: key parsing (input), config normalisation, the session protocol round-trip, detach keeping panes alive, and the vt scrollback ring.

🎬 Video coming here: Adding a new action from start to finish.

Clone this wiki locally