diff options
Diffstat (limited to 'commands/z.go')
-rw-r--r-- | commands/z.go | 28 |
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) } } |