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

View file

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

View file

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