Skip to content

refactor: cache lipgloss styles per theme change; auto-select light theme - #856

Merged
edenreich merged 2 commits into
mainfrom
refactor/832-cache-lipgloss-styles
Jul 11, 2026
Merged

edenreich merged 2 commits into
mainfrom
refactor/832-cache-lipgloss-styles

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Summary

Closes #832.

Style cache (internal/ui/styles/provider.go): all theme-derived styles are pre-baked into a themedStyles struct built once per theme by buildThemedStyles, deduped to 27 fields (methods sharing identical chains share one style). Render* methods only call .Render(); width-parameterized ones apply .Width(w) to the cached base. Invalidation is a lazy theme-name check per access — no event plumbing, so the theme-change flow and all ~15 NewProvider call sites are untouched. Theme-independent styles are package-level vars. NewStyle() now appears only in the builder and that var block (the issue's grep criterion).

Background detection (internal/app/chat.go): when no theme is configured, Init returns tea.RequestBackgroundColor and a light tea.BackgroundColorMsg switches to github-light via the existing updateAllComponentsWithNewTheme() path. The built-in chat.theme default changes from "tokyo-night" to "" = auto (previously "unconfigured" was undetectable); the container already treats "" as "keep the tokyo-night default", so dark terminals see zero change and an explicit config or /theme choice always wins.

Verification

  • A throwaway snapshot harness rendered all 55 Render* calls across all 4 themes before/after the refactor — output was byte-identical.
  • Drove the real TUI in tmux against the mock gateway: live /theme switch to github-light flipped the frame's ANSI palette, proving the cache rebuilds without invalidation wiring.
  • New tests: provider_test.go (cache reuse + rebuild on switch), background_theme_test.go (light/dark/configured-theme branches).
  • task test (51 packages), task lint (0 issues), task test:e2e all pass.

Note: counterfeiter fakes return "" for the theme name, which collided with using "" as the "not built" sentinel — handled with an explicit built flag on the provider.

When no theme is configured, request the terminal background color on
startup (tea.RequestBackgroundColor) and switch to github-light when the
terminal reports a light background. The built-in chat.theme default
changes from "tokyo-night" to "" meaning auto: the dark default is
kept unless a light background is detected; an explicit config or /theme
choice always wins.

Refs #832
Pre-bake all theme-derived styles into a themedStyles struct built once
per theme instead of constructing NewStyle() chains inside every Render*
call per frame. Invalidation is a lazy theme-name check per access, so
the theme-change flow and every NewProvider call site stay untouched.
Rendering output is byte-identical across all four themes.

Closes #832
@edenreich
edenreich requested a review from a team as a code owner July 11, 2026 08:41
@edenreich
edenreich merged commit c80d92d into main Jul 11, 2026
1 check passed
@edenreich
edenreich deleted the refactor/832-cache-lipgloss-styles branch July 11, 2026 08:47
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.142.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REFACTOR] Cache lipgloss styles per theme change; optional terminal background detection

1 participant