github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/autocomplete/autobranch.go (about) 1 package autocomplete 2 3 import ( 4 "sort" 5 "strings" 6 7 "github.com/lmorg/murex/lang" 8 "github.com/lmorg/murex/lang/types" 9 ) 10 11 func autoBranch(items *[]string) { 12 // Is recursive search enabled? 13 recursiveSearch, _ := lang.ShellProcess.Config.Get("shell", "recursive-enabled", types.Boolean) 14 //if err != nil { 15 // recursiveSearch = false 16 //} 17 18 if recursiveSearch.(bool) { 19 sort.Sort(treeSorter(*items)) 20 } else { 21 // Only show top level 22 cropBranches(items) 23 // dedup group 24 } 25 } 26 27 type treeSorter []string 28 29 func (ts treeSorter) Len() int { return len(ts) } 30 func (ts treeSorter) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] } 31 32 func (ts treeSorter) Less(i, j int) bool { 33 iLen := abstractSize(ts[i]) 34 jLen := abstractSize(ts[j]) 35 36 if iLen == jLen { 37 return ts[i] < ts[j] 38 } 39 40 return iLen < jLen 41 } 42 43 func abstractSize(s string) int { 44 count := strings.Count(s, "/") 45 switch { 46 case count == 0: 47 return 0 48 case count == 1: 49 if s[len(s)-1] != '/' { 50 return 1 51 } 52 return 2 53 default: 54 return 3 55 } 56 } 57 58 func cropBranches(tree *[]string) { 59 for branch := range *tree { 60 61 for i := 0; i < len((*tree)[branch])-1; i++ { 62 if (*tree)[branch][i] == '/' { 63 (*tree)[branch] = (*tree)[branch][:i+1] 64 break 65 } 66 } 67 68 } 69 }