Improve tab completion in command mode

Pressing tab when all suggestions start with the same substring
will insert that substring (this is how bash autocompletion works).
This commit is contained in:
Zachary Yedidia 2016-08-28 13:05:07 -04:00
parent da7bc997ff
commit aa9dbdde3e
4 changed files with 40 additions and 1 deletions

View file

@ -49,6 +49,10 @@ func FileComplete(input string) (string, []string) {
} else {
chosen = suggestions[0]
}
} else {
if len(dirs) > 1 {
chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/"
}
}
return chosen, suggestions

View file

@ -195,6 +195,10 @@ func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Comple
chosen, suggestions = OptionComplete(currentArg)
}
if len(suggestions) > 1 {
chosen = chosen + CommonSubstring(suggestions...)
}
if chosen != "" {
if len(args) > 1 {
chosen = " " + chosen

View file

@ -182,6 +182,34 @@ func runePos(p int, str string) int {
return utf8.RuneCountInString(str[:p])
}
func lcs(a, b string) string {
arunes := []rune(a)
brunes := []rune(b)
lcs := ""
for i, r := range arunes {
if i >= len(brunes) {
break
}
if r == brunes[i] {
lcs += string(r)
} else {
break
}
}
return lcs
}
func CommonSubstring(arr ...string) string {
commonStr := arr[0]
for _, str := range arr[1:] {
commonStr = lcs(commonStr, str)
}
return commonStr
}
// Abs is a simple absolute value function for ints
func Abs(n int) int {
if n < 0 {

View file

@ -1,6 +1,9 @@
package main
import "testing"
import (
"fmt"
"testing"
)
func TestNumOccurences(t *testing.T) {
var tests = []struct {