Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Simple Filepicker Bubble #273

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
118 changes: 118 additions & 0 deletions filepicker/delegate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package filepicker

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type fileNameStyles struct {
fileNotSelected lipgloss.Style
fileRegular lipgloss.Style
fileDirectory lipgloss.Style
fileSymLink lipgloss.Style
fileBlockDevice lipgloss.Style
fileSelected lipgloss.Style
filterMatch lipgloss.Style
}

func newFileNameStyles() (s fileNameStyles) {
s.fileNotSelected = lipgloss.NewStyle()
s.fileRegular = lipgloss.NewStyle()
s.fileDirectory = lipgloss.NewStyle().Bold(true).
Foreground(lipgloss.Color("32"))
s.fileSymLink = lipgloss.NewStyle().Bold(true).
Foreground(lipgloss.Color("36"))
s.fileBlockDevice = lipgloss.NewStyle().Bold(true).
Foreground(lipgloss.Color("33")).
Background(lipgloss.Color("40"))
s.fileSelected = lipgloss.NewStyle().
Background(lipgloss.Color("#FFFF00")).Foreground(lipgloss.Color("#000000"))
s.filterMatch = lipgloss.NewStyle().Underline(true)
return s
}

func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
d := list.NewDefaultDelegate()

d.UpdateFunc = func(msg tea.Msg, m *list.Model) tea.Cmd {
var title string

if i, ok := m.SelectedItem().(listItem); ok {
title = i.value
} else {
return nil
}
statusMessageStyle := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#04B575", Dark: "#04B575"}).Render

switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.choose):
return m.NewStatusMessage(statusMessageStyle("You chose " + title))

case key.Matches(msg, keys.remove):
index := m.Index()
m.RemoveItem(index)
if len(m.Items()) == 0 {
keys.remove.SetEnabled(false)
}
return m.NewStatusMessage(statusMessageStyle("Deleted " + title))
}
}

return nil
}

help := []key.Binding{keys.choose, keys.remove}

d.ShortHelpFunc = func() []key.Binding {
return help
}

d.FullHelpFunc = func() [][]key.Binding {
return [][]key.Binding{help}
}

return d
}

type delegateKeyMap struct {
choose key.Binding
remove key.Binding
}

// Additional short help entries. This satisfies the help.KeyMap interface and
// is entirely optional.
func (d delegateKeyMap) ShortHelp() []key.Binding {
return []key.Binding{
d.choose,
d.remove,
}
}

// Additional full help entries. This satisfies the help.KeyMap interface and
// is entirely optional.
func (d delegateKeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{
d.choose,
d.remove,
},
}
}

func newDelegateKeyMap() *delegateKeyMap {
return &delegateKeyMap{
choose: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "choose"),
),
remove: key.NewBinding(
key.WithKeys("x", "backspace"),
key.WithHelp("x", "delete"),
),
}
}
Loading