docs(example): add textarea dynamic height example (#1639)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by GitHub · parent 7df1e65 · Browse files · 3 files·+98 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+98 added−7 removed
- other:+98 added−7 removed
Provenance
- Commit
bbe4faf- Tree
6360d2f- Author
Christian Rocha <christian@rocha.is>- Committer
GitHub <noreply@github.com>- Parent
7df1e65
3 files changed+98 added−7 removed
added:examples/dynamic-textarea/main.go+91 added
1+package main2+3+import (4+ "fmt"5+ "log"6+ "strings"7+8+ "charm.land/bubbles/v2/textarea"9+ tea "charm.land/bubbletea/v2"10+)11+12+func main() {13+ p := tea.NewProgram(initialModel())14+ if _, err := p.Run(); err != nil {15+ log.Fatal(err)16+ }17+}18+19+type model struct {20+ textarea textarea.Model21+}22+23+func initialModel() model {24+ ti := textarea.New()25+ ti.Placeholder = "Schnrr..."26+ ti.ShowLineNumbers = true27+ ti.DynamicHeight = true28+ ti.MinHeight = 329+ ti.MaxHeight = 1530+ ti.MaxContentHeight = 2031+ ti.SetWidth(60)32+ ti.SetVirtualCursor(false)33+ ti.Focus()34+35+ return model{textarea: ti}36+}37+38+func (m model) Init() tea.Cmd {39+ return tea.Batch(textarea.Blink, tea.RequestBackgroundColor)40+}41+42+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {43+ var cmds []tea.Cmd44+ var cmd tea.Cmd45+46+ switch msg := msg.(type) {47+ case tea.BackgroundColorMsg:48+ m.textarea.SetStyles(textarea.DefaultStyles(msg.IsDark()))49+ case tea.KeyPressMsg:50+ switch msg.String() {51+ case "ctrl+c":52+ return m, tea.Quit53+ }54+ }55+56+ m.textarea, cmd = m.textarea.Update(msg)57+ cmds = append(cmds, cmd)58+ return m, tea.Batch(cmds...)59+}60+61+func (m model) statusView() string {62+ return fmt.Sprintf(63+ "\nHeight: %d · Lines: %d · Cursor: (%d, %d) · Scroll: %.0f%%",64+ m.textarea.Height(),65+ m.textarea.LineCount(),66+ m.textarea.Line(),67+ m.textarea.Column(),68+ m.textarea.ScrollPercent()*100,69+ )70+}71+72+func (m model) View() tea.View {73+ const gap = 174+75+ var c *tea.Cursor76+ if !m.textarea.VirtualCursor() {77+ c = m.textarea.Cursor()78+ c.Y += gap79+ }80+81+ f := strings.Repeat("\n", gap)82+ f += strings.Join([]string{83+ m.textarea.View(),84+ m.statusView(),85+ "\n(ctrl+c to quit)",86+ }, "\n")87+88+ v := tea.NewView(f)89+ v.Cursor = c90+ return v91+}modified:examples/go.mod+3 added-3 removed
3 unmodified lines4 5 replace charm.land/bubbletea/v2 => ../6 7 require (8- charm.land/bubbles/v2 v2.0.09- charm.land/bubbletea/v2 v2.0.08+ charm.land/bubbles/v2 v2.1.09+ charm.land/bubbletea/v2 v2.0.210 charm.land/glamour/v2 v2.0.011 charm.land/lipgloss/v2 v2.0.212 github.com/charmbracelet/colorprofile v0.4.313 github.com/charmbracelet/harmonica v0.2.018 unmodified lines32 github.com/clipperhouse/uax29/v2 v2.7.0 // indirect33 github.com/dlclark/regexp2 v1.11.0 // indirect34 github.com/dustin/go-humanize v1.0.1 // indirect35 github.com/gorilla/css v1.0.1 // indirect36- github.com/mattn/go-runewidth v0.0.20 // indirect36+ github.com/mattn/go-runewidth v0.0.21 // indirect37 github.com/microcosm-cc/bluemonday v1.0.27 // indirect38 github.com/muesli/cancelreader v0.2.2 // indirect39 github.com/rivo/uniseg v0.4.7 // indirect40 github.com/sahilm/fuzzy v0.1.1 // indirect8 unmodified linesmodified:examples/go.sum+4 added-4 removed
1-charm.land/bubbles/v2 v2.0.0 h1:tE3eK/pHjmtrDiRdoC9uGNLgpopOd8fjhEe31B/ai5s=2-charm.land/bubbles/v2 v2.0.0/go.mod h1:rCHoleP2XhU8um45NTuOWBPNVHxnkXKTiZqcclL/qOI=1+charm.land/bubbles/v2 v2.1.0 h1:YSnNh5cPYlYjPxRrzs5VEn3vwhtEn3jVGRBT3M7/I0g=2+charm.land/bubbles/v2 v2.1.0/go.mod h1:l97h4hym2hvWBVfmJDtrEHHCtkIKeTEb3TTJ4ZOB3wY=3 charm.land/glamour/v2 v2.0.0 h1:IDBoqLEy7Hdpb9VOXN+khLP/XSxtJy1VsHuW/yF87+U=4 charm.land/glamour/v2 v2.0.0/go.mod h1:kjq9WB0s8vuUYZNYey2jp4Lgd9f4cKdzAw88FZtpj/w=5 charm.land/lipgloss/v2 v2.0.2 h1:xFolbF8JdpNkM2cEPTfXEcW1p6NRzOWTSamRfYEw8cs=6 charm.land/lipgloss/v2 v2.0.2/go.mod h1:KjPle2Qd3YmvP1KL5OMHiHysGcNwq6u83MUjYkFvEkM=50 unmodified lines57 github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag=58 github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=59 github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=60 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=61-github.com/mattn/go-runewidth v0.0.20 h1:WcT52H91ZUAwy8+HUkdM3THM6gXqXuLJi9O3rjcQQaQ=62-github.com/mattn/go-runewidth v0.0.20/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=61+github.com/mattn/go-runewidth v0.0.21 h1:jJKAZiQH+2mIinzCJIaIG9Be1+0NR+5sz/lYEEjdM8w=62+github.com/mattn/go-runewidth v0.0.21/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=63 github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=64 github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=65 github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=66 github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=24 unmodified lines