fix(cli): migrate to bubbletea v2 to drop the init-time terminal probe
Fixes #250.
Every openapi invocation, including --version and spec lint, stalls for about five seconds and leaks ESC ] 11 ; ? ESC \ ESC [ 6 n into stdout when stdout is a terminal but stdin is not (CI log capture, script, expect, editors that shell out). The cause is bubbletea v1's tea_init.go, whose init() calls lipgloss.HasDarkBackground() eagerly so the terminal answers the background-colour query (OSC 11) before any Program takes over stdin. That runs before main, so it fires for every subcommand even though only spec explore and spec snip ever draw a TUI, and when no answer comes back termenv waits out its five-second OSCTimeout. The comment on that init() says the workaround would be removed in v2.
It was. Bubble Tea v2 has no init-time probe, lipgloss v2 has no package-level renderer and no background detection, and muesli/termenv (which sent the query) drops out of the dependency graph entirely. This PR moves cmd/openapi to charm.land/bubbletea/v2, charm.land/lipgloss/v2 and charm.land/bubbles/v2. Only the cmd/openapi module depends on charm, so the root module is untouched.
API changes applied
- Import paths:
github.com/charmbracelet/{bubbletea,lipgloss,bubbles}becomecharm.land/{bubbletea,lipgloss,bubbles}/v2(the v2 modules declare thecharm.landvanity path; thegithub.com/.../v2form does not resolve). Model.View() stringbecomesModel.View() tea.View, wrapping the rendered string withtea.NewView.tea.WithAltScreen()program option is gone; the explore and snip views now setAltScreen = trueon the returnedtea.View. The file-path input prompt never used the alt screen and still does not.tea.KeyMsgis now an interface; the models matchtea.KeyPressMsgand switch onmsg.String()("enter","esc","ctrl+c") instead ofmsg.Typeconstants.lipgloss.Coloris now a function returningcolor.Color, so the method colour map andGetMethodColorusecolor.Colorand calllipgloss.Color(...)per entry.- bubbles
textinput:ti.Width = 60becomesti.SetWidth(60).
Space key
In v1, KeyMsg.String() for the space bar returned " ". In v2 it returns "space", so the fold/select toggle in model.go now matches case "space":. Every other key the explorer handles (q, ?, esc, j/k, arrows, ctrl+d/ctrl+u, g/G, enter, a/A) keeps its v1 string. The only configured selection action key is w, so no ActionKey config needed changing, but any future ActionKey{Key: " "} would have to be written as "space". New tests in model_keys_test.go drive Update with tea.KeyPressMsg{Code: tea.KeySpace} and cover the fold toggle, the selection toggle, cursor movement, the action key, and the quit keys.
Compatibility
- Colour output: lipgloss v2 emits truecolor and leaves downsampling to Bubble Tea's renderer, whereas v1 downsampled itself based on the detected profile. On 256-colour or 16-colour terminals the TUI palette may render slightly differently. Non-TUI commands are unaffected.
- The TUI still emits its own terminal queries (synchronised output, kitty keyboard protocol, modifyOtherKeys) when
spec exploreorspec snipactually runs. Those are scoped to the interactive session and no longer happen at process start. .github/dependabot.ymllists only/andjsonschema/oas3/testsundergomod, so the charm dependencies incmd/openapiwill not be bumped automatically. Addingcmd/openapito thatdirectorieslist is a possible follow-up.- golangci-lint's
exhaustivefinding on the oldtea.KeyTypeswitch ininput.gogoes away because the switch is now on strings. No new lint findings are introduced; the remaining hits in the touched files are pre-existing onmain.
Testing
Run from cmd/openapi (the root ./... pattern does not descend into this nested module, so mise test at the root does not exercise it):
cd cmd/openapi
go build ./...
go vet ./...
go test -race -count=1 ./...
go mod tidy && git diff --exit-code go.mod go.sum # tidy is a no-op
gofmt -l . # only commands/overlay/validate.go, pre-existing on main
cd ../.. && golangci-lint run ./cmd/openapi/... # 48 findings, all pre-existing on main (main has 49)
Cross-compiles cleanly for windows/amd64, linux/amd64, linux/arm64 and darwin/amd64.
Reproduction of the issue, stdout on a pty and stdin closed, before (main at e70ae27) and after this branch:
$ printf 'openapi: 3.1.0\ninfo:\n title: t\n version: "1"\npaths: {}\n' > /tmp/spec.yaml
$ /usr/bin/time -p script -q /dev/null ./openapi spec lint /tmp/spec.yaml < /dev/null | cat -v | head -1
Before:
^D^H^H^[]11;?^[\^[[6nLinting OpenAPI document: /tmp/spec.yaml^M
real 5.06
After:
^D^H^HLinting OpenAPI document: /tmp/spec.yaml^M
real 0.57
$ /usr/bin/time -p script -q /dev/null ./openapi --version < /dev/null | cat -v | head -1
Before:
^D^H^H^[]11;?^[\^[[6nv0.0.0-20260920052111-e70ae27ae5d7^M
real 5.02
After:
^D^H^Hv0.0.0-20260920060402-72adb6c288cd^M
real 0.03
The leading ^D^H^H in both is macOS script echoing the EOF from /dev/null and is not produced by the binary. The ^[]11;?^[\ (OSC 11 background query) and ^[[6n (cursor position report) are gone in the after output, and the five-second stall with them.
$ strings ./openapi | grep -c HasDarkBackground
Before: 9. After: 0.
TUI smoke test on a three-operation spec, driven through a pty with a 100x30 window: spec explore enters the alt screen (ESC[?1049h), draws the header and the operation list, j moves the highlight to the second row, space unfolds that row (shows Summary: List users and Operation ID: listUsers), a second space folds it again, and q leaves the alt screen (ESC[?1049l) and exits 0. The main build behaves identically in the same harness.
Summary by cubic
Fixes #250 by migrating cmd/openapi to Bubble Tea v2. Bubble Tea v1 ran a terminal background-color probe during init(), so every openapi command (even --version and spec lint) stalled about five seconds and leaked escape sequences when stdout was a terminal but stdin was not. v2 has no init-time probe; the TUI only queries the terminal when spec explore or spec snip runs.
What changed
- Moves
cmd/openapitocharm.land/bubbletea/v2,charm.land/lipgloss/v2, andcharm.land/bubbles/v2; only this module depends on charm, so the root module is untouched. - Views return
tea.ViewwithAltScreen = trueinstead of usingtea.WithAltScreen(), and key handling matchestea.KeyPressMsgstrings where space is"space". - Adds tests that drive every explorer key handler through
Update, including cursor viewport-bounds assertions.
Compatibility
lipglossv2 leaves color downsampling to Bubble Tea's renderer, so the TUI palette may render slightly differently on 256- or 16-color terminals.- Action keys configured as
" "must be written as"space".