refactor(tui): split god-package into per-mode handlers#32
Open
axelgar wants to merge 1 commit into
Open
Conversation
The TUI was a single Model with 50+ flat fields, one 677-line Update(), one 514-line View(), and 11 modes interleaved via boolean flag checks. Refactoring was risky and no mode had isolated tests. Stage 1a: introduce a Mode enum and dispatcher. Update() and View() now route through `switch m.mode` instead of overlapping if-chains. Stage 3: extract per-mode files (mode_<mode>.go) for list, create, create-from-remote, delete, diff, pr-creating, pr-generating, agent-select, and error-log. view.go shrinks to 27 lines; update.go keeps only non-key message handlers and shared helpers. Stage 2: replace flat fields with typed sub-structs (listState, createState, deleteState, diffState, prState, agentSelectState, errorLogState). Active-mode booleans (creating, deleting, …) are gone; m.mode is the single source of truth. Adds resetToList() for exhaustive recovery to the list view. Stage 4: extract test helpers to helpers_test.go. Add regression tests covering three latent bugs that the dispatcher made visible: - Bug A: errMsg used to clear only some mode flags. resetToList() now exhaustively zeroes every per-mode sub-struct. - Bug B: prGenerating did not gate keys, so pressing 'n' during PR generation opened the create dialog on top of the loading screen. ModePRGenerating now has its own handler that swallows non-esc keys. - Bug C: a late prContentGeneratedMsg arriving after esc would silently flip into ModePRCreating. esc now sets pr.cancelled and the message handler drops the late content. All 237 tests across 10 packages pass. go vet clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pkg/tui/(one 677-lineUpdate(), one 514-lineView(), 50+ flat fields onModel, 10 interleaved modes) into aMode-dispatched architecture with per-mode handler files (mode_<mode>.go) and typed per-mode state sub-structs.view.goshrinks from 514 to 27 lines;update.gofrom 677 to 314 (the rest is non-key message handlers, which deliberately stay in the dispatcher). Biggest single file is nowmode_list.goat ~446 lines.errMsgreset,prGeneratingnot gating keys, and a PR-content async race after esc.What changed
Modeenum + dispatcherUpdate/Viewroute viaswitch m.modemode_list.go,mode_create.go,mode_create_remote.go,mode_delete.go,mode_diff.go,mode_pr_creating.go,mode_pr_generating.go,mode_agentselect.go,mode_errorlog.golistState,createState,deleteState,diffState,prState,agentSelectState,errorLogState. Active-mode booleans removed;m.modeis the single source of truthhelpers_test.gofor shared test helpers; newmode_pr_generating_test.gowith regression tests for bugs A/B/CLatent bugs fixed
errMsgpreviously cleared onlycreating,filtering,prCreatingwhile leavingdeleting,diffViewing,agentSelecting,showErrLog,prGenerating, etc. hanging. Newm.resetToList()zeroes every per-mode sub-struct.prGeneratinghad no key guard, so pressingn/i/rduring PR content generation stacked a create dialog over the loading screen.ModePRGeneratingnow has its own handler that swallows non-esc keys.prContentGeneratedMsgarriving after the user pressedescsilently flipped intoModePRCreating.escnow setspr.cancelled; the message handler drops the late content and resets the flag.Test plan
go test ./...— all 237 tests pass across 10 packages (96 existing TUI + 8 new regression tests for bugs A/B/C)go vet ./...clean./opentreeand exercise each mode end-to-end:/), error log (E)n(plain),i(issue),r(remote)x(single + batch)dp(verify generating screen swallows keys; esc cancels even if content arrives later)A🤖 Generated with Claude Code