Filepicker Example (#683)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: filepicker example * fix: use new API * docs(examples): update file selection comment in filepicker example * chore: bump bubbles ---------
authored by Maas Lalani · committed by GitHub · parent a136799 · Browse files · 3 files·+79 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Christian Rocha <christian@rocha.is>
Diff stats
3 files+79 added−3 removed
- other:+79 added−3 removed
Provenance
- Commit
90c9124- Tree
d8d7a35- Author
Maas Lalani <maas@lalani.dev>- Committer
GitHub <noreply@github.com>- Parent
a136799
3 files changed+79 added−3 removed
added:examples/file-picker/main.go+69 added
1+package main2+3+import (4+ "fmt"5+ "os"6+ "strings"7+8+ "github.com/charmbracelet/bubbles/filepicker"9+ tea "github.com/charmbracelet/bubbletea"10+)11+12+type model struct {13+ filepicker filepicker.Model14+ selectedFile string15+ quitting bool16+}17+18+func (m model) Init() tea.Cmd {19+ return m.filepicker.Init()20+}21+22+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {23+ switch msg := msg.(type) {24+ case tea.KeyMsg:25+ switch msg.String() {26+ case "ctrl+c", "q":27+ m.quitting = true28+ return m, tea.Quit29+ }30+ }31+32+ var cmd tea.Cmd33+ m.filepicker, cmd = m.filepicker.Update(msg)34+35+ // Did the user select a file?36+ if didSelect, path := m.filepicker.DidSelectFile(msg); didSelect {37+ // Get the path of the selected file.38+ m.selectedFile = path39+ }40+41+ return m, cmd42+}43+44+func (m model) View() string {45+ if m.quitting {46+ return ""47+ }48+ var s strings.Builder49+ s.WriteString("\n ")50+ if m.selectedFile == "" {51+ s.WriteString("Pick a file:")52+ } else {53+ s.WriteString("Selected file: " + m.filepicker.Styles.Selected.Render(m.selectedFile))54+ }55+ s.WriteString("\n\n" + m.filepicker.View() + "\n")56+ return s.String()57+}58+59+func main() {60+ fp := filepicker.New()61+ fp.Path, _ = os.UserHomeDir()62+63+ m := model{64+ filepicker: fp,65+ }66+ tm, _ := tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()67+ mm := tm.(model)68+ fmt.Println("\n You selected: " + m.filepicker.Styles.Selected.Render(mm.selectedFile) + "\n")69+}modified:examples/go.mod+2 added-1 removed
1 unmodified line2 3 go 1.174 5 require (6- github.com/charmbracelet/bubbles v0.15.06+ github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b7 github.com/charmbracelet/bubbletea v0.23.28 github.com/charmbracelet/glamour v0.6.09 github.com/charmbracelet/harmonica v0.2.010 github.com/charmbracelet/lipgloss v0.6.010 unmodified lines21 github.com/aymanbagabas/go-osc52 v1.2.1 // indirect22 github.com/aymerick/douceur v0.2.0 // indirect23 github.com/containerd/console v1.0.3 // indirect24 github.com/dlclark/regexp2 v1.4.0 // indirect25+ github.com/dustin/go-humanize v1.0.1 // indirect26 github.com/gorilla/css v1.0.0 // indirect27 github.com/mattn/go-localereader v0.0.1 // indirect28 github.com/mattn/go-runewidth v0.0.14 // indirect29 github.com/microcosm-cc/bluemonday v1.0.21 // indirect15 unmodified linesmodified:examples/go.sum+8 added-2 removed
5 unmodified lines6 github.com/aymanbagabas/go-osc52 v1.2.1 h1:q2sWUyDcozPLcLabEMd+a+7Ea2DitxZVN9hTxab9L4E=7 github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=8 github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=9 github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=10-github.com/charmbracelet/bubbles v0.15.0 h1:c5vZ3woHV5W2b8YZI1q7v4ZNQaPetfHuoHzx+56Z6TI=11-github.com/charmbracelet/bubbles v0.15.0/go.mod h1:Y7gSFbBzlMpUDR/XM9MhZI374Q+1p1kluf1uLl8iK74=10+github.com/charmbracelet/bubbles v0.15.1-0.20230303180827-c01b49080882 h1:bhZOZDpFHFKZhROCENdN+cUHNknH3MY8CUE2xJ9a3HM=11+github.com/charmbracelet/bubbles v0.15.1-0.20230303180827-c01b49080882/go.mod h1:39HL8bnL0foloiENA/KvD+3mNg5SqWQV2Qh3eY/4ey4=12+github.com/charmbracelet/bubbles v0.15.1-0.20230303214459-958a0ea710f1 h1:BcxSgchb4ZV1lJe9W9+LhQj6S3JouC39yYswVI4YOrw=13+github.com/charmbracelet/bubbles v0.15.1-0.20230303214459-958a0ea710f1/go.mod h1:39HL8bnL0foloiENA/KvD+3mNg5SqWQV2Qh3eY/4ey4=14+github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b h1:K9dWJ2spDhDhIrqnchjG867djPxWWe3mwdk6RdLMfhg=15+github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b/go.mod h1:39HL8bnL0foloiENA/KvD+3mNg5SqWQV2Qh3eY/4ey4=16 github.com/charmbracelet/glamour v0.6.0 h1:wi8fse3Y7nfcabbbDuwolqTqMQPMnVPeZhDM273bISc=17 github.com/charmbracelet/glamour v0.6.0/go.mod h1:taqWV4swIMMbWALc0m7AfE9JkPSU8om2538k9ITBxOc=18 github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=19 github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=5 unmodified lines25 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=26 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=27 github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=28 github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=29+github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=30+github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=31 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=32 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=33 github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=34 github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=89 unmodified lines