Skip to content

Commit 03c78f2

Browse files
committed
fix(startup): cache terminal background detection
1 parent 4d05650 commit 03c78f2

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

cmd/jjui/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,22 @@ func run() int {
162162
fmt.Fprintf(os.Stderr, "Error loading config.lua: %v\n", err)
163163
return 1
164164
} else if luaSource != "" {
165+
appContext.TerminalHasDarkBackground = lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
166+
appContext.TerminalThemeDetected = true
165167
if err := scripting.RunSetup(appContext, config.Current, luaSource); err != nil {
166168
fmt.Fprintf(os.Stderr, "Error in config.lua: %v\n", err)
167169
return 1
168170
}
169171
}
170172

171173
var theme map[string]config.Color
174+
if !appContext.TerminalThemeDetected {
175+
appContext.TerminalHasDarkBackground = lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
176+
appContext.TerminalThemeDetected = true
177+
}
172178

173179
var defaultThemeName string
174-
if lipgloss.HasDarkBackground(os.Stdin, os.Stdout) {
180+
if appContext.TerminalHasDarkBackground {
175181
defaultThemeName = "default_dark"
176182
} else {
177183
defaultThemeName = "default_light"
@@ -184,7 +190,7 @@ func run() int {
184190
}
185191

186192
var userThemeName string
187-
if lipgloss.HasDarkBackground(os.Stdin, os.Stdout) {
193+
if appContext.TerminalHasDarkBackground {
188194
userThemeName = config.Current.UI.Theme.Dark
189195
} else {
190196
userThemeName = config.Current.UI.Theme.Light

internal/scripting/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package scripting
22

33
import (
44
"fmt"
5-
"os"
65
"path/filepath"
76
"strings"
87

9-
"charm.land/lipgloss/v2"
108
"github.com/idursun/jjui/internal/config"
119
uicontext "github.com/idursun/jjui/internal/ui/context"
1210
lua "github.com/yuin/gopher-lua"
@@ -61,7 +59,7 @@ func RunSetup(ctx *uicontext.MainContext, current *config.Config, source string)
6159
configTable.RawSetString("repo", lua.LString(ctx.Location))
6260

6361
terminalTable := L.NewTable()
64-
terminalTable.RawSetString("dark_mode", lua.LBool(lipgloss.HasDarkBackground(os.Stdin, os.Stdout)))
62+
terminalTable.RawSetString("dark_mode", lua.LBool(ctx.TerminalHasDarkBackground))
6563
terminalTable.RawSetString("bg", lua.LString(""))
6664
terminalTable.RawSetString("fg", lua.LString(""))
6765
configTable.RawSetString("terminal", terminalTable)

internal/ui/context/main_context.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ type SelectedOperation = common.SelectedOperation
2323

2424
type MainContext struct {
2525
CommandRunner
26-
SelectedItem SelectedItem // Single item where cursor is hover.
27-
CheckedItems []SelectedItem // Items checked ✓ by the user.
28-
Location string
29-
JJConfig *config.JJConfig
30-
DefaultRevset string
31-
CurrentRevset string
32-
Histories *config.Histories
33-
ScriptVM *lua.LState
26+
SelectedItem SelectedItem // Single item where cursor is hover.
27+
CheckedItems []SelectedItem // Items checked ✓ by the user.
28+
Location string
29+
JJConfig *config.JJConfig
30+
DefaultRevset string
31+
CurrentRevset string
32+
TerminalHasDarkBackground bool
33+
TerminalThemeDetected bool
34+
Histories *config.Histories
35+
ScriptVM *lua.LState
3436
}
3537

3638
func NewAppContext(location string, aps *askpass.Server) *MainContext {

0 commit comments

Comments
 (0)