Skip to content
Myr

main.go

This file

Path

examples/cursor-style/main.go

79 lines · 1.4 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 1–79why, one step back
1package main
23import (4 "fmt"5 "os"67 tea "charm.land/bubbletea/v2"8)910type model struct {11 cursor tea.Cursor12 blink bool13}1415func (m model) Init() tea.Cmd {16 return nil17}1819func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {20 switch msg := msg.(type) {21 case tea.KeyPressMsg:22 switch msg.String() {23 case "ctrl+c", "q":24 return m, tea.Quit25 case "h", "left":26 m.cursor.Shape--27 if m.cursor.Shape < tea.CursorBlock {28 m.cursor.Shape = tea.CursorBar29 }30 case "l", "right":31 m.cursor.Shape++32 if m.cursor.Shape > tea.CursorBar {33 m.cursor.Shape = tea.CursorBlock34 }35 }36 }37 m.blink = !m.blink38 return m, nil39}4041func (m model) View() tea.View {42 v := tea.NewView("Press left/right to change the cursor style, q or ctrl+c to quit." +43 "\n\n" +44 " <- This is the cursor (a " + m.describeCursor() + ")")45 c := tea.NewCursor(0, 2)46 c.Shape = m.cursor.Shape47 c.Blink = m.blink48 v.Cursor = c49 return v50}5152func (m model) describeCursor() string {53 var adj, noun string5455 if m.blink {56 adj = "blinking"57 } else {58 adj = "steady"59 }6061 switch m.cursor.Shape {62 case tea.CursorBlock:63 noun = "block"64 case tea.CursorUnderline:65 noun = "underline"66 case tea.CursorBar:67 noun = "bar"68 }6970 return fmt.Sprintf("%s %s", adj, noun)71}7273func main() {74 p := tea.NewProgram(model{blink: true})75 if _, err := p.Run(); err != nil {76 fmt.Fprintf(os.Stderr, "Error: %v", err)77 os.Exit(1)78 }79}