aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-16 11:42:15 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-28 19:24:46 +0200
commit9a4518476d8c8f28340c6b44cd808e6d58fbeb98 (patch)
tree7187d819b77df909980b91cf0eebf6b033316dfe
parent803a10d4fabbd30533721db2f5efaefbccd88cdd (diff)
downloadaerc-9a4518476d8c8f28340c6b44cd808e6d58fbeb98.tar.gz
aerc-9a4518476d8c8f28340c6b44cd808e6d58fbeb98.zip
commands: remove command set execute api
Do not expose the execution of a command via its command set. Instead, require a single command object to be resolved in order to execute it. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Koni Marti <koni.marti@gmail.com> Tested-by: Moritz Poldrack <moritz@poldrack.dev> Tested-by: Inwit <inwit@sindominio.net>
-rw-r--r--commands/commands.go14
-rw-r--r--main.go60
2 files changed, 30 insertions, 44 deletions
diff --git a/commands/commands.go b/commands/commands.go
index 1c303484..57f2e6dc 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -111,17 +111,13 @@ func templateData(
return data.Data()
}
-func (cmds *Commands) ExecuteCommand(cmdline string) error {
+func ExecuteCommand(cmd Command, cmdline string) error {
args := opt.LexArgs(cmdline)
- name, err := args.ArgSafe(0)
- if err != nil {
- return errors.New("Expected a command after template evaluation.")
- }
- if cmd, ok := cmds.dict()[name]; ok {
- log.Tracef("executing command %s", args.String())
- return cmd.Execute(args.Args())
+ if args.Count() == 0 {
+ return errors.New("No arguments")
}
- return NoSuchCommand(name)
+ log.Tracef("executing command %s", args.String())
+ return cmd.Execute(args.Args())
}
// expand template expressions
diff --git a/main.go b/main.go
index 8ac70cf9..2734164a 100644
--- a/main.go
+++ b/main.go
@@ -65,40 +65,37 @@ func getCommands(selected ui.Drawable) []*commands.Commands {
// Expand non-ambiguous command abbreviations.
//
-// :q --> :quit
-// :ar --> :archive
-// :im --> :import-mbox
-func expandAbbreviations(name string, sets []*commands.Commands) string {
- name = strings.TrimLeft(name, ":")
- candidate := ""
+// q --> quit
+// ar --> archive
+// im --> import-mbox
+func expandAbbreviations(name string, sets []*commands.Commands) (string, commands.Command) {
+ var cmd commands.Command
+ candidate := name
+
for _, set := range sets {
- if set.ByName(name) != nil {
+ cmd = set.ByName(name)
+ if cmd != nil {
// Direct match, return it directly.
- return name
+ return name, cmd
}
// Check for partial matches.
for _, n := range set.Names() {
if !strings.HasPrefix(n, name) {
continue
}
- if candidate != "" {
+ if cmd != nil {
// We have more than one command partially
// matching the input. We can't expand such an
// abbreviation, so return the command as is so
// it can raise an error later.
- return name
+ return name, nil
}
// We have a partial match.
candidate = n
+ cmd = set.ByName(n)
}
}
- // As we are here, we could have a command name matching our partial
- // name in `cmd`. In that case we replace the name in `cmd` with the
- // full name, otherwise we simply return `cmd` as is.
- if candidate != "" {
- name = candidate
- }
- return name
+ return candidate, cmd
}
func execCommand(
@@ -109,30 +106,23 @@ func execCommand(
if err != nil {
return err
}
+ cmdline = strings.TrimLeft(cmdline, ":")
name, rest, didCut := strings.Cut(cmdline, " ")
cmds := getCommands(app.SelectedTabContent())
- cmdline = expandAbbreviations(name, cmds)
+ name, cmd := expandAbbreviations(name, cmds)
+ if cmd == nil {
+ return commands.NoSuchCommand(name)
+ }
+ cmdline = name
if didCut {
cmdline += " " + rest
}
- for i, set := range cmds {
- err := set.ExecuteCommand(cmdline)
- if err != nil {
- if errors.As(err, new(commands.NoSuchCommand)) {
- if i == len(cmds)-1 {
- return err
- }
- continue
- }
- if errors.As(err, new(commands.ErrorExit)) {
- ui.Exit()
- return nil
- }
- return err
- }
- break
+ err = commands.ExecuteCommand(cmd, cmdline)
+ if errors.As(err, new(commands.ErrorExit)) {
+ ui.Exit()
+ return nil
}
- return nil
+ return err
}
func getCompletions(cmd string) ([]string, string) {