Fix comp.File

pull/53/head
rwxrob 2 years ago
parent 026c2742e4
commit b7a01bf50e
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -5,55 +5,60 @@ package comp
import (
"path/filepath"
"strings"
"github.com/rwxrob/fn/filt"
"github.com/rwxrob/fs"
"github.com/rwxrob/fs/dir"
)
// File returns all file names for the directory and file prefix
// passed. If nothing is passed assumes the current working directory.
// This completer is roughly based on the behavior and appearance of the
// bash shell with forward slashes as separators and escaped spaces. By
// using this completer (instead of the shell) the command line
// interface remains consistent across all runtimes.
// File returns all file names for the directory and file prefix passed.
// If nothing is passed assumes the current working directory. This
// completer is roughly based on the behavior of the bash shell with
// forward slashes as separators and escaped spaces. By using this
// completer (instead of the shell) the command line interface remains
// consistent across all runtimes. Note that unlike bash completion no
// indication of the type of file is provided.
func File(x Command, args ...string) []string {
// no completion if we already have one
if len(args) > 1 {
return []string{}
}
if args == nil || (len(args) > 0 && args[0] == "") {
return dir.EntriesWithSlash(".")
}
// catch edge cases
if len(args) == 0 {
if x != nil {
return []string{x.GetName()} // will add tailing space
}
return dir.Entries("")
return dir.EntriesWithSlash("")
}
// no prefix of any kind, just a space following command
if args[0] == "" {
return dir.Entries("")
}
first := strings.TrimRight(args[0], string(filepath.Separator))
d, pre := filepath.Split(first)
d, pre := filepath.Split(args[0])
list := filt.BaseHasPrefix(dir.Entries(d), pre)
if d == "" {
list := filt.HasPrefix(dir.Entries("."), pre)
if len(list) == 1 && fs.IsDir(list[0]) {
return dir.EntriesWithSlash(list[0])
}
return dir.AddSlash(list)
}
for {
list := filt.BaseHasPrefix(dir.Entries(d), pre)
if len(list) > 1 {
return list
return dir.AddSlash(list)
}
if len(list) == 1 && fs.IsDir(list[0]) {
list = dir.Entries(list[0])
if fs.IsDir(list[0]) {
d = list[0]
continue
}
break
return dir.AddSlash(list)
}
// just a single file left in the list
return list
return []string{}
}

@ -13,10 +13,13 @@ import (
func ExampleFile() {
os.Chdir("testdata/file")
defer os.Chdir("../..")
fmt.Println(comp.File(nil))
fmt.Println(comp.File(nil, ""))
fmt.Println(comp.File(nil, "fo"))
fmt.Println(comp.File(nil, "foo"))
fmt.Println(comp.File(nil, "bar"))
fmt.Println(comp.File(nil, "bar/"))
fmt.Println(comp.File(nil, "bar/fo"))
fmt.Println(comp.File(nil, "bar/foo"))
fmt.Println(comp.File(nil, "com"))
@ -33,6 +36,8 @@ func ExampleFile() {
// [bar/ blah/ come/ foo/ foo.go other/]
// [foo/ foo.go]
// [foo/ foo.go]
// [bar/foo/ bar/foo.go bar/other]
// [bar/foo/ bar/foo.go bar/other]
// [bar/foo/ bar/foo.go]
// [bar/foo/ bar/foo.go]
// [come/one]

@ -8,7 +8,7 @@ import (
"github.com/rwxrob/bonzai"
"github.com/rwxrob/bonzai/comp"
"github.com/rwxrob/bonzai/filt"
"github.com/rwxrob/fn/filt"
)
func ExampleStandard() {

Loading…
Cancel
Save