mirror of https://github.com/rwxrob/bonzai
parent
499a9d1f83
commit
c81f800235
@ -1 +0,0 @@
|
||||
# Future Home of Other Completion Methods
|
@ -1,65 +0,0 @@
|
||||
// Copyright 2022 Robert S. Muhlestein.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package comp
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/rwxrob/bonzai"
|
||||
"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 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 bonzai.Command, args ...string) []string {
|
||||
|
||||
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.EntriesWithSlash("")
|
||||
}
|
||||
|
||||
first := strings.TrimRight(args[0], string(filepath.Separator))
|
||||
d, pre := filepath.Split(first)
|
||||
|
||||
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 dir.AddSlash(list)
|
||||
}
|
||||
if fs.IsDir(list[0]) {
|
||||
d = list[0]
|
||||
continue
|
||||
}
|
||||
return dir.AddSlash(list)
|
||||
}
|
||||
|
||||
return []string{}
|
||||
}
|
@ -1,75 +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 ExampleFile_nil() {
|
||||
os.Chdir("testdata/file")
|
||||
defer os.Chdir("../..")
|
||||
fmt.Println(comp.File(nil))
|
||||
fmt.Println(comp.File(nil, ""))
|
||||
//Output:
|
||||
// [bar/ blah/ come/ foo.go]
|
||||
// [bar/ blah/ come/ foo.go]
|
||||
}
|
||||
|
||||
func ExampleFile_pre_File() {
|
||||
os.Chdir("testdata/file")
|
||||
defer os.Chdir("../..")
|
||||
fmt.Println(comp.File(nil, "fo"))
|
||||
//Output:
|
||||
// [foo.go]
|
||||
}
|
||||
|
||||
func ExampleFile_pre_Dir_Only() {
|
||||
os.Chdir("testdata/file")
|
||||
defer os.Chdir("../..")
|
||||
fmt.Println(comp.File(nil, "bar"))
|
||||
fmt.Println(comp.File(nil, "bar/"))
|
||||
//Output:
|
||||
// [bar/foo.go bar/other]
|
||||
// [bar/foo.go bar/other]
|
||||
}
|
||||
|
||||
func ExampleFile_pre_Dir_or_Files() {
|
||||
os.Chdir("testdata/file")
|
||||
defer os.Chdir("../..")
|
||||
fmt.Println(comp.File(nil, "b"))
|
||||
//Output:
|
||||
// [bar/ blah/]
|
||||
}
|
||||
|
||||
func ExampleFile_pre_Dir_Specific() {
|
||||
os.Chdir("testdata/file")
|
||||
defer os.Chdir("../..")
|
||||
fmt.Println(comp.File(nil, "blah"))
|
||||
//Output:
|
||||
// [blah/file1 blah/file2]
|
||||
}
|
||||
|
||||
func ExampleFile_pre_Dir_Recurse() {
|
||||
os.Chdir("testdata/file")
|
||||
defer os.Chdir("../..")
|
||||
fmt.Println(comp.File(nil, "com"))
|
||||
fmt.Println(comp.File(nil, "come/"))
|
||||
//Output:
|
||||
// [come/one]
|
||||
// [come/one]
|
||||
}
|
||||
|
||||
func ExampleFile_dir_File() {
|
||||
os.Chdir("testdata/file")
|
||||
defer os.Chdir("../..")
|
||||
fmt.Println(comp.File(nil, "bar/fo"))
|
||||
fmt.Println(comp.File(nil, "bar/foo"))
|
||||
//Output:
|
||||
// [bar/foo.go]
|
||||
// [bar/foo.go]
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
// Copyright 2022 Robert S. Muhlestein.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package comp
|
||||
|
||||
import (
|
||||
"github.com/rwxrob/bonzai"
|
||||
"github.com/rwxrob/fn/filt"
|
||||
"github.com/rwxrob/structs/set/text/set"
|
||||
)
|
||||
|
||||
// Standard completion is resolved as follows:
|
||||
//
|
||||
// 1. If leaf has Completer function, delegate to it
|
||||
//
|
||||
// 2. If leaf has no arguments, return all Commands and Params
|
||||
//
|
||||
// 3. If first argument is the name of a Command return it only even
|
||||
// if in the Hidden list
|
||||
//
|
||||
// 4. Otherwise, return every Command or Param that is not in the
|
||||
// Hidden list and HasPrefix matching the first arg
|
||||
//
|
||||
// See bonzai.Completer.
|
||||
func Standard(x bonzai.Command, args ...string) []string {
|
||||
|
||||
// if has completer, delegate
|
||||
if c := x.GetCompleter(); c != nil {
|
||||
return c(x, args...)
|
||||
}
|
||||
|
||||
// not sure we've completed the command name itself yet
|
||||
if len(args) == 0 {
|
||||
return []string{x.GetName()}
|
||||
}
|
||||
|
||||
// build list of visible commands and params
|
||||
list := []string{}
|
||||
list = append(list, x.GetCommandNames()...)
|
||||
list = append(list, x.GetParams()...)
|
||||
list = set.Minus[string, string](list, x.GetHidden())
|
||||
|
||||
if len(args) == 0 {
|
||||
return list
|
||||
}
|
||||
|
||||
return filt.HasPrefix(list, args[0])
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
// Copyright 2022 Robert S. Muhlestein.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package comp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/rwxrob/bonzai"
|
||||
"github.com/rwxrob/bonzai/comp"
|
||||
Z "github.com/rwxrob/bonzai/z"
|
||||
"github.com/rwxrob/fn/filt"
|
||||
)
|
||||
|
||||
func ExampleStandard() {
|
||||
foo := new(Z.Cmd)
|
||||
foo.Params = []string{"box"}
|
||||
foo.Add("bar")
|
||||
foo.Add("blah")
|
||||
|
||||
// if no args, we have to assume the command isn't finished yet
|
||||
fmt.Println(comp.Standard(foo))
|
||||
|
||||
// we know it's not a command, but no prefix just yet
|
||||
// (usually this is when a space has been added after the command)
|
||||
fmt.Println(comp.Standard(foo, ""))
|
||||
|
||||
// everything that begins with a (nothing)
|
||||
fmt.Println(comp.Standard(foo, `a`))
|
||||
|
||||
// everything that begins with b (which is everything)
|
||||
fmt.Println(comp.Standard(foo, `b`))
|
||||
|
||||
// everything that begins with bl (just blah)
|
||||
fmt.Println(comp.Standard(foo, `bl`))
|
||||
|
||||
// give own completer for days of the week
|
||||
foo.Completer = func(cmd bonzai.Command, args ...string) []string {
|
||||
list := []string{"mon", "tue", "wed", "thu", "fri", "sat", "sun"}
|
||||
if len(args) == 0 {
|
||||
return list
|
||||
}
|
||||
return filt.HasPrefix(list, args[0])
|
||||
}
|
||||
fmt.Println(comp.Standard(foo, `t`))
|
||||
|
||||
//Output:
|
||||
// []
|
||||
// [bar blah box]
|
||||
// []
|
||||
// [bar blah box]
|
||||
// [blah]
|
||||
// [tue thu]
|
||||
|
||||
}
|
Loading…
Reference in New Issue