aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2024-01-28 00:52:20 +0100
committerRobin Jarry <robin@jarry.cc>2024-02-11 21:20:23 +0100
commit28ee8e65a45d38f10938571fddea4b137a1de115 (patch)
tree11e393d4eb3da83f91da5b35f1004c971dd255d0 /config
parent2dcc83efda3efd61dffc75324e434ef32d322b8f (diff)
downloadaerc-28ee8e65a45d38f10938571fddea4b137a1de115.tar.gz
binds: parse annotations from keybind config
Parse inline comments in binds.conf as annotations to the corresponding keybinds. Note that a space is required before the comment symbol, so the comment delimiter is " # ". Example: p = :postpone<Enter> # I'll work on it later where "I'll work on it later" is the annotation. When a comment symbol ("#") is needed in the value part of the keybind, it should be escaped ("\#"). Comment symbols can be used without restriction in the annotation itself. Signed-off-by: Koni Marti <koni.marti@gmail.com> Tested-by: Bence Ferdinandy <bence@ferdinandy.com> Reviewed-by: Bence Ferdinandy <bence@ferdinandy.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'config')
-rw-r--r--config/binds.go21
-rw-r--r--config/binds_test.go2
2 files changed, 17 insertions, 6 deletions
diff --git a/config/binds.go b/config/binds.go
index b65aa1ab..9647388d 100644
--- a/config/binds.go
+++ b/config/binds.go
@@ -49,6 +49,8 @@ type KeyStroke struct {
type Binding struct {
Output []KeyStroke
Input []KeyStroke
+
+ Annotation string
}
type KeyBindings struct {
@@ -84,7 +86,7 @@ func defaultBindsConfig() *BindingConfig {
wizard := NewKeyBindings()
wizard.ExKey = KeyStroke{Key: tcell.KeyCtrlE}
wizard.Globals = false
- quit, _ := ParseBinding("<C-q>", ":quit<Enter>")
+ quit, _ := ParseBinding("<C-q>", ":quit<Enter>", "Quit aerc")
wizard.Add(quit)
return &BindingConfig{
Global: NewKeyBindings(),
@@ -105,6 +107,10 @@ func parseBindsFromFile(root string, filename string) error {
log.Debugf("Parsing key bindings configuration from %s", filename)
binds, err := ini.LoadSources(ini.LoadOptions{
KeyValueDelimiters: "=",
+ // IgnoreInlineComment is set to true which tells ini's parser
+ // to treat comments (#) on the same line as part of the value;
+ // hence we need cut the comment off ourselves later
+ IgnoreInlineComment: true,
}, filename)
if err != nil {
return err
@@ -167,6 +173,9 @@ func parseBinds(root string, filename string) error {
func LoadBindingSection(sec *ini.Section) (*KeyBindings, error) {
bindings := NewKeyBindings()
for key, value := range sec.KeysHash() {
+ var annotation string
+ value, annotation, _ = strings.Cut(value, " # ")
+ value = strings.TrimSpace(value)
switch key {
case "$ex":
strokes, err := ParseKeyStrokes(value)
@@ -195,7 +204,8 @@ func LoadBindingSection(sec *ini.Section) (*KeyBindings, error) {
}
bindings.CompleteKey = strokes[0]
default:
- binding, err := ParseBinding(key, value)
+ annotation = strings.TrimSpace(annotation)
+ binding, err := ParseBinding(key, value, annotation)
if err != nil {
return nil, err
}
@@ -766,7 +776,7 @@ func ParseKeyStrokes(keystrokes string) ([]KeyStroke, error) {
return strokes, nil
}
-func ParseBinding(input, output string) (*Binding, error) {
+func ParseBinding(input, output, annotation string) (*Binding, error) {
in, err := ParseKeyStrokes(input)
if err != nil {
return nil, err
@@ -776,7 +786,8 @@ func ParseBinding(input, output string) (*Binding, error) {
return nil, err
}
return &Binding{
- Input: in,
- Output: out,
+ Input: in,
+ Output: out,
+ Annotation: annotation,
}, nil
}
diff --git a/config/binds_test.go b/config/binds_test.go
index dab3b9f1..b21c1887 100644
--- a/config/binds_test.go
+++ b/config/binds_test.go
@@ -13,7 +13,7 @@ func TestGetBinding(t *testing.T) {
bindings := NewKeyBindings()
add := func(binding, cmd string) {
- b, _ := ParseBinding(binding, cmd)
+ b, _ := ParseBinding(binding, cmd, "")
bindings.Add(b)
}