-
Notifications
You must be signed in to change notification settings - Fork 29
/
dirtree.go
177 lines (146 loc) · 4.12 KB
/
dirtree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package dirtree
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/knipferrc/fm/icons"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/truncate"
)
// Model is a struct to represent the properties of a dirtree.
type Model struct {
Files []fs.DirEntry
FilePaths []string
Width int
Cursor int
ShowIcons bool
ShowHidden bool
SelectedItemColor lipgloss.AdaptiveColor
UnselectedItemColor lipgloss.AdaptiveColor
}
// NewModel creates a new instance of a dirtree.
func NewModel(showIcons bool, selectedItemColor, unselectedItemColor lipgloss.AdaptiveColor) Model {
return Model{
Cursor: 0,
ShowIcons: showIcons,
ShowHidden: true,
SelectedItemColor: selectedItemColor,
UnselectedItemColor: unselectedItemColor,
}
}
// SetContent sets the files currently displayed in the tree.
func (m *Model) SetContent(files []fs.DirEntry) {
m.Files = files
}
// SetFilePaths sets an array of file paths.
func (m *Model) SetFilePaths(filePaths []string) {
m.FilePaths = filePaths
}
// GetFilePaths returns an array of file paths.
func (m Model) GetFilePaths() []string {
return m.FilePaths
}
// SetSize updates the size of the dirtree, useful when resizing the terminal.
func (m *Model) SetSize(width int) {
m.Width = width
}
// GotoTop goes to the top of the tree.
func (m *Model) GotoTop() {
m.Cursor = 0
}
// GotoBottom goes to the bottom of the tree.
func (m *Model) GotoBottom() {
m.Cursor = len(m.Files) - 1
}
// GetSelectedFile returns the currently selected file in the tree.
func (m Model) GetSelectedFile() (os.FileInfo, error) {
if len(m.Files) > 0 {
fileInfo, err := m.Files[m.Cursor].Info()
if err != nil {
return nil, err
}
return fileInfo, nil
}
return nil, nil
}
// GetCursor gets the position of the cursor in the tree.
func (m Model) GetCursor() int {
return m.Cursor
}
// GoDown goes down the tree by one.
func (m *Model) GoDown() {
m.Cursor++
}
// GoUp goes up the tree by one.
func (m *Model) GoUp() {
m.Cursor--
}
// GetTotalFiles returns the total number of files in the tree.
func (m Model) GetTotalFiles() int {
return len(m.Files)
}
// ToggleHidden toggles the visibility of hidden files.
func (m *Model) ToggleHidden() {
m.ShowHidden = !m.ShowHidden
}
// dirItem returns a string representation of a directory item.
func (m Model) dirItem(selected bool, fileInfo fs.FileInfo) string {
icon, color := icons.GetIcon(fileInfo.Name(), filepath.Ext(fileInfo.Name()), icons.GetIndicator(fileInfo.Mode()))
fileIcon := fmt.Sprintf("%s%s", color, icon)
switch {
case m.ShowIcons && selected:
return fmt.Sprintf("%s\033[0m %s", fileIcon, lipgloss.NewStyle().
Bold(true).
Foreground(m.SelectedItemColor).
Render(fileInfo.Name()))
case m.ShowIcons && !selected:
return fmt.Sprintf("%s\033[0m %s", fileIcon, lipgloss.NewStyle().
Bold(true).
Foreground(m.UnselectedItemColor).
Render(fileInfo.Name()))
case !m.ShowIcons && selected:
return lipgloss.NewStyle().
Bold(true).
Foreground(m.SelectedItemColor).
Render(fileInfo.Name())
default:
return lipgloss.NewStyle().
Bold(true).
Foreground(m.UnselectedItemColor).
Render(fileInfo.Name())
}
}
// View returns a string representation of the current tree.
func (m Model) View() string {
curFiles := ""
if len(m.Files) == 0 {
return "Directory is empty"
}
for i, file := range m.Files {
var modTimeColor lipgloss.AdaptiveColor
if m.Cursor == i {
modTimeColor = m.SelectedItemColor
} else {
modTimeColor = m.UnselectedItemColor
}
fileInfo, err := file.Info()
if err != nil {
return err.Error()
}
modTime := lipgloss.NewStyle().
Align(lipgloss.Right).
Foreground(modTimeColor).
Render(fileInfo.ModTime().
Format("2006-01-02 15:04:05"),
)
dirItem := lipgloss.NewStyle().Width(m.Width - lipgloss.Width(modTime) - 2).Render(
truncate.StringWithTail(
m.dirItem(m.Cursor == i, fileInfo), uint(m.Width-lipgloss.Width(modTime)), "...",
),
)
row := lipgloss.JoinHorizontal(lipgloss.Top, dirItem, modTime)
curFiles += fmt.Sprintf("%s\n", row)
}
return curFiles
}