Add directories to comp.File completer

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

@ -0,0 +1,26 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package comp
import (
"github.com/rwxrob/bonzai/filter"
"github.com/rwxrob/bonzai/util"
)
// File returns all file names for the directory and file prefix
// passed. If nothing is passed assumes the current working directory.
func File(x Command, args ...string) []string {
match := ""
dir := "."
if len(args) > 0 {
match = args[0]
}
list := []string{}
list = append(list, util.Files(dir)...)
list = filter.HasPrefix(list, match)
return list
}

@ -0,0 +1,25 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package comp_test
import (
"fmt"
"os"
"github.com/rwxrob/bonzai/comp"
)
func ExampleFile() {
os.Chdir("testdata/file")
defer os.Chdir("../..")
fmt.Println(comp.File(nil))
fmt.Println(comp.File(nil, "b"))
fmt.Println(comp.File(nil, "blah"))
fmt.Println(comp.File(nil, "blah/"))
//Output:
// [bar/ blah/ come/ foo/ other/]
// [bar/ blah/]
// [blah/]
// [dir1/ dir2/ file1 file2]
}

@ -1,28 +0,0 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package comp
import (
"github.com/rwxrob/bonzai/filter"
"github.com/rwxrob/bonzai/util"
)
// Files returns all file names for the directory and file prefix
// passed. If nothing is passed assumes the current working directory.
func Files(x Command, args ...string) []string {
match := ""
if args != nil && len(args) > 0 {
match = args[0]
}
list := []string{}
// TODO if file separators detected, drill down to the proper
// directory then check the leaf as the prefix of files
list = append(list, util.Files(match)...)
return filter.HasPrefix(list, match)
}

@ -1,19 +0,0 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package comp_test
import (
"fmt"
"os"
"github.com/rwxrob/bonzai/comp"
)
func ExampleFiles() {
os.Chdir("testdata/files")
defer os.Chdir("../..")
fmt.Println(comp.Files(nil))
//Output:
// [bar blah come foo other]
}

@ -3,7 +3,9 @@
package filter
import "strings"
import (
"strings"
)
// HasPrefix filters the Text input set and returns only those elements
// that have the give prefix.

@ -6,6 +6,9 @@ import (
"github.com/rwxrob/bonzai/filter"
)
// Files returns a slice of strings matching the names of the files
// within the given directory adding a slash to the end of any
// directories.
func Files(dir string) []string {
if dir == "" {
dir = "."
@ -13,7 +16,11 @@ func Files(dir string) []string {
files := []string{}
finfo, _ := os.ReadDir(dir)
for _, f := range finfo {
files = append(files, f.Name())
name := f.Name()
if f.IsDir() {
name += "/"
}
files = append(files, name)
}
return files
}

@ -12,6 +12,9 @@ func ExampleFiles() {
// Output:
// bar
// blah
// dir1/
// dir2/
// dir3/
// foo
// other
// some
@ -24,6 +27,9 @@ func ExampleFiles_empty() {
// Output:
// bar
// blah
// dir1/
// dir2/
// dir3/
// foo
// other
// some

Loading…
Cancel
Save