thrupty

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: GPL-2.0 Imports: 22 Imported by: 0

README

thruPTY

Go Reference

throughput + through PTY + True PTY — a high-throughput, cross-platform GPU-accelerated terminal emulator in Go, built to move bytes fast and behave like a real PTY.

thrupty keeps the architecture that makes refterm — Casey Muratori's reference terminal renderer — fast, and replaces the Windows-specific C layer with idiomatic, cross-platform Go. The design follows Casey's lecture series; the solutions we deliberately preserve:

  • Circular source buffer with double mapping — the scrollback ring is mapped twice back-to-back in the virtual address space, so a read that wraps around the physical end is returned as one contiguous slice (zero-copy).
  • SIMD parser with runtime dispatch — the hot scan path selects AVX2, SSE2, or SWAR at startup depending on CPU features (~32/18/5.6 GB/s on plain text).
  • Double-buffered line table — layout replays into a fresh line table and swaps it atomically; the render thread never blocks on the parser.
  • Cell-expansion rendering — the whole grid is drawn in a single fullscreen-triangle GPU pass; the shader expands each pixel into the correct glyph sample.

Features (MVP 0.1)

  • Full VT/ANSI coverage — the state machine is ported from f4 together with its test suite: SGR, scroll regions, alt-screen, 256-color/TrueColor with an OSC 4 palette, and more.
  • Unicode done right — MSDF text rendering with CJK wide glyphs, combining marks, and ligatures.
  • Pluggable render hosts — thrupty's own WebGPU renderer (cellgpu) or embedding into the vtui renderers: gogpu (GPU window), x11, wayland, or the console ANSI path (--tty).
  • Fast interactive shell — on Windows the default is direct anonymous pipes (no ConPTY) with thrupty's own line discipline: a vtui-based command line editor with history, completion, and busy-switching between the editor and the running child.
  • Modern input protocols — Kitty keyboard protocol, win32-input-mode, SGR mouse, bracketed paste, far2l extensions (clipboard, notifications, window size), OSC 52 clipboard.

Quick start

go build ./cmd/thrupty
./thrupty          # thrupty.exe on Windows

Command-line flags:

Flag Meaning Default
--gui, -g cellgpu|gogpu|x11|wayland Render host (x11/wayland are Unix-only) gogpu
--tty, -t Render through vtui's console ANSI path (wins over --gui) off
--shell, -s pipes|conpty|pty Child process transport pipes on Windows, pty elsewhere
--exec <cmd> [args...] Run a command on raw pipes, refterm-style (throughput benchmarks, non-interactive tools)
--demo, -d Static demo content instead of a shell off
--font, -f <path>; --font-size <pt> Font selection system monospace, 16 pt
--size, -w WxH Window size in pixels (cellgpu renderer only) 1280x720

All options accept both POSIX dashes (--gui, -g) and, on Windows, slash style (/gui, /g); run thrupty --help for the full reference.

Debug helpers: --smoke-test runs a headless parser/renderer self-check; THRUPTY_INPUT_LOG=1 (or a file path) traces input translation into thrupty_input.log.

Embedding

The executable is a thin wrapper over the root package, so thrupty can be driven in-process:

package main

import (
	"os"

	"github.com/Dazzar56/thrupty"
)

func main() {
	os.Exit(thrupty.Main(os.Args))
}

Architecture

shell ──pty/pipes──▶ Session ──▶ ByteRing ──▶ parser ──▶ text grid (termcell)
                        (termui goroutine)        │            │
                          versioned snapshots ◀───┘            ▼
                              │                     renderer: bake ▶ 1 GPU pass
                              ▼
                    vtuibridge ▶ vtui hosts (gogpu / x11 / wayland / ANSI)
  • ByteRing — double-mapped circular source buffer + SIMD scanner.
  • terminal — VT/ANSI state machine, line table, layout.
  • termcell — cell value types shared by terminal and renderer.
  • renderer — WebGPU cell-expansion renderer (the cellgpu host).
  • pty — ConPTY on Windows, /dev/ptmx on Unix, behind one interface.
  • termui — the terminal as a self-contained vtui component (session goroutine, snapshots, command line).
  • vtuibridge — composites vtui overlays over the live grid.
  • cmd/thrupty — the executable (thin wrapper over thrupty.Main).

Status

MVP 0.1. Targets Windows, Linux, and macOS; runtime-verified on Windows and Linux so far (macOS builds cleanly but is untested on real hardware).

Roadmap: packaging thrupty as an embeddable component and swapping it in for the built-in terminal of f4 — see F4_INTEGRATION.md for the readiness assessment and plan. Developer-facing architecture notes live in AGENTS.md.

License

thruPTY is distributed under the GNU General Public License v2 — see LICENSE. As a Go port of refterm (GPL-2.0), it is a derivative work and carries the same license. Code ported from f4, vtui and vtinput (BSD 3-Clause, © unxed) is used in compliance with its permissive terms — credits and notices live in NOTICE.

Documentation

Overview

Package thrupty is the entry point of the thruPTY terminal emulator (throughput + through PTY + True PTY). It wires the child-process transport (ConPTY, Unix PTY, or direct pipes), the VT/ANSI terminal state machine, and the render host — thrupty's own WebGPU renderer or one of the vtui backends — into a running application. Main is exported so that cmd/thrupty is a thin wrapper and tests can drive the full startup path in-process.

thrupty preserves the high-throughput architecture of refterm, Casey Muratori's reference terminal renderer: a double-mapped circular source buffer (ByteRing), a SIMD parser with runtime AVX2/SSE2/SWAR dispatch (terminal), a double-buffered line table, and single-pass cell-expansion GPU rendering (renderer).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HexColor

func HexColor(rrggbb string) uint32

func Main

func Main(args []string) int

Main is the entry point for the thrupty executable. It is exported so that cmd/thrupty can call it, and also so that tests can exercise the full startup path without spawning a subprocess.

By default Main spawns the system shell on a PTY and renders it. The default renderer is the vtui-hosted gogpu window (--gui=gogpu); --gui also accepts x11/wayland (Unix only), cellgpu selects thrupty's own WebGPU renderer, and --tty renders through vtui's console ANSI path. The legacy demo content is available with --demo, the smoke test with --smoke-test. With --exec <cmd> [args...] the command runs directly on anonymous pipes, bypassing ConPTY entirely — the refterm architecture, used for throughput benchmarks and non-interactive tools.

func TranslateInput

func TranslateInput(e *vtinput.InputEvent, win32Mode bool, kittyFlags int, appCursorKeys bool) string

TranslateInput converts thrupty input events into ANSI sequences that interactive shell apps expect (ported from f4 input_translation.go).

func TranslateKeyToKitty

func TranslateKeyToKitty(e *vtinput.InputEvent, flags int, appCursorKeys bool) string

func TranslateLegacySpecialKey

func TranslateLegacySpecialKey(e *vtinput.InputEvent, appCursorKeys bool) string

TranslateLegacySpecialKey encodes non-printable keys as ANSI sequences.

func TranslateMouseInput

func TranslateMouseInput(e *vtinput.InputEvent) string

TranslateMouseInput encodes a mouse event as an SGR (1006) mouse sequence.

Types

type Far2lHandler

type Far2lHandler struct {
	// Write delivers replies to the child process (pty.Write).
	Write func([]byte)

	// GetSize returns the terminal grid size in cells (columns, rows).
	GetSize func() (w, h int)

	// Notify shows a desktop notification requested by the guest ('n' command).
	// May be nil, in which case notifications are dropped.
	Notify func(title, text string)
	// contains filtered or unexported fields
}

Far2lHandler processes far2l APC sequences arriving from the child process and writes replies back to the PTY.

func (*Far2lHandler) HandleFar2lAPC

func (h *Far2lHandler) HandleFar2lAPC(s string)

HandleFar2lAPC handles one APC payload (the bytes between ESC _ and the terminator). Robustness: any garbage before the actual marker is skipped.

func (*Far2lHandler) ProcessFar2lInteract

func (h *Far2lHandler) ProcessFar2lInteract(data []byte)

ProcessFar2lInteract handles one decoded far2l interact request.

func (*Far2lHandler) SendFar2lTerminalSize

func (h *Far2lHandler) SendFar2lTerminalSize()

SendFar2lTerminalSize pushes the current grid size to the guest.

type InputModes

type InputModes struct {
	Win32InputMode     bool
	KittyFlags         int
	AppCursorKeys      bool
	BracketedPasteMode bool
	MouseTrackingMode  int
}

InputModes mirrors the terminal mode flags that affect input translation. They are tracked by the terminal parser (see terminal.Terminal).

type PTYInput

type PTYInput struct {
	// Sink receives fully assembled input events (after the key+text merge
	// and the PreTranslate hook).  Set in normal operation: the terminal
	// thread's consumer translates and writes.  When nil, events are
	// translated synchronously via Write (unit-test path).
	Sink func(*vtinput.InputEvent)

	// Write delivers translated input to the child process.  Used only
	// when Sink is nil.
	Write func([]byte)

	// Modes returns the terminal's current input-related mode flags.
	// If nil, all modes are assumed off.
	Modes func() InputModes

	// ReadClipboard reads the system clipboard for Ctrl+Shift+V paste.
	// May be nil, in which case the shortcut is inert.
	ReadClipboard func() (string, error)

	// PreTranslate intercepts a fully assembled input event (after the
	// key+text merge) before ANSI translation. Returning true consumes the
	// event: nothing is written to the PTY. The vtui overlay uses this to
	// take over input while one of its frames is open. May be nil.
	PreTranslate func(*vtinput.InputEvent) bool
	// contains filtered or unexported fields
}

PTYInput converts renderer key/text events into vtinput events. The translation to ANSI bytes and the (potentially blocking) write to the child happen on the terminal thread: assembled events are handed to Sink.

func (*PTYInput) HandleKey

func (in *PTYInput) HandleKey(ev renderer.KeyEvent)

HandleKey processes a physical key press or release from the renderer.

func (*PTYInput) HandleMouse

func (in *PTYInput) HandleMouse(e *vtinput.InputEvent)

HandleMouse forwards a mouse event (the vtui overlay gets it first via PreTranslate, then the sink or the synchronous path).

func (*PTYInput) HandleMouseEvent

func (in *PTYInput) HandleMouseEvent(ev renderer.MouseEvent)

HandleMouseEvent converts a renderer mouse event into a vtinput mouse event (tracking the held-button mask like a Win32 MOUSE_EVENT_RECORD) and forwards it for SGR mouse translation.

func (*PTYInput) HandlePaste

func (in *PTYInput) HandlePaste(text string)

HandlePaste sends pasted text to the child, wrapping it in bracketed-paste markers when the application enabled mode 2004 (f4 panels_frame.go). With a sink attached the paste travels as events (PasteStart marker, payload, PasteEnd marker) so the write happens on the terminal thread; the payload rides in Far2lData — InputEvent has no dedicated text field — and is written verbatim by the consumer.

func (*PTYInput) HandleText

func (in *PTYInput) HandleText(text string)

HandleText processes text input produced by a key press.

Directories

Path Synopsis
Package ByteRing implements the circular source buffer at the heart of thrupty's throughput story: the backing store is mapped twice back-to-back in the virtual address space, so a read that wraps around the physical end of the allocation is returned as a single contiguous slice — zero-copy ring reads, exactly like refterm's source buffer.
Package ByteRing implements the circular source buffer at the heart of thrupty's throughput story: the backing store is mapped twice back-to-back in the virtual address space, so a read that wraps around the physical end of the allocation is returned as a single contiguous slice — zero-copy ring reads, exactly like refterm's source buffer.
cmd
thrupty command
Command thrupty is the thruPTY terminal emulator executable.
Command thrupty is the thruPTY terminal emulator executable.
Package pty abstracts child-process transport behind the PtyBackend interface: ConPTY on Windows and /dev/ptmx on Unix (Linux, macOS, the BSDs, Solaris).
Package pty abstracts child-process transport behind the PtyBackend interface: ConPTY on Windows and /dev/ptmx on Unix (Linux, macOS, the BSDs, Solaris).
Package renderer is thrupty's own WebGPU render host — the cellgpu backend.
Package renderer is thrupty's own WebGPU render host — the cellgpu backend.
Package termcell holds the small value types shared by the terminal state machine and the GPU renderer: the per-cell text cell (the grid's source of truth), the baked per-cell GPU payload, glyph atlas tile info, and shaped-cell runs.
Package termcell holds the small value types shared by the terminal state machine and the GPU renderer: the per-cell text cell (the grid's source of truth), the baked per-cell GPU payload, glyph atlas tile info, and shaped-cell runs.
Package terminal is thrupty's VT/ANSI state machine.
Package terminal is thrupty's VT/ANSI state machine.
Package termui extracts the terminal into a self-contained component (TerminalUI) with its own goroutine, step 3 of the vtui integration roadmap (see AGENTS.md, Phase 5).
Package termui extracts the terminal into a self-contained component (TerminalUI) with its own goroutine, step 3 of the vtui integration roadmap (see AGENTS.md, Phase 5).
Package vtuibridge connects the vtui UI toolkit to thrupty's GPU renderer.
Package vtuibridge connects the vtui UI toolkit to thrupty's GPU renderer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL