How charmbracelet/gum works
Purpose and Runtime
Gum is a Go CLI for adding interactive, styled terminal UX to shell scripts. It exposes composable subcommands that read arguments/stdin, render terminal UIs or formatted output, write results to stdout, and signal outcomes through exit codes. Primary runtime boundary is the user’s terminal/stdin/stdout rather than a web or server process.
Paths: main.go, gum.go, go.mod
Process Entry and Command Routing
main.go starts the executable; the root command infrastructure in gum.go dispatches to independently implemented subcommands. Each command conventionally separates Cobra-style command wiring, option/env binding, and core execution into command.go, options.go, and implementation files. Completion is a sibling command surface for shell integration.
Paths: main.go, gum.go, completion/command.go
Interactive Selection and Input Stage
Interactive commands wrap Charm’s terminal UI ecosystem (Bubbles/Lip Gloss) to collect user choices or text. choose selects supplied or piped candidates; filter fuzzy-filters stdin; input and write collect short and multiline text; confirm maps a decision to shell-compatible status; file navigates filesystem choices. Their output is intended for command substitution and pipelines.
Paths: choose/choose.go, filter/filter.go, input/input.go
Terminal Display and Layout Stage
Non-prompt rendering commands transform text into terminal presentation: style applies colors, borders, alignment, padding, and margins; join composes rendered blocks; table parses/render-selects tabular input; pager provides navigable document viewing. These commands preserve CLI composition by consuming stdin or arguments and emitting rendered text/selections.
Paths: style/lipgloss.go, table/table.go, pager/pager.go
Content Formatting and Logging
format converts text through markdown, code, template, or emoji-oriented formatting modes. log emits leveled and optionally structured terminal logs. These are non-interactive output stages that can be embedded in scripts, unlike the TUI-driven selectors.
Paths: format/command.go, format/formats.go, log/command.go
Command Execution Supervision
spin launches an external command while rendering spinner progress, using pseudo-terminal support where needed; it terminates its UI based on child-process completion and can expose child output. This is the main boundary where Gum orchestrates another OS process rather than merely transforming terminal input.
Paths: spin/spin.go, spin/pty.go, spin/spinners.go
Shared CLI/Terminal Boundaries
Internal packages centralize cross-command behavior: stdin detection/reading, TTY capability handling, timeout contexts, exit behavior, and option decoding such as alignment. This layer isolates environment-sensitive behavior and makes command implementations consistent across interactive and piped use.
Paths: internal/stdin/stdin.go, internal/tty/tty.go, internal/timeout/context.go
Distribution and Automation
The project is built as a Go binary and ships through Docker, Nix, and GoReleaser-managed multi-platform releases. GitHub Actions handles build, lint, nightly, and release workflows. External operational dependencies are terminal capabilities, shell completion targets, and Charm’s Go UI libraries; no persistent service or database is indicated.
Paths: Dockerfile, .goreleaser.yml, .github/workflows/goreleaser.yml
Main entry points: gum.go Root command router · spin.go Process supervisor · main.go Executable entry · stdin.go Shared terminal I/O
Components 18
CLI Runtime
Go executable and command routing.
- Executable entry
main.go - Root command router · Registers and dispatches Gum subcommands.
gum.go - Shell completion
completion/command.go
Interactive Commands
Terminal UI commands for shell pipelines.
- Selection & text input · Choose, filter, input, and write collect values for command substitution.
choose/choose.go - Fuzzy filter · Filters candidates read from stdin.
filter/filter.go - Text input · Collects short text input.
input/input.go
Rendering Commands
Text presentation and non-interactive output.
- Style & layout · Applies Lip Gloss styling and block layout.
style/lipgloss.go - Table renderer · Parses and renders selectable tabular input.
table/table.go - Document pager · Provides navigable terminal document viewing.
pager/pager.go - Content formatter · Formats markdown, code, templates, and emoji text.
format/formats.go - Terminal logger · Emits leveled or structured logs.
log/command.go
Platform & Delivery
Terminal boundaries, process supervision, and release automation.
- Process supervisor · Runs a child command while displaying spinner progress.
spin/spin.go - Pseudo-terminal support · Connects supervised child processes to terminal I/O when needed.
spin/pty.go - Shared terminal I/O · Normalizes stdin reading, TTY handling, timeouts, and exit behavior.
internal/stdin/stdin.go - Go build module
go.mod - Release pipeline · Builds multi-platform releases through GoReleaser and GitHub Actions.
.goreleaser.yml - Container image
Dockerfile
Other components
- User terminal / shell · Supplies arguments, stdin, TTY capabilities, and receives stdout and exit codes.
Connections 23
- User terminal / shell → Executable entry: invokes
- Executable entry → Root command router: starts
- Root command router → Shell completion: dispatches
- Root command router → Selection & text input: dispatches
- Root command router → Fuzzy filter: dispatches
- Root command router → Text input: dispatches
- Root command router → Style & layout: dispatches
- Root command router → Table renderer: dispatches
- Root command router → Document pager: dispatches
- Root command router → Content formatter: dispatches
- Root command router → Terminal logger: dispatches
- Root command router → Process supervisor: dispatches
- Selection & text input → Shared terminal I/O: uses
- Fuzzy filter → Shared terminal I/O: reads stdin via
- Text input → Shared terminal I/O: uses TTY via
- Style & layout → User terminal / shell: writes rendered stdout
- Content formatter → User terminal / shell: writes formatted stdout
- Terminal logger → User terminal / shell: writes logs
- Process supervisor → Pseudo-terminal support: uses
- Process supervisor → User terminal / shell: supervises child output
- Go build module → Executable entry: builds
- Release pipeline → Go build module: packages
- Release pipeline → Container image: publishes