Skip to content

Commit

Permalink
fix: row/col mixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ALX99 committed May 18, 2024
1 parent 34a7b60 commit c10ae91
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 28 deletions.
54 changes: 39 additions & 15 deletions internal/models/main_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type dirLoaded struct {
type clearPrevCWD struct{}

type position struct {
c, r int
r, c int
}

type Model struct {
Expand Down Expand Up @@ -61,7 +61,7 @@ func (m Model) Init() tea.Cmd {

func (m Model) cursorOffset() int {
// m.logCursor()
return m.cursor.c*m.maxRows + m.cursor.r
return (m.cursor.c * m.maxRows) + m.cursor.r
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -85,10 +85,12 @@ 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 < len(m.files)/m.maxRows {
m.cursor.r = min(m.cursor.r+1, min(len(m.files), m.maxRows)-1)
} else if m.cursor.c == len(m.files)/m.maxRows {
m.cursor.r = min(m.cursor.r+1, len(m.files)%m.maxRows-1)
if m.cursor.c == 0 {
m.cursor.r = min(m.cursor.r+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

Expand All @@ -98,7 +100,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case m.cfg.Settings.Keymap.NavRight:
m.cursor.c++
if m.cursorOffset() >= len(m.files) {
m.cursor.c-- // undo the cursor move
m.cursor.c--
}
return m, nil
case m.cfg.Settings.Keymap.NavOut:
Expand All @@ -120,13 +122,35 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

m.maxRows = min(defaultMaxRows, max(1, msg.Height-3))

m.trySelectFile(m.files, fName)
m.trySelectFile(fName)

return m, nil

case dirLoaded:
oldDir := m.cwd
newDir := msg.path

/// special case
if oldDir == newDir {
m.files = msg.files
if len(m.files) > 0 {
name, ok := m.cachedDirSelections[newDir]
if ok {
m.trySelectFile(name)
if m.cursor.r == 0 && m.cursor.c == 0 {
delete(m.cachedDirSelections, newDir)
}
} else {
m.trySelectFile(m.files[min(m.cursorOffset(), len(m.files)-1)].Name())
}
m.cachedDirSelections[newDir] = m.files[m.cursorOffset()].Name()
} else {
delete(m.cachedDirSelections, newDir)
m.setCursor(0, 0)
}
return m, nil
}

if m.prevCWD == "" && oldDir != newDir {
m.prevCWD = oldDir
}
Expand All @@ -145,7 +169,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
fName = path.Base(oldDir)
}

m.trySelectFile(m.files, fName)
m.trySelectFile(fName)
m.clearAnimAt = time.Now().Add(pathAnimDuration)

return m, func() tea.Msg {
Expand Down Expand Up @@ -251,23 +275,23 @@ func (m Model) loadDir(path string) tea.Cmd {
}

func (m Model) logCursor() {
log.Debug().Msgf("cursor.r: %v, cursor.c: %v, maxRows: %v", m.cursor.r, m.cursor.c, m.maxRows)
log.Debug().Msgf("cursor(%v, %v)", m.cursor.c, m.cursor.r)
}

func (m *Model) setCursor(r, c int) {
m.cursor.r = r
m.cursor.c = c
m.cursor.c = r
m.cursor.r = c
}

// trySelectFile tries to select a file by name or sets the cursor to the first file
// if the file is not found.
func (m *Model) trySelectFile(files []fs.DirEntry, fName string) {
index := slices.IndexFunc(files, func(dir fs.DirEntry) bool {
func (m *Model) trySelectFile(fName string) {
index := slices.IndexFunc(m.files, func(dir fs.DirEntry) bool {
return dir.Name() == fName
})

if index != -1 {
m.setCursor(index%m.maxRows, index/m.maxRows)
m.setCursor(index/m.maxRows, index%m.maxRows)
} else {
m.setCursor(0, 0)
}
Expand Down
115 changes: 102 additions & 13 deletions internal/models/main_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: true},
dirEntry{name: "ex", isDir: true},
},
cursor: position{c: 0, r: 1},
cursor: position{r: 1, c: 0},
cachedDirSelections: map[string]string{"/cache": "ex"},
maxRows: 10,
prevCWD: "/currpath",
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 0, r: 1},
cursor: position{r: 1, c: 0},
cachedDirSelections: map[string]string{},
maxRows: 10,
},
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 0, r: 1},
cursor: position{r: 1, c: 0},
cachedDirSelections: map[string]string{},
maxRows: 10,
},
Expand All @@ -255,7 +255,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 0, r: 1},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 10,
},
Expand All @@ -271,7 +271,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 0, r: 1},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 10,
},
Expand All @@ -289,7 +289,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file2", isDir: false},
dirEntry{name: "file3", isDir: false},
},
cursor: position{c: 0, r: 1},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 2,
},
Expand All @@ -306,7 +306,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file2", isDir: false},
dirEntry{name: "file3", isDir: false},
},
cursor: position{c: 0, r: 1},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 2,
},
Expand All @@ -323,7 +323,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 1, r: 0},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
Expand All @@ -339,7 +339,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 0, r: 0},
cursor: position{r: 0, c: 0},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
Expand All @@ -356,7 +356,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 0, r: 0},
cursor: position{r: 0, c: 0},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
Expand All @@ -372,7 +372,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 1, r: 0},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
Expand All @@ -389,7 +389,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 1, r: 0},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
Expand All @@ -405,7 +405,7 @@ func TestModel_Update(t *testing.T) {
dirEntry{name: "file1", isDir: false},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 1, r: 0},
cursor: position{r: 0, c: 1},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
Expand Down Expand Up @@ -435,6 +435,95 @@ func TestModel_Update(t *testing.T) {
},
want1Nil: true,
},
{
name: "Load same directory",
fields: fields{
cwd: "/special",
files: []fs.DirEntry{},
cursor: position{},
cachedDirSelections: map[string]string{"/special": "xx"},
maxRows: 10,
},
args: args{
msg: dirLoaded{
path: "/special",
files: []fs.DirEntry{
dirEntry{name: "specialDir", isDir: true},
},
},
},
want: Model{
cwd: "/special",
files: []fs.DirEntry{
dirEntry{name: "specialDir", isDir: true},
},
cursor: position{},
cachedDirSelections: map[string]string{"/special": "specialDir"},
maxRows: 10,
},
want1Nil: true,
},
{
name: "Load same directory when files deleted",
fields: fields{
cwd: "/special",
files: []fs.DirEntry{
dirEntry{name: "dir1", isDir: true},
dirEntry{name: "file2", isDir: false},
},
cursor: position{r: 1},
cachedDirSelections: map[string]string{"/special": "xx"},
maxRows: 1,
},
args: args{
msg: dirLoaded{
path: "/special",
files: []fs.DirEntry{},
},
},
want: Model{
cwd: "/special",
files: []fs.DirEntry{},
cursor: position{},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
want1Nil: true,
},
{
name: "Load same directory when previous selected files deleted",
fields: fields{
cwd: "/special",
files: []fs.DirEntry{
dirEntry{name: "dir1", isDir: true},
dirEntry{name: "file2", isDir: false},
dirEntry{name: "file3", isDir: false},
},
cursor: position{c: 2},
cachedDirSelections: map[string]string{},
maxRows: 1,
},
args: args{
msg: dirLoaded{
path: "/special",
files: []fs.DirEntry{
dirEntry{name: "dir1", isDir: true},
dirEntry{name: "file2", isDir: false},
},
},
},
want: Model{
cwd: "/special",
files: []fs.DirEntry{
dirEntry{name: "dir1", isDir: true},
dirEntry{name: "file2", isDir: false},
},
cursor: position{c: 1},
cachedDirSelections: map[string]string{"/special": "file2"},
maxRows: 1,
},
want1Nil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit c10ae91

Please sign in to comment.