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

30
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,18 +47,20 @@ func NewLineReader(prompt string) *LineReader {
} }
if len(fields) == 1 { if len(fields) == 1 {
fileCompletions := append(completions, readline.FilenameCompleter(query, ctx)...) prefixes := []string{"./", "../", "/", "~/"}
// filter out executables for _, prefix := range prefixes {
for _, f := range fileCompletions { if strings.HasPrefix(fields[0], prefix) {
name := strings.Replace(f, "~", homedir, 1) fileCompletions := append(completions, readline.FilenameCompleter(query, ctx)...)
if info, err := os.Stat(name); err == nil && info.Mode().Perm() & 0100 == 0 { // filter out executables
continue for _, f := range fileCompletions {
name := strings.Replace(f, "~", homedir, 1)
if info, err := os.Stat(name); err == nil && info.Mode().Perm() & 0100 == 0 {
continue
}
completions = append(completions, f)
}
return completions
} }
completions = append(completions, f)
}
if len(completions) != 0 {
return completions
} }
// filter out executables, but in path // filter out executables, but in 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