@@ -45,16 +45,19 @@ func New() Model {
45
45
selectedStack : newStack (),
46
46
minStack : newStack (),
47
47
maxStack : newStack (),
48
- KeyMap : DefaultKeyMap ,
49
- Styles : DefaultStyles ,
48
+ KeyMap : DefaultKeyMap () ,
49
+ Styles : DefaultStyles () ,
50
50
}
51
51
}
52
52
53
53
type errorMsg struct {
54
54
err error
55
55
}
56
56
57
- type readDirMsg []os.DirEntry
57
+ type readDirMsg struct {
58
+ id int
59
+ entries []os.DirEntry
60
+ }
58
61
59
62
const (
60
63
marginBottom = 5
@@ -76,16 +79,18 @@ type KeyMap struct {
76
79
}
77
80
78
81
// DefaultKeyMap defines the default keybindings.
79
- var DefaultKeyMap = KeyMap {
80
- GoToTop : key .NewBinding (key .WithKeys ("g" ), key .WithHelp ("g" , "first" )),
81
- GoToLast : key .NewBinding (key .WithKeys ("G" ), key .WithHelp ("G" , "last" )),
82
- Down : key .NewBinding (key .WithKeys ("j" , "down" , "ctrl+n" ), key .WithHelp ("j" , "down" )),
83
- Up : key .NewBinding (key .WithKeys ("k" , "up" , "ctrl+p" ), key .WithHelp ("k" , "up" )),
84
- PageUp : key .NewBinding (key .WithKeys ("K" , "pgup" ), key .WithHelp ("pgup" , "page up" )),
85
- PageDown : key .NewBinding (key .WithKeys ("J" , "pgdown" ), key .WithHelp ("pgdown" , "page down" )),
86
- Back : key .NewBinding (key .WithKeys ("h" , "backspace" , "left" , "esc" ), key .WithHelp ("h" , "back" )),
87
- Open : key .NewBinding (key .WithKeys ("l" , "right" , "enter" ), key .WithHelp ("l" , "open" )),
88
- Select : key .NewBinding (key .WithKeys ("enter" ), key .WithHelp ("enter" , "select" )),
82
+ func DefaultKeyMap () KeyMap {
83
+ return KeyMap {
84
+ GoToTop : key .NewBinding (key .WithKeys ("g" ), key .WithHelp ("g" , "first" )),
85
+ GoToLast : key .NewBinding (key .WithKeys ("G" ), key .WithHelp ("G" , "last" )),
86
+ Down : key .NewBinding (key .WithKeys ("j" , "down" , "ctrl+n" ), key .WithHelp ("j" , "down" )),
87
+ Up : key .NewBinding (key .WithKeys ("k" , "up" , "ctrl+p" ), key .WithHelp ("k" , "up" )),
88
+ PageUp : key .NewBinding (key .WithKeys ("K" , "pgup" ), key .WithHelp ("pgup" , "page up" )),
89
+ PageDown : key .NewBinding (key .WithKeys ("J" , "pgdown" ), key .WithHelp ("pgdown" , "page down" )),
90
+ Back : key .NewBinding (key .WithKeys ("h" , "backspace" , "left" , "esc" ), key .WithHelp ("h" , "back" )),
91
+ Open : key .NewBinding (key .WithKeys ("l" , "right" , "enter" ), key .WithHelp ("l" , "open" )),
92
+ Select : key .NewBinding (key .WithKeys ("enter" ), key .WithHelp ("enter" , "select" )),
93
+ }
89
94
}
90
95
91
96
// Styles defines the possible customizations for styles in the file picker.
@@ -104,18 +109,26 @@ type Styles struct {
104
109
}
105
110
106
111
// DefaultStyles defines the default styling for the file picker.
107
- var DefaultStyles = Styles {
108
- DisabledCursor : lipgloss .NewStyle ().Foreground (lipgloss .Color ("247" )),
109
- Cursor : lipgloss .NewStyle ().Foreground (lipgloss .Color ("212" )),
110
- Symlink : lipgloss .NewStyle ().Foreground (lipgloss .Color ("36" )),
111
- Directory : lipgloss .NewStyle ().Foreground (lipgloss .Color ("99" )),
112
- File : lipgloss .NewStyle (),
113
- DisabledFile : lipgloss .NewStyle ().Foreground (lipgloss .Color ("243" )),
114
- DisabledSelected : lipgloss .NewStyle ().Foreground (lipgloss .Color ("247" )),
115
- Permission : lipgloss .NewStyle ().Foreground (lipgloss .Color ("244" )),
116
- Selected : lipgloss .NewStyle ().Foreground (lipgloss .Color ("212" )).Bold (true ),
117
- FileSize : lipgloss .NewStyle ().Foreground (lipgloss .Color ("240" )).Width (fileSizeWidth ).Align (lipgloss .Right ),
118
- EmptyDirectory : lipgloss .NewStyle ().Foreground (lipgloss .Color ("240" )).PaddingLeft (paddingLeft ).SetString ("Bummer. No Files Found." ),
112
+ func DefaultStyles () Styles {
113
+ return DefaultStylesWithRenderer (lipgloss .DefaultRenderer ())
114
+ }
115
+
116
+ // DefaultStylesWithRenderer defines the default styling for the file picker,
117
+ // with a given Lip Gloss renderer.
118
+ func DefaultStylesWithRenderer (r * lipgloss.Renderer ) Styles {
119
+ return Styles {
120
+ DisabledCursor : r .NewStyle ().Foreground (lipgloss .Color ("247" )),
121
+ Cursor : r .NewStyle ().Foreground (lipgloss .Color ("212" )),
122
+ Symlink : r .NewStyle ().Foreground (lipgloss .Color ("36" )),
123
+ Directory : r .NewStyle ().Foreground (lipgloss .Color ("99" )),
124
+ File : r .NewStyle (),
125
+ DisabledFile : r .NewStyle ().Foreground (lipgloss .Color ("243" )),
126
+ DisabledSelected : r .NewStyle ().Foreground (lipgloss .Color ("247" )),
127
+ Permission : r .NewStyle ().Foreground (lipgloss .Color ("244" )),
128
+ Selected : r .NewStyle ().Foreground (lipgloss .Color ("212" )).Bold (true ),
129
+ FileSize : r .NewStyle ().Foreground (lipgloss .Color ("240" )).Width (fileSizeWidth ).Align (lipgloss .Right ),
130
+ EmptyDirectory : r .NewStyle ().Foreground (lipgloss .Color ("240" )).PaddingLeft (paddingLeft ).SetString ("Bummer. No Files Found." ),
131
+ }
119
132
}
120
133
121
134
// Model represents a file picker.
@@ -187,7 +200,7 @@ func (m Model) popView() (int, int, int) {
187
200
return m .selectedStack .Pop (), m .minStack .Pop (), m .maxStack .Pop ()
188
201
}
189
202
190
- func readDir (path string , showHidden bool ) tea.Cmd {
203
+ func ( m Model ) readDir (path string , showHidden bool ) tea.Cmd {
191
204
return func () tea.Msg {
192
205
dirEntries , err := os .ReadDir (path )
193
206
if err != nil {
@@ -202,7 +215,7 @@ func readDir(path string, showHidden bool) tea.Cmd {
202
215
})
203
216
204
217
if showHidden {
205
- return readDirMsg ( dirEntries )
218
+ return readDirMsg { id : m . id , entries : dirEntries }
206
219
}
207
220
208
221
var sanitizedDirEntries []os.DirEntry
@@ -213,20 +226,23 @@ func readDir(path string, showHidden bool) tea.Cmd {
213
226
}
214
227
sanitizedDirEntries = append (sanitizedDirEntries , dirEntry )
215
228
}
216
- return readDirMsg ( sanitizedDirEntries )
229
+ return readDirMsg { id : m . id , entries : sanitizedDirEntries }
217
230
}
218
231
}
219
232
220
233
// Init initializes the file picker model.
221
234
func (m Model ) Init () tea.Cmd {
222
- return readDir (m .CurrentDirectory , m .ShowHidden )
235
+ return m . readDir (m .CurrentDirectory , m .ShowHidden )
223
236
}
224
237
225
238
// Update handles user interactions within the file picker model.
226
239
func (m Model ) Update (msg tea.Msg ) (Model , tea.Cmd ) {
227
240
switch msg := msg .(type ) {
228
241
case readDirMsg :
229
- m .files = msg
242
+ if msg .id != m .id {
243
+ break
244
+ }
245
+ m .files = msg .entries
230
246
m .max = m .Height - 1
231
247
case tea.WindowSizeMsg :
232
248
if m .AutoHeight {
@@ -294,7 +310,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
294
310
m .min = 0
295
311
m .max = m .Height - 1
296
312
}
297
- return m , readDir (m .CurrentDirectory , m .ShowHidden )
313
+ return m , m . readDir (m .CurrentDirectory , m .ShowHidden )
298
314
case key .Matches (msg , m .KeyMap .Open ):
299
315
if len (m .files ) == 0 {
300
316
break
@@ -335,7 +351,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
335
351
m .selected = 0
336
352
m .min = 0
337
353
m .max = m .Height - 1
338
- return m , readDir (m .CurrentDirectory , m .ShowHidden )
354
+ return m , m . readDir (m .CurrentDirectory , m .ShowHidden )
339
355
}
340
356
}
341
357
return m , nil
0 commit comments