Skip to content

Instantly share code, notes, and snippets.

@Broderick-Westrope
Created September 19, 2024 00:53
Show Gist options
  • Save Broderick-Westrope/220e091d84803026fc934f264e8c1f4f to your computer and use it in GitHub Desktop.
Save Broderick-Westrope/220e091d84803026fc934f264e8c1f4f to your computer and use it in GitHub Desktop.
Example of KeyRelease Not Working - Go BubbleTea v2alpha1
module test
go 1.22.4
require github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.1
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.13.0 // indirect
github.com/charmbracelet/x/ansi v0.3.2 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/charmbracelet/x/windows v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
)
package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea/v2"
"os"
)
func main() {
opts := []tea.ProgramOption{
tea.WithKeyboardEnhancements(tea.WithKeyReleases),
}
if _, err := tea.NewProgram(Model{}, opts...).Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}
var _ tea.Model = Model{}
type Model struct {
pressCount int
releaseCount int
}
func (m Model) Init() (tea.Model, tea.Cmd) {
return m, nil
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q":
return m, tea.Quit
case "space":
switch msg.(type) {
case tea.KeyPressMsg:
m.pressCount++
case tea.KeyReleaseMsg:
m.releaseCount++
}
}
}
return m, nil
}
func (m Model) View() string {
var output string
output += fmt.Sprintf("Presses: %d\nReleases: %d\n", m.pressCount, m.releaseCount)
output += "\nPress q to quit.\n"
return output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment