mirror of
https://github.com/anotherhadi/settuings.git
synced 2026-10-05 16:18:24 +02:00
@@ -0,0 +1,26 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"charm.land/bubbles/v2/key"
|
||||
"github.com/anotherhadi/settuings/internal/config"
|
||||
)
|
||||
|
||||
type AudioKeyMap struct {
|
||||
Select key.Binding
|
||||
Refresh key.Binding
|
||||
Mute key.Binding
|
||||
Test key.Binding
|
||||
}
|
||||
|
||||
func newAudioKeyMap(cfg config.AudioKeys) AudioKeyMap {
|
||||
return AudioKeyMap{
|
||||
Select: binding(cfg.Select, "set as default"),
|
||||
Refresh: binding(cfg.Refresh, "refresh"),
|
||||
Mute: binding(cfg.Mute, "mute/unmute"),
|
||||
Test: binding(cfg.Test, "test"),
|
||||
}
|
||||
}
|
||||
|
||||
func (a AudioKeyMap) Bindings() []key.Binding {
|
||||
return []key.Binding{a.Select, a.Refresh, a.Mute, a.Test}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"charm.land/bubbles/v2/key"
|
||||
"github.com/anotherhadi/settuings/internal/config"
|
||||
)
|
||||
|
||||
type BluetoothKeyMap struct {
|
||||
Select key.Binding
|
||||
Refresh key.Binding
|
||||
Forget key.Binding
|
||||
ToggleTrust key.Binding
|
||||
Info key.Binding
|
||||
TogglePower key.Binding
|
||||
}
|
||||
|
||||
func newBluetoothKeyMap(cfg config.BluetoothKeys) BluetoothKeyMap {
|
||||
return BluetoothKeyMap{
|
||||
Select: binding(cfg.Select, "connect/pair"),
|
||||
Refresh: binding(cfg.Refresh, "scan"),
|
||||
Forget: binding(cfg.Forget, "forget"),
|
||||
ToggleTrust: binding(cfg.ToggleTrust, "toggle trust"),
|
||||
Info: binding(cfg.Info, "device info"),
|
||||
TogglePower: binding(cfg.TogglePower, "toggle bluetooth"),
|
||||
}
|
||||
}
|
||||
|
||||
func (b BluetoothKeyMap) Bindings() []key.Binding {
|
||||
return []key.Binding{
|
||||
b.Select, b.Refresh, b.Info, b.ToggleTrust, b.Forget, b.TogglePower,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"charm.land/bubbles/v2/key"
|
||||
"github.com/anotherhadi/settuings/internal/config"
|
||||
)
|
||||
|
||||
type DocsKeyMap struct {
|
||||
Search key.Binding
|
||||
SearchReset key.Binding
|
||||
SearchNext key.Binding
|
||||
SearchPrev key.Binding
|
||||
}
|
||||
|
||||
func newDocsKeyMap(cfg config.DocsKeys) DocsKeyMap {
|
||||
return DocsKeyMap{
|
||||
Search: binding(cfg.Search, "search"),
|
||||
SearchReset: binding(cfg.SearchReset, "reset search"),
|
||||
SearchNext: binding(cfg.SearchNext, "next match"),
|
||||
SearchPrev: binding(cfg.SearchPrev, "prev match"),
|
||||
}
|
||||
}
|
||||
|
||||
func (d DocsKeyMap) Bindings() []key.Binding {
|
||||
return []key.Binding{d.Search, d.SearchReset, d.SearchNext, d.SearchPrev}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"charm.land/bubbles/v2/key"
|
||||
"github.com/anotherhadi/settuings/internal/config"
|
||||
)
|
||||
|
||||
type GlobalKeyMap struct {
|
||||
Quit key.Binding
|
||||
OpenLogs key.Binding
|
||||
ToggleSidebar key.Binding
|
||||
Help key.Binding
|
||||
Up key.Binding
|
||||
Down key.Binding
|
||||
Left key.Binding
|
||||
Right key.Binding
|
||||
CycleFocus key.Binding
|
||||
Escape key.Binding
|
||||
ScrollUp key.Binding
|
||||
ScrollDown key.Binding
|
||||
GotoTop key.Binding
|
||||
GotoBottom key.Binding
|
||||
PrevPage key.Binding
|
||||
NextPage key.Binding
|
||||
}
|
||||
|
||||
func newGlobalKeyMap(cfg config.GlobalKeys) GlobalKeyMap {
|
||||
return GlobalKeyMap{
|
||||
Quit: binding(cfg.Quit, "quit"),
|
||||
OpenLogs: binding(cfg.OpenLogs, "open logs"),
|
||||
ToggleSidebar: binding(cfg.ToggleSidebar, "toggle sidebar"),
|
||||
Help: binding(cfg.Help, "help"),
|
||||
Up: binding(cfg.Up, "up"),
|
||||
Down: binding(cfg.Down, "down"),
|
||||
Left: binding(cfg.Left, "scroll left"),
|
||||
Right: binding(cfg.Right, "scroll right"),
|
||||
CycleFocus: binding(cfg.CycleFocus, "cycle focus"),
|
||||
Escape: binding(cfg.Escape, "cancel"),
|
||||
ScrollUp: binding(cfg.ScrollUp, "scroll up"),
|
||||
ScrollDown: binding(cfg.ScrollDown, "scroll down"),
|
||||
GotoTop: binding(cfg.GotoTop, "go to top"),
|
||||
GotoBottom: binding(cfg.GotoBottom, "go to bottom"),
|
||||
PrevPage: binding(cfg.PrevPage, "prev page"),
|
||||
NextPage: binding(cfg.NextPage, "next page"),
|
||||
}
|
||||
}
|
||||
|
||||
func (g GlobalKeyMap) Bindings() []key.Binding {
|
||||
return []key.Binding{
|
||||
g.Up, g.Down, g.Left, g.Right, g.CycleFocus,
|
||||
g.Quit, g.Escape, g.Help,
|
||||
g.OpenLogs, g.ToggleSidebar,
|
||||
g.ScrollUp, g.ScrollDown,
|
||||
g.GotoTop, g.GotoBottom, g.PrevPage, g.NextPage,
|
||||
}
|
||||
}
|
||||
|
||||
// CommonBindings returns keys available on every page.
|
||||
func (g GlobalKeyMap) CommonBindings() []key.Binding {
|
||||
return []key.Binding{g.Quit, g.Help, g.OpenLogs, g.ToggleSidebar}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"charm.land/bubbles/v2/key"
|
||||
"github.com/anotherhadi/settuings/internal/config"
|
||||
)
|
||||
|
||||
type NetworkKeyMap struct {
|
||||
Select key.Binding
|
||||
Refresh key.Binding
|
||||
Forget key.Binding
|
||||
ToggleAuto key.Binding
|
||||
RevealSecret key.Binding
|
||||
Known key.Binding
|
||||
Info key.Binding
|
||||
Disconnect key.Binding
|
||||
ToggleRadio key.Binding
|
||||
}
|
||||
|
||||
func newNetworkKeyMap(cfg config.NetworkKeys) NetworkKeyMap {
|
||||
return NetworkKeyMap{
|
||||
Select: binding(cfg.Select, "select"),
|
||||
Refresh: binding(cfg.Refresh, "refresh"),
|
||||
Forget: binding(cfg.Forget, "forget"),
|
||||
ToggleAuto: binding(cfg.ToggleAuto, "toggle autoconnect"),
|
||||
RevealSecret: binding(cfg.RevealSecret, "show/hide password"),
|
||||
Known: binding(cfg.Known, "known networks"),
|
||||
Info: binding(cfg.Info, "network info"),
|
||||
Disconnect: binding(cfg.Disconnect, "disconnect"),
|
||||
ToggleRadio: binding(cfg.ToggleRadio, "toggle wifi"),
|
||||
}
|
||||
}
|
||||
|
||||
func (n NetworkKeyMap) Bindings() []key.Binding {
|
||||
return []key.Binding{
|
||||
n.Select, n.Refresh, n.Known, n.Info, n.Disconnect, n.ToggleRadio,
|
||||
n.ToggleAuto, n.RevealSecret, n.Forget,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user