2022-02-27 23:17:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-04-21 18:01:59 +00:00
|
|
|
"errors"
|
2022-02-27 23:17:51 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"os"
|
2022-04-21 18:01:59 +00:00
|
|
|
|
|
|
|
"hilbish/util"
|
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2022-02-27 23:17:51 +00:00
|
|
|
)
|
|
|
|
|
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-05-01 04:49:59 +00:00
|
|
|
fullPath, _ := filepath.Abs(util.ExpandHome(query + strings.TrimPrefix(f, filePref)))
|
2022-04-20 17:06:46 +00:00
|
|
|
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-05-01 04:49:59 +00:00
|
|
|
path, _ := filepath.Abs(util.ExpandHome(filepath.Dir(query)))
|
2022-04-20 17:06:46 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
files, _ := os.ReadDir(path)
|
|
|
|
for _, file := range files {
|
|
|
|
if strings.HasPrefix(file.Name(), baseName) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func escapeFilename(fname string) string {
|
|
|
|
args := []string{
|
|
|
|
"\"", "\\\"",
|
|
|
|
"'", "\\'",
|
|
|
|
"`", "\\`",
|
|
|
|
" ", "\\ ",
|
|
|
|
"(", "\\(",
|
|
|
|
")", "\\)",
|
|
|
|
"[", "\\[",
|
|
|
|
"]", "\\]",
|
|
|
|
"$", "\\$",
|
|
|
|
"&", "\\&",
|
|
|
|
"*", "\\*",
|
|
|
|
">", "\\>",
|
|
|
|
"<", "\\<",
|
|
|
|
"|", "\\|",
|
|
|
|
}
|
|
|
|
|
|
|
|
r := strings.NewReplacer(args...)
|
|
|
|
return r.Replace(fname)
|
2022-02-27 23:17:51 +00:00
|
|
|
}
|
2022-04-21 18:01:59 +00:00
|
|
|
|
2022-04-23 01:16:35 +00:00
|
|
|
func completionLoader(rtm *rt.Runtime) *rt.Table {
|
|
|
|
exports := map[string]util.LuaExport{
|
|
|
|
"files": {luaFileComplete, 3, false},
|
|
|
|
"bins": {luaBinaryComplete, 3, false},
|
|
|
|
"call": {callLuaCompleter, 4, false},
|
|
|
|
"handler": {completionHandler, 2, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := rt.NewTable()
|
|
|
|
util.SetExports(rtm, mod, exports)
|
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2022-04-24 03:59:03 +00:00
|
|
|
// left as a shim, might doc in the same way as hilbish functions
|
2022-04-23 01:16:35 +00:00
|
|
|
func completionHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-04-24 03:59:03 +00:00
|
|
|
return c.Next(), nil
|
2022-04-23 01:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 18:01:59 +00:00
|
|
|
func callLuaCompleter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.CheckNArgs(4); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
completer, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
query, err := c.StringArg(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx, err := c.StringArg(2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fields, err := c.TableArg(3)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var completecb *rt.Closure
|
|
|
|
var ok bool
|
|
|
|
if completecb, ok = luaCompletions[completer]; !ok {
|
|
|
|
return nil, errors.New("completer " + completer + " does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// we must keep the holy 80 cols
|
|
|
|
completerReturn, err := rt.Call1(l.MainThread(),
|
|
|
|
rt.FunctionValue(completecb), rt.StringValue(query),
|
|
|
|
rt.StringValue(ctx), rt.TableValue(fields))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, completerReturn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func luaFileComplete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
query, ctx, fds, err := getCompleteParams(t, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:16:35 +00:00
|
|
|
completions, pfx := fileComplete(query, ctx, fds)
|
2022-04-21 18:01:59 +00:00
|
|
|
luaComps := rt.NewTable()
|
|
|
|
|
|
|
|
for i, comp := range completions {
|
|
|
|
luaComps.Set(rt.IntValue(int64(i + 1)), rt.StringValue(comp))
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:16:35 +00:00
|
|
|
return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
|
2022-04-21 18:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func luaBinaryComplete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
query, ctx, fds, err := getCompleteParams(t, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:16:35 +00:00
|
|
|
completions, pfx := binaryComplete(query, ctx, fds)
|
2022-04-21 18:01:59 +00:00
|
|
|
luaComps := rt.NewTable()
|
|
|
|
|
|
|
|
for i, comp := range completions {
|
|
|
|
luaComps.Set(rt.IntValue(int64(i + 1)), rt.StringValue(comp))
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:16:35 +00:00
|
|
|
return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
|
2022-04-21 18:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCompleteParams(t *rt.Thread, c *rt.GoCont) (string, string, []string, error) {
|
|
|
|
if err := c.CheckNArgs(3); err != nil {
|
|
|
|
return "", "", []string{}, err
|
|
|
|
}
|
|
|
|
query, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", []string{}, err
|
|
|
|
}
|
|
|
|
ctx, err := c.StringArg(1)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", []string{}, err
|
|
|
|
}
|
|
|
|
fields, err := c.TableArg(2)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var fds []string
|
|
|
|
util.ForEach(fields, func(k rt.Value, v rt.Value) {
|
|
|
|
if v.Type() == rt.StringType {
|
|
|
|
fds = append(fds, v.AsString())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return query, ctx, fds, err
|
|
|
|
}
|