Skip to content

Commit

Permalink
feat(examples): add print-key example
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Aug 15, 2024
1 parent 4d2072b commit e16559d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/print-key/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"log"

tea "github.com/charmbracelet/bubbletea"
)

type model struct{}

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

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
}
return m, tea.Printf("You pressed: %s\n", msg.String())
}
return m, nil
}

func (m model) View() string {
return "Press any key to see it printed to the terminal. Press 'ctrl+c' to quit."
}

func main() {
p := tea.NewProgram(model{}, tea.WithEnhancedKeyboard())
if _, err := p.Run(); err != nil {
log.Printf("Error running program: %v", err)
}
}

0 comments on commit e16559d

Please sign in to comment.