Skip to content
Myr

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

3 files+98 added−7 removed

added:examples/dynamic-textarea/main.go+91 added
1package main23import (4 "fmt"5 "log"6 "strings"78 "charm.land/bubbles/v2/textarea"9 tea "charm.land/bubbletea/v2"10)1112func main() {13 p := tea.NewProgram(initialModel())14 if _, err := p.Run(); err != nil {15 log.Fatal(err)16 }17}1819type model struct {20 textarea textarea.Model21}2223func 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()3435 return model{textarea: ti}36}3738func (m model) Init() tea.Cmd {39 return tea.Batch(textarea.Blink, tea.RequestBackgroundColor)40}4142func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {43 var cmds []tea.Cmd44 var cmd tea.Cmd4546 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 }5556 m.textarea, cmd = m.textarea.Update(msg)57 cmds = append(cmds, cmd)58 return m, tea.Batch(cmds...)59}6061func (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}7172func (m model) View() tea.View {73 const gap = 17475 var c *tea.Cursor76 if !m.textarea.VirtualCursor() {77 c = m.textarea.Cursor()78 c.Y += gap79 }8081 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")8788 v := tea.NewView(f)89 v.Cursor = c90 return v91}
modified:examples/go.mod+3 added-3 removed
3 unmodified lines
45replace charm.land/bubbletea/v2 => ../67require (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.0
18 unmodified lines
32 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 // indirect
8 unmodified lines
modified:examples/go.sum+4 added-4 removed
1charm.land/bubbles/v2 v2.0.0 h1:tE3eK/pHjmtrDiRdoC9uGNLgpopOd8fjhEe31B/ai5s=2charm.land/bubbles/v2 v2.0.0/go.mod h1:rCHoleP2XhU8um45NTuOWBPNVHxnkXKTiZqcclL/qOI=1charm.land/bubbles/v2 v2.1.0 h1:YSnNh5cPYlYjPxRrzs5VEn3vwhtEn3jVGRBT3M7/I0g=2charm.land/bubbles/v2 v2.1.0/go.mod h1:l97h4hym2hvWBVfmJDtrEHHCtkIKeTEb3TTJ4ZOB3wY=3charm.land/glamour/v2 v2.0.0 h1:IDBoqLEy7Hdpb9VOXN+khLP/XSxtJy1VsHuW/yF87+U=4charm.land/glamour/v2 v2.0.0/go.mod h1:kjq9WB0s8vuUYZNYey2jp4Lgd9f4cKdzAw88FZtpj/w=5charm.land/lipgloss/v2 v2.0.2 h1:xFolbF8JdpNkM2cEPTfXEcW1p6NRzOWTSamRfYEw8cs=6charm.land/lipgloss/v2 v2.0.2/go.mod h1:KjPle2Qd3YmvP1KL5OMHiHysGcNwq6u83MUjYkFvEkM=
50 unmodified lines
57github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag=58github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=59github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=60github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=61github.com/mattn/go-runewidth v0.0.20 h1:WcT52H91ZUAwy8+HUkdM3THM6gXqXuLJi9O3rjcQQaQ=62github.com/mattn/go-runewidth v0.0.20/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=61github.com/mattn/go-runewidth v0.0.21 h1:jJKAZiQH+2mIinzCJIaIG9Be1+0NR+5sz/lYEEjdM8w=62github.com/mattn/go-runewidth v0.0.21/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=63github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=64github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=65github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=66github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
24 unmodified lines