-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
58 lines (45 loc) · 800 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package prompt
import (
"github.com/rivo/uniseg"
"strings"
)
func min(a, b int) int {
if a > b {
return b
}
return a
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func abs(value int) int {
if value < 0 {
return -value
}
return value
}
func wrapString(toWrap string, width int) []string {
if len(toWrap) == 0 {
return []string{""}
}
wrapped := make([]string, 0, (len(toWrap)/width)+1)
gc := uniseg.NewGraphemes(toWrap)
current := strings.Builder{}
currentLength := 0
for gc.Next() {
current.WriteString(gc.Str())
currentLength++
if currentLength == width {
wrapped = append(wrapped, current.String())
current.Reset()
currentLength = 0
}
}
if currentLength > 0 {
wrapped = append(wrapped, current.String())
}
return wrapped
}