improve autocomplete

This commit is contained in:
Aidan 2020-07-18 14:19:58 -07:00
parent d5edb2bad2
commit 956044cfae
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 19 additions and 9 deletions

View file

@ -20,7 +20,6 @@ package main
import (
"os"
"fmt"
"strings"
"strconv"
"github.com/peterh/liner"
"gitlab.com/whom/shs/ast"
@ -108,8 +107,7 @@ func main() {
line.SetCtrlCAborts(true)
line.SetCompleter(func(line string) (c []string) {
strtoks := strings.Split(line, " ")
return util.ShellCompleter(strtoks[len(strtoks) - 1], vars, funcs)
return util.ShellCompleter(line, vars, funcs)
})
var histFile *os.File

View file

@ -74,9 +74,10 @@ func call(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
} else {
cmd = exec.Command(path)
}
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
signalChan := make(chan os.Signal, 2)
signal.Notify(signalChan, sigs...)

View file

@ -19,8 +19,8 @@ package util
import (
"fmt"
"io/ioutil"
"strings"
"io/ioutil"
"gitlab.com/whom/shs/log"
"gitlab.com/whom/shs/ast"
)
@ -28,7 +28,18 @@ import (
// wrap this in a lambda that passes in the vt and ft
// I suppose it could be more optimal. Fix if it bothers you
func ShellCompleter(line string, vt ast.VarTable, ft ast.FuncTable) []string {
dir, path, tok := getPathBase(line)
var head, tail string
idx := strings.LastIndex(line, " ")
if idx > 0 {
head = line[:idx+1]
tail = line[idx+1:]
} else {
head = ""
tail = line
}
dir, path, tok := getPathBase(tail)
compSource := []string{}
if !path {
@ -47,7 +58,7 @@ func ShellCompleter(line string, vt ast.VarTable, ft ast.FuncTable) []string {
}
} else {
for _, f := range fobjs {
compSource = append(compSource, f.Name())
compSource = append(compSource, dir + "/" + f.Name())
}
}
@ -58,8 +69,8 @@ func ShellCompleter(line string, vt ast.VarTable, ft ast.FuncTable) []string {
completions := []string{}
for _, i := range compSource {
if strings.HasPrefix(i, line) {
completions = append(completions, i)
if strings.HasPrefix(i, tail) {
completions = append(completions, head + i)
}
}