diff options
author | Koni Marti <koni.marti@gmail.com> | 2023-05-10 23:56:23 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-05-16 13:39:17 +0200 |
commit | 5c9d22bb84eb8a6ddd4477bae3c081964d6e7b51 (patch) | |
tree | 263083c1f55d60d55d2b005d6504c64393432186 /commands/completion_helpers.go | |
parent | cd68adc4630ed834eeac33f09ef58891c18c2dee (diff) | |
download | aerc-5c9d22bb84eb8a6ddd4477bae3c081964d6e7b51.tar.gz |
commands: add OptionsProvider and OptionCompleter
Improve command completion by supporting option flags and option
arguments completion. Option completion is activated when the command
implements the OptionsProvider interface. Implementing the
OptionCompleter allows the completion of individual option arguments.
The completion interfaces harmonizes the completion behavior in aerc,
makes the completion code clearer and simplifies the completion
functionality.
With this patch, the Complete method on commands will only have to deal
with the actual completion, i.e. paths, folders, etc and not worry about
options. To remove all options and its mandatory arguments from args,
use commands.Operands().
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands/completion_helpers.go')
-rw-r--r-- | commands/completion_helpers.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/commands/completion_helpers.go b/commands/completion_helpers.go new file mode 100644 index 00000000..96a423ee --- /dev/null +++ b/commands/completion_helpers.go @@ -0,0 +1,78 @@ +package commands + +import ( + "fmt" + "net/mail" + "strings" + + "git.sr.ht/~rjarry/aerc/completer" + "git.sr.ht/~rjarry/aerc/config" + "git.sr.ht/~rjarry/aerc/log" + "git.sr.ht/~rjarry/aerc/widgets" +) + +// GetAddress uses the address-book-cmd for address completion +func GetAddress(aerc *widgets.Aerc, search string) []string { + var options []string + + cmd := aerc.SelectedAccount().AccountConfig().AddressBookCmd + if cmd == "" { + cmd = config.Compose.AddressBookCmd + if cmd == "" { + return nil + } + } + + cmpl := completer.New(cmd, func(err error) { + aerc.PushError( + fmt.Sprintf("could not complete header: %v", err)) + log.Warnf("could not complete header: %v", err) + }) + + if len(search) > config.Ui.CompletionMinChars && cmpl != nil { + addrList, _ := cmpl.ForHeader("to")(search) + for _, full := range addrList { + addr, err := mail.ParseAddress(full) + if err != nil { + continue + } + options = append(options, addr.Address) + } + } + + return options +} + +// GetFlagList returns a list of available flags for completion +func GetFlagList() []string { + return []string{"Seen", "Answered", "Flagged"} +} + +// GetDateList returns a list of date terms for completion +func GetDateList() []string { + return []string{ + "today", "yesterday", "this_week", "this_month", + "this_year", "last_week", "last_month", "last_year", + "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", + "Saturday", "Sunday", + } +} + +// Operands returns a slice without any option flags or mandatory option +// arguments +func Operands(args []string, spec string) []string { + var result []string + for i := 0; i < len(args); i++ { + if s := args[i]; s == "--" { + return args[i+1:] + } else if strings.HasPrefix(s, "-") && len(spec) > 0 { + r := string(s[len(s)-1]) + ":" + if strings.Contains(spec, r) { + i++ + } + continue + } + result = append(result, args[i]) + } + return result +} |