1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2025-07-06 19:30:22 +02:00
aerc/lib/pama/switch.go
Koni Marti f07038bd98 patch: add auto-switch option
Add an auto-switch option that changes the project of the patch manager
based on the subject line of a message if it contains a '[PATCH
<project>]' segment.

A subject line with '[PATCH aerc v2]' would switch to the 'aerc' project
if that project is available in the patch manager.

The auto switching can be activated per account by adding
'pama-auto-switch = true' to your account config.

Implements: https://todo.sr.ht/~rjarry/aerc/226
Changelog-added: Auto-switch projects based on the message subject
 for the :patch command.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2024-08-24 15:49:17 +02:00

65 lines
1.2 KiB
Go

package pama
import (
"fmt"
"regexp"
"time"
"git.sr.ht/~rjarry/aerc/lib/log"
)
func (m PatchManager) SwitchProject(name string) error {
c, err := m.CurrentProject()
if err == nil {
if c.Name == name {
return nil
}
}
names, err := m.store().Names()
if err != nil {
return storeErr(err)
}
found := false
for _, n := range names {
if n == name {
found = true
break
}
}
if !found {
return fmt.Errorf("Project '%s' not found", name)
}
return storeErr(m.store().SetCurrent(name))
}
var switchDebouncer *time.Timer
func DebouncedSwitchProject(name string) {
if switchDebouncer != nil {
if switchDebouncer.Stop() {
log.Debugf("pama: switch debounced")
}
}
if name == "" {
return
}
switchDebouncer = time.AfterFunc(500*time.Millisecond, func() {
if err := New().SwitchProject(name); err != nil {
log.Debugf("could not switch to project %s: %v",
name, err)
} else {
log.Debugf("project switch to project %s", name)
}
})
}
var fromSubject = regexp.MustCompile(
`\[\s*(RFC|DRAFT|[Dd]raft)*\s*(PATCH|[Pp]atch)\s+([^\s\]]+)\s*[vV]*[0-9/]*\s*\] `)
func FromSubject(s string) string {
matches := fromSubject.FindStringSubmatch(s)
if len(matches) >= 3 {
return matches[3]
}
return ""
}