summaryrefslogtreecommitdiff
path: root/internal/switcher/keys.go
blob: 73ba7104cd1a37a5f45384f7e555652ca8e13391 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package switcher

import (
	"strings"

	"github.com/charmbracelet/bubbles/key"
)

const (
	cpHex  = "x"
	cpRGB  = "r"
	cpHSL  = "s"
	cpCMYK = "c"
)

type keybinds struct {
	next, prev, copy, help, insert, esc, confirm, quit key.Binding
}

func newKeybinds() keybinds {
	return keybinds{
		next: key.NewBinding(
			key.WithKeys("tab"),
			key.WithHelp("tab", "next picker"),
		),
		prev: key.NewBinding(
			key.WithKeys("shift+tab"),
			key.WithHelp("shift+tab", "prev picker"),
		),
		copy: key.NewBinding(
			key.WithKeys(cpHex, cpRGB, cpHSL, cpCMYK),
			key.WithHelp(
				strings.Join([]string{cpHex, cpRGB, cpHSL, cpCMYK}, "/"),
				"copy color",
			),
		),
		help: key.NewBinding(
			key.WithKeys("?"),
			key.WithHelp("?", "help"),
		),
		insert: key.NewBinding(
			key.WithKeys("i", ":"),
			key.WithHelp("i", "manual input"),
		),
		esc: key.NewBinding(
			key.WithKeys("esc"),
			key.WithHelp("esc", "exit manual input"),
			key.WithDisabled(),
		),
		confirm: key.NewBinding(
			key.WithKeys("enter"),
			key.WithHelp("enter", "confirm manual input"),
			key.WithDisabled(),
		),
		quit: key.NewBinding(
			key.WithKeys("q", "ctrl+c"),
			key.WithHelp("q", "quit"),
		),
	}
}

func Keys() []key.Binding {
	k := newKeybinds()
	return []key.Binding{k.next, k.prev, k.copy, k.insert, k.esc, k.confirm, k.help, k.quit}
}

func shortKeys() [][]key.Binding {
	keys := make([][]key.Binding, 2)
	rows := 2
	cRow := 0
	for i := 0; i < len(Keys()); i++ {
		keys[cRow] = append(keys[cRow], Keys()[i])
		cRow++
		if cRow == rows {
			cRow = 0
		}
	}
	return keys
}

func (m Model) AllKeys() [][]key.Binding {
	keys := make([][]key.Binding, len(m.pickers[m.active].AllKeys())+1)
	keys[0] = Keys()
	copy(keys[1:], m.pickers[m.active].AllKeys())
	return keys
	// return append(m.pickers[m.active].AllKeys(), Keys())
}