Skip to content
Myr

docs(examples): filepicker AllowedTypes example (#713)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Maas Lalani · committed by GitHub · parent 8254e0e · Browse files · 1 file+26 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+26 added−1 removed

  • other:+26 added−1 removed

1 file+26 added−1 removed

modified:examples/file-picker/main.go+26 added-1 removed
1package main23import (4 "errors"5 "fmt"6 "os"7 "strings"8 "time"910 "github.com/charmbracelet/bubbles/filepicker"11 tea "github.com/charmbracelet/bubbletea"12)
1 unmodified line
14type model struct {15 filepicker filepicker.Model16 selectedFile string17 quitting bool18 err error19}2021type clearErrorMsg struct{}2223func clearErrorAfter(t time.Duration) tea.Cmd {24 return tea.Tick(t, func(_ time.Time) tea.Msg {25 return clearErrorMsg{}26 })27}2829func (m model) Init() tea.Cmd {30 return m.filepicker.Init()31}32
4 unmodified lines
37 case "ctrl+c", "q":38 m.quitting = true39 return m, tea.Quit40 }41 case clearErrorMsg:42 m.err = nil43 }4445 var cmd tea.Cmd46 m.filepicker, cmd = m.filepicker.Update(msg)
3 unmodified lines
50 // Get the path of the selected file.51 m.selectedFile = path52 }5354 // Did the user select a disabled file?55 // This is only necessary to display an error to the user.56 if didSelect, path := m.filepicker.DidSelectDisabledFile(msg); didSelect {57 // Let's clear the selectedFile and display an error.58 m.err = errors.New(path + " is not valid.")59 m.selectedFile = ""60 return m, tea.Batch(cmd, clearErrorAfter(2*time.Second))61 }6263 return m, cmd64}6566func (m model) View() string {
1 unmodified line
68 return ""69 }70 var s strings.Builder71 s.WriteString("\n ")72 if m.err != nil {73 s.WriteString(m.filepicker.Styles.DisabledFile.Render(m.err.Error()))50 if m.selectedFile == "" {74 } else if m.selectedFile == "" {75 s.WriteString("Pick a file:")76 } else {77 s.WriteString("Selected file: " + m.filepicker.Styles.Selected.Render(m.selectedFile))78 }
2 unmodified lines
81}8283func main() {84 fp := filepicker.New()85 fp.AllowedTypes = []string{".mod", ".sum", ".go", ".txt", ".md"}86 fp.CurrentDirectory, _ = os.UserHomeDir()8788 m := model{89 filepicker: fp,
5 unmodified lines