Work on completion of file args

This commit is contained in:
Kovid Goyal
2022-09-15 18:43:52 +05:30
parent 833e9625f9
commit 5d89a6c3c4
6 changed files with 208 additions and 32 deletions

47
tools/completion/kitty.go Normal file
View File

@@ -0,0 +1,47 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package completion
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"golang.org/x/sys/unix"
)
var _ = fmt.Print
func complete_kitty(completions *Completions, word string, arg_num int) {
exes := complete_executables_in_path(word)
if len(exes) > 0 {
mg := completions.add_match_group("Executables in PATH")
for _, exe := range exes {
mg.add_match(exe)
}
}
if len(word) > 0 && (filepath.IsAbs(word) || strings.HasPrefix(word, "./")) {
mg := completions.add_match_group("Executables")
mg.IsFiles = true
complete_files(word, func(q, abspath string, d fs.DirEntry) error {
if d.IsDir() {
// only allow directories that have sub-dirs or executable files in them
entries, err := os.ReadDir(abspath)
if err == nil {
for _, x := range entries {
if x.IsDir() || unix.Access(filepath.Join(abspath, x.Name()), unix.X_OK) == nil {
mg.add_match(q)
}
}
}
} else if unix.Access(abspath, unix.X_OK) == nil {
mg.add_match(q)
}
return nil
})
}
}