diff options
author | Robin Jarry <robin@jarry.cc> | 2023-12-22 15:51:14 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-01-17 11:52:59 +0100 |
commit | 3622f7a99e91bfe38eab9e91dc982aafa00fe211 (patch) | |
tree | 071af82a070719d2b4f46dca430a892941f70214 | |
parent | 765761c5bf49799e7d74535b9727062c869fc358 (diff) | |
download | aerc-3622f7a99e91bfe38eab9e91dc982aafa00fe211.tar.gz |
cd: fix completion of folders with a space
Folders that contain spaces are surrounded by quotes. They can never end
with '/'. Hence they are never returned in the completion results.
Update CompletePath with an additional onlyDirs argument to take care of
this before quotes are inserted.
Fixes: abe228b14d97 ("commands: use completion from go-opt")
Fixes: https://todo.sr.ht/~rjarry/aerc/204
Reported-by: Bence Ferdinandy <bence@ferdinandy.com>
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
-rw-r--r-- | commands/account/export-mbox.go | 2 | ||||
-rw-r--r-- | commands/account/import-mbox.go | 2 | ||||
-rw-r--r-- | commands/cd.go | 13 | ||||
-rw-r--r-- | commands/compose/attach.go | 2 | ||||
-rw-r--r-- | commands/eml.go | 2 | ||||
-rw-r--r-- | commands/msgview/save.go | 2 | ||||
-rw-r--r-- | commands/util.go | 4 | ||||
-rw-r--r-- | commands/util_test.go | 13 |
8 files changed, 21 insertions, 19 deletions
diff --git a/commands/account/export-mbox.go b/commands/account/export-mbox.go index 2eed570a..6a36d375 100644 --- a/commands/account/export-mbox.go +++ b/commands/account/export-mbox.go @@ -30,7 +30,7 @@ func (ExportMbox) Aliases() []string { } func (*ExportMbox) CompleteFilename(arg string) []string { - return commands.CompletePath(arg) + return commands.CompletePath(arg, false) } func (e ExportMbox) Execute(args []string) error { diff --git a/commands/account/import-mbox.go b/commands/account/import-mbox.go index e326edce..c38aeb8f 100644 --- a/commands/account/import-mbox.go +++ b/commands/account/import-mbox.go @@ -31,7 +31,7 @@ func (ImportMbox) Aliases() []string { } func (*ImportMbox) CompleteFilename(arg string) []string { - return commands.CompletePath(arg) + return commands.CompletePath(arg, false) } func (i ImportMbox) Execute(args []string) error { diff --git a/commands/cd.go b/commands/cd.go index d2dc5310..cd206c3c 100644 --- a/commands/cd.go +++ b/commands/cd.go @@ -3,7 +3,6 @@ package commands import ( "errors" "os" - "strings" "git.sr.ht/~rjarry/aerc/app" "git.sr.ht/~rjarry/aerc/lib/xdg" @@ -24,17 +23,7 @@ func (ChangeDirectory) Aliases() []string { } func (*ChangeDirectory) CompleteTarget(arg string) []string { - completions := CompletePath(arg) - - var dirs []string - for _, c := range completions { - // filter out non-directories - if strings.HasSuffix(c, "/") { - dirs = append(dirs, c) - } - } - - return dirs + return CompletePath(arg, true) } func (cd ChangeDirectory) Execute(args []string) error { diff --git a/commands/compose/attach.go b/commands/compose/attach.go index 49d63bb8..52ce55cb 100644 --- a/commands/compose/attach.go +++ b/commands/compose/attach.go @@ -36,7 +36,7 @@ func (Attach) Aliases() []string { } func (*Attach) CompletePath(arg string) []string { - return commands.CompletePath(arg) + return commands.CompletePath(arg, false) } func (a Attach) Execute(args []string) error { diff --git a/commands/eml.go b/commands/eml.go index 06a78ffd..ebf3abfa 100644 --- a/commands/eml.go +++ b/commands/eml.go @@ -24,7 +24,7 @@ func (Eml) Aliases() []string { } func (*Eml) CompletePath(arg string) []string { - return CompletePath(arg) + return CompletePath(arg, false) } func (e Eml) Execute(args []string) error { diff --git a/commands/msgview/save.go b/commands/msgview/save.go index 9f2b71fc..bcaf9eb2 100644 --- a/commands/msgview/save.go +++ b/commands/msgview/save.go @@ -42,7 +42,7 @@ func (*Save) CompletePath(arg string) []string { if defaultPath != "" && !isAbsPath(arg) { arg = filepath.Join(defaultPath, arg) } - return commands.CompletePath(xdg.ExpandHome(arg)) + return commands.CompletePath(arg, false) } func (s Save) Execute(args []string) error { diff --git a/commands/util.go b/commands/util.go index e7940017..af8d3dfa 100644 --- a/commands/util.go +++ b/commands/util.go @@ -71,7 +71,7 @@ func QuickTerm(args []string, stdin io.Reader) (*app.Terminal, error) { } // CompletePath provides filesystem completions given a starting path. -func CompletePath(path string) []string { +func CompletePath(path string, onlyDirs bool) []string { if path == ".." || strings.HasSuffix(path, "/..") { return []string{path + "/"} } @@ -93,6 +93,8 @@ func CompletePath(path string) []string { for _, m := range matches { if isDir(m) { m += "/" + } else if onlyDirs { + continue } if strings.HasPrefix(filepath.Base(m), ".") && !includeHidden { continue diff --git a/commands/util_test.go b/commands/util_test.go index 80113fd5..16b072d2 100644 --- a/commands/util_test.go +++ b/commands/util_test.go @@ -14,6 +14,7 @@ func TestCompletePath(t *testing.T) { vectors := []struct { arg string + onlyDirs bool expected []string }{ { @@ -21,6 +22,11 @@ func TestCompletePath(t *testing.T) { expected: []string{"baz/", "foo.ini", "foo/"}, }, { + arg: "", + onlyDirs: true, + expected: []string{"baz/", "foo/"}, + }, + { arg: ".", expected: []string{".hidden/", ".keep-me"}, }, @@ -44,10 +50,15 @@ func TestCompletePath(t *testing.T) { "../testdata/foo/", }, }, + { + arg: "../testdata/f", + onlyDirs: true, + expected: []string{"../testdata/foo/"}, + }, } for _, vec := range vectors { t.Run(vec.arg, func(t *testing.T) { - res := commands.CompletePath(vec.arg) + res := commands.CompletePath(vec.arg, vec.onlyDirs) assert.Equal(t, vec.expected, res) }) } |