How xfbs/tmux-rs works
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: server.rs Server · window_.rs Window · spawn.rs PTY proc · client_.rs Client
Components 22
Runtime boundary
Server/client split and external control surfaces
- Main · Program startup and top-level dispatch
src/main.rs - Library · Shared crate surface and module wiring
src/lib.rs - Server · Long-lived tmux state and client multiplexing
src/server.rs - Client · Terminal-facing client event and display side
src/client_.rs - Control · Non-interactive command/control interface
src/control.rs - Notify · Server notifications and alerts delivery
src/control_notify.rs
Core model
Sessions, windows, panes, and layout state
Interaction path
Input, bindings, commands, and control mode
- Input · Raw terminal input decoding and dispatch
src/input.rs - Key map · Key binding tables and key-string translation
src/key_bindings_.rs - Cmd parse · Command-string parsing into typed command objects
src/cmd_parse.rs - Cmd queue · Queued command execution against server state
src/cmd_/cmd_queue.rs - Commands · tmux command implementations and actions
src/cmd_/mod.rs
Screen & terminal
Screen buffers, redraw, and TTY emission
Support systems
Config, environment, formatting, process, and compatibility
Connections 26
- 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