From 98839a42f20616c53dfcbbf5a1052097043899b7 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Tue, 16 Aug 2016 22:31:17 +0300 Subject: [PATCH] basic shell completion for single match --- comp.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + ui.go | 6 +++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/comp.go b/comp.go index e3fdf64..6d18812 100644 --- a/comp.go +++ b/comp.go @@ -42,6 +42,37 @@ func matchWord(s string, words []string) string { return s } +func matchExec(s string) string { + var match string + + paths := strings.Split(envPath, ":") + + for _, p := range paths { + fi, err := ioutil.ReadDir(p) + if err != nil { + log.Print(err) + } + + for _, f := range fi { + if strings.HasPrefix(f.Name(), s) { + if !f.Mode().IsRegular() || f.Mode()&0111 == 0 { + continue + } + if match != "" { + return s + } + match = f.Name() + } + } + } + + if match != "" { + return match + " " + } + + return s +} + func matchFile(s string) string { var match string @@ -109,3 +140,28 @@ func compCmd(acc []rune) []rune { return acc } + +func compShell(acc []rune) []rune { + if len(acc) == 0 || acc[len(acc)-1] == ' ' { + return acc + } + + s := string(acc) + f := strings.Fields(s) + + switch len(f) { + case 0: // do nothing + case 1: + return []rune(matchExec(s)) + default: + ret := []rune(f[0]) + ret = append(ret, ' ') + for i := 1; i < len(f); i++ { + name := matchFile(f[i]) + ret = append(ret, []rune(name)...) + } + return ret + } + + return acc +} diff --git a/main.go b/main.go index 74621df..8e9b5a4 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ var ( envUser = os.Getenv("USER") envHome = os.Getenv("HOME") envHost = os.Getenv("HOSTNAME") + envPath = os.Getenv("PATH") envShell = os.Getenv("SHELL") ) diff --git a/ui.go b/ui.go index 05d5a84..60d746f 100644 --- a/ui.go +++ b/ui.go @@ -446,7 +446,11 @@ func (ui *UI) prompt(pref string) string { termbox.Flush() return string(acc) case termbox.KeyTab: - acc = compCmd(acc) + if pref == ":" { + acc = compCmd(acc) + } else { + acc = compShell(acc) + } case termbox.KeyEsc: return "" }