2023-03-06 11:54:26 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-31 17:18:24 -04:00
|
|
|
"errors"
|
2023-03-06 11:54:26 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-05-31 17:18:24 -04:00
|
|
|
"time"
|
2023-03-06 11:54:26 -05:00
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/filepicker"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
filepicker filepicker.Model
|
|
|
|
selectedFile string
|
|
|
|
quitting bool
|
2023-05-31 17:18:24 -04:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type clearErrorMsg struct{}
|
|
|
|
|
|
|
|
func clearErrorAfter(t time.Duration) tea.Cmd {
|
|
|
|
return tea.Tick(t, func(_ time.Time) tea.Msg {
|
|
|
|
return clearErrorMsg{}
|
|
|
|
})
|
2023-03-06 11:54:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return m.filepicker.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
|
|
|
case "ctrl+c", "q":
|
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
2023-05-31 17:18:24 -04:00
|
|
|
case clearErrorMsg:
|
|
|
|
m.err = nil
|
2023-03-06 11:54:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.filepicker, cmd = m.filepicker.Update(msg)
|
|
|
|
|
|
|
|
// Did the user select a file?
|
|
|
|
if didSelect, path := m.filepicker.DidSelectFile(msg); didSelect {
|
|
|
|
// Get the path of the selected file.
|
|
|
|
m.selectedFile = path
|
|
|
|
}
|
|
|
|
|
2023-05-31 17:18:24 -04:00
|
|
|
// Did the user select a disabled file?
|
|
|
|
// This is only necessary to display an error to the user.
|
|
|
|
if didSelect, path := m.filepicker.DidSelectDisabledFile(msg); didSelect {
|
|
|
|
// Let's clear the selectedFile and display an error.
|
|
|
|
m.err = errors.New(path + " is not valid.")
|
|
|
|
m.selectedFile = ""
|
|
|
|
return m, tea.Batch(cmd, clearErrorAfter(2*time.Second))
|
|
|
|
}
|
|
|
|
|
2023-03-06 11:54:26 -05:00
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) View() string {
|
|
|
|
if m.quitting {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var s strings.Builder
|
|
|
|
s.WriteString("\n ")
|
2023-05-31 17:18:24 -04:00
|
|
|
if m.err != nil {
|
|
|
|
s.WriteString(m.filepicker.Styles.DisabledFile.Render(m.err.Error()))
|
|
|
|
} else if m.selectedFile == "" {
|
2023-03-06 11:54:26 -05:00
|
|
|
s.WriteString("Pick a file:")
|
|
|
|
} else {
|
|
|
|
s.WriteString("Selected file: " + m.filepicker.Styles.Selected.Render(m.selectedFile))
|
|
|
|
}
|
|
|
|
s.WriteString("\n\n" + m.filepicker.View() + "\n")
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fp := filepicker.New()
|
2023-05-31 17:18:24 -04:00
|
|
|
fp.AllowedTypes = []string{".mod", ".sum", ".go", ".txt", ".md"}
|
2023-05-31 14:20:18 -04:00
|
|
|
fp.CurrentDirectory, _ = os.UserHomeDir()
|
2023-03-06 11:54:26 -05:00
|
|
|
|
|
|
|
m := model{
|
|
|
|
filepicker: fp,
|
|
|
|
}
|
|
|
|
tm, _ := tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()
|
|
|
|
mm := tm.(model)
|
|
|
|
fmt.Println("\n You selected: " + m.filepicker.Styles.Selected.Render(mm.selectedFile) + "\n")
|
|
|
|
}
|