Skip to content
Myr

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

3 files+111 added−7 removed

added:examples/autocomplete/main.go+104 added
1package main23import (4 "encoding/json"5 "fmt"6 "io"7 "log"8 "net/http"910 "github.com/charmbracelet/bubbles/textinput"11 tea "github.com/charmbracelet/bubbletea"12)1314func main() {15 p := tea.NewProgram(initialModel())16 if _, err := p.Run(); err != nil {17 log.Fatal(err)18 }19}2021type gotReposSuccessMsg []repo22type gotReposErrMsg error2324type repo struct {25 Name string `json:"name"`26}2728const reposURL = "https://api.github.com/orgs/charmbracelet/repos"2930func getRepos() tea.Msg {31 req, err := http.NewRequest(http.MethodGet, reposURL, nil)32 if err != nil {33 return gotReposErrMsg(err)34 }3536 req.Header.Add("Accept", "application/vnd.github+json")37 req.Header.Add("X-GitHub-Api-Version", "2022-11-28")3839 resp, err := http.DefaultClient.Do(req)40 if err != nil {41 return gotReposErrMsg(err)42 }43 defer resp.Body.Close() // nolint: errcheck4445 data, err := io.ReadAll(resp.Body)46 if err != nil {47 return gotReposErrMsg(err)48 }4950 var repos []repo5152 err = json.Unmarshal(data, &repos)53 if err != nil {54 return gotReposErrMsg(err)55 }5657 return gotReposSuccessMsg(repos)58}5960type model struct {61 textInput textinput.Model62}6364func 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}7374func (m model) Init() tea.Cmd {75 return tea.Batch(getRepos, textinput.Blink)76}7778func (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 }9293 var cmd tea.Cmd94 m.textInput, cmd = m.textInput.Update(msg)95 return m, cmd96}9798func (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 line
23go 1.1745require (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-2bd6fa14c46a
20 unmodified lines
32 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 // indirect
6 unmodified lines
modified:examples/go.sum+4 added-4 removed
9 unmodified lines
10github.com/aymanbagabas/go-udiff v0.1.0 h1:9Dpklm2oBBhMxIFbMffmPvDaF7vOYfv9B5HXVr42KMU=11github.com/aymanbagabas/go-udiff v0.1.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=12github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=13github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=14github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=15github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=14github.com/charmbracelet/bubbles v0.16.2-0.20230821152602-eda891258c02 h1:MruS04uPbUJHF0MQzwOlh9yco1pHwPDLI7qGMu25IME=15github.com/charmbracelet/bubbles v0.16.2-0.20230821152602-eda891258c02/go.mod h1:XUdibuVUiMfcfKTRla58bmY3TWsdjgF+Rp8pvimQLck=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=
47 unmodified lines
67github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=68github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=69github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=70github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=71github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=72github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=71github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=72github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=73github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=74github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=75github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=76github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
49 unmodified lines