Pressing tab when all suggestions start with the same substring
will insert that substring (this is how bash autocompletion works).
} else {
chosen = suggestions[0]
}
+ } else {
+ if len(dirs) > 1 {
+ chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/"
+ }
}
return chosen, suggestions
chosen, suggestions = OptionComplete(currentArg)
}
+ if len(suggestions) > 1 {
+ chosen = chosen + CommonSubstring(suggestions...)
+ }
+
if chosen != "" {
if len(args) > 1 {
chosen = " " + chosen
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 {
package main
-import "testing"
+import (
+ "fmt"
+ "testing"
+)
func TestNumOccurences(t *testing.T) {
var tests = []struct {