Skip to content

Commit fe57be9

Browse files
committed
[fix] add default colors if bg/fg detection fails
1 parent 219a406 commit fe57be9

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

internal/app.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Model struct {
3333
allClusterNamespaces []model.ClusterNamespaces
3434
width, height int
3535
initialized bool
36+
stylesLoaded bool
3637
gotFirstContainers bool
3738
seenFirstContainer bool
3839
toast toast.Model
@@ -69,6 +70,7 @@ func InitialModel(c Config) Model {
6970
func (m Model) Init() (tea.Model, tea.Cmd) {
7071
return m, tea.Batch(
7172
tea.Tick(constants.BatchUpdateLogsInterval, func(t time.Time) tea.Msg { return message.BatchUpdateLogsMsg{} }),
73+
tea.Tick(constants.CheckStylesLoadedDuration, func(t time.Time) tea.Msg { return message.CheckStylesLoadedMsg{} }),
7274
tea.RequestForegroundColor,
7375
tea.RequestBackgroundColor,
7476
)
@@ -83,11 +85,13 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8385

8486
// handle these regardless of m.err
8587
switch msg := msg.(type) {
88+
case tea.KeyMsg:
89+
if key.Matches(msg, m.keyMap.Quit) {
90+
return m, m.cleanupCmd()
91+
}
92+
8693
case message.CleanupCompleteMsg:
8794
return m, tea.Quit
88-
89-
case tea.KeyMsg:
90-
return m.handleKeyMsg(msg)
9195
}
9296

9397
if m.err != nil {
@@ -98,6 +102,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
98102
switch msg := msg.(type) {
99103
case message.ErrMsg:
100104
m.err = msg.Err
105+
return m, nil
101106

102107
case tea.BackgroundColorMsg:
103108
m.termStyleData.SetBackground(msg)
@@ -113,6 +118,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
113118
}
114119
return m, nil
115120

121+
case message.CheckStylesLoadedMsg:
122+
if !m.stylesLoaded {
123+
m.setStyles(style.DefaultStyles)
124+
}
125+
return m, nil
126+
116127
// WindowSizeMsg arrives once on startup, then again every time the window is resized
117128
case tea.WindowSizeMsg:
118129
m.width, m.height = msg.Width, msg.Height
@@ -128,6 +139,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
128139
m.syncDimensions()
129140
return m, tea.Batch(cmds...)
130141

142+
case tea.KeyMsg:
143+
return m.handleKeyMsg(msg)
144+
131145
case message.AttemptUpdateSinceTimeMsg:
132146
m, cmd = m.attemptUpdateSinceTime()
133147
cmds = append(cmds, cmd)
@@ -397,10 +411,6 @@ func (m Model) handleKeyMsg(msg tea.KeyMsg) (Model, tea.Cmd) {
397411
var cmd tea.Cmd
398412
var cmds []tea.Cmd
399413

400-
if key.Matches(msg, m.keyMap.Quit) {
401-
return m, m.cleanupCmd()
402-
}
403-
404414
if !m.initialized {
405415
return m, nil
406416
}
@@ -1086,6 +1096,7 @@ func (m *Model) setFullscreen(fullscreen bool) {
10861096

10871097
func (m *Model) setStyles(styles style.Styles) {
10881098
m.styles = styles
1099+
m.stylesLoaded = true
10891100
m.pages[page.EntitiesPageType] = m.pages[page.EntitiesPageType].WithStyles(styles)
10901101
m.pages[page.LogsPageType] = m.pages[page.LogsPageType].WithStyles(styles)
10911102
m.pages[page.SingleLogPageType] = m.pages[page.SingleLogPageType].WithStyles(styles)

internal/constants/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var GetNextContainerDeltasDuration = 300 * time.Millisecond
2020
// the newly acquired logs from all the containers. In between updates, it accumulates logs from received messages
2121
var BatchUpdateLogsInterval = 200 * time.Millisecond
2222

23+
// CheckStylesLoaded controls how long to wait before checking if terminal foreground/background colors have been detected
24+
var CheckStylesLoadedDuration = 200 * time.Millisecond
25+
2326
// AttemptUpdateSinceTimeInterval controls the cadence at which a "since time" update is attempted
2427
var AttemptUpdateSinceTimeInterval = 500 * time.Millisecond
2528

internal/message/message.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type CleanupCompleteMsg struct{}
88

99
type BatchUpdateLogsMsg struct{}
1010

11+
type CheckStylesLoadedMsg struct{}
12+
1113
type StartMaintainEntitySelectionMsg struct{}
1214

1315
type AttemptUpdateSinceTimeMsg struct{}

internal/style/style.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"image/color"
99
)
1010

11+
var (
12+
blue = lipgloss.Color("6")
13+
lilac = lipgloss.Color("189")
14+
green = lipgloss.Color("46")
15+
)
16+
1117
type TermStyleData struct {
1218
ForegroundDetected bool
1319
Foreground color.Color
@@ -52,6 +58,21 @@ type Styles struct {
5258
RightBorder lipgloss.Style
5359
}
5460

61+
var DefaultStyles = Styles{
62+
Unset: lipgloss.NewStyle(),
63+
Alt: lipgloss.NewStyle().Foreground(lipgloss.Color("#ffffff")),
64+
Bold: lipgloss.NewStyle().Bold(true),
65+
Inverse: lipgloss.NewStyle().Foreground(lipgloss.Color("#000000")).Background(lipgloss.Color("#ffffff")),
66+
BoldUnderline: lipgloss.NewStyle().Bold(true).Underline(true),
67+
InverseUnderline: lipgloss.NewStyle().Foreground(lipgloss.Color("#000000")).Background(lipgloss.Color("#ffffff")).Underline(true),
68+
AltInverse: lipgloss.NewStyle().Foreground(lipgloss.Color("#141414")).Background(lipgloss.Color("#cbcbcb")),
69+
Underline: lipgloss.NewStyle().Underline(true),
70+
Blue: lipgloss.NewStyle().Background(blue).Foreground(lipgloss.Color("#000000")),
71+
Lilac: lipgloss.NewStyle().Background(lilac).Foreground(lipgloss.Color("#000000")),
72+
Green: lipgloss.NewStyle().Background(green).Foreground(lipgloss.Color("#000000")),
73+
RightBorder: lipgloss.NewStyle().Border(lipgloss.ThickBorder(), false, true, false, false).BorderForeground(lilac),
74+
}
75+
5576
func NewStyles(data TermStyleData) Styles {
5677
if !data.IsComplete() {
5778
panic(fmt.Errorf("NewStyles called with incomplete TermStyleData"))
@@ -64,8 +85,6 @@ func NewStyles(data TermStyleData) Styles {
6485
fgLightDark := lipgloss.LightDark(data.ForegroundIsDark)
6586
bgLightDark := lipgloss.LightDark(data.BackgroundIsDark)
6687

67-
lilac := lipgloss.Color("189")
68-
6988
return Styles{
7089
Unset: lipgloss.NewStyle().Foreground(data.Foreground).Background(data.Background),
7190

@@ -85,11 +104,11 @@ func NewStyles(data TermStyleData) Styles {
85104

86105
Underline: lipgloss.NewStyle().Underline(true),
87106

88-
Blue: lipgloss.NewStyle().Background(lipgloss.Color("6")).Foreground(lipgloss.Color("#000000")),
107+
Blue: lipgloss.NewStyle().Background(blue).Foreground(lipgloss.Color("#000000")),
89108

90109
Lilac: lipgloss.NewStyle().Background(lilac).Foreground(lipgloss.Color("#000000")),
91110

92-
Green: lipgloss.NewStyle().Background(lipgloss.Color("46")).Foreground(lipgloss.Color("#000000")),
111+
Green: lipgloss.NewStyle().Background(green).Foreground(lipgloss.Color("#000000")),
93112

94113
RightBorder: lipgloss.NewStyle().Border(lipgloss.ThickBorder(), false, true, false, false).BorderForeground(lilac),
95114
}

0 commit comments

Comments
 (0)