Skip to content

Commit 583b111

Browse files
authored
fix: color styles after filtering (#22)
1 parent 77e2d18 commit 583b111

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

internal/app/app_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,52 @@ func toAppModel(teaModel tea.Model, cmd tea.Cmd) (app.Model, tea.Cmd) {
233233

234234
return appModel, cmd
235235
}
236+
237+
func TestAppViewFilterClear(t *testing.T) {
238+
const termIncluded = "included"
239+
240+
const jsonFile = `
241+
{"time":"1970-01-01T00:00:00.00","level":"INFO","message": "` + termIncluded + `"}
242+
`
243+
244+
appModel := newTestModel(t, []byte(jsonFile))
245+
246+
rendered := appModel.View()
247+
assert.Contains(t, rendered, termIncluded)
248+
249+
// Open filter.
250+
appModel, _ = toAppModel(appModel.Update(tea.KeyMsg{
251+
Type: tea.KeyRunes,
252+
Runes: []rune{'f'},
253+
}))
254+
assert.True(t, appModel.IsFilterShown(), appModel.View())
255+
256+
// Filter to exclude everything.
257+
appModel, _ = toAppModel(appModel.Update(tea.KeyMsg{
258+
Type: tea.KeyRunes,
259+
Runes: []rune(termIncluded + "_not_found"),
260+
}))
261+
appModel, cmd := toAppModel(appModel.Update(tea.KeyMsg{
262+
Type: tea.KeyEnter,
263+
}))
264+
assert.False(t, appModel.IsFilterShown(), appModel.View())
265+
266+
appModel, _ = toAppModel(appModel.Update(cmd()))
267+
268+
rendered = appModel.View()
269+
assert.NotContains(t, rendered, termIncluded)
270+
271+
// Come back
272+
appModel, cmd = toAppModel(appModel.Update(tea.KeyMsg{
273+
Type: tea.KeyEsc,
274+
}))
275+
assert.False(t, appModel.IsFilterShown(), appModel.View())
276+
277+
appModel, _ = toAppModel(appModel.Update(cmd()))
278+
279+
// Assert.
280+
if assert.False(t, appModel.IsFiltered()) {
281+
rendered = appModel.View()
282+
assert.Contains(t, rendered, termIncluded)
283+
}
284+
}

internal/app/handler.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,26 @@ func (m Model) handleWindowSizeMsg(msg tea.WindowSizeMsg) Model {
7070
func (m Model) handleLogEntriesMsg(msg source.LogEntries) Model {
7171
if len(m.allLogEntries) == 0 {
7272
m.allLogEntries = msg
73+
}
7374

74-
tableStyles := getTableStyles()
75-
tableStyles.RenderCell = func(value string, rowID, columnID int) string {
76-
style := tableStyles.Cell
75+
m.table.SetRows(msg.Rows())
76+
m.filteredLogEntries = msg
7777

78-
if columnID == cellIDLogLevel {
79-
return removeClearSequence(
80-
m.getLogLevelStyle(style, rowID).Render(value),
81-
)
82-
}
78+
tableStyles := getTableStyles()
79+
tableStyles.RenderCell = func(value string, rowID, columnID int) string {
80+
style := tableStyles.Cell
8381

84-
return style.Render(value)
82+
if columnID == cellIDLogLevel {
83+
return removeClearSequence(
84+
m.getLogLevelStyle(style, rowID).Render(value),
85+
)
8586
}
8687

87-
m.table.SetStyles(tableStyles)
88+
return style.Render(value)
8889
}
8990

90-
m.table.SetRows(msg.Rows())
91-
m.filteredLogEntries = msg
91+
m.table.SetStyles(tableStyles)
92+
9293
m.table.UpdateViewport()
9394

9495
return m

0 commit comments

Comments
 (0)