aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-12-30 13:56:43 +0100
committerRobin Jarry <robin@jarry.cc>2024-01-17 11:53:02 +0100
commitc80acdcfed20699725a59e314b02edd0fc1995e0 (patch)
treed35b7b441658a18ef94ae6c4c6630b5663f260e2
parent3622f7a99e91bfe38eab9e91dc982aafa00fe211 (diff)
downloadaerc-c80acdcfed20699725a59e314b02edd0fc1995e0.tar.gz
z: fix arguments handling
Having both a string argument with a default value and an optional list of strings breaks completion with go-opt. Replace with a single argument and update all code accordingly. Fixes: abe228b14d97 ("commands: use completion from go-opt") Signed-off-by: Robin Jarry <robin@jarry.cc> Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
-rw-r--r--commands/z.go28
1 files changed, 16 insertions, 12 deletions
diff --git a/commands/z.go b/commands/z.go
index 2ea65e0c..7b93b65d 100644
--- a/commands/z.go
+++ b/commands/z.go
@@ -5,11 +5,12 @@ import (
"os"
"os/exec"
"strings"
+
+ "git.sr.ht/~rjarry/aerc/lib/xdg"
)
type Zoxide struct {
- Target string `opt:"folder" default:"~" complete:"CompleteFolder"`
- Args []string `opt:"..." required:"false" metavar:"<query>..."`
+ Args []string `opt:"..." required:"false" metavar:"<query>..." complete:"CompleteFolder"`
}
func ZoxideAdd(arg string) error {
@@ -20,7 +21,7 @@ func ZoxideAdd(arg string) error {
}
func ZoxideQuery(args []string) (string, error) {
- zargs := append([]string{"query"}, args[1:]...)
+ zargs := append([]string{"query"}, args...)
cmd := exec.Command("zoxide", zargs...)
res, err := cmd.Output()
return strings.TrimSuffix(string(res), "\n"), err
@@ -44,20 +45,23 @@ func (*Zoxide) CompleteFolder(arg string) []string {
// Execute calls zoxide add and query and delegates actually changing the
// directory to ChangeDirectory
func (z Zoxide) Execute(args []string) error {
- switch z.Target {
- case "-", "~":
+ if len(z.Args) == 0 {
+ z.Args = []string{"~"}
+ }
+ if len(z.Args) == 1 && (z.Args[0] == "~" || z.Args[0] == "-") {
if previousDir != "" {
err := ZoxideAdd(previousDir)
if err != nil {
return err
}
}
- return ChangeDirectory{}.Execute(args)
- default:
- _, err := os.Stat(z.Target)
- if err != nil {
+ return ChangeDirectory{Target: z.Args[0]}.Execute(args)
+ } else {
+ target := xdg.ExpandHome(z.Args[0])
+ _, err := os.Stat(target)
+ if err != nil || len(z.Args) > 1 {
// not a file, assume zoxide query
- res, err := ZoxideQuery(args)
+ res, err := ZoxideQuery(z.Args)
if err != nil {
return errors.New("zoxide: no match found")
} else {
@@ -70,11 +74,11 @@ func (z Zoxide) Execute(args []string) error {
}
} else {
- err := ZoxideAdd(z.Target)
+ err := ZoxideAdd(target)
if err != nil {
return err
}
- return ChangeDirectory{}.Execute(args)
+ return ChangeDirectory{Target: target}.Execute(args)
}
}