You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bonzai/comp/file.go

60 lines
1.3 KiB
Go

// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package comp
import (
"path/filepath"
"github.com/rwxrob/bonzai/check"
"github.com/rwxrob/bonzai/filt"
"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.
// 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.
func File(x Command, args ...string) []string {
// no completion if we already have one
if len(args) > 1 {
return []string{}
}
// catch edge cases
if len(args) == 0 {
if x != nil {
return []string{x.GetName()} // will add tailing space
}
return util.Files("")
}
// no prefix of any kind, just a space following command
if args[0] == "" {
return util.Files("")
}
dir, pre := filepath.Split(args[0])
list := filt.BaseHasPrefix(util.Files(dir), pre)
for {
if len(list) > 1 {
return list
}
if len(list) == 1 && check.IsDir(list[0]) {
list = util.Files(list[0])
continue
}
break
}
// just a single file left in the list
return list
}