▍ humdrum codex / glint v1.0.2
license AGPL-3.0
1.0 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package keyprobe is a tiny `glint keys` diagnostic that shows exactly what
// key events the terminal delivers — useful for discovering what Cmd+Arrow,
// Alt+Arrow, and friends send so they can be bound.
package keyprobe

import (
	"fmt"
	"strings"

	tea "github.com/charmbracelet/bubbletea"
)

type model struct{ lines []string }

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

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	if k, ok := msg.(tea.KeyMsg); ok {
		if k.Type == tea.KeyCtrlC {
			return m, tea.Quit
		}
		m.lines = append(m.lines, fmt.Sprintf("String=%-12q Type=%-3d Alt=%-5v Runes=%q", k.String(), int(k.Type), k.Alt, string(k.Runes)))
		if len(m.lines) > 20 {
			m.lines = m.lines[len(m.lines)-20:]
		}
	}
	return m, nil
}

func (m model) View() string {
	return "glint key probe — press keys (Cmd+Arrows, Alt+Arrows, Cmd+Delete, …). Ctrl+C to quit.\n\n" +
		strings.Join(m.lines, "\n") + "\n"
}

// Run launches the probe.
func Run() error {
	_, err := tea.NewProgram(model{}, tea.WithAltScreen()).Run()
	return err
}