Debian Codegraph @

src/util/util.go

raw
1 package util
2
3 import (
4 "cmp"
5 "math"
6 "os"
7 "strconv"
8 "strings"
9
10 "github.com/mattn/go-isatty"
11 "github.com/rivo/uniseg"
12 )
13
14 // StringWidth returns string width where each CR/LF character takes 1 column
15 func StringWidth(s string) int {
16 return uniseg.StringWidth(s) + strings.Count(s, "\n") + strings.Count(s, "\r")
17 }
18
19 // RunesWidth returns runes width
20 func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {
21 return StringsWidth(string(runes), prefixWidth, tabstop, limit)
22 }
23
24 // StringsWidth returns the width of the string
25 func StringsWidth(str string, prefixWidth int, tabstop int, limit int) (int, int) {
26 width := 0
27 gr := uniseg.NewGraphemes(str)
28 idx := 0
29 for gr.Next() {
30 rs := gr.Runes()
31 var w int
32 if len(rs) == 1 && rs[0] == '\t' {
33 w = tabstop - (prefixWidth+width)%tabstop
34 } else {
35 w = StringWidth(string(rs))
36 }
37 width += w
38 if width > limit {
39 return width, idx
40 }
41 idx += len(rs)
42 }
43 return width, -1
44 }
45
46 // Truncate returns the truncated runes and its width
47 func Truncate(input string, limit int) ([]rune, int) {
48 runes := []rune{}
49 width := 0
50 gr := uniseg.NewGraphemes(input)
51 for gr.Next() {
52 rs := gr.Runes()
53 w := StringWidth(string(rs))
54 if width+w > limit {
55 return runes, width
56 }
57 width += w
58 runes = append(runes, rs...)
59 }
60 return runes, width
61 }
62
63 func Constrain[T cmp.Ordered](val, minimum, maximum T) T {
64 return max(min(val, maximum), minimum)
65 }
66
67 func AsUint16(val int) uint16 {
68 if val > math.MaxUint16 {
69 return math.MaxUint16
70 } else if val < 0 {
71 return 0
72 }
73 return uint16(val)
74 }
75
76 // IsTty returns true if the file is a terminal
77 func IsTty(file *os.File) bool {
78 fd := file.Fd()
79 return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
80 }
81
82 // RunOnce runs the given function only once
83 func RunOnce(f func()) func() {
84 once := Once(true)
85 return func() {
86 if once() {
87 f()
88 }
89 }
90 }
91
92 // Once returns a function that returns the specified boolean value only once
93 func Once(nextResponse bool) func() bool {
94 state := nextResponse
95 return func() bool {
96 prevState := state
97 state = !nextResponse
98 return prevState
99 }
100 }
101
102 // RepeatToFill repeats the given string to fill the given width
103 func RepeatToFill(str string, length int, limit int) string {
104 times := limit / length
105 rest := limit % length
106 output := strings.Repeat(str, times)
107 if rest > 0 {
108 for _, r := range str {
109 rest -= uniseg.StringWidth(string(r))
110 if rest < 0 {
111 break
112 }
113 output += string(r)
114 if rest == 0 {
115 break
116 }
117 }
118 }
119 return output
120 }
121
122 // ToKebabCase converts the given CamelCase string to kebab-case
123 func ToKebabCase(s string) string {
124 name := ""
125 for i, r := range s {
126 if i > 0 && r >= 'A' && r <= 'Z' {
127 name += "-"
128 }
129 name += string(r)
130 }
131 return strings.ToLower(name)
132 }
133
134 // CompareVersions compares two version strings
135 func CompareVersions(v1, v2 string) int {
136 parts1 := strings.Split(v1, ".")
137 parts2 := strings.Split(v2, ".")
138
139 atoi := func(s string) int {
140 n, e := strconv.Atoi(s)
141 if e != nil {
142 return 0
143 }
144 return n
145 }
146
147 for i := 0; i < max(len(parts1), len(parts2)); i++ {
148 var p1, p2 int
149 if i < len(parts1) {
150 p1 = atoi(parts1[i])
151 }
152 if i < len(parts2) {
153 p2 = atoi(parts2[i])
154 }
155
156 if p1 > p2 {
157 return 1
158 } else if p1 < p2 {
159 return -1
160 }
161 }
162 return 0
163 }