Skip to content

Commit a69f856

Browse files
committed
chore: update examples
1 parent 59f9794 commit a69f856

File tree

50 files changed

+211
-193
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+211
-193
lines changed

examples/autocomplete/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
func main() {
1818
p := tea.NewProgram(initialModel())
19-
if _, err := p.Run(); err != nil {
19+
if err := p.Run(); err != nil {
2020
log.Fatal(err)
2121
}
2222
}
@@ -101,11 +101,11 @@ func initialModel() model {
101101
return model{textInput: ti, help: h, keymap: km}
102102
}
103103

104-
func (m model) Init() (tea.Model, tea.Cmd) {
104+
func (m model) Init() (model, tea.Cmd) {
105105
return m, tea.Batch(getRepos, textinput.Blink)
106106
}
107107

108-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
108+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
109109
switch msg := msg.(type) {
110110
case tea.KeyPressMsg:
111111
switch msg.String() {

examples/capability/main.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ type model struct {
1414
width int
1515
}
1616

17-
var _ tea.Model = model{}
18-
1917
// Init implements tea.Model.
20-
func (m model) Init() (tea.Model, tea.Cmd) {
18+
func (m model) Init() (model, tea.Cmd) {
2119
m.input = textinput.New()
2220
m.input.Placeholder = "Enter capability name to request"
2321
return m, m.input.Focus()
2422
}
2523

2624
// Update implements tea.Model.
27-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
25+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
2826
var cmd tea.Cmd
2927
switch msg := msg.(type) {
3028
case tea.WindowSizeMsg:
@@ -59,7 +57,7 @@ func (m model) View() fmt.Stringer {
5957
}
6058

6159
func main() {
62-
if _, err := tea.NewProgram(model{}).Run(); err != nil {
60+
if err := tea.NewProgram(model{}).Run(); err != nil {
6361
log.Fatal(err)
6462
}
6563
}

examples/cellbuffer/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ type model struct {
143143
}
144144

145145
func (m model) Init() (model, tea.Cmd) {
146-
return m, animate()
146+
return m, tea.Batch(
147+
animate(),
148+
tea.EnableMouseCellMotion,
149+
tea.EnterAltScreen,
150+
)
147151
}
148152

149153
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
@@ -193,7 +197,7 @@ func main() {
193197
spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),
194198
}
195199

196-
p := tea.NewProgram(m, tea.WithAltScreen[model](), tea.WithMouseCellMotion[model]())
200+
p := tea.NewProgram(m)
197201
if err := p.Run(); err != nil {
198202
fmt.Println("Uh oh:", err)
199203
os.Exit(1)

examples/chat/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const gap = "\n\n"
1919

2020
func main() {
2121
p := tea.NewProgram(initialModel())
22-
if _, err := p.Run(); err != nil {
22+
if err := p.Run(); err != nil {
2323
fmt.Fprintf(os.Stderr, "Oof: %v\n", err)
2424
}
2525
}
@@ -63,11 +63,11 @@ Type a message and press Enter to send.`)
6363
}
6464
}
6565

66-
func (m model) Init() (tea.Model, tea.Cmd) {
66+
func (m model) Init() (model, tea.Cmd) {
6767
return m, textarea.Blink
6868
}
6969

70-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
70+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
7171
switch msg := msg.(type) {
7272
case tea.WindowSizeMsg:
7373
m.viewport.SetWidth(msg.Width)

examples/colorprofile/main.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ var myFancyColor color.Color
1515

1616
type model struct{}
1717

18-
var _ tea.Model = model{}
19-
2018
// Init implements tea.Model.
21-
func (m model) Init() (tea.Model, tea.Cmd) {
19+
func (m model) Init() (model, tea.Cmd) {
2220
return m, tea.Batch(
2321
tea.RequestCapability("RGB"),
2422
tea.RequestCapability("Tc"),
2523
)
2624
}
2725

2826
// Update implements tea.Model.
29-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
27+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
3028
switch msg := msg.(type) {
3129
case tea.KeyMsg:
3230
return m, tea.Quit
@@ -47,8 +45,9 @@ func (m model) View() fmt.Stringer {
4745
func main() {
4846
myFancyColor, _ = colorful.Hex("#6b50ff")
4947

50-
p := tea.NewProgram(model{}, tea.WithColorProfile(colorprofile.TrueColor))
51-
if _, err := p.Run(); err != nil {
48+
p := tea.NewProgram(model{})
49+
p.Profile = colorprofile.TrueColor
50+
if err := p.Run(); err != nil {
5251
log.Fatal(err)
5352
}
5453
}

examples/composable-views/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ func newModel(timeout time.Duration) mainModel {
7373
return m
7474
}
7575

76-
func (m mainModel) Init() (tea.Model, tea.Cmd) {
76+
func (m mainModel) Init() (mainModel, tea.Cmd) {
7777
// start the timer and spinner on program start
7878
timer, cmd := m.timer.Init()
7979
m.timer = timer
8080
return m, tea.Batch(cmd, m.spinner.Tick)
8181
}
8282

83-
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
83+
func (m mainModel) Update(msg tea.Msg) (mainModel, tea.Cmd) {
8484
var cmd tea.Cmd
8585
var cmds []tea.Cmd
8686
switch msg := msg.(type) {
@@ -160,7 +160,7 @@ func (m *mainModel) resetSpinner() {
160160
func main() {
161161
p := tea.NewProgram(newModel(defaultTime))
162162

163-
if _, err := p.Run(); err != nil {
163+
if err := p.Run(); err != nil {
164164
log.Fatal(err)
165165
}
166166
}

examples/credit-card-form/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func main() {
1515
p := tea.NewProgram(initialModel())
1616

17-
if _, err := p.Run(); err != nil {
17+
if err := p.Run(); err != nil {
1818
log.Fatal(err)
1919
}
2020
}
@@ -124,11 +124,11 @@ func initialModel() model {
124124
}
125125
}
126126

127-
func (m model) Init() (tea.Model, tea.Cmd) {
127+
func (m model) Init() (model, tea.Cmd) {
128128
return m, textinput.Blink
129129
}
130130

131-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
131+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
132132
var cmds []tea.Cmd = make([]tea.Cmd, len(m.inputs))
133133

134134
switch msg := msg.(type) {

examples/cursor-style/main.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@ import (
88
)
99

1010
type model struct {
11-
style tea.CursorStyle
11+
shape tea.CursorShape
1212
blink bool
1313
}
1414

15-
func (m model) Init() (tea.Model, tea.Cmd) {
15+
func (m model) Init() (model, tea.Cmd) {
1616
m.blink = true
1717
return m, nil
1818
}
1919

20-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
20+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
2121
switch msg := msg.(type) {
2222
case tea.KeyPressMsg:
2323
switch msg.String() {
2424
case "ctrl+q", "q":
2525
return m, tea.Quit
2626
case "h", "left":
27-
if m.style == tea.CursorBlock && m.blink {
27+
if m.shape == tea.CursorBlock && m.blink {
2828
break
2929
}
3030
if m.blink {
31-
m.style--
31+
m.shape--
3232
}
3333
m.blink = !m.blink
3434
case "l", "right":
35-
if m.style == tea.CursorBar && !m.blink {
35+
if m.shape == tea.CursorBar && !m.blink {
3636
break
3737
}
3838
if !m.blink {
39-
m.style++
39+
m.shape++
4040
}
4141
m.blink = !m.blink
4242
}
@@ -49,7 +49,7 @@ func (m model) View() fmt.Stringer {
4949
"\n\n" +
5050
" <- This is the cursor (a " + m.describeCursor() + ")")
5151
f.Cursor = tea.NewCursor(0, 2)
52-
f.Cursor.Style = m.style
52+
f.Cursor.Shape = m.shape
5353
f.Cursor.Blink = m.blink
5454
return f
5555
}
@@ -63,7 +63,7 @@ func (m model) describeCursor() string {
6363
adj = "steady"
6464
}
6565

66-
switch m.style {
66+
switch m.shape {
6767
case tea.CursorBlock:
6868
noun = "block"
6969
case tea.CursorUnderline:
@@ -77,7 +77,7 @@ func (m model) describeCursor() string {
7777

7878
func main() {
7979
p := tea.NewProgram(model{})
80-
if _, err := p.Run(); err != nil {
80+
if err := p.Run(); err != nil {
8181
fmt.Fprintf(os.Stderr, "Error: %v", err)
8282
os.Exit(1)
8383
}

examples/debounce/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ type model struct {
2626
tag int
2727
}
2828

29-
func (m model) Init() (tea.Model, tea.Cmd) {
29+
func (m model) Init() (model, tea.Cmd) {
3030
return m, nil
3131
}
3232

33-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
33+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
3434
switch msg := msg.(type) {
3535
case tea.KeyPressMsg:
3636
// Increment the tag on the model...
@@ -59,7 +59,7 @@ func (m model) View() fmt.Stringer {
5959
}
6060

6161
func main() {
62-
if _, err := tea.NewProgram(model{}).Run(); err != nil {
62+
if err := tea.NewProgram(model{}).Run(); err != nil {
6363
fmt.Println("uh oh:", err)
6464
os.Exit(1)
6565
}

examples/doom-fire/main.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ type model struct {
2323
startTime time.Time
2424
}
2525

26-
func (m model) Init() (tea.Model, tea.Cmd) {
27-
return m, tick
26+
func (m model) Init() (model, tea.Cmd) {
27+
return m, tea.Batch(
28+
tick,
29+
tea.EnterAltScreen,
30+
)
2831
}
2932

30-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
33+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
3134
switch msg := msg.(type) {
3235
case tea.KeyPressMsg:
3336
if msg.String() == "q" || msg.String() == "ctrl+c" {
@@ -128,8 +131,8 @@ func initialModel() model {
128131
}
129132

130133
func main() {
131-
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
132-
if _, err := p.Run(); err != nil {
134+
p := tea.NewProgram(initialModel())
135+
if err := p.Run(); err != nil {
133136
fmt.Printf("Error running program: %v", err)
134137
}
135138
}

examples/exec/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ type model struct {
2626
err error
2727
}
2828

29-
func (m model) Init() (tea.Model, tea.Cmd) {
29+
func (m model) Init() (model, tea.Cmd) {
3030
return m, nil
3131
}
3232

33-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
33+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
3434
switch msg := msg.(type) {
3535
case tea.KeyPressMsg:
3636
switch msg.String() {
@@ -64,7 +64,7 @@ func (m model) View() fmt.Stringer {
6464

6565
func main() {
6666
m := model{}
67-
if _, err := tea.NewProgram(m).Run(); err != nil {
67+
if err := tea.NewProgram(m).Run(); err != nil {
6868
fmt.Println("Error running program:", err)
6969
os.Exit(1)
7070
}

examples/file-picker/main.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ func clearErrorAfter(t time.Duration) tea.Cmd {
2626
})
2727
}
2828

29-
func (m model) Init() (tea.Model, tea.Cmd) {
29+
func (m model) Init() (model, tea.Cmd) {
3030
fp, cmd := m.filepicker.Init()
3131
m.filepicker = fp
32-
return m, cmd
32+
return m, tea.Batch(
33+
tea.EnterAltScreen,
34+
cmd,
35+
)
3336
}
3437

35-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
38+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
3639
switch msg := msg.(type) {
3740
case tea.KeyPressMsg:
3841
switch msg.String() {
@@ -90,7 +93,10 @@ func main() {
9093
m := model{
9194
filepicker: fp,
9295
}
93-
tm, _ := tea.NewProgram(&m, tea.WithAltScreen()).Run()
94-
mm := tm.(model)
95-
fmt.Println("\n You selected: " + m.filepicker.Styles.Selected.Render(mm.selectedFile) + "\n")
96+
tm := tea.NewProgram(&m)
97+
if err := tm.Run(); err != nil {
98+
fmt.Println("Error running program:", err)
99+
os.Exit(1)
100+
}
101+
fmt.Println("\n You selected: " + m.filepicker.Styles.Selected.Render(tm.Model.selectedFile) + "\n")
96102
}

examples/focus-blur/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func main() {
1414
// assume we start focused...
1515
focused: true,
1616
reporting: true,
17-
}, tea.WithReportFocus())
18-
if _, err := p.Run(); err != nil {
17+
})
18+
if err := p.Run(); err != nil {
1919
log.Fatal(err)
2020
}
2121
}
@@ -25,11 +25,11 @@ type model struct {
2525
reporting bool
2626
}
2727

28-
func (m model) Init() (tea.Model, tea.Cmd) {
29-
return m, nil
28+
func (m model) Init() (model, tea.Cmd) {
29+
return m, tea.EnabledReportFocus
3030
}
3131

32-
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
32+
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
3333
switch msg := msg.(type) {
3434
case tea.FocusMsg:
3535
m.focused = true

0 commit comments

Comments
 (0)