Skip to content
Myr

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

3 files+79 added−3 removed

added:examples/file-picker/main.go+69 added
1package main23import (4 "fmt"5 "os"6 "strings"78 "github.com/charmbracelet/bubbles/filepicker"9 tea "github.com/charmbracelet/bubbletea"10)1112type model struct {13 filepicker filepicker.Model14 selectedFile string15 quitting bool16}1718func (m model) Init() tea.Cmd {19 return m.filepicker.Init()20}2122func (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 }3132 var cmd tea.Cmd33 m.filepicker, cmd = m.filepicker.Update(msg)3435 // 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 }4041 return m, cmd42}4344func (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}5859func main() {60 fp := filepicker.New()61 fp.Path, _ = os.UserHomeDir()6263 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 line
23go 1.1745require (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.0
10 unmodified lines
21 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 // indirect
15 unmodified lines
modified:examples/go.sum+8 added-2 removed
5 unmodified lines
6github.com/aymanbagabas/go-osc52 v1.2.1 h1:q2sWUyDcozPLcLabEMd+a+7Ea2DitxZVN9hTxab9L4E=7github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=8github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=9github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=10github.com/charmbracelet/bubbles v0.15.0 h1:c5vZ3woHV5W2b8YZI1q7v4ZNQaPetfHuoHzx+56Z6TI=11github.com/charmbracelet/bubbles v0.15.0/go.mod h1:Y7gSFbBzlMpUDR/XM9MhZI374Q+1p1kluf1uLl8iK74=10github.com/charmbracelet/bubbles v0.15.1-0.20230303180827-c01b49080882 h1:bhZOZDpFHFKZhROCENdN+cUHNknH3MY8CUE2xJ9a3HM=11github.com/charmbracelet/bubbles v0.15.1-0.20230303180827-c01b49080882/go.mod h1:39HL8bnL0foloiENA/KvD+3mNg5SqWQV2Qh3eY/4ey4=12github.com/charmbracelet/bubbles v0.15.1-0.20230303214459-958a0ea710f1 h1:BcxSgchb4ZV1lJe9W9+LhQj6S3JouC39yYswVI4YOrw=13github.com/charmbracelet/bubbles v0.15.1-0.20230303214459-958a0ea710f1/go.mod h1:39HL8bnL0foloiENA/KvD+3mNg5SqWQV2Qh3eY/4ey4=14github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b h1:K9dWJ2spDhDhIrqnchjG867djPxWWe3mwdk6RdLMfhg=15github.com/charmbracelet/bubbles v0.15.1-0.20230306155959-3372cf1aea2b/go.mod h1:39HL8bnL0foloiENA/KvD+3mNg5SqWQV2Qh3eY/4ey4=16github.com/charmbracelet/glamour v0.6.0 h1:wi8fse3Y7nfcabbbDuwolqTqMQPMnVPeZhDM273bISc=17github.com/charmbracelet/glamour v0.6.0/go.mod h1:taqWV4swIMMbWALc0m7AfE9JkPSU8om2538k9ITBxOc=18github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=19github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
5 unmodified lines
25github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=26github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=27github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=28github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=29github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=30github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=31github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=32github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=33github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=34github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
89 unmodified lines