# xfbs/tmux-rs architecture

> Architecture diagram of the GitHub repository xfbs/tmux-rs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xfbs/tmux-rs
- Repository: https://github.com/xfbs/tmux-rs

## Overview

1. **Project shape**
   - This is a Rust port of `tmux`, so the core architecture is still the classic tmux split: a long-lived **server** manages state, and one or more **clients** connect to it over terminal/TTY boundaries.
   - The repo is organized as a single Rust crate with a large `src/` tree, plus regression tests, unit/integration tests, and fuzz targets.

2. **Process and control boundary**
   - `src/main.rs`, `src/lib.rs`, `src/server.rs`, `src/client_.rs`, and `src/control.rs` define the top-level runtime and external interaction points.
   - The server owns global state and multiplexes clients; clients are the terminal-facing side that read input and receive screen updates.
   - Control-mode support and notifications are separate concerns from the interactive terminal UI, which matters for diagramming external interfaces.

3. **Core tmux data model**
   - The central domain objects are modeled in modules such as `session_.rs`, `window_.rs`, `pane`-related modules (`window_*`, `pane` logic is spread across window/copy/capture modules), and `session_`/`window_tree.rs`.
   - The hierarchy is the familiar tmux structure: **server → sessions → windows → panes**.
   - `layout.rs`, `layout_custom.rs`, and `layout_set.rs` handle window/pane arrangement, while `resize.rs` and `window_tree.rs` manage structural changes.

4. **Command subsystem**
   - `src/cmd_/` contains one module per tmux command: attach, split-window, new-session, list-* commands, kill-* commands, set-option, send-keys, etc.
   - `src/cmd_parse.lalrpop` and `src/cmd_parse.rs` provide command parsing, so command strings are turned into typed command objects before execution.
   - `cmd_queue.rs` is the execution pipeline: parsed commands are queued and run against server/session/window state rather than acting directly in the parser.

5. **Input, key bindings, and user interaction**
   - `input.rs`, `input_keys.rs`, `key_bindings_.rs`, `key_string.rs`, and `keyc_mouse_key.rs` form the user-input path.
   - Terminal key sequences are decoded, matched against bindings, and translated into tmux commands or pane input.
   - This layer is a key boundary in the architecture because it bridges raw terminal events into the command subsystem.

6. **TTY, terminal, and screen rendering**
   - `tty_.rs`, `tty_term_.rs`, `tty_keys.rs`, `tty_features.rs`, and `screen_write.rs`/`screen_redraw.rs` implement terminal capability detection and drawing.
   - `screen_.rs`, `grid_.rs`, `grid_view.rs`, and `grid_reader_.rs` represent the in-memory screen buffer and how it is read/written.
   - The render path is: server state changes → redraw logic computes visible screen → TTY layer emits terminal escape sequences to clients.

7. **Pane content and PTY integration**
   - `spawn.rs`, `tty_.rs`, `job_.rs`, `proc.rs`, and `osdep.rs` are the process/PTY side of the system.
   - Panes are backed by spawned child processes attached to pseudo-terminals, and tmux relays keystrokes to them while capturing their output into grids.
   - Modules like `copy-mode`, `capture-pane`, `paste`, and `window_buffer.rs` expose or manipulate buffered pane contents.

8. **Formatting, styling, and metadata**
   - `format.rs`, `format_draw_.rs`, `style_.rs`, `attributes.rs`, `colour.rs`, `status.rs`, and `options_.rs` control appearance and user-configurable formatting.
   - `names.rs`, `mode_tree.rs`, and the various `show-*`/`list-*` commands expose internal state for status lines, prompts, menus, and display panes.
   - This layer is important because tmux UI is heavily data-driven: many displayed strings are generated from format expressions and options.

9. **Configuration and environment handling**
   - `cfg_.rs`, `arguments.rs`, `environ_.rs`, `options_table.rs`, and the `source-file`/`set-environment` commands manage startup configuration and runtime environment.
   - The repo includes many regression configs under `regress/conf/`, which suggests config parsing and command semantics are a major compatibility surface.
   - Environment propagation is part of the server/session boundary, since child panes inherit controlled environments rather than the client shell’s state directly.

10. **Compatibility and low-level support**
    - `src/compat/` contains portability shims for platform-specific or libc-adjacent behavior such as `getopt`, `strlcpy`, `imsg`, `fdforkpty`, and `systemd`.
    - `libc.rs`, `xmalloc.rs`, `utf8.rs`, `unicode`-adjacent helpers, and `bitstr.rs` support the low-level runtime that tmux traditionally depends on.
    - The README’s warning that the project is “almost entirely unsafe Rust” is consistent with this layer being performance- and system-call-heavy.

11. **Non-interactive surfaces**
    - `control_notify.rs`, `server_acl.rs`, `notify.rs`, `alerts.rs`, and `popup.rs` represent auxiliary UI/control paths beyond the normal pane display.
    - `menu_.rs`, `command_prompt.rs`, `display_menu`, and `display_message` provide transient overlays and prompts, which sit on top of the main screen model.

12. **Testing and verification**
    - There are three strong test strata: Rust tests under `tests/`, shell-based regression tests under `regress/`, and fuzzers under `fuzz/`.
    - The fuzz targets cover parsers and risky text-processing paths such as command parsing, style parsing, input parsing, UTF-8 decoding, regex substitution, and sandbox behavior.
    - This suggests the architecture has many boundary-heavy components where malformed input is a primary failure mode.

13. **Build and tooling**
    - The project uses Cargo, `build.rs`, LALRPOP for command parsing, and CI workflows for build, coverage, and wiki updates.
    - The presence of `nextest`, `clippy.toml`, `rustfmt.toml`, and Nix files indicates a fairly mature Rust toolchain around a still-experimental codebase.
    - For an architecture diagram, the main external dependencies worth showing are the terminal/terminfo database and the spawned child processes inside panes.

14. **Mental model for a diagram**
    - The simplest accurate diagram is: **terminal client input → key/input parsing → command queue / server state → layout/screen computation → TTY output to terminal**, with a side branch from **pane output via PTYs → grid buffer → redraw**.
    - Around that core, place configuration, options, formatting, and control-mode surfaces as supporting subsystems.

Main entry points: [`src/server.rs`](https://github.com/xfbs/tmux-rs/blob/main/src/server.rs) (Server), [`src/window_.rs`](https://github.com/xfbs/tmux-rs/blob/main/src/window_.rs) (Window), [`src/spawn.rs`](https://github.com/xfbs/tmux-rs/blob/main/src/spawn.rs) (PTY proc), [`src/client_.rs`](https://github.com/xfbs/tmux-rs/blob/main/src/client_.rs) (Client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime boundary"]
  node_main["Main<br/>entry<br/>[main.rs]"]
  node_lib["Library<br/>crate<br/>[lib.rs]"]
  node_server["Server<br/>state owner<br/>[server.rs]"]
  node_client["Client<br/>[client_.rs]"]
  node_control["Control<br/>[control.rs]"]
  node_notify["Notify<br/>events<br/>[control_notify.rs]"]
end

subgraph group_model["Core model"]
  node_session["Session<br/>[session_.rs]"]
  node_window["Window<br/>[window_.rs]"]
  node_tree["Tree<br/>[window_tree.rs]"]
  node_layout["Layout<br/>[layout.rs]"]
end

subgraph group_interaction["Interaction path"]
  node_input["Input<br/>event parser<br/>[input.rs]"]
  node_keys["Key map<br/>bindings<br/>[key_bindings_.rs]"]
  node_cmdparse["Cmd parse<br/>parser<br/>[cmd_parse.rs]"]
  node_queue["Cmd queue<br/>execution pipeline<br/>[cmd_queue.rs]"]
  node_cmds["Commands<br/>command modules<br/>[mod.rs]"]
end

subgraph group_render["Screen & terminal"]
  node_grid["Grid<br/>screen buffer<br/>[grid_.rs]"]
  node_screen["Screen<br/>[screen_.rs]"]
  node_tty["TTY<br/>terminal I/O<br/>[tty_.rs]"]
end

subgraph group_support["Support systems"]
  node_config["Config<br/>[cfg_.rs]"]
  node_formatting["Format<br/>presentation<br/>[format.rs]"]
  node_process["PTY proc<br/>pty/process<br/>[spawn.rs]"]
  node_compat["Compat<br/>portability<br/>[mod.rs]"]
end

node_main -->|"start"| node_server
node_main -->|"connect"| node_client
node_lib -->|"exposes"| node_server
node_lib -->|"exposes"| node_cmds
node_server -->|"owns"| node_session
node_session -->|"contains"| node_window
node_window -->|"uses"| node_tree
node_window -->|"arranges"| node_layout
node_layout -->|"projects"| node_grid
node_grid -->|"feeds"| node_screen
node_screen -->|"draws"| node_tty
node_client -->|"reads"| node_input
node_input -->|"matches"| node_keys
node_keys -->|"triggers"| node_cmdparse
node_cmdparse -->|"enqueues"| node_queue
node_queue -->|"runs"| node_cmds
node_cmds -->|"mutates"| node_server
node_control -->|"submits"| node_queue
node_notify -->|"sends"| node_client
node_config -->|"configures"| node_server
node_config -->|"parses"| node_cmdparse
node_formatting -->|"renders"| node_screen
node_process -->|"backs panes"| node_window
node_process -->|"captures"| node_grid
node_compat -->|"supports"| node_process
node_compat -->|"supports"| node_tty

click node_main "https://github.com/xfbs/tmux-rs/blob/main/src/main.rs"
click node_lib "https://github.com/xfbs/tmux-rs/blob/main/src/lib.rs"
click node_server "https://github.com/xfbs/tmux-rs/blob/main/src/server.rs"
click node_client "https://github.com/xfbs/tmux-rs/blob/main/src/client_.rs"
click node_control "https://github.com/xfbs/tmux-rs/blob/main/src/control.rs"
click node_notify "https://github.com/xfbs/tmux-rs/blob/main/src/control_notify.rs"
click node_session "https://github.com/xfbs/tmux-rs/blob/main/src/session_.rs"
click node_window "https://github.com/xfbs/tmux-rs/blob/main/src/window_.rs"
click node_tree "https://github.com/xfbs/tmux-rs/blob/main/src/window_tree.rs"
click node_layout "https://github.com/xfbs/tmux-rs/blob/main/src/layout.rs"
click node_grid "https://github.com/xfbs/tmux-rs/blob/main/src/grid_.rs"
click node_screen "https://github.com/xfbs/tmux-rs/blob/main/src/screen_.rs"
click node_tty "https://github.com/xfbs/tmux-rs/blob/main/src/tty_.rs"
click node_input "https://github.com/xfbs/tmux-rs/blob/main/src/input.rs"
click node_keys "https://github.com/xfbs/tmux-rs/blob/main/src/key_bindings_.rs"
click node_cmdparse "https://github.com/xfbs/tmux-rs/blob/main/src/cmd_parse.rs"
click node_queue "https://github.com/xfbs/tmux-rs/blob/main/src/cmd_/cmd_queue.rs"
click node_cmds "https://github.com/xfbs/tmux-rs/blob/main/src/cmd_/mod.rs"
click node_config "https://github.com/xfbs/tmux-rs/blob/main/src/cfg_.rs"
click node_formatting "https://github.com/xfbs/tmux-rs/blob/main/src/format.rs"
click node_process "https://github.com/xfbs/tmux-rs/blob/main/src/spawn.rs"
click node_compat "https://github.com/xfbs/tmux-rs/blob/main/src/compat/mod.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_lib,node_server,node_client,node_control,node_notify toneBlue
class node_session,node_window,node_tree,node_layout toneAmber
class node_input,node_keys,node_cmdparse,node_queue,node_cmds toneMint
class node_grid,node_screen,node_tty toneRose
class node_config,node_formatting,node_process,node_compat toneIndigo
```

## Components

### Runtime boundary

Server/client split and external control surfaces

- [Main](https://github.com/xfbs/tmux-rs/blob/main/src/main.rs): entry · Program startup and top-level dispatch · `src/main.rs`
- [Library](https://github.com/xfbs/tmux-rs/blob/main/src/lib.rs): crate · Shared crate surface and module wiring · `src/lib.rs`
- [Server](https://github.com/xfbs/tmux-rs/blob/main/src/server.rs): state owner · Long-lived tmux state and client multiplexing · `src/server.rs`
- [Client](https://github.com/xfbs/tmux-rs/blob/main/src/client_.rs): terminal client · Terminal-facing client event and display side · `src/client_.rs`
- [Control](https://github.com/xfbs/tmux-rs/blob/main/src/control.rs): control mode · Non-interactive command/control interface · `src/control.rs`
- [Notify](https://github.com/xfbs/tmux-rs/blob/main/src/control_notify.rs): events · Server notifications and alerts delivery · `src/control_notify.rs`

### Core model

Sessions, windows, panes, and layout state

- [Session](https://github.com/xfbs/tmux-rs/blob/main/src/session_.rs): session state · Session lifecycle and session-level state · `src/session_.rs`
- [Window](https://github.com/xfbs/tmux-rs/blob/main/src/window_.rs): window state · Window-level state and pane grouping · `src/window_.rs`
- [Tree](https://github.com/xfbs/tmux-rs/blob/main/src/window_tree.rs): window tree · Structural hierarchy and navigation across panes/windows · `src/window_tree.rs`
- [Layout](https://github.com/xfbs/tmux-rs/blob/main/src/layout.rs): layout engine · Pane arrangement and layout computation · `src/layout.rs`

### Interaction path

Input, bindings, commands, and control mode

- [Input](https://github.com/xfbs/tmux-rs/blob/main/src/input.rs): event parser · Raw terminal input decoding and dispatch · `src/input.rs`
- [Key map](https://github.com/xfbs/tmux-rs/blob/main/src/key_bindings_.rs): bindings · Key binding tables and key-string translation · `src/key_bindings_.rs`
- [Cmd parse](https://github.com/xfbs/tmux-rs/blob/main/src/cmd_parse.rs): parser · Command-string parsing into typed command objects · `src/cmd_parse.rs`
- [Cmd queue](https://github.com/xfbs/tmux-rs/blob/main/src/cmd_/cmd_queue.rs): execution pipeline · Queued command execution against server state · `src/cmd_/cmd_queue.rs`
- [Commands](https://github.com/xfbs/tmux-rs/blob/main/src/cmd_/mod.rs): command modules · tmux command implementations and actions · `src/cmd_/mod.rs`

### Screen & terminal

Screen buffers, redraw, and TTY emission

- [Grid](https://github.com/xfbs/tmux-rs/blob/main/src/grid_.rs): screen buffer · In-memory terminal grid and view/read access · `src/grid_.rs`
- [Screen](https://github.com/xfbs/tmux-rs/blob/main/src/screen_.rs): screen model · Visible screen state and redraw coordination · `src/screen_.rs`
- [TTY](https://github.com/xfbs/tmux-rs/blob/main/src/tty_.rs): terminal I/O · Terminal capability detection and escape-sequence output · `src/tty_.rs`

### Support systems

Config, environment, formatting, process, and compatibility

- [Config](https://github.com/xfbs/tmux-rs/blob/main/src/cfg_.rs): startup config · Arguments, config files, and environment setup · `src/cfg_.rs`
- [Format](https://github.com/xfbs/tmux-rs/blob/main/src/format.rs): presentation · Format expansion, style, and status rendering · `src/format.rs`
- [PTY proc](https://github.com/xfbs/tmux-rs/blob/main/src/spawn.rs): pty/process · Pane process spawning and job/proc integration · `src/spawn.rs`
- [Compat](https://github.com/xfbs/tmux-rs/blob/main/src/compat/mod.rs): portability · System compatibility and low-level shims · `src/compat/mod.rs`

## Connections

- Main → Server: start · Launches the long-lived server runtime
- Main → Client: connect · Creates or attaches terminal clients
- Library → Server: exposes · Crate wiring makes server components available
- Library → Commands: exposes · Command subsystem is part of the public crate graph
- Server → Session: owns · Server manages sessions as primary state
- Session → Window: contains · Sessions organize windows
- Window → Tree: uses · Window structure is navigated and rearranged through the tree
- Window → Layout: arranges · Windows depend on layout computation for pane placement
- Layout → Grid: projects · Layout determines visible grid regions
- Grid → Screen: feeds · Grid state is read into visible screen state
- Screen → TTY: draws · Redraw logic emits terminal output through TTY
- Client → Input: reads · Client receives raw terminal input
- Input → Key map: matches · Decoded input is matched against bindings
- Key map → Cmd parse: triggers · Bindings map keys to commands or pane input
- Cmd parse → Cmd queue: enqueues · Parsed commands are queued for execution
- Cmd queue → Commands: runs · Execution pipeline dispatches command implementations
- Commands → Server: mutates · Commands operate on server/session/window state
- Control → Cmd queue: submits · Control mode injects commands into the same pipeline
- Notify → Client: sends · Notifications and alerts reach clients
- Config → Server: configures · Startup configuration initializes server state
- Config → Cmd parse: parses · Config files are parsed as command streams
- Format → Screen: renders · Format and style data shape status and overlays
- PTY proc → Window: backs panes · Child processes attach to panes inside windows
- PTY proc → Grid: captures · PTY output is captured into grid-backed buffers
- Compat → PTY proc: supports · Compatibility shims underpin PTY and process handling
- Compat → TTY: supports · Low-level portability helpers support terminal I/O
