-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add print-key example
- Loading branch information
1 parent
4d2072b
commit e16559d
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |