Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 28, 2025
1 parent 59f9794 commit a69f856
Show file tree
Hide file tree
Showing 50 changed files with 211 additions and 193 deletions.
6 changes: 3 additions & 3 deletions examples/autocomplete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
if err := p.Run(); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -101,11 +101,11 @@ func initialModel() model {
return model{textInput: ti, help: h, keymap: km}
}

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

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.KeyPressMsg:
switch msg.String() {
Expand Down
8 changes: 3 additions & 5 deletions examples/capability/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ type model struct {
width int
}

var _ tea.Model = model{}

// Init implements tea.Model.
func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
m.input = textinput.New()
m.input.Placeholder = "Enter capability name to request"
return m, m.input.Focus()
}

// Update implements tea.Model.
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) Update(msg tea.Msg) (model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
Expand Down Expand Up @@ -59,7 +57,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 {
log.Fatal(err)
}
}
Expand Down
8 changes: 6 additions & 2 deletions examples/cellbuffer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ type model struct {
}

func (m model) Init() (model, tea.Cmd) {
return m, animate()
return m, tea.Batch(
animate(),
tea.EnableMouseCellMotion,
tea.EnterAltScreen,
)
}

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

p := tea.NewProgram(m, tea.WithAltScreen[model](), tea.WithMouseCellMotion[model]())
p := tea.NewProgram(m)
if err := p.Run(); err != nil {
fmt.Println("Uh oh:", err)
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions examples/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const gap = "\n\n"

func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
if err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Oof: %v\n", err)
}
}
Expand Down Expand Up @@ -63,11 +63,11 @@ Type a message and press Enter to send.`)
}
}

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

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.WindowSizeMsg:
m.viewport.SetWidth(msg.Width)
Expand Down
11 changes: 5 additions & 6 deletions examples/colorprofile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ var myFancyColor color.Color

type model struct{}

var _ tea.Model = model{}

// Init implements tea.Model.
func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
return m, tea.Batch(
tea.RequestCapability("RGB"),
tea.RequestCapability("Tc"),
)
}

// Update implements tea.Model.
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.KeyMsg:
return m, tea.Quit
Expand All @@ -47,8 +45,9 @@ func (m model) View() fmt.Stringer {
func main() {
myFancyColor, _ = colorful.Hex("#6b50ff")

p := tea.NewProgram(model{}, tea.WithColorProfile(colorprofile.TrueColor))
if _, err := p.Run(); err != nil {
p := tea.NewProgram(model{})
p.Profile = colorprofile.TrueColor
if err := p.Run(); err != nil {
log.Fatal(err)
}
}
6 changes: 3 additions & 3 deletions examples/composable-views/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func newModel(timeout time.Duration) mainModel {
return m
}

func (m mainModel) Init() (tea.Model, tea.Cmd) {
func (m mainModel) Init() (mainModel, tea.Cmd) {
// start the timer and spinner on program start
timer, cmd := m.timer.Init()
m.timer = timer
return m, tea.Batch(cmd, m.spinner.Tick)
}

func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m mainModel) Update(msg tea.Msg) (mainModel, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
Expand Down Expand Up @@ -160,7 +160,7 @@ func (m *mainModel) resetSpinner() {
func main() {
p := tea.NewProgram(newModel(defaultTime))

if _, err := p.Run(); err != nil {
if err := p.Run(); err != nil {
log.Fatal(err)
}
}
6 changes: 3 additions & 3 deletions examples/credit-card-form/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func main() {
p := tea.NewProgram(initialModel())

if _, err := p.Run(); err != nil {
if err := p.Run(); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -124,11 +124,11 @@ func initialModel() model {
}
}

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) {
var cmds []tea.Cmd = make([]tea.Cmd, len(m.inputs))

switch msg := msg.(type) {
Expand Down
20 changes: 10 additions & 10 deletions examples/cursor-style/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ import (
)

type model struct {
style tea.CursorStyle
shape tea.CursorShape
blink bool
}

func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
m.blink = true
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.KeyPressMsg:
switch msg.String() {
case "ctrl+q", "q":
return m, tea.Quit
case "h", "left":
if m.style == tea.CursorBlock && m.blink {
if m.shape == tea.CursorBlock && m.blink {
break
}
if m.blink {
m.style--
m.shape--
}
m.blink = !m.blink
case "l", "right":
if m.style == tea.CursorBar && !m.blink {
if m.shape == tea.CursorBar && !m.blink {
break
}
if !m.blink {
m.style++
m.shape++
}
m.blink = !m.blink
}
Expand All @@ -49,7 +49,7 @@ func (m model) View() fmt.Stringer {
"\n\n" +
" <- This is the cursor (a " + m.describeCursor() + ")")
f.Cursor = tea.NewCursor(0, 2)
f.Cursor.Style = m.style
f.Cursor.Shape = m.shape
f.Cursor.Blink = m.blink
return f
}
Expand All @@ -63,7 +63,7 @@ func (m model) describeCursor() string {
adj = "steady"
}

switch m.style {
switch m.shape {
case tea.CursorBlock:
noun = "block"
case tea.CursorUnderline:
Expand All @@ -77,7 +77,7 @@ func (m model) describeCursor() string {

func main() {
p := tea.NewProgram(model{})
if _, err := p.Run(); err != nil {
if err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
os.Exit(1)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/debounce/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type model struct {
tag int
}

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.KeyPressMsg:
// Increment the tag on the model...
Expand Down Expand Up @@ -59,7 +59,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("uh oh:", err)
os.Exit(1)
}
Expand Down
13 changes: 8 additions & 5 deletions examples/doom-fire/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ type model struct {
startTime time.Time
}

func (m model) Init() (tea.Model, tea.Cmd) {
return m, tick
func (m model) Init() (model, tea.Cmd) {
return m, tea.Batch(
tick,
tea.EnterAltScreen,
)
}

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.KeyPressMsg:
if msg.String() == "q" || msg.String() == "ctrl+c" {
Expand Down Expand Up @@ -128,8 +131,8 @@ func initialModel() model {
}

func main() {
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
p := tea.NewProgram(initialModel())
if err := p.Run(); err != nil {
fmt.Printf("Error running program: %v", err)
}
}
6 changes: 3 additions & 3 deletions examples/exec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type model struct {
err error
}

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.KeyPressMsg:
switch msg.String() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func (m model) View() fmt.Stringer {

func main() {
m := model{}
if _, err := tea.NewProgram(m).Run(); err != nil {
if err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down
18 changes: 12 additions & 6 deletions examples/file-picker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ func clearErrorAfter(t time.Duration) tea.Cmd {
})
}

func (m model) Init() (tea.Model, tea.Cmd) {
func (m model) Init() (model, tea.Cmd) {
fp, cmd := m.filepicker.Init()
m.filepicker = fp
return m, cmd
return m, tea.Batch(
tea.EnterAltScreen,
cmd,
)
}

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.KeyPressMsg:
switch msg.String() {
Expand Down Expand Up @@ -90,7 +93,10 @@ func main() {
m := model{
filepicker: fp,
}
tm, _ := tea.NewProgram(&m, tea.WithAltScreen()).Run()
mm := tm.(model)
fmt.Println("\n You selected: " + m.filepicker.Styles.Selected.Render(mm.selectedFile) + "\n")
tm := tea.NewProgram(&m)
if err := tm.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
fmt.Println("\n You selected: " + m.filepicker.Styles.Selected.Render(tm.Model.selectedFile) + "\n")
}
10 changes: 5 additions & 5 deletions examples/focus-blur/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func main() {
// assume we start focused...
focused: true,
reporting: true,
}, tea.WithReportFocus())
if _, err := p.Run(); err != nil {
})
if err := p.Run(); err != nil {
log.Fatal(err)
}
}
Expand All @@ -25,11 +25,11 @@ type model struct {
reporting bool
}

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

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.FocusMsg:
m.focused = true
Expand Down
Loading

0 comments on commit a69f856

Please sign in to comment.