2022-02-27 23:17:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-04-20 17:06:46 +00:00
|
|
|
func fileComplete(query, ctx string, fields []string) ([]string, string) {
|
|
|
|
return matchPath(query)
|
2022-02-27 23:17:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 19:59:00 +00:00
|
|
|
func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
|
|
|
var completions []string
|
|
|
|
|
|
|
|
prefixes := []string{"./", "../", "/", "~/"}
|
|
|
|
for _, prefix := range prefixes {
|
|
|
|
if strings.HasPrefix(query, prefix) {
|
2022-04-20 17:06:46 +00:00
|
|
|
fileCompletions, filePref := matchPath(query)
|
2022-03-05 19:59:00 +00:00
|
|
|
if len(fileCompletions) != 0 {
|
|
|
|
for _, f := range fileCompletions {
|
2022-04-20 17:06:46 +00:00
|
|
|
fullPath, _ := filepath.Abs(expandHome(query + strings.TrimPrefix(f, filePref)))
|
|
|
|
if err := findExecutable(fullPath, false, true); err != nil {
|
2022-03-05 19:59:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
completions = append(completions, f)
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 17:06:46 +00:00
|
|
|
return completions, filePref
|
2022-03-05 19:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter out executables, but in path
|
|
|
|
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
|
|
|
// print dir to stderr for debugging
|
|
|
|
// search for an executable which matches our query string
|
|
|
|
if matches, err := filepath.Glob(filepath.Join(dir, query + "*")); err == nil {
|
|
|
|
// get basename from matches
|
|
|
|
for _, match := range matches {
|
|
|
|
// check if we have execute permissions for our match
|
2022-03-18 00:22:30 +00:00
|
|
|
err := findExecutable(match, true, false)
|
|
|
|
if err != nil {
|
2022-03-05 19:59:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// get basename from match
|
|
|
|
name := filepath.Base(match)
|
|
|
|
// add basename to completions
|
|
|
|
completions = append(completions, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add lua registered commands to completions
|
|
|
|
for cmdName := range commands {
|
|
|
|
if strings.HasPrefix(cmdName, query) {
|
|
|
|
completions = append(completions, cmdName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:56:22 +00:00
|
|
|
completions = removeDupes(completions)
|
|
|
|
|
2022-03-05 19:59:00 +00:00
|
|
|
return completions, query
|
|
|
|
}
|
|
|
|
|
2022-04-20 17:06:46 +00:00
|
|
|
func matchPath(query string) ([]string, string) {
|
2022-02-27 23:17:51 +00:00
|
|
|
var entries []string
|
2022-04-20 17:06:46 +00:00
|
|
|
var baseName string
|
2022-02-27 23:17:51 +00:00
|
|
|
|
2022-04-20 17:06:46 +00:00
|
|
|
path, _ := filepath.Abs(expandHome(filepath.Dir(query)))
|
|
|
|
if string(query) == "" {
|
|
|
|
// filepath base below would give us "."
|
|
|
|
// which would cause a match of only dotfiles
|
|
|
|
path, _ = filepath.Abs(".")
|
|
|
|
} else if !strings.HasSuffix(query, string(os.PathSeparator)) {
|
|
|
|
baseName = filepath.Base(query)
|
|
|
|
}
|
2022-02-27 23:17:51 +00:00
|
|
|
|
2022-04-20 17:06:46 +00:00
|
|
|
files, _ := os.ReadDir(path)
|
|
|
|
for _, file := range files {
|
2022-04-20 22:18:42 +00:00
|
|
|
if strings.HasPrefix(strings.ToLower(file.Name()), strings.ToLower(baseName)) {
|
2022-04-20 17:06:46 +00:00
|
|
|
entry := file.Name()
|
|
|
|
if file.IsDir() {
|
|
|
|
entry = entry + string(os.PathSeparator)
|
2022-02-27 23:17:51 +00:00
|
|
|
}
|
2022-04-20 17:06:46 +00:00
|
|
|
entry = escapeFilename(entry)
|
|
|
|
entries = append(entries, entry)
|
2022-02-27 23:17:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-05 19:59:00 +00:00
|
|
|
|
2022-04-20 17:06:46 +00:00
|
|
|
return entries, baseName
|
2022-02-27 23:17:51 +00:00
|
|
|
}
|
2022-03-15 01:22:10 +00:00
|
|
|
|
2022-04-20 17:06:46 +00:00
|
|
|
func escapeFilename(fname string) string {
|
|
|
|
args := []string{
|
|
|
|
"\"", "\\\"",
|
|
|
|
"'", "\\'",
|
|
|
|
"`", "\\`",
|
|
|
|
" ", "\\ ",
|
|
|
|
"(", "\\(",
|
|
|
|
")", "\\)",
|
|
|
|
"[", "\\[",
|
|
|
|
"]", "\\]",
|
|
|
|
"$", "\\$",
|
|
|
|
"&", "\\&",
|
|
|
|
"*", "\\*",
|
|
|
|
">", "\\>",
|
|
|
|
"<", "\\<",
|
|
|
|
"|", "\\|",
|
2022-03-15 01:22:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 17:06:46 +00:00
|
|
|
r := strings.NewReplacer(args...)
|
|
|
|
return r.Replace(fname)
|
2022-03-15 01:22:10 +00:00
|
|
|
}
|
2022-04-20 22:18:42 +00:00
|
|
|
|