-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(textinput): example for autocompletion with charmbracelet reposi…
…tories (#803) * feat: autocompletion with charmbracelet repositories * chore(deps): bump bubbles
- Loading branch information
1 parent
f6f65ae
commit 1ad1277
Showing
3 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func main() { | ||
p := tea.NewProgram(initialModel()) | ||
if _, err := p.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type gotReposSuccessMsg []repo | ||
type gotReposErrMsg error | ||
|
||
type repo struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
const reposURL = "https://api.github.com/orgs/charmbracelet/repos" | ||
|
||
func getRepos() tea.Msg { | ||
req, err := http.NewRequest(http.MethodGet, reposURL, nil) | ||
if err != nil { | ||
return gotReposErrMsg(err) | ||
} | ||
|
||
req.Header.Add("Accept", "application/vnd.github+json") | ||
req.Header.Add("X-GitHub-Api-Version", "2022-11-28") | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return gotReposErrMsg(err) | ||
} | ||
defer resp.Body.Close() // nolint: errcheck | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return gotReposErrMsg(err) | ||
} | ||
|
||
var repos []repo | ||
|
||
err = json.Unmarshal(data, &repos) | ||
if err != nil { | ||
return gotReposErrMsg(err) | ||
} | ||
|
||
return gotReposSuccessMsg(repos) | ||
} | ||
|
||
type model struct { | ||
textInput textinput.Model | ||
} | ||
|
||
func initialModel() model { | ||
ti := textinput.New() | ||
ti.Placeholder = "Bubbletea" | ||
ti.Focus() | ||
ti.CharLimit = 50 | ||
ti.Width = 20 | ||
ti.ShowSuggestions = true | ||
return model{textInput: ti} | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return tea.Batch(getRepos, textinput.Blink) | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc: | ||
return m, tea.Quit | ||
} | ||
case gotReposSuccessMsg: | ||
var suggestions []string | ||
for _, r := range msg { | ||
suggestions = append(suggestions, r.Name) | ||
} | ||
m.textInput.SetSuggestions(suggestions) | ||
} | ||
|
||
var cmd tea.Cmd | ||
m.textInput, cmd = m.textInput.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
return fmt.Sprintf( | ||
"What’s your favorite Charm repository?\n\n%s\n\n%s\n", | ||
m.textInput.View(), | ||
"(esc to quit)", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters