feat(textinput): example for autocompletion with charmbracelet repositories (#803)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: autocompletion with charmbracelet repositories * chore(deps): bump bubbles
authored by Maas Lalani · committed by GitHub · parent f6f65ae · Browse files · 3 files·+111 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+111 added−7 removed
- other:+111 added−7 removed
Provenance
- Commit
1ad1277- Tree
a562976- Author
Maas Lalani <maas@lalani.dev>- Committer
GitHub <noreply@github.com>- Parent
f6f65ae
3 files changed+111 added−7 removed
added:examples/autocomplete/main.go+104 added
1+package main2+3+import (4+ "encoding/json"5+ "fmt"6+ "io"7+ "log"8+ "net/http"9+10+ "github.com/charmbracelet/bubbles/textinput"11+ tea "github.com/charmbracelet/bubbletea"12+)13+14+func main() {15+ p := tea.NewProgram(initialModel())16+ if _, err := p.Run(); err != nil {17+ log.Fatal(err)18+ }19+}20+21+type gotReposSuccessMsg []repo22+type gotReposErrMsg error23+24+type repo struct {25+ Name string `json:"name"`26+}27+28+const reposURL = "https://api.github.com/orgs/charmbracelet/repos"29+30+func getRepos() tea.Msg {31+ req, err := http.NewRequest(http.MethodGet, reposURL, nil)32+ if err != nil {33+ return gotReposErrMsg(err)34+ }35+36+ req.Header.Add("Accept", "application/vnd.github+json")37+ req.Header.Add("X-GitHub-Api-Version", "2022-11-28")38+39+ resp, err := http.DefaultClient.Do(req)40+ if err != nil {41+ return gotReposErrMsg(err)42+ }43+ defer resp.Body.Close() // nolint: errcheck44+45+ data, err := io.ReadAll(resp.Body)46+ if err != nil {47+ return gotReposErrMsg(err)48+ }49+50+ var repos []repo51+52+ err = json.Unmarshal(data, &repos)53+ if err != nil {54+ return gotReposErrMsg(err)55+ }56+57+ return gotReposSuccessMsg(repos)58+}59+60+type model struct {61+ textInput textinput.Model62+}63+64+func initialModel() model {65+ ti := textinput.New()66+ ti.Placeholder = "Bubbletea"67+ ti.Focus()68+ ti.CharLimit = 5069+ ti.Width = 2070+ ti.ShowSuggestions = true71+ return model{textInput: ti}72+}73+74+func (m model) Init() tea.Cmd {75+ return tea.Batch(getRepos, textinput.Blink)76+}77+78+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {79+ switch msg := msg.(type) {80+ case tea.KeyMsg:81+ switch msg.Type {82+ case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:83+ return m, tea.Quit84+ }85+ case gotReposSuccessMsg:86+ var suggestions []string87+ for _, r := range msg {88+ suggestions = append(suggestions, r.Name)89+ }90+ m.textInput.SetSuggestions(suggestions)91+ }92+93+ var cmd tea.Cmd94+ m.textInput, cmd = m.textInput.Update(msg)95+ return m, cmd96+}97+98+func (m model) View() string {99+ return fmt.Sprintf(100+ "What’s your favorite Charm repository?\n\n%s\n\n%s\n",101+ m.textInput.View(),102+ "(esc to quit)",103+ )104+}modified:examples/go.mod+3 added-3 removed
1 unmodified line2 3 go 1.174 5 require (6- github.com/charmbracelet/bubbles v0.16.17- github.com/charmbracelet/bubbletea v0.24.16+ github.com/charmbracelet/bubbles v0.16.2-0.20230821152602-eda891258c027+ github.com/charmbracelet/bubbletea v0.24.28 github.com/charmbracelet/glamour v0.6.09 github.com/charmbracelet/harmonica v0.2.010 github.com/charmbracelet/lipgloss v0.7.111 github.com/charmbracelet/x/exp/teatest v0.0.0-20230508155401-2bd6fa14c46a20 unmodified lines32 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect33 github.com/muesli/cancelreader v0.2.2 // indirect34 github.com/olekukonko/tablewriter v0.0.5 // indirect35 github.com/rivo/uniseg v0.2.0 // indirect36- github.com/sahilm/fuzzy v0.1.0 // indirect36+ github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect37 github.com/yuin/goldmark v1.5.2 // indirect38 github.com/yuin/goldmark-emoji v1.0.1 // indirect39 golang.org/x/net v0.7.0 // indirect40 golang.org/x/sync v0.1.0 // indirect6 unmodified linesmodified:examples/go.sum+4 added-4 removed
9 unmodified lines10 github.com/aymanbagabas/go-udiff v0.1.0 h1:9Dpklm2oBBhMxIFbMffmPvDaF7vOYfv9B5HXVr42KMU=11 github.com/aymanbagabas/go-udiff v0.1.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=12 github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=13 github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=14-github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=15-github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=14+github.com/charmbracelet/bubbles v0.16.2-0.20230821152602-eda891258c02 h1:MruS04uPbUJHF0MQzwOlh9yco1pHwPDLI7qGMu25IME=15+github.com/charmbracelet/bubbles v0.16.2-0.20230821152602-eda891258c02/go.mod h1:XUdibuVUiMfcfKTRla58bmY3TWsdjgF+Rp8pvimQLck=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=47 unmodified lines67 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=68 github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=69 github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=70 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=71-github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=72-github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=71+github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=72+github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=73 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=74 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=75 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=76 github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=49 unmodified lines