github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/autocomplete/sort.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package autocomplete
     5  
     6  func sortCompletions(items []string) {
     7  	sortCompletionsR(items, 0, len(items)-1)
     8  }
     9  
    10  func sortCompletionsR(items []string, start, end int) {
    11  	if (end - start) < 1 {
    12  		return
    13  	}
    14  
    15  	pivot := items[end]
    16  	splitIndex := start
    17  
    18  	for i := start; i < end; i++ {
    19  		if isLt(noColon(items[i]), noColon(pivot)) {
    20  			temp := items[splitIndex]
    21  
    22  			items[splitIndex] = items[i]
    23  			items[i] = temp
    24  
    25  			splitIndex++
    26  		}
    27  	}
    28  
    29  	items[end] = items[splitIndex]
    30  	items[splitIndex] = pivot
    31  
    32  	sortCompletionsR(items, start, splitIndex-1)
    33  	sortCompletionsR(items, splitIndex+1, end)
    34  }
    35  
    36  func isLt(a, b string) bool {
    37  	switch {
    38  	case len(a) == 0 || len(b) == 0:
    39  		return a < b
    40  
    41  	case a[0] == '-':
    42  		if b[0] == '-' {
    43  			return a < b
    44  		}
    45  		return false
    46  
    47  	case b[0] == '-':
    48  		return true
    49  
    50  	default:
    51  		return a < b
    52  	}
    53  }
    54  
    55  func noColon(s string) string {
    56  	if len(s) > 1 && s[len(s)-1] == ':' {
    57  		s = s[:len(s)-1]
    58  	}
    59  	return s
    60  }