From 6b8948ab6dc9a3a9fcf9f2ce860cf5b1d28f2d37 Mon Sep 17 00:00:00 2001 From: Maksym Kryvchun Date: Mon, 27 May 2024 15:43:23 +0200 Subject: [PATCH] feat(#56): show counter of filtered rows (#70) --- internal/app/helper.go | 2 +- internal/app/state.go | 4 ++-- internal/app/stateerror.go | 16 +++++++------- internal/app/stateerror_test.go | 4 ++-- internal/app/statefiltered.go | 34 ++++++++++++++++------------- internal/app/statefiltered_test.go | 17 ++++++++------- internal/app/statefiltering.go | 24 ++++++++++---------- internal/app/statefiltering_test.go | 22 +++++++++---------- internal/app/stateinitial.go | 16 +++++++------- internal/app/stateinitial_test.go | 6 ++--- internal/app/stateloaded.go | 34 ++++++++++++++--------------- internal/app/stateloaded_test.go | 10 ++++----- internal/app/stateviewrow.go | 22 +++++++++---------- internal/app/stateviewrow_test.go | 8 +++---- 14 files changed, 112 insertions(+), 107 deletions(-) diff --git a/internal/app/helper.go b/internal/app/helper.go index 8f1e44e..9fcad06 100644 --- a/internal/app/helper.go +++ b/internal/app/helper.go @@ -102,7 +102,7 @@ func (h helper) handleLogEntriesLoadedMsg( func (h helper) handleOpenJSONRowRequestedMsg( msg events.OpenJSONRowRequestedMsg, - previousState state, + previousState stateModel, ) (tea.Model, tea.Cmd) { if msg.Index < 0 || msg.Index >= len(msg.LogEntries) { return previousState, nil diff --git a/internal/app/state.go b/internal/app/state.go index 4d3485a..642b4e0 100644 --- a/internal/app/state.go +++ b/internal/app/state.go @@ -6,9 +6,9 @@ import ( tea "github.com/charmbracelet/bubbletea" ) -type state interface { +type stateModel interface { tea.Model fmt.Stringer - withApplication(application Application) (state, tea.Cmd) + withApplication(application Application) (stateModel, tea.Cmd) } diff --git a/internal/app/stateerror.go b/internal/app/stateerror.go index 32778b2..71cade3 100644 --- a/internal/app/stateerror.go +++ b/internal/app/stateerror.go @@ -6,32 +6,32 @@ import ( tea "github.com/charmbracelet/bubbletea" ) -// StateError is a failure message state. -type StateError struct { +// StateErrorModel is a failure message state. +type StateErrorModel struct { helper err error } -func newStateError(application Application, err error) StateError { - return StateError{ +func newStateError(application Application, err error) StateErrorModel { + return StateErrorModel{ helper: helper{Application: application}, err: err, } } // Init initializes component. It implements tea.Model. -func (s StateError) Init() tea.Cmd { +func (s StateErrorModel) Init() tea.Cmd { return nil } // View renders component. It implements tea.Model. -func (s StateError) View() string { +func (s StateErrorModel) View() string { return fmt.Sprintf("Something went wrong: %s.", s.err) } // Update handles events. It implements tea.Model. -func (s StateError) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (s StateErrorModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { s.helper = s.helper.Update(msg) switch msg.(type) { @@ -43,6 +43,6 @@ func (s StateError) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } // String implements fmt.Stringer. -func (s StateError) String() string { +func (s StateErrorModel) String() string { return modelValue(s) } diff --git a/internal/app/stateerror_test.go b/internal/app/stateerror_test.go index 42aa6ef..283f9cf 100644 --- a/internal/app/stateerror_test.go +++ b/internal/app/stateerror_test.go @@ -21,7 +21,7 @@ func TestStateError(t *testing.T) { model := newTestModel(t, assets.ExampleJSONLog()) model = handleUpdate(model, events.ErrorOccuredMsg{Err: errTest}) - _, ok := model.(app.StateError) + _, ok := model.(app.StateErrorModel) assert.Truef(t, ok, "%s", model) t.Run("rendered", func(t *testing.T) { @@ -52,7 +52,7 @@ func TestStateError(t *testing.T) { model := handleUpdate(model, events.ViewRowsReloadRequestedMsg{}) - _, ok := model.(app.StateError) + _, ok := model.(app.StateErrorModel) require.Truef(t, ok, "%s", model) }) } diff --git a/internal/app/statefiltered.go b/internal/app/statefiltered.go index f6e2eda..ed7ecbc 100644 --- a/internal/app/statefiltered.go +++ b/internal/app/statefiltered.go @@ -1,6 +1,8 @@ package app import ( + "fmt" + "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" @@ -8,11 +10,11 @@ import ( "github.com/hedhyw/json-log-viewer/internal/pkg/source" ) -// StateFiltered is a state that shows filtered records. -type StateFiltered struct { +// StateFilteredModel is a state that shows filtered records. +type StateFilteredModel struct { helper - previousState StateLoaded + previousState StateLoadedModel table logsTableModel logEntries source.LazyLogEntries @@ -22,10 +24,10 @@ type StateFiltered struct { func newStateFiltered( application Application, - previousState StateLoaded, + previousState StateLoadedModel, filterText string, -) StateFiltered { - return StateFiltered{ +) StateFilteredModel { + return StateFilteredModel{ helper: helper{Application: application}, previousState: previousState, @@ -37,7 +39,7 @@ func newStateFiltered( } // Init initializes component. It implements tea.Model. -func (s StateFiltered) Init() tea.Cmd { +func (s StateFilteredModel) Init() tea.Cmd { return func() tea.Msg { return events.LogEntriesLoadedMsg( s.previousState.logEntries.Filter(s.filterText), @@ -46,14 +48,16 @@ func (s StateFiltered) Init() tea.Cmd { } // View renders component. It implements tea.Model. -func (s StateFiltered) View() string { - footer := s.Application.FooterStyle.Render(" filtered by: " + s.filterText) +func (s StateFilteredModel) View() string { + footer := s.Application.FooterStyle.Render( + fmt.Sprintf("filtered %d by: %s", len(s.logEntries), s.filterText), + ) return s.BaseStyle.Render(s.table.View()) + "\n" + footer } // Update handles events. It implements tea.Model. -func (s StateFiltered) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (s StateFilteredModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmdBatch []tea.Cmd s.helper = s.helper.Update(msg) @@ -86,7 +90,7 @@ func (s StateFiltered) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return s, tea.Batch(cmdBatch...) } -func (s StateFiltered) handleLogEntriesLoadedMsg( +func (s StateFilteredModel) handleLogEntriesLoadedMsg( msg events.LogEntriesLoadedMsg, ) (tea.Model, tea.Cmd) { s.logEntries = source.LazyLogEntries(msg) @@ -95,7 +99,7 @@ func (s StateFiltered) handleLogEntriesLoadedMsg( return s, s.table.Init() } -func (s StateFiltered) handleFilterKeyClickedMsg() (tea.Model, tea.Cmd) { +func (s StateFilteredModel) handleFilterKeyClickedMsg() (tea.Model, tea.Cmd) { state := newStateFiltering( s.Application, s.previousState, @@ -104,7 +108,7 @@ func (s StateFiltered) handleFilterKeyClickedMsg() (tea.Model, tea.Cmd) { return initializeModel(state) } -func (s StateFiltered) handleRequestOpenJSON() (tea.Model, tea.Cmd) { +func (s StateFilteredModel) handleRequestOpenJSON() (tea.Model, tea.Cmd) { if len(s.logEntries) == 0 { return s, events.BackKeyClicked } @@ -112,7 +116,7 @@ func (s StateFiltered) handleRequestOpenJSON() (tea.Model, tea.Cmd) { return s, events.OpenJSONRowRequested(s.logEntries, s.table.Cursor()) } -func (s StateFiltered) withApplication(application Application) (state, tea.Cmd) { +func (s StateFilteredModel) withApplication(application Application) (stateModel, tea.Cmd) { s.Application = application var cmd tea.Cmd @@ -122,6 +126,6 @@ func (s StateFiltered) withApplication(application Application) (state, tea.Cmd) } // String implements fmt.Stringer. -func (s StateFiltered) String() string { +func (s StateFilteredModel) String() string { return modelValue(s) } diff --git a/internal/app/statefiltered_test.go b/internal/app/statefiltered_test.go index 17a5630..8488b98 100644 --- a/internal/app/statefiltered_test.go +++ b/internal/app/statefiltered_test.go @@ -40,7 +40,7 @@ func TestStateFiltered(t *testing.T) { lines := strings.Split(model.View(), "\n") assert.Contains(t, lines[len(lines)-1], ">") - _, ok := model.(app.StateFiltering) + _, ok := model.(app.StateFilteringModel) assert.Truef(t, ok, "%s", model) // Write term to search by. @@ -54,10 +54,11 @@ func TestStateFiltered(t *testing.T) { Type: tea.KeyEnter, }) - _, ok = model.(app.StateFiltered) + _, ok = model.(app.StateFilteredModel) if assert.Truef(t, ok, "%s", model) { rendered = model.View() assert.Contains(t, rendered, termIncluded) + assert.Contains(t, rendered, "filtered 1 by: "+termIncluded) assert.NotContains(t, rendered, termExcluded) } @@ -69,7 +70,7 @@ func TestStateFiltered(t *testing.T) { Runes: []rune{'f'}, }) - _, ok := model.(app.StateFiltering) + _, ok := model.(app.StateFilteringModel) assert.Truef(t, ok, "%s", model) }) @@ -80,14 +81,14 @@ func TestStateFiltered(t *testing.T) { Type: tea.KeyEnter, }) - _, ok := model.(app.StateViewRow) + _, ok := model.(app.StateViewRowModel) assert.Truef(t, ok, "%s", model) model = handleUpdate(model, tea.KeyMsg{ Type: tea.KeyEsc, }) - _, ok = model.(app.StateFiltered) + _, ok = model.(app.StateFilteredModel) assert.Truef(t, ok, "%s", model) }) @@ -96,7 +97,7 @@ func TestStateFiltered(t *testing.T) { model := handleUpdate(model, events.ErrorOccuredMsg{Err: getTestError()}) - _, ok := model.(app.StateError) + _, ok := model.(app.StateErrorModel) assert.Truef(t, ok, "%s", model) }) @@ -107,7 +108,7 @@ func TestStateFiltered(t *testing.T) { Type: tea.KeyUp, }) - _, ok = model.(app.StateFiltered) + _, ok = model.(app.StateFilteredModel) assert.Truef(t, ok, "%s", model) }) @@ -118,7 +119,7 @@ func TestStateFiltered(t *testing.T) { Type: tea.KeyEsc, }) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) assert.Truef(t, ok, "%s", model) }) diff --git a/internal/app/statefiltering.go b/internal/app/statefiltering.go index db08300..504ede3 100644 --- a/internal/app/statefiltering.go +++ b/internal/app/statefiltering.go @@ -8,11 +8,11 @@ import ( "github.com/hedhyw/json-log-viewer/internal/pkg/events" ) -// StateFiltering is a state to prompt for filter term. -type StateFiltering struct { +// StateFilteringModel is a state to prompt for filter term. +type StateFilteringModel struct { helper - previousState StateLoaded + previousState StateLoadedModel table logsTableModel textInput textinput.Model @@ -21,12 +21,12 @@ type StateFiltering struct { func newStateFiltering( application Application, - previousState StateLoaded, -) StateFiltering { + previousState StateLoadedModel, +) StateFilteringModel { textInput := textinput.New() textInput.Focus() - return StateFiltering{ + return StateFilteringModel{ helper: helper{Application: application}, previousState: previousState, @@ -38,17 +38,17 @@ func newStateFiltering( } // Init initializes component. It implements tea.Model. -func (s StateFiltering) Init() tea.Cmd { +func (s StateFilteringModel) Init() tea.Cmd { return nil } // View renders component. It implements tea.Model. -func (s StateFiltering) View() string { +func (s StateFilteringModel) View() string { return s.BaseStyle.Render(s.table.View()) + "\n" + s.textInput.View() } // Update handles events. It implements tea.Model. -func (s StateFiltering) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (s StateFilteringModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmdBatch []tea.Cmd s.helper = s.helper.Update(msg) @@ -76,7 +76,7 @@ func (s StateFiltering) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return s, tea.Batch(cmdBatch...) } -func (s StateFiltering) handleKeyMsg(msg tea.KeyMsg) tea.Cmd { +func (s StateFilteringModel) handleKeyMsg(msg tea.KeyMsg) tea.Cmd { if len(msg.Runes) == 1 { return nil } @@ -84,7 +84,7 @@ func (s StateFiltering) handleKeyMsg(msg tea.KeyMsg) tea.Cmd { return s.helper.handleKeyMsg(msg) } -func (s StateFiltering) handleEnterKeyClickedMsg() (tea.Model, tea.Cmd) { +func (s StateFilteringModel) handleEnterKeyClickedMsg() (tea.Model, tea.Cmd) { if s.textInput.Value() == "" { return s, events.BackKeyClicked } @@ -97,6 +97,6 @@ func (s StateFiltering) handleEnterKeyClickedMsg() (tea.Model, tea.Cmd) { } // String implements fmt.Stringer. -func (s StateFiltering) String() string { +func (s StateFilteringModel) String() string { return modelValue(s) } diff --git a/internal/app/statefiltering_test.go b/internal/app/statefiltering_test.go index dd8739f..102a2e0 100644 --- a/internal/app/statefiltering_test.go +++ b/internal/app/statefiltering_test.go @@ -23,7 +23,7 @@ func TestStateFiltering(t *testing.T) { Type: tea.KeyRunes, Runes: []rune{'f'}, }) - _, ok := model.(app.StateFiltering) + _, ok := model.(app.StateFilteringModel) assert.Truef(t, ok, "%s", model) t.Run("input_hotkeys", func(t *testing.T) { @@ -39,7 +39,7 @@ func TestStateFiltering(t *testing.T) { Runes: []rune{'f'}, }) - _, ok := model.(app.StateFiltering) + _, ok := model.(app.StateFilteringModel) assert.Truef(t, ok, "%s", model) }) @@ -50,7 +50,7 @@ func TestStateFiltering(t *testing.T) { Type: tea.KeyEsc, }) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) assert.Truef(t, ok, "%s", model) }) @@ -61,7 +61,7 @@ func TestStateFiltering(t *testing.T) { Type: tea.KeyEnter, }) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) require.Truef(t, ok, "%s", model) }) @@ -79,7 +79,7 @@ func TestStateFiltering(t *testing.T) { model := handleUpdate(model, events.ErrorOccuredMsg{Err: getTestError()}) - _, ok := model.(app.StateError) + _, ok := model.(app.StateErrorModel) assert.Truef(t, ok, "%s", model) }) @@ -90,7 +90,7 @@ func TestStateFiltering(t *testing.T) { Type: tea.KeyUp, }) - _, ok := model.(app.StateFiltering) + _, ok := model.(app.StateFilteringModel) assert.Truef(t, ok, "%s", model) }) } @@ -115,7 +115,7 @@ func TestStateFilteringReset(t *testing.T) { Runes: []rune{'f'}, }) - _, ok := model.(app.StateFiltering) + _, ok := model.(app.StateFilteringModel) assert.Truef(t, ok, "%s", model) // Filter to exclude everything. @@ -127,7 +127,7 @@ func TestStateFilteringReset(t *testing.T) { Type: tea.KeyEnter, }) - _, ok = model.(app.StateFiltered) + _, ok = model.(app.StateFilteredModel) assert.Truef(t, ok, "%s", model) t.Run("record_not_included", func(t *testing.T) { @@ -135,7 +135,7 @@ func TestStateFilteringReset(t *testing.T) { rendered := model.View() - index := strings.Index(rendered, "filtered by:") + index := strings.Index(rendered, "filtered 0 by:") if assert.Greater(t, index, 0) { rendered = rendered[:index] } @@ -147,7 +147,7 @@ func TestStateFilteringReset(t *testing.T) { Type: tea.KeyEsc, }) - _, ok = model.(app.StateLoaded) + _, ok = model.(app.StateLoadedModel) assert.Truef(t, ok, "%s", model) // Assert. @@ -165,7 +165,7 @@ func TestStateFilteringReset(t *testing.T) { assert.NotNil(t, model) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) assert.Truef(t, ok, "%s", model) }) } diff --git a/internal/app/stateinitial.go b/internal/app/stateinitial.go index 6a49a0c..7b7d133 100644 --- a/internal/app/stateinitial.go +++ b/internal/app/stateinitial.go @@ -8,29 +8,29 @@ import ( "github.com/hedhyw/json-log-viewer/internal/pkg/events" ) -// StateInitial is an initial loading state. -type StateInitial struct { +// StateInitialModel is an initial loading state. +type StateInitialModel struct { helper } -func newStateInitial(application Application) StateInitial { - return StateInitial{ +func newStateInitial(application Application) StateInitialModel { + return StateInitialModel{ helper: helper{Application: application}, } } // Init initializes component. It implements tea.Model. -func (s StateInitial) Init() tea.Cmd { +func (s StateInitialModel) Init() tea.Cmd { return s.helper.LoadEntries } // View renders component. It implements tea.Model. -func (s StateInitial) View() string { +func (s StateInitialModel) View() string { return "Loading..." } // Update handles events. It implements tea.Model. -func (s StateInitial) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (s StateInitialModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { s.helper = s.helper.Update(msg) switch msg := msg.(type) { @@ -46,6 +46,6 @@ func (s StateInitial) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } // String implements fmt.Stringer. -func (s StateInitial) String() string { +func (s StateInitialModel) String() string { return modelValue(s) } diff --git a/internal/app/stateinitial_test.go b/internal/app/stateinitial_test.go index 699f518..2a46a7b 100644 --- a/internal/app/stateinitial_test.go +++ b/internal/app/stateinitial_test.go @@ -18,7 +18,7 @@ func TestStateInitial(t *testing.T) { model := app.NewModel("", config.GetDefaultConfig()) - _, ok := model.(app.StateInitial) + _, ok := model.(app.StateInitialModel) require.Truef(t, ok, "%s", model) t.Run("stringer", func(t *testing.T) { @@ -35,7 +35,7 @@ func TestStateInitial(t *testing.T) { model := handleUpdate(model, events.ErrorOccuredMsg{Err: getTestError()}) - _, ok := model.(app.StateError) + _, ok := model.(app.StateErrorModel) assert.Truef(t, ok, "%s", model) }) @@ -54,7 +54,7 @@ func TestStateInitial(t *testing.T) { model := handleUpdate(model, events.ViewRowsReloadRequestedMsg{}) - _, ok := model.(app.StateInitial) + _, ok := model.(app.StateInitialModel) require.Truef(t, ok, "%s", model) }) } diff --git a/internal/app/stateloaded.go b/internal/app/stateloaded.go index bc1d1e3..0a50f66 100644 --- a/internal/app/stateloaded.go +++ b/internal/app/stateloaded.go @@ -11,8 +11,8 @@ import ( "github.com/hedhyw/json-log-viewer/internal/pkg/source" ) -// StateLoaded is a state that shows all loaded records. -type StateLoaded struct { +// StateLoadedModel is a state that shows all loaded records. +type StateLoadedModel struct { helper initCmd tea.Cmd @@ -30,10 +30,10 @@ func newStateViewLogs( application Application, logEntries source.LazyLogEntries, lastReloadAt time.Time, -) StateLoaded { +) StateLoadedModel { table := newLogsTableModel(application, logEntries) - return StateLoaded{ + return StateLoadedModel{ helper: helper{Application: application}, initCmd: table.Init(), @@ -49,12 +49,12 @@ func newStateViewLogs( } // Init initializes component. It implements tea.Model. -func (s StateLoaded) Init() tea.Cmd { +func (s StateLoadedModel) Init() tea.Cmd { return s.initCmd } // View renders component. It implements tea.Model. -func (s StateLoaded) View() string { +func (s StateLoadedModel) View() string { if s.reloading { return s.viewTable() + "\nreloading..." } @@ -62,18 +62,18 @@ func (s StateLoaded) View() string { return s.viewTable() + s.viewHelp() } -func (s StateLoaded) viewTable() string { +func (s StateLoadedModel) viewTable() string { return s.BaseStyle.Render(s.table.View()) } -func (s StateLoaded) viewHelp() string { +func (s StateLoadedModel) viewHelp() string { return "\n" + s.help.View(s.keys) } // Update handles events. It implements tea.Model. // // nolint: cyclop // Many events in switch case. -func (s StateLoaded) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (s StateLoadedModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmdBatch []tea.Cmd s.helper = s.helper.Update(msg) @@ -108,7 +108,7 @@ func (s StateLoaded) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return s, tea.Batch(cmdBatch...) } -func (s StateLoaded) handleKeyMsg(msg tea.KeyMsg) []tea.Cmd { +func (s StateLoadedModel) handleKeyMsg(msg tea.KeyMsg) []tea.Cmd { var cmdBatch []tea.Cmd cmdBatch = appendCmd(cmdBatch, s.helper.handleKeyMsg(msg)) @@ -120,7 +120,7 @@ func (s StateLoaded) handleKeyMsg(msg tea.KeyMsg) []tea.Cmd { return cmdBatch } -func (s StateLoaded) handleArrowUpKeyClicked() tea.Cmd { +func (s StateLoadedModel) handleArrowUpKeyClicked() tea.Cmd { if s.table.Cursor() == 0 { return events.ViewRowsReloadRequested } @@ -128,7 +128,7 @@ func (s StateLoaded) handleArrowUpKeyClicked() tea.Cmd { return nil } -func (s StateLoaded) handleRequestOpenJSON() (tea.Model, tea.Cmd) { +func (s StateLoadedModel) handleRequestOpenJSON() (tea.Model, tea.Cmd) { if len(s.logEntries) == 0 { return s, tea.Quit } @@ -136,7 +136,7 @@ func (s StateLoaded) handleRequestOpenJSON() (tea.Model, tea.Cmd) { return s, events.OpenJSONRowRequested(s.logEntries, s.table.Cursor()) } -func (s StateLoaded) handleViewRowsReloadRequestedMsg() (tea.Model, tea.Cmd) { +func (s StateLoadedModel) handleViewRowsReloadRequestedMsg() (tea.Model, tea.Cmd) { if time.Since(s.lastReloadAt) < s.Config.ReloadThreshold { return s, nil } @@ -147,11 +147,11 @@ func (s StateLoaded) handleViewRowsReloadRequestedMsg() (tea.Model, tea.Cmd) { return s, s.helper.LoadEntries } -func (s StateLoaded) handleFilterKeyClickedMsg() (tea.Model, tea.Cmd) { +func (s StateLoadedModel) handleFilterKeyClickedMsg() (tea.Model, tea.Cmd) { return initializeModel(newStateFiltering(s.helper.Application, s)) } -func (s StateLoaded) withApplication(application Application) (state, tea.Cmd) { +func (s StateLoadedModel) withApplication(application Application) (stateModel, tea.Cmd) { s.helper.Application = application var cmd tea.Cmd @@ -161,10 +161,10 @@ func (s StateLoaded) withApplication(application Application) (state, tea.Cmd) { } // String implements fmt.Stringer. -func (s StateLoaded) String() string { +func (s StateLoadedModel) String() string { return modelValue(s) } -func (s StateLoaded) Application() Application { +func (s StateLoadedModel) Application() Application { return s.helper.Application } diff --git a/internal/app/stateloaded_test.go b/internal/app/stateloaded_test.go index 21c0cd8..f56f684 100644 --- a/internal/app/stateloaded_test.go +++ b/internal/app/stateloaded_test.go @@ -22,7 +22,7 @@ func TestStateLoadedEmpty(t *testing.T) { model := newTestModel(t, []byte("")) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) require.Truef(t, ok, "%s", model) model, cmd := model.Update(events.EnterKeyClicked()) @@ -37,7 +37,7 @@ func TestStateLoaded(t *testing.T) { model := newTestModel(t, []byte(jsonFile)) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) require.Truef(t, ok, "%s", model) t.Run("stringer", func(t *testing.T) { @@ -54,7 +54,7 @@ func TestStateLoaded(t *testing.T) { model := handleUpdate(model, events.ErrorOccuredMsg{Err: getTestError()}) - _, ok = model.(app.StateError) + _, ok = model.(app.StateErrorModel) assert.Truef(t, ok, "%s", model) }) } @@ -190,7 +190,7 @@ func BenchmarkStateLoadedBig(b *testing.B) { model := newTestModel(b, []byte(`{}`)) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) if !ok { b.Fatal(model.View()) } @@ -208,7 +208,7 @@ func BenchmarkStateLoadedBig(b *testing.B) { func overwriteFileInStateLoaded(tb testing.TB, model tea.Model, content []byte) { tb.Helper() - stateLoaded, ok := model.(app.StateLoaded) + stateLoaded, ok := model.(app.StateLoadedModel) require.True(tb, ok) err := os.WriteFile( diff --git a/internal/app/stateviewrow.go b/internal/app/stateviewrow.go index 16ae580..d7d6e10 100644 --- a/internal/app/stateviewrow.go +++ b/internal/app/stateviewrow.go @@ -9,11 +9,11 @@ import ( "github.com/hedhyw/json-log-viewer/internal/pkg/widgets" ) -// StateViewRow is a state that shows extended JSON view. -type StateViewRow struct { +// StateViewRowModel is a state that shows extended JSON view. +type StateViewRowModel struct { helper - previousState state + previousState stateModel initCmd tea.Cmd logEntry source.LogEntry @@ -25,11 +25,11 @@ type StateViewRow struct { func newStateViewRow( application Application, logEntry source.LogEntry, - previousState state, -) StateViewRow { + previousState stateModel, +) StateViewRowModel { jsonViewModel, cmd := widgets.NewJSONViewModel(logEntry.Line, application.LastWindowSize) - return StateViewRow{ + return StateViewRowModel{ helper: helper{Application: application}, previousState: previousState, @@ -43,17 +43,17 @@ func newStateViewRow( } // Init initializes component. It implements tea.Model. -func (s StateViewRow) Init() tea.Cmd { +func (s StateViewRowModel) Init() tea.Cmd { return s.initCmd } // View renders component. It implements tea.Model. -func (s StateViewRow) View() string { +func (s StateViewRowModel) View() string { return s.jsonView.View() } // Update handles events. It implements tea.Model. -func (s StateViewRow) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (s StateViewRowModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd s.helper = s.helper.Update(msg) @@ -76,7 +76,7 @@ func (s StateViewRow) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return s, cmd } -func (s StateViewRow) handleKeyMsg(msg tea.KeyMsg) tea.Cmd { +func (s StateViewRowModel) handleKeyMsg(msg tea.KeyMsg) tea.Cmd { if key.Matches(msg, s.keys.ToggleViewArrow) { return nil } @@ -85,6 +85,6 @@ func (s StateViewRow) handleKeyMsg(msg tea.KeyMsg) tea.Cmd { } // String implements fmt.Stringer. -func (s StateViewRow) String() string { +func (s StateViewRowModel) String() string { return modelValue(s) } diff --git a/internal/app/stateviewrow_test.go b/internal/app/stateviewrow_test.go index d7acd09..954df85 100644 --- a/internal/app/stateviewrow_test.go +++ b/internal/app/stateviewrow_test.go @@ -20,14 +20,14 @@ func TestStateViewRow(t *testing.T) { model = handleUpdate(model, tea.KeyMsg{Type: tea.KeyEnter}) - _, ok := model.(app.StateViewRow) + _, ok := model.(app.StateViewRowModel) require.Truef(t, ok, "%s", model) t.Run("close", func(t *testing.T) { t.Parallel() model := handleUpdate(model, tea.KeyMsg{Type: tea.KeyEnter}) - _, ok := model.(app.StateLoaded) + _, ok := model.(app.StateLoadedModel) require.Truef(t, ok, "%s", model) }) @@ -45,7 +45,7 @@ func TestStateViewRow(t *testing.T) { model := handleUpdate(model, events.ErrorOccuredMsg{Err: getTestError()}) - _, ok := model.(app.StateError) + _, ok := model.(app.StateErrorModel) assert.Truef(t, ok, "%s", model) }) @@ -55,7 +55,7 @@ func TestStateViewRow(t *testing.T) { Type: tea.KeyRight, }) - _, ok := model.(app.StateViewRow) + _, ok := model.(app.StateViewRowModel) assert.Truef(t, ok, "%s", model) }) }