Skip to content

Commit

Permalink
refactor: goDown to it's own method
Browse files Browse the repository at this point in the history
  • Loading branch information
ALX99 committed May 19, 2024
1 parent 6606537 commit a5aaf03
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions internal/models/main_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,49 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case m.cfg.Settings.Keymap.NavUp:
m.cursor.r = max(0, m.cursor.r-1)

case m.cfg.Settings.Keymap.NavDown:
if m.cursor.c == 0 {
m.cursor.r = min(m.cursor.r+1, min(len(m.files)-1, m.maxRows-1))
} else {
if m.cursorOffset() < len(m.files)-1 {
m.cursor.r = min(m.cursor.r+1, m.maxRows-1)
}
}
return m, nil
return m.goDown(), nil

case m.cfg.Settings.Keymap.NavLeft:
m.cursor.c = max(0, m.cursor.c-1)

return m, nil
case m.cfg.Settings.Keymap.NavRight:
m.cursor.c++
if m.cursorOffset() >= len(m.files) {
m.cursor.c--
}

return m, nil
case m.cfg.Settings.Keymap.NavOut:
return m, m.loadDir(path.Dir(m.cwd))

case m.cfg.Settings.Keymap.NavIn:
if len(m.files) > 0 && m.files[m.cursorOffset()].IsDir() {
return m, m.loadDir(path.Join(m.cwd, m.files[m.cursorOffset()].Name()))
}

case m.cfg.Settings.Keymap.NavHome:
home, err := os.UserHomeDir()
if err != nil {
m.lastError = err
return m, nil
}
return m, m.loadDir(home)

case m.cfg.Settings.Keymap.Delete:
if len(m.files) <= 0 {
return m, nil
}
// we optimistically believe that the file will be deleted
delete(m.cachedDirSelections, m.cwd)
return m, sequentially(
func() tea.Msg {
return osi.RemoveAll(path.Join(m.cwd, m.files[m.cursorOffset()].Name()))
},
m.loadDir(m.cwd),
)
if len(m.files) > 0 {
// we optimistically believe that the file will be deleted
delete(m.cachedDirSelections, m.cwd)
Expand Down Expand Up @@ -328,6 +338,17 @@ func (m Model) writeLastWD() error {
return err
}

func (m Model) goDown() Model {
if m.cursor.c == 0 {
m.cursor.r = min(m.cursor.r+1, min(len(m.files)-1, m.maxRows-1))
} else {
if m.cursorOffset() < len(m.files)-1 {
m.cursor.r = min(m.cursor.r+1, m.maxRows-1)
}
}
return m
}

// sequentially produces a command that sequentially executes the given
// commands.
// The tea.Msg returned is the first non-nil message returned by a Cmd.
Expand Down

0 comments on commit a5aaf03

Please sign in to comment.