Skip to content

Commit 95b0498

Browse files
committed
refactor!: remove program options
This replace program options by exposing them in the Program struct.
1 parent 8203a5e commit 95b0498

File tree

11 files changed

+114
-625
lines changed

11 files changed

+114
-625
lines changed

examples/altscreen-toggle/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ type model struct {
1919
suspending bool
2020
}
2121

22-
func (m model) Init() (tea.Model, tea.Cmd) {
22+
func (m model) Init() (model, tea.Cmd) {
2323
return m, nil
2424
}
2525

26-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
26+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
2727
switch msg := msg.(type) {
2828
case tea.ResumeMsg:
2929
m.suspending = false
@@ -79,7 +79,7 @@ func (m model) View() fmt.Stringer {
7979
}
8080

8181
func main() {
82-
if _, err := tea.NewProgram(model{}).Run(); err != nil {
82+
if err := tea.NewProgram(model{}).Run(); err != nil {
8383
fmt.Println("Error running program:", err)
8484
os.Exit(1)
8585
}

examples/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module examples
33
go 1.23.1
44

55
require (
6-
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250114183437-fbe642df174c
7-
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250114201644-43a5b4dd0af0
6+
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016
7+
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250123211610-443afa6fa0c1
88
github.com/charmbracelet/colorprofile v0.1.10
99
github.com/charmbracelet/glamour v0.8.0
1010
github.com/charmbracelet/harmonica v0.2.0

examples/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp
1414
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
1515
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
1616
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
17-
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250114183437-fbe642df174c h1:hhR5M/3Wt/mKLTPF/MyvA4/WWtnTmIzLXo69pW/9S5s=
18-
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250114183437-fbe642df174c/go.mod h1:M271uOSMoLQsiVV1yhFZx6JprPQCVXgLYpSEbWXtidM=
17+
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016 h1:qLul2uRoupbiXXjixWPE3yk5tMKEhWEmTHOGZpewFqs=
18+
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016/go.mod h1:DBfWo/ohdtbvjyDk/Q4UffdRd/rRKfYTwTqHdPFVHT8=
1919
github.com/charmbracelet/colorprofile v0.1.10 h1:k6jIGJg4bPWvHZqcoLjFxH1bm9uT28Ysxg8guonDJ1Y=
2020
github.com/charmbracelet/colorprofile v0.1.10/go.mod h1:6wPrSSR4QtwYtOY3h0bLRw5YOUAIKWlZIJ02CTAsZsk=
2121
github.com/charmbracelet/glamour v0.8.0 h1:tPrjL3aRcQbn++7t18wOpgLyl8wrOHUEDS7IZ68QtZs=

examples/list-fancy/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type model struct {
105105
delegateKeys *delegateKeyMap
106106
}
107107

108-
func (m model) Init() (tea.Model, tea.Cmd) {
108+
func (m model) Init() (model, tea.Cmd) {
109109
m.once = new(sync.Once)
110110
return m, tea.Batch(
111111
tea.RequestBackgroundColor,
@@ -163,7 +163,7 @@ func (m *model) updateListProperties() {
163163
m.list.SetSize(m.width-h, m.height-v)
164164
}
165165

166-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
166+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
167167
var cmds []tea.Cmd
168168

169169
switch msg := msg.(type) {
@@ -244,7 +244,7 @@ func (m model) View() fmt.Stringer {
244244
}
245245

246246
func main() {
247-
if _, err := tea.NewProgram(model{}).Run(); err != nil {
247+
if err := tea.NewProgram(model{}).Run(); err != nil {
248248
fmt.Println("Error running program:", err)
249249
os.Exit(1)
250250
}

examples/pipe/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func main() {
4545

4646
model := newModel(strings.TrimSpace(b.String()))
4747

48-
if _, err := tea.NewProgram(model).Run(); err != nil {
48+
if err := tea.NewProgram(model).Run(); err != nil {
4949
fmt.Println("Couldn't start program:", err)
5050
os.Exit(1)
5151
}
@@ -68,11 +68,11 @@ func newModel(initialValue string) (m model) {
6868
return
6969
}
7070

71-
func (m model) Init() (tea.Model, tea.Cmd) {
71+
func (m model) Init() (model, tea.Cmd) {
7272
return m, textinput.Blink
7373
}
7474

75-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
75+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
7676
if key, ok := msg.(tea.KeyMsg); ok {
7777
switch key.String() {
7878
case "ctrl+c", "esc", "enter":

examples/textarea/main.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ import (
1313
)
1414

1515
func main() {
16-
p := tea.NewProgram(initialModel())
16+
// p := tea.NewProgram(initialModel())
17+
m := initialModel()
18+
p := &tea.Program[model]{
19+
Init: m.Init,
20+
Update: func(m model, msg tea.Msg) (model, tea.Cmd) {
21+
return m.Update(msg)
22+
},
23+
View: func(m model) fmt.Stringer {
24+
return m.View()
25+
},
26+
}
1727

18-
if _, err := p.Run(); err != nil {
28+
if err := p.Run(); err != nil {
1929
log.Fatal(err)
2030
}
2131
}
@@ -31,21 +41,21 @@ func initialModel() model {
3141
ti := textarea.New()
3242
ti.Placeholder = "Once upon a time..."
3343
ti.Focus()
34-
ti.Cursor.SetMode(cursor.CursorHide)
44+
ti.VirtualCursor.SetMode(cursor.CursorHide)
3545

3646
return model{
3747
textarea: ti,
3848
err: nil,
3949
}
4050
}
4151

42-
func (m model) Init() (tea.Model, tea.Cmd) {
52+
func (m model) Init() (model, tea.Cmd) {
4353
return m, tea.Batch(
4454
textarea.Blink,
4555
)
4656
}
4757

48-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
58+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
4959
var cmds []tea.Cmd
5060
var cmd tea.Cmd
5161

@@ -87,7 +97,8 @@ func (m model) View() fmt.Stringer {
8797
"(ctrl+c to quit)",
8898
) + "\n\n")
8999

90-
x, y := m.textarea.CursorPosition()
100+
cur := m.textarea.Cursor()
101+
x, y := cur.Position.X, cur.Position.Y
91102
f.Cursor = tea.NewCursor(x+xOffset, y+yOffset)
92103

93104
return f

0 commit comments

Comments
 (0)