-
Notifications
You must be signed in to change notification settings - Fork 4
/
keys.go
111 lines (105 loc) · 2.87 KB
/
keys.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/lipgloss"
)
type KeyMap struct {
Next, Prev, Quit, Help, Run, FullScreen, PageUp, PageDown, Up, Down, Enter,
Delete, Autocomplete, LoadSession, SaveSession, ToggleCheckbox, ToggleJSON, SaveJSON,
Payload key.Binding
}
func (k KeyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Help, k.Quit}
}
func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Next, k.Prev, k.Enter, k.Run, k.Delete, k.ToggleCheckbox},
{k.FullScreen, k.Help, k.Quit, k.LoadSession, k.SaveSession, k.Autocomplete},
{k.ToggleJSON, k.SaveJSON, k.Payload, k.PageDown, k.PageUp},
}
}
var keys = KeyMap{
LoadSession: key.NewBinding(
key.WithKeys("ctrl+l"),
key.WithHelp("Ctrl+l", "load session"),
),
SaveSession: key.NewBinding(
key.WithKeys("ctrl+s"),
key.WithHelp("Ctrl+s", "save session"),
),
Autocomplete: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("Tab", "autocomplete"),
),
Next: key.NewBinding(
key.WithKeys("shift+right"),
key.WithHelp("Shift+right", "next item"),
),
Prev: key.NewBinding(
key.WithKeys("shift+left"),
key.WithHelp("Shift+left", "prev item"),
),
Quit: key.NewBinding(
key.WithKeys("ctrl+q", "ctrl+c"),
key.WithHelp("Ctrl+q", "quit"),
),
Help: key.NewBinding(
key.WithKeys("ctrl+h"),
key.WithHelp("Ctrl+h", "toggle help"),
),
Delete: key.NewBinding(
key.WithKeys("ctrl+d"),
key.WithHelp("Ctrl+d", "delete item"),
),
Run: key.NewBinding(
key.WithKeys("ctrl+g"),
key.WithHelp("Ctrl+g", "run request"),
),
FullScreen: key.NewBinding(
key.WithKeys("ctrl+f"),
key.WithHelp("Ctrl+f", "toggle full screen"),
),
PageUp: key.NewBinding(
key.WithKeys("pgup"),
key.WithHelp("PgUp", "scroll up body of resposne"),
),
PageDown: key.NewBinding(
key.WithKeys("pgdown"),
key.WithHelp("PgDn", "scroll down body of response"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("Enter", "set value"),
),
ToggleCheckbox: key.NewBinding(
key.WithKeys(" "),
key.WithHelp("Space", "toggle checkbox"),
),
ToggleJSON: key.NewBinding(
key.WithKeys("ctrl+j"),
key.WithHelp("Ctrl+j", "toggle JSON payload"),
),
SaveJSON: key.NewBinding(
key.WithKeys("alt+enter"),
key.WithHelp("Alt+enter", "save JSON payload"),
),
Payload: key.NewBinding(
key.WithKeys("ctrl+p"),
key.WithHelp("Ctrl+p", "add payload"),
),
}
// Helper struct for linking together help and key bindings.
type KeyStroke struct {
keys KeyMap
help help.Model
}
// Create new instance of [KeyStroke].
func NewKeyStroke(keyStyle, descStyle lipgloss.Color) KeyStroke {
h := help.New()
h.Styles.FullKey = h.Styles.FullKey.Foreground(keyStyle)
h.Styles.FullDesc = h.Styles.FullDesc.Foreground(descStyle)
h.Styles.ShortKey = h.Styles.FullKey
h.Styles.ShortDesc = h.Styles.FullDesc
return KeyStroke{keys: keys, help: h}
}