Skip to content

Commit

Permalink
fix: color styles after filtering (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw committed Jul 25, 2023
1 parent 77e2d18 commit 583b111
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
49 changes: 49 additions & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,52 @@ func toAppModel(teaModel tea.Model, cmd tea.Cmd) (app.Model, tea.Cmd) {

return appModel, cmd
}

func TestAppViewFilterClear(t *testing.T) {
const termIncluded = "included"

const jsonFile = `
{"time":"1970-01-01T00:00:00.00","level":"INFO","message": "` + termIncluded + `"}
`

appModel := newTestModel(t, []byte(jsonFile))

rendered := appModel.View()
assert.Contains(t, rendered, termIncluded)

// Open filter.
appModel, _ = toAppModel(appModel.Update(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune{'f'},
}))
assert.True(t, appModel.IsFilterShown(), appModel.View())

// Filter to exclude everything.
appModel, _ = toAppModel(appModel.Update(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune(termIncluded + "_not_found"),
}))
appModel, cmd := toAppModel(appModel.Update(tea.KeyMsg{
Type: tea.KeyEnter,
}))
assert.False(t, appModel.IsFilterShown(), appModel.View())

appModel, _ = toAppModel(appModel.Update(cmd()))

rendered = appModel.View()
assert.NotContains(t, rendered, termIncluded)

// Come back
appModel, cmd = toAppModel(appModel.Update(tea.KeyMsg{
Type: tea.KeyEsc,
}))
assert.False(t, appModel.IsFilterShown(), appModel.View())

appModel, _ = toAppModel(appModel.Update(cmd()))

// Assert.
if assert.False(t, appModel.IsFiltered()) {
rendered = appModel.View()
assert.Contains(t, rendered, termIncluded)
}
}
25 changes: 13 additions & 12 deletions internal/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,26 @@ func (m Model) handleWindowSizeMsg(msg tea.WindowSizeMsg) Model {
func (m Model) handleLogEntriesMsg(msg source.LogEntries) Model {
if len(m.allLogEntries) == 0 {
m.allLogEntries = msg
}

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

if columnID == cellIDLogLevel {
return removeClearSequence(
m.getLogLevelStyle(style, rowID).Render(value),
)
}
tableStyles := getTableStyles()
tableStyles.RenderCell = func(value string, rowID, columnID int) string {
style := tableStyles.Cell

return style.Render(value)
if columnID == cellIDLogLevel {
return removeClearSequence(
m.getLogLevelStyle(style, rowID).Render(value),
)
}

m.table.SetStyles(tableStyles)
return style.Render(value)
}

m.table.SetRows(msg.Rows())
m.filteredLogEntries = msg
m.table.SetStyles(tableStyles)

m.table.UpdateViewport()

return m
Expand Down

0 comments on commit 583b111

Please sign in to comment.