From b336a5c9e19adba31bec1da51e093a11e09a8ead Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 19 Sep 2023 22:51:49 +0200 Subject: commands: pass raw command line down to template evaluation Some commands need to invoke others and/or run shell commands. For this, we need the raw command line as entered by the user. Pass it down the call chain just before it is split to invoke the command Execute method. Remove unit tests for the template expand() test which does have any added value now that it is performed on a single string without any quote juggling. Update all code to handle a single string instead of a list of arguments. Introduce a new dependency on git.sr.ht/~rjarry/go-opt to deal with shell splitting. This is in preparation for using opt.ArgsToStruct to parse arguments for all aerc commands. There should be no functional change after this patch. Signed-off-by: Robin Jarry Reviewed-by: Koni Marti Tested-by: Moritz Poldrack Tested-by: Inwit --- main.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 95c9615b..b72bf713 100644 --- a/main.go +++ b/main.go @@ -68,16 +68,13 @@ func getCommands(selected ui.Drawable) []*commands.Commands { // :q --> :quit // :ar --> :archive // :im --> :import-mbox -func expandAbbreviations(cmd []string, sets []*commands.Commands) []string { - if len(cmd) == 0 { - return cmd - } - name := strings.TrimLeft(cmd[0], ":") +func expandAbbreviations(name string, sets []*commands.Commands) string { + name = strings.TrimLeft(name, ":") candidate := "" for _, set := range sets { if set.ByName(name) != nil { // Direct match, return it directly. - return cmd + return name } // Check for partial matches. for _, n := range set.Names() { @@ -89,7 +86,7 @@ func expandAbbreviations(cmd []string, sets []*commands.Commands) []string { // matching the input. We can't expand such an // abbreviation, so return the command as is so // it can raise an error later. - return cmd + return name } // We have a partial match. candidate = n @@ -99,19 +96,23 @@ func expandAbbreviations(cmd []string, sets []*commands.Commands) []string { // 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 != "" { - cmd[0] = candidate + name = candidate } - return cmd + return name } func execCommand( - cmd []string, + cmdline string, acct *config.AccountConfig, msg *models.MessageInfo, ) error { + name, rest, didCut := strings.Cut(cmdline, " ") cmds := getCommands(app.SelectedTabContent()) - cmd = expandAbbreviations(cmd, cmds) + cmdline = expandAbbreviations(name, cmds) + if didCut { + cmdline += " " + rest + } for i, set := range cmds { - err := set.ExecuteCommand(cmd, acct, msg) + err := set.ExecuteCommand(cmdline, acct, msg) if err != nil { if errors.As(err, new(commands.NoSuchCommand)) { if i == len(cmds)-1 { -- cgit