Files
settuings/internal/keys/keys.go
T
Hadi 59b31e5ca3 init
Signed-off-by: Hadi <[email protected]>
2026-08-13 00:58:37 +02:00

67 lines
1.4 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
}
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),
}
}
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
}