Skip to content

Commit edefcba

Browse files
committed
refactor: move sortFiles to utils
1 parent 3f9a3cd commit edefcba

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

pkg/ui/mainModel.go

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package ui
22

33
import (
44
"fmt"
5-
"path/filepath"
6-
"slices"
75
"strings"
86

97
"github.com/bluekeyes/go-gitdiff/gitdiff"
@@ -257,6 +255,10 @@ func (m mainModel) View() string {
257255
)
258256
}
259257

258+
type fileTreeMsg struct {
259+
files []*gitdiff.File
260+
}
261+
260262
func (m mainModel) fetchFileTree() tea.Msg {
261263
// TODO: handle error
262264
files, _, err := gitdiff.Parse(strings.NewReader(m.input + "\n"))
@@ -268,41 +270,6 @@ func (m mainModel) fetchFileTree() tea.Msg {
268270
return fileTreeMsg{files: files}
269271
}
270272

271-
type fileTreeMsg struct {
272-
files []*gitdiff.File
273-
}
274-
275-
func sortFiles(files []*gitdiff.File) {
276-
slices.SortFunc(files, func(a *gitdiff.File, b *gitdiff.File) int {
277-
nameA := filenode.GetFileName(a)
278-
nameB := filenode.GetFileName(b)
279-
dira := filepath.Dir(nameA)
280-
dirb := filepath.Dir(nameB)
281-
if dira != "." && dirb != "." && dira == dirb {
282-
return strings.Compare(strings.ToLower(nameA), strings.ToLower(nameB))
283-
}
284-
285-
if dira != "." && dirb == "." {
286-
return -1
287-
}
288-
if dirb != "." && dira == "." {
289-
return 1
290-
}
291-
292-
if dira != "." && dirb != "." {
293-
if strings.HasPrefix(dira, dirb) {
294-
return -1
295-
}
296-
297-
if strings.HasPrefix(dirb, dira) {
298-
return 1
299-
}
300-
}
301-
302-
return strings.Compare(strings.ToLower(nameA), strings.ToLower(nameB))
303-
})
304-
}
305-
306273
func (m mainModel) footerView() string {
307274
return lipgloss.NewStyle().
308275
Width(m.width).
@@ -336,11 +303,6 @@ func (m mainModel) sidebarWidth() int {
336303
}
337304
}
338305

339-
func (m mainModel) setDiffViewerDimensions() tea.Cmd {
340-
dfCmd := m.diffViewer.SetSize(m.width-m.sidebarWidth(), m.height-footerHeight-headerHeight)
341-
return dfCmd
342-
}
343-
344306
func (m *mainModel) stopSearch() {
345307
m.searching = false
346308
m.search.SetValue("")

pkg/ui/utils.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ui
2+
3+
import (
4+
"path/filepath"
5+
"slices"
6+
"strings"
7+
8+
"github.com/bluekeyes/go-gitdiff/gitdiff"
9+
10+
"github.com/dlvhdr/diffnav/pkg/filenode"
11+
)
12+
13+
func sortFiles(files []*gitdiff.File) {
14+
slices.SortFunc(files, func(a *gitdiff.File, b *gitdiff.File) int {
15+
nameA := filenode.GetFileName(a)
16+
nameB := filenode.GetFileName(b)
17+
dira := filepath.Dir(nameA)
18+
dirb := filepath.Dir(nameB)
19+
if dira != "." && dirb != "." && dira == dirb {
20+
return strings.Compare(strings.ToLower(nameA), strings.ToLower(nameB))
21+
}
22+
23+
if dira != "." && dirb == "." {
24+
return -1
25+
}
26+
if dirb != "." && dira == "." {
27+
return 1
28+
}
29+
30+
if dira != "." && dirb != "." {
31+
if strings.HasPrefix(dira, dirb) {
32+
return -1
33+
}
34+
35+
if strings.HasPrefix(dirb, dira) {
36+
return 1
37+
}
38+
}
39+
40+
return strings.Compare(strings.ToLower(nameA), strings.ToLower(nameB))
41+
})
42+
}

0 commit comments

Comments
 (0)