Skip to content

Commit

Permalink
feat(examples): add cursor style example
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 16, 2024
1 parent ad61ed6 commit d95e275
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions examples/cursor-style/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"
"os"

tea "github.com/charmbracelet/bubbletea/v2"
)

type model struct {
style tea.CursorStyle
steady bool
}

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

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "ctrl+q", "q":
return m, tea.Quit
case "left":
if m.style == tea.CursorBlock && !m.steady {
break
}
if !m.steady {
m.style--
}
m.steady = !m.steady
cmd = tea.SetCursorStyle(m.style, m.steady)
case "right":
if m.style == tea.CursorBar && m.steady {
break
}
if m.steady {
m.style++
}
m.steady = !m.steady
cmd = tea.SetCursorStyle(m.style, m.steady)
}
}
return m, cmd
}

func (m model) View() string {
return "Press left/right to change the cursor style, q or ctrl+c to quit." +
"\n\n" +
" <- This is a cursor"
}

func main() {
p := tea.NewProgram(model{})
if _, err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
os.Exit(1)
}
}

0 comments on commit d95e275

Please sign in to comment.