main.go
This file
Path
examples/pipe/main.go
97 lines · 1.8 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.
feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 1–14why, one step back1package main23// An example illustrating how to pipe in data to a Bubble Tea application.4// More so, this serves as proof that Bubble Tea will automatically listen for5// keystrokes when input is not a TTY, such as when data is piped or redirected6// in.78import (9 "bufio"10 "fmt"11 "io"12 "os"13 "strings"14Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 15–17why, one step back15 "charm.land/bubbles/v2/textinput"16 tea "charm.land/bubbletea/v2"17 "charm.land/lipgloss/v2"feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 18–60why, one step back18)1920func main() {21 stat, err := os.Stdin.Stat()22 if err != nil {23 panic(err)24 }2526 if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 {27 fmt.Println("Try piping in some text.")28 os.Exit(1)29 }3031 reader := bufio.NewReader(os.Stdin)32 var b strings.Builder3334 for {35 r, _, err := reader.ReadRune()36 if err != nil && err == io.EOF {37 break38 }39 _, err = b.WriteRune(r)40 if err != nil {41 fmt.Println("Error getting input:", err)42 os.Exit(1)43 }44 }4546 model := newModel(strings.TrimSpace(b.String()))4748 if _, err := tea.NewProgram(model).Run(); err != nil {49 fmt.Println("Couldn't start program:", err)50 os.Exit(1)51 }52}5354type model struct {55 userInput textinput.Model56}5758func newModel(initialValue string) (m model) {59 i := textinput.New()60 i.Prompt = ""Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 61–66why, one step back6162 s := i.Styles()63 s.Cursor.Color = lipgloss.Color("63")64 i.SetStyles(s)6566 i.SetWidth(48)feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 67–71why, one step back67 i.SetValue(initialValue)68 i.CursorEnd()69 i.Focus()7071 m.userInput = iMerge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 72why, one step back72 return mfeat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 73–80why, one step back73}7475func (m model) Init() tea.Cmd {76 return textinput.Blink77}7879func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {80 if key, ok := msg.(tea.KeyMsg); ok {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 81–82why, one step back81 switch key.String() {82 case "ctrl+c", "esc", "enter":feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 83–91why, one step back83 return m, tea.Quit84 }85 }8687 var cmd tea.Cmd88 m.userInput, cmd = m.userInput.Update(msg)89 return m, cmd90}91Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 92–93why, one step back92func (m model) View() tea.View {93 return tea.NewView(fmt.Sprintf(feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 94–95why, one step back94 "\nYou piped in: %s\n\nPress ^C to exit",95 m.userInput.View(),Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 96why, one step back96 ))feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
line 97why, one step back97}