aboutsummaryrefslogtreecommitdiffstats
path: root/config/binds.go
diff options
context:
space:
mode:
authorVitaly Ovchinnikov <v@ovch.ru>2023-10-22 16:17:38 +0000
committerRobin Jarry <robin@jarry.cc>2023-11-02 11:58:44 +0100
commit1977d1f1ac4491fe0625c554a4643ecf60b36466 (patch)
tree5b95314c12bef7bb22400637d35579cf78241817 /config/binds.go
parenteadaec1b3c0cc3439f54595d0748e564d4129728 (diff)
downloadaerc-1977d1f1ac4491fe0625c554a4643ecf60b36466.tar.gz
binds: remove duplicated and empty bindings
Add keybindings filtering, so if the context binding is defined, the parent one is removed and is not shown in the list at all. Also add keybinding removing, so if the context defines an empty binding, both this and parent one are removed from the list. This way you can disable and hide specific bindings in contexts. Changelog-added: Disable parent context bindings by declaring them empty. Signed-off-by: Vitaly Ovchinnikov <v@ovch.ru> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'config/binds.go')
-rw-r--r--config/binds.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/config/binds.go b/config/binds.go
index 1b2799f2..4552030d 100644
--- a/config/binds.go
+++ b/config/binds.go
@@ -272,6 +272,53 @@ func NewKeyBindings() *KeyBindings {
}
}
+func areBindingsInputsEqual(a, b *Binding) bool {
+ if len(a.Input) != len(b.Input) {
+ return false
+ }
+
+ for idx := range a.Input {
+ if a.Input[idx] != b.Input[idx] {
+ return false
+ }
+ }
+
+ return true
+}
+
+// this scans the bindings slice for copies and leaves just the first ones
+// it also removes empty bindings, the ones that do nothing, so you can
+// override and erase parent bindings with the context ones
+func filterAndCleanBindings(bindings []*Binding) []*Binding {
+ // 1. remove a binding if we already have one with the same input
+ res1 := []*Binding{}
+ for _, b := range bindings {
+ // do we already have one here?
+ found := false
+ for _, r := range res1 {
+ if areBindingsInputsEqual(b, r) {
+ found = true
+ break
+ }
+ }
+
+ // add it if we don't
+ if !found {
+ res1 = append(res1, b)
+ }
+ }
+
+ // 2. clean up the empty bindings
+ res2 := []*Binding{}
+ for _, b := range res1 {
+ if len(b.Output) > 0 {
+ res2 = append(res2, b)
+ }
+ }
+
+ return res2
+}
+
func MergeBindings(bindings ...*KeyBindings) *KeyBindings {
merged := NewKeyBindings()
for _, b := range bindings {
@@ -280,6 +327,7 @@ func MergeBindings(bindings ...*KeyBindings) *KeyBindings {
break
}
}
+ merged.Bindings = filterAndCleanBindings(merged.Bindings)
merged.ExKey = bindings[0].ExKey
merged.Globals = bindings[0].Globals
return merged