Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions filepicker/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func nextID() int {
func New() Model {
return Model{
id: nextID(),
filter: "",
CurrentDirectory: ".",
Cursor: ">",
AllowedTypes: []string{},
Expand All @@ -37,6 +38,7 @@ func New() Model {
DirAllowed: false,
FileAllowed: true,
AutoHeight: true,
SearchMode: false,
height: 0,
maxIdx: 0,
minIdx: 0,
Expand Down Expand Up @@ -74,6 +76,7 @@ type KeyMap struct {
Back key.Binding
Open key.Binding
Select key.Binding
Search key.Binding
}

// DefaultKeyMap defines the default keybindings.
Expand All @@ -88,6 +91,10 @@ func DefaultKeyMap() KeyMap {
Back: key.NewBinding(key.WithKeys("h", "backspace", "left", "esc"), key.WithHelp("h", "back")),
Open: key.NewBinding(key.WithKeys("l", "right", "enter"), key.WithHelp("l", "open")),
Select: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
Search: key.NewBinding(
key.WithKeys("/"),
key.WithHelp("/", "search"),
),
}
}

Expand Down Expand Up @@ -127,6 +134,7 @@ func DefaultStyles() Styles {
type Model struct {
id int

filter string
// Path is the path which the user has selected with the file picker.
Path string

Expand All @@ -139,11 +147,13 @@ type Model struct {

KeyMap KeyMap
files []os.DirEntry
filteredFiles []os.DirEntry
ShowPermissions bool
ShowSize bool
ShowHidden bool
DirAllowed bool
FileAllowed bool
SearchMode bool

FileSelected string
selected int
Expand Down Expand Up @@ -195,6 +205,7 @@ func (m *Model) popView() (int, int, int) {
}

func (m Model) readDir(path string, showHidden bool) tea.Cmd {

return func() tea.Msg {
dirEntries, err := os.ReadDir(path)
if err != nil {
Expand All @@ -218,6 +229,11 @@ func (m Model) readDir(path string, showHidden bool) tea.Cmd {
if isHidden {
continue
}

if !strings.HasPrefix(dirEntry.Name(), m.filter) {
continue
}

sanitizedDirEntries = append(sanitizedDirEntries, dirEntry)
}
return readDirMsg{id: m.id, entries: sanitizedDirEntries}
Expand All @@ -232,18 +248,24 @@ func (m *Model) SetHeight(h int) {
}
}

func (m Model) Filter() string {
return m.filter
}

// Height returns the height of the file picker.
func (m Model) Height() int {
return m.height
}

// Init initializes the file picker model.
func (m Model) Init() tea.Cmd {

return m.readDir(m.CurrentDirectory, m.ShowHidden)
}

// Update handles user interactions within the file picker model.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

switch msg := msg.(type) {
case readDirMsg:
if msg.id != m.id {
Expand All @@ -257,7 +279,33 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}
m.maxIdx = m.Height() - 1
case tea.KeyPressMsg:
if m.SearchMode {
switch msg.Code {
case tea.KeyEsc:
m.SearchMode = false
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)

case tea.KeyBackspace:
m.selected = 0
m.minIdx = 0
m.maxIdx = m.Height() - 1
if len(m.filter) > 0 {
m.filter = m.filter[:len(m.filter)-1]
}
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
}

if msg.Text != "" {
m.filter += msg.Text
m.selected = 0
m.minIdx = 0
m.maxIdx = m.Height() - 1
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
}
}
switch {
case key.Matches(msg, m.KeyMap.Search):
m.SearchMode = true
case key.Matches(msg, m.KeyMap.GoToTop):
m.selected = 0
m.minIdx = 0
Expand Down Expand Up @@ -309,6 +357,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.maxIdx = m.minIdx + m.Height()
}
case key.Matches(msg, m.KeyMap.Back):

if m.SearchMode {
m.SearchMode = false
return m, nil
}

m.CurrentDirectory = filepath.Dir(m.CurrentDirectory)
if m.selectedStack.Length() > 0 {
m.selected, m.minIdx, m.maxIdx = m.popView()
Expand Down Expand Up @@ -360,6 +414,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.maxIdx = m.Height() - 1
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
}

}
return m, nil
}
Expand Down