Skip to content

Commit dfea3fc

Browse files
committed
feat: Clean up filepanel access
1 parent 4836a1b commit dfea3fc

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

src/internal/file_panel.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,10 @@ func (panel *FilePanel) RefreshData(displayDotFile bool) {
521521
}
522522
panel.LastTimeGetElement = time.Now()
523523
}
524+
525+
func (panel *FilePanel) ResetIndexIfInvalid() {
526+
if panel.Cursor < 0 || panel.Cursor >= panel.ElementCount() {
527+
panel.Cursor = 0
528+
panel.RenderIndex = 0
529+
}
530+
}

src/internal/model.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,7 @@ func (m *model) updateFilePanelsState(msg tea.Msg) tea.Cmd {
436436

437437
// TODO : This is like duct taping a bigger problem
438438
// The code should never reach this state.
439-
if focusPanel.Cursor < 0 {
440-
focusPanel.Cursor = 0
441-
}
439+
focusPanel.ResetIndexIfInvalid()
442440

443441
return cmd
444442
}

src/internal/model_navigation_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ func TestFilePanelNavigation(t *testing.T) {
124124
m := defaultTestModel(tt.startDir)
125125
m.getFocusedFilePanel().Cursor = tt.startCursor
126126
m.getFocusedFilePanel().RenderIndex = tt.startRender
127-
m.getFocusedFilePanel().SearchBar.SetValue("asdf")
128127
for _, s := range tt.keyInput {
129128
TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg(s))
130129
}
@@ -141,7 +140,7 @@ func TestFilePanelNavigation(t *testing.T) {
141140
TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg("cd "+tt.startDir))
142141
TeaUpdateWithErrCheck(m, tea.KeyMsg{Type: tea.KeyEnter})
143142

144-
// Make sure we have original curson and render
143+
// Make sure we have original cursor and render
145144
assert.Equal(t, tt.startCursor, m.getFocusedFilePanel().Cursor)
146145
assert.Equal(t, tt.startRender, m.getFocusedFilePanel().RenderIndex)
147146
})

src/internal/model_render.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ func (m *model) filePanelRender() string {
3232
// check if cursor or render out of range
3333
// TODO - instead of this, have a filepanel.validateAndFix(), and log Error
3434
// This should not ever happen
35-
if filePanel.Cursor > filePanel.ElementCount()-1 {
36-
filePanel.Cursor = 0
37-
filePanel.RenderIndex = 0
38-
}
39-
m.fileModel.filePanels[i] = filePanel
35+
m.fileModel.filePanels[i].ResetIndexIfInvalid()
4036

4137
// TODO : Move this to a utility function and clarify the calculation via comments
4238
// Maybe even write unit tests

0 commit comments

Comments
 (0)