mirror of
https://github.com/anotherhadi/settuings.git
synced 2026-10-05 16:18:24 +02:00
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package keys
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"charm.land/bubbles/v2/key"
|
|
"github.com/anotherhadi/settuings/internal/config"
|
|
)
|
|
|
|
type KeyMap struct {
|
|
Global GlobalKeyMap
|
|
Docs DocsKeyMap
|
|
Network NetworkKeyMap
|
|
Bluetooth BluetoothKeyMap
|
|
Audio AudioKeyMap
|
|
Power PowerKeyMap
|
|
Input InputKeyMap
|
|
}
|
|
|
|
var Keys *KeyMap
|
|
|
|
func Init(cfg *config.Config) {
|
|
kb := cfg.Keybindings
|
|
Keys = &KeyMap{
|
|
Global: newGlobalKeyMap(kb.Global),
|
|
Docs: newDocsKeyMap(kb.Docs),
|
|
Network: newNetworkKeyMap(kb.Network),
|
|
Bluetooth: newBluetoothKeyMap(kb.Bluetooth),
|
|
Audio: newAudioKeyMap(kb.Audio),
|
|
Power: newPowerKeyMap(kb.Power),
|
|
Input: newInputKeyMap(kb.Input),
|
|
}
|
|
}
|
|
|
|
func parseKeys(s string) []string {
|
|
parts := strings.Split(s, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
if k := strings.TrimSpace(p); k != "" {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func binding(s, help string) key.Binding {
|
|
keys := parseKeys(s)
|
|
display := strings.Join(keys, "/")
|
|
return key.NewBinding(key.WithKeys(keys...), key.WithHelp(display, help))
|
|
}
|
|
|
|
// ChunkByWidth splits bindings into columns sized to fit the terminal width.
|
|
func ChunkByWidth(bindings []key.Binding, termWidth int) [][]key.Binding {
|
|
cols := termWidth / 26
|
|
if cols < 2 {
|
|
cols = 2
|
|
} else if cols > 7 {
|
|
cols = 7
|
|
}
|
|
perCol := (len(bindings) + cols - 1) / cols
|
|
var out [][]key.Binding
|
|
for i := 0; i < len(bindings); i += perCol {
|
|
end := i + perCol
|
|
if end > len(bindings) {
|
|
end = len(bindings)
|
|
}
|
|
out = append(out, bindings[i:end])
|
|
}
|
|
return out
|
|
}
|