basic shell completion for single match

This commit is contained in:
Gokcehan 2016-08-16 22:31:17 +03:00
parent 656a9c837a
commit 98839a42f2
3 changed files with 62 additions and 1 deletions

56
comp.go
View File

@ -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
}

View File

@ -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")
)

4
ui.go
View File

@ -446,7 +446,11 @@ func (ui *UI) prompt(pref string) string {
termbox.Flush()
return string(acc)
case termbox.KeyTab:
if pref == ":" {
acc = compCmd(acc)
} else {
acc = compShell(acc)
}
case termbox.KeyEsc:
return ""
}