bubbletea/examples/file-picker/main.go

70 lines
1.3 KiB
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"os"
"strings"
"github.com/charmbracelet/bubbles/filepicker"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
filepicker filepicker.Model
selectedFile string
quitting bool
}
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
}
}
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
}
return m, cmd
}
func (m model) View() string {
if m.quitting {
return ""
}
var s strings.Builder
s.WriteString("\n ")
if m.selectedFile == "" {
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()
fp.Path, _ = os.UserHomeDir()
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")
}