feat(ui): scaffold internal/ui with Writer + theme + mode (#74 PR1)#102
Merged
Conversation
Establishes the single source of truth for user-facing output ahead of the larger #74 migration. Four files in a new internal/ui package: - mode.go: Mode type (PlainMode / FancyMode) and the single DecideMode decision point (--no-color, NO_COLOR env, TTY detection for stdout + stdin). Centralizing the TTY check here keeps lipgloss's color profile detection and our gating in sync. - theme.go: DefaultTheme with 6 lipgloss styles (Success/Error/Warning/Info/Dim/Heading). Muted colors so output reads as informational, not decorative. - writer.go: Writer interface (6 methods), NewWriter constructor, plain + fancy implementations. FancyMode forces termenv.TrueColor so ANSI escapes emit even when stdout is not a TTY (the test-only escape hatch). - writer_test.go: 5 tests pinning the routing rules (stdout vs stderr per level, trailing-newline contract, ANSI + glyph presence in FancyMode, NO_COLOR override, Mode.String). Root.go's PersistentPreRunE now resolves the Writer once per invocation and stashes it on cmd.Context() under an unexported key. warnInterruptedUpgrade takes the Writer and routes its hint lines through w.Warn. Subcommands read it back via writerFromCmd(cmd). nodeup version is the proof-of-concept migration: every fmt.Fprintf becomes w.Println(fmt.Sprintf(...)). The byte-level output shape is identical (Println renders raw bytes in both modes), so the existing TestVersionCmd_* tests pass unchanged. A new TestVersionCmd_UsesInjectedWriter exercises the NewRootCmd wiring end-to-end and pins the PlainMode byte contract (no ANSI, no glyphs). Closes #74 (PR1 of 4).
|
✔️ c207a05 - Conventional commits check succeeded. |
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
Scaffolds the new
internal/uipackage and migratesnodeup versionto use it, closing #74 PR1 of 4. PR1 establishes the Writer surface
(theme + mode decision + plain/fancy implementations) so the rest of
the migration has a well-defined dependency to move call-sites to in
PR2 (spinners), PR3 (interactive prompts), and PR4 (the remaining
direct-print sites in upgrade/check/list/packages/config/report).
What this PR does
internal/ui/mode.go— newModetype(
PlainMode/FancyMode) and a singleDecideMode(noColor bool)decision point. The rules:
--no-colorflag → PlainMode,NO_COLORenv → PlainMode, non-TTY stdout/stdin → PlainMode, otherwise
FancyMode. TTY detection is centralized here (a small
isTerminalhelper aroundos.File.Stat()checkingos.ModeCharDevice) so lipgloss's own detection and our gatingnever disagree.
internal/ui/theme.go—Themestruct holding 6 lipglossstyles (
Success,Error,Warning,Info,Dim,Heading)and
DefaultTheme()constructor. Colors deliberately muted —nodeup output is informational, not decorative. The accent
colors (success green, error red, warning amber) are
AA-readable against the standard dark terminal background.
internal/ui/writer.go—Writerinterface with 6 methods(
Success/Info/Warn/Error/Println/Mode),NewWriterconstructor, and two implementations:
plainWriter(every methodis a single
io.WriteString, no decoration) andfancyWriter(each method renders the message with the corresponding
Themestyle).NewWriterforceslipgloss.SetColorProfile(termenv.TrueColor)in FancyMode soANSI escapes are emitted even when stdout isn't a real TTY (the
test-only escape hatch the package's tests rely on).
internal/cli/root.go—PersistentPreRunEnow resolvesthe active
ui.Writeronce per invocation (callsui.DecideMode(--no-color)and constructs the right Writer) andstashes it on
cmd.Context()under an unexportedwriterCtxKey{}key. Subcommands pull it out via awriterFromCmd(cmd)helper.warnInterruptedUpgradenow takesthe writer and routes its hint lines through
w.Warn.internal/cli/version.go— the PoC migration. Everyfmt.Fprintf(out, ...)becomesw.Println(fmt.Sprintf(...)).The byte-level output shape stays identical (version.go calls
Printlnwhich renders raw bytes in both modes), so theexisting
TestVersionCmd_*tests pass unchanged.internal/cli/version_test.go— addedTestVersionCmd_UsesInjectedWriterwhich constructs a fullNewRootCmd(...)(so PersistentPreRunE runs) and exercisesnodeup versionend-to-end, confirming the Writer is correctlythreaded and the PlainMode output is byte-clean (no ANSI, no
glyphs).
CHANGELOG.md— entry under[Unreleased] / ### Added.What this PR does NOT do (by design)
upgrade.go'slong-running steps is PR2.
cleanup.go'sy/N + any manager-picker is PR3.
check.go,list.go,packages.go,config.go,root.go(the rest), and the finalreport.gosummary block move to the new Writer in PR4.
be re-pulled by
go mod tidyin PR2/PR3 when the spinner /prompt methods are actually used.
Verification
Coverage went from 59.6% → 62.2% (the new ui package brings 67.7%
on its own, and the cli package climbed from 31.1% to 40.1% because
the new TestVersionCmd_UsesInjectedWriter covers NewRootCmd's
writer-wiring path).
Refs