Skip to content

Commit

Permalink
refactor!: remove program options
Browse files Browse the repository at this point in the history
This replace program options by exposing them in the Program struct.
  • Loading branch information
aymanbagabas committed Jan 27, 2025
1 parent 8203a5e commit 95b0498
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 625 deletions.
6 changes: 3 additions & 3 deletions examples/altscreen-toggle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type model struct {
suspending bool
}

func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
return m, nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
switch msg := msg.(type) {
case tea.ResumeMsg:
m.suspending = false
Expand Down Expand Up @@ -79,7 +79,7 @@ func (m model) View() fmt.Stringer {
}

func main() {
if _, err := tea.NewProgram(model{}).Run(); err != nil {
if err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module examples
go 1.23.1

require (
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250114183437-fbe642df174c
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250114201644-43a5b4dd0af0
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250123211610-443afa6fa0c1
github.com/charmbracelet/colorprofile v0.1.10
github.com/charmbracelet/glamour v0.8.0
github.com/charmbracelet/harmonica v0.2.0
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250114183437-fbe642df174c h1:hhR5M/3Wt/mKLTPF/MyvA4/WWtnTmIzLXo69pW/9S5s=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250114183437-fbe642df174c/go.mod h1:M271uOSMoLQsiVV1yhFZx6JprPQCVXgLYpSEbWXtidM=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016 h1:qLul2uRoupbiXXjixWPE3yk5tMKEhWEmTHOGZpewFqs=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016/go.mod h1:DBfWo/ohdtbvjyDk/Q4UffdRd/rRKfYTwTqHdPFVHT8=
github.com/charmbracelet/colorprofile v0.1.10 h1:k6jIGJg4bPWvHZqcoLjFxH1bm9uT28Ysxg8guonDJ1Y=
github.com/charmbracelet/colorprofile v0.1.10/go.mod h1:6wPrSSR4QtwYtOY3h0bLRw5YOUAIKWlZIJ02CTAsZsk=
github.com/charmbracelet/glamour v0.8.0 h1:tPrjL3aRcQbn++7t18wOpgLyl8wrOHUEDS7IZ68QtZs=
Expand Down
6 changes: 3 additions & 3 deletions examples/list-fancy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type model struct {
delegateKeys *delegateKeyMap
}

func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
m.once = new(sync.Once)
return m, tea.Batch(
tea.RequestBackgroundColor,
Expand Down Expand Up @@ -163,7 +163,7 @@ func (m *model) updateListProperties() {
m.list.SetSize(m.width-h, m.height-v)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
var cmds []tea.Cmd

switch msg := msg.(type) {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (m model) View() fmt.Stringer {
}

func main() {
if _, err := tea.NewProgram(model{}).Run(); err != nil {
if err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/pipe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {

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

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

func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
return m, textinput.Blink
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
if key, ok := msg.(tea.KeyMsg); ok {
switch key.String() {
case "ctrl+c", "esc", "enter":
Expand Down
23 changes: 17 additions & 6 deletions examples/textarea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ import (
)

func main() {
p := tea.NewProgram(initialModel())
// p := tea.NewProgram(initialModel())
m := initialModel()
p := &tea.Program[model]{
Init: m.Init,
Update: func(m model, msg tea.Msg) (model, tea.Cmd) {
return m.Update(msg)
},
View: func(m model) fmt.Stringer {
return m.View()
},
}

if _, err := p.Run(); err != nil {
if err := p.Run(); err != nil {
log.Fatal(err)
}
}
Expand All @@ -31,21 +41,21 @@ func initialModel() model {
ti := textarea.New()
ti.Placeholder = "Once upon a time..."
ti.Focus()
ti.Cursor.SetMode(cursor.CursorHide)
ti.VirtualCursor.SetMode(cursor.CursorHide)

return model{
textarea: ti,
err: nil,
}
}

func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
return m, tea.Batch(
textarea.Blink,
)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd

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

x, y := m.textarea.CursorPosition()
cur := m.textarea.Cursor()
x, y := cur.Position.X, cur.Position.Y
f.Cursor = tea.NewCursor(x+xOffset, y+yOffset)

return f
Expand Down
Loading

0 comments on commit 95b0498

Please sign in to comment.