-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
122 lines (102 loc) · 2.55 KB
/
util.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package tulip
import (
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
func inClipRecion(x, y, x1, y1, x2, y2 int) bool {
return (x >= x1) && (x <= x2) &&
(y >= y1) && (y <= y2)
}
// Returns the number of screen cells ocupied by the string
func EmitStr(x1, y1, x2, y2 int, style tcell.Style, str string) int {
cellX := x1
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
if w == 1 {
if inClipRecion(cellX, y1, x1, y1, x2, y2) {
Screen.SetContent(cellX, y1, c, comb, style)
}
cellX += w
} else { // w == 2
inClip1 := inClipRecion(cellX, y1, x1, y1, x2, y2) // Is 1st half of the rune visible?
inClip2 := inClipRecion(cellX+1, y1, x1, y1, x2, y2) // Is 2nd half of the rune visible?
if inClip1 && inClip2 { // Both rune parts are visible
Screen.SetContent(cellX, y1, c, comb, style)
cellX += w
} else if inClip1 && !inClip2 { // 1st part is visible, 2nd is not
// Just print ' ' instead of the entire double-width rune
comb = []rune{c}
c = ' '
w = 1
Screen.SetContent(cellX, y1, c, comb, style)
cellX += w
} else if !inClip1 && inClip2 { // 1st part isn't visible, 2nd is visible
// Just print ' ' instead of the entire double-width rune
comb = []rune{c}
c = ' '
w = 1
Screen.SetContent(cellX+1, y1, c, comb, style)
cellX += w
} else { // Neither rune part is visible
cellX += w
}
}
}
return cellX - x1
}
func StrCellWidth(s string) int {
len := 0
for _, c := range s {
w := runewidth.RuneWidth(c)
if w == 0 {
w = 1
}
len += w
}
return len
}
func erase(screenX1, screenY1, screenX2, screenY2 int, st tcell.Style, bk rune) {
for x := screenX1; x <= screenX2; x++ {
for y := screenY1; y <= screenY2; y++ {
Screen.SetContent(x, y, bk, nil, st)
}
}
}
// func minInt(a, b int) int {
// if (a < b) {
// return a
// }
// return b
// }
func maxInt(a, b int) int {
if (a > b) {
return a
}
return b
}
// // Checks if i is in [left; right]
// func in (i, left, right int) bool {
// return i >= left && i <= right
// }
// // reverseInt - reverses the slice of ints
// func reverseInt(s []int) {
// for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
// s[i], s[j] = s[j], s[i]
// }
// }
// // splitToGigits - splits the int number into a slice of its digits
// func splitToDigits(n int) []int{
// var _ret []int
// for n !=0 {
// _ret = append(_ret, n % 10)
// n /= 10
// }
// reverseInt(_ret)
// return _ret
// }