fix: executable tab completion

dev
TorchedSammy 2021-11-23 19:09:07 -05:00
parent c329d21e7d
commit e8a69d36bb
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 17 additions and 13 deletions

12
rl.go
View File

@ -25,9 +25,12 @@ func NewLineReader(prompt string) *LineReader {
var completions []string var completions []string
// trim whitespace from ctx // trim whitespace from ctx
ctx = strings.TrimLeft(ctx, " ") ctx = strings.TrimLeft(ctx, " ")
if len(ctx) == 0 {
return []string{}
}
fields := strings.Split(ctx, " ") fields := strings.Split(ctx, " ")
if len(fields) == 0 { if len(fields) == 0 {
return nil return []string{}
} }
for aliases[fields[0]] != "" { for aliases[fields[0]] != "" {
@ -44,6 +47,9 @@ func NewLineReader(prompt string) *LineReader {
} }
if len(fields) == 1 { if len(fields) == 1 {
prefixes := []string{"./", "../", "/", "~/"}
for _, prefix := range prefixes {
if strings.HasPrefix(fields[0], prefix) {
fileCompletions := append(completions, readline.FilenameCompleter(query, ctx)...) fileCompletions := append(completions, readline.FilenameCompleter(query, ctx)...)
// filter out executables // filter out executables
for _, f := range fileCompletions { for _, f := range fileCompletions {
@ -53,10 +59,9 @@ func NewLineReader(prompt string) *LineReader {
} }
completions = append(completions, f) completions = append(completions, f)
} }
if len(completions) != 0 {
return completions return completions
} }
}
// filter out executables, but in path // filter out executables, but in path
for _, dir := range filepath.SplitList(os.Getenv("PATH")) { for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
@ -100,7 +105,6 @@ func NewLineReader(prompt string) *LineReader {
if cmpTbl, ok := luacompleteTable.(*lua.LTable); ok { if cmpTbl, ok := luacompleteTable.(*lua.LTable); ok {
cmpTbl.ForEach(func(key lua.LValue, value lua.LValue) { cmpTbl.ForEach(func(key lua.LValue, value lua.LValue) {
// print key and value to stderr for debugging
// if key is a number (index), we just check and complete that // if key is a number (index), we just check and complete that
if key.Type() == lua.LTNumber { if key.Type() == lua.LTNumber {
// if we have only 2 fields then this is fine // if we have only 2 fields then this is fine