1package util23import (4 "math"5 "os"6 "strconv"7 "strings"8 "time"910 "github.com/mattn/go-isatty"11 "github.com/rivo/uniseg"12)1314// StringWidth returns string width where each CR/LF character takes 1 column15func StringWidth(s string) int {16 return uniseg.StringWidth(s) + strings.Count(s, "\n") + strings.Count(s, "\r")17}1819// RunesWidth returns runes width20func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {21 width := 022 gr := uniseg.NewGraphemes(string(runes))23 idx := 024 for gr.Next() {25 rs := gr.Runes()26 var w int27 if len(rs) == 1 && rs[0] == '\t' {28 w = tabstop - (prefixWidth+width)%tabstop29 } else {30 w = StringWidth(string(rs))31 }32 width += w33 if width > limit {34 return width, idx35 }36 idx += len(rs)37 }38 return width, -139}4041// Truncate returns the truncated runes and its width42func Truncate(input string, limit int) ([]rune, int) {43 runes := []rune{}44 width := 045 gr := uniseg.NewGraphemes(input)46 for gr.Next() {47 rs := gr.Runes()48 w := StringWidth(string(rs))49 if width+w > limit {50 return runes, width51 }52 width += w53 runes = append(runes, rs...)54 }55 return runes, width56}5758// Max returns the largest integer59func Max(first int, second int) int {60 if first >= second {61 return first62 }63 return second64}6566// Max16 returns the largest integer67func Max16(first int16, second int16) int16 {68 if first >= second {69 return first70 }71 return second72}7374// Max32 returns the largest 32-bit integer75func Max32(first int32, second int32) int32 {76 if first > second {77 return first78 }79 return second80}8182// Min returns the smallest integer83func Min(first int, second int) int {84 if first <= second {85 return first86 }87 return second88}8990// Min32 returns the smallest 32-bit integer91func Min32(first int32, second int32) int32 {92 if first <= second {93 return first94 }95 return second96}9798// Constrain32 limits the given 32-bit integer with the upper and lower bounds99func Constrain32(val int32, min int32, max int32) int32 {100 if val < min {101 return min102 }103 if val > max {104 return max105 }106 return val107}108109// Constrain limits the given integer with the upper and lower bounds110func Constrain(val int, min int, max int) int {111 if val < min {112 return min113 }114 if val > max {115 return max116 }117 return val118}119120func AsUint16(val int) uint16 {121 if val > math.MaxUint16 {122 return math.MaxUint16123 } else if val < 0 {124 return 0125 }126 return uint16(val)127}128129// DurWithin limits the given time.Duration with the upper and lower bounds130func DurWithin(131 val time.Duration, min time.Duration, max time.Duration) time.Duration {132 if val < min {133 return min134 }135 if val > max {136 return max137 }138 return val139}140141// IsTty returns true if the file is a terminal142func IsTty(file *os.File) bool {143 fd := file.Fd()144 return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)145}146147// RunOnce runs the given function only once148func RunOnce(f func()) func() {149 once := Once(true)150 return func() {151 if once() {152 f()153 }154 }155}156157// Once returns a function that returns the specified boolean value only once158func Once(nextResponse bool) func() bool {159 state := nextResponse160 return func() bool {161 prevState := state162 state = !nextResponse163 return prevState164 }165}166167// RepeatToFill repeats the given string to fill the given width168func RepeatToFill(str string, length int, limit int) string {169 times := limit / length170 rest := limit % length171 output := strings.Repeat(str, times)172 if rest > 0 {173 for _, r := range str {174 rest -= uniseg.StringWidth(string(r))175 if rest < 0 {176 break177 }178 output += string(r)179 if rest == 0 {180 break181 }182 }183 }184 return output185}186187// ToKebabCase converts the given CamelCase string to kebab-case188func ToKebabCase(s string) string {189 name := ""190 for i, r := range s {191 if i > 0 && r >= 'A' && r <= 'Z' {192 name += "-"193 }194 name += string(r)195 }196 return strings.ToLower(name)197}198199// CompareVersions compares two version strings200func CompareVersions(v1, v2 string) int {201 parts1 := strings.Split(v1, ".")202 parts2 := strings.Split(v2, ".")203204 atoi := func(s string) int {205 n, e := strconv.Atoi(s)206 if e != nil {207 return 0208 }209 return n210 }211212 for i := 0; i < Max(len(parts1), len(parts2)); i++ {213 var p1, p2 int214 if i < len(parts1) {215 p1 = atoi(parts1[i])216 }217 if i < len(parts2) {218 p2 = atoi(parts2[i])219 }220221 if p1 > p2 {222 return 1223 } else if p1 < p2 {224 return -1225 }226 }227 return 0228}