2022-02-25 09:17:41 +00:00
|
|
|
// Copyright 2022 Robert S. Muhlestein.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package comp
|
|
|
|
|
|
|
|
import (
|
2022-02-27 06:18:55 +00:00
|
|
|
"path/filepath"
|
2022-02-26 02:46:36 +00:00
|
|
|
|
2022-02-27 06:18:55 +00:00
|
|
|
"github.com/rwxrob/bonzai/check"
|
2022-02-26 02:46:36 +00:00
|
|
|
"github.com/rwxrob/bonzai/filt"
|
2022-02-25 09:17:41 +00:00
|
|
|
"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.
|
2022-02-27 06:18:55 +00:00
|
|
|
// 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.
|
2022-02-25 09:17:41 +00:00
|
|
|
func File(x Command, args ...string) []string {
|
2022-02-27 06:18:55 +00:00
|
|
|
|
|
|
|
// 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
|
2022-02-26 02:46:36 +00:00
|
|
|
}
|
2022-02-27 06:18:55 +00:00
|
|
|
return util.Files("")
|
2022-02-25 09:17:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 06:18:55 +00:00
|
|
|
// 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 {
|
2022-02-25 09:17:41 +00:00
|
|
|
|
2022-02-27 06:18:55 +00:00
|
|
|
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
|
2022-02-25 09:17:41 +00:00
|
|
|
}
|