added shell tab complete
This commit is contained in:
parent
44beab651c
commit
d5edb2bad2
4 changed files with 125 additions and 4 deletions
90
util/shell_complete.go
Normal file
90
util/shell_complete.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/* SHS: Syntactically Homogeneous Shell
|
||||
* Copyright (C) 2019 Aidan Hahn
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"gitlab.com/whom/shs/log"
|
||||
"gitlab.com/whom/shs/ast"
|
||||
)
|
||||
|
||||
// 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)
|
||||
compSource := []string{}
|
||||
|
||||
if !path {
|
||||
dir = "."
|
||||
} else {
|
||||
line = tok
|
||||
}
|
||||
|
||||
fobjs, err := ioutil.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Log(log.DEBUG,
|
||||
"couldnt read dir " + dir + ": " + err.Error(),
|
||||
"complete")
|
||||
if path {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
for _, f := range fobjs {
|
||||
compSource = append(compSource, f.Name())
|
||||
}
|
||||
}
|
||||
|
||||
if !path {
|
||||
compSource = append(compSource, ast.ListVars(vt)...)
|
||||
compSource = append(compSource, ast.ListFuncs(ft)...)
|
||||
}
|
||||
|
||||
completions := []string{}
|
||||
for _, i := range compSource {
|
||||
if strings.HasPrefix(i, line) {
|
||||
completions = append(completions, i)
|
||||
}
|
||||
}
|
||||
|
||||
return completions
|
||||
}
|
||||
|
||||
// returns everything up to the last '/'
|
||||
// as well as whether or not a / was found
|
||||
// and finally the token after the last /
|
||||
func getPathBase(in string) (string, bool, string) {
|
||||
if in == "" {
|
||||
return "", false, ""
|
||||
}
|
||||
|
||||
isPath := false
|
||||
|
||||
i := len(in) - 1
|
||||
for i > 0 {
|
||||
if in[i] == '/' {
|
||||
isPath = true
|
||||
break
|
||||
}
|
||||
|
||||
i -= 1
|
||||
}
|
||||
|
||||
return in[:i], isPath, in[i+1:]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue