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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
package tui
import (
"strings"
"unicode"
)
type Cell struct {
Text string
Style Style
Width uint8
Link string
}
func NewCell(r rune, style Style) Cell {
return Cell{
Text: string(r),
Style: style,
Width: uint8(RuneWidth(r)),
}
}
func NewCellWithWidth(r rune, style Style, width uint8) Cell {
text := ""
if width != 0 && r != 0 {
text = string(r)
}
return Cell{
Text: text,
Style: style,
Width: width,
}
}
func newClusterCell(text string, width uint8, style Style, link string) Cell {
if width != 0 && len(text) > 0 {
text = strings.Clone(text)
} else if width == 0 {
text = ""
}
return Cell{
Text: text,
Style: style,
Width: width,
Link: link,
}
}
func (c Cell) IsContinuation() bool {
return c.Width == 0
}
func (c Cell) Equal(other Cell) bool {
return c.Text == other.Text && c.Style.Equal(other.Style) && c.Width == other.Width && c.Link == other.Link
}
func (c Cell) IsEmpty() bool {
if c.Text == "" {
return true
}
if c.Text == " " {
return c.Style.Equal(NewStyle())
}
return false
}
func RuneWidth(r rune) int {
if r < 0 || r > unicode.MaxRune {
return 1
}
if r < 0x20 || (r >= 0x7F && r < 0xA0) {
return 1
}
if isZeroWidthRune(r) {
return 1
}
if inRuneRanges(r, eastAsianWideRanges) || inRuneRanges(r, emojiWideRanges) {
return 2
}
return 1
}
type runeRange struct {
min rune
max rune
}
var eastAsianWideRanges = []runeRange{
{min: 0x1100, max: 0x115F},
{min: 0x2329, max: 0x232A},
{min: 0x2E80, max: 0x303E},
{min: 0x3040, max: 0xA4CF},
{min: 0xAC00, max: 0xD7A3},
{min: 0xF900, max: 0xFAFF},
{min: 0xFE10, max: 0xFE19},
{min: 0xFE30, max: 0xFE6F},
{min: 0xFF00, max: 0xFF60},
{min: 0xFFE0, max: 0xFFE6},
{min: 0x1B000, max: 0x1B12F},
{min: 0x1B130, max: 0x1B167},
{min: 0x20000, max: 0x2FFFD},
{min: 0x30000, max: 0x3FFFD},
}
var emojiWideRanges = []runeRange{
{min: 0x1F004, max: 0x1F004},
{min: 0x1F0CF, max: 0x1F0CF},
{min: 0x1F18E, max: 0x1F18E},
{min: 0x1F191, max: 0x1F19A},
{min: 0x1F1E6, max: 0x1F1FF},
{min: 0x1F201, max: 0x1F202},
{min: 0x1F21A, max: 0x1F21A},
{min: 0x1F22F, max: 0x1F22F},
{min: 0x1F232, max: 0x1F23A},
{min: 0x1F250, max: 0x1F251},
{min: 0x1F300, max: 0x1F64F},
{min: 0x1F680, max: 0x1F6FF},
{min: 0x1F7E0, max: 0x1F7EB},
{min: 0x1F900, max: 0x1F9FF},
{min: 0x1FA70, max: 0x1FAFF},
}
func isZeroWidthRune(r rune) bool {
return unicode.In(r, unicode.Mn, unicode.Me, unicode.Cf, unicode.Variation_Selector, unicode.Join_Control)
}
func inRuneRanges(r rune, ranges []runeRange) bool {
for _, rr := range ranges {
if r >= rr.min && r <= rr.max {
return true
}
}
return false
}
|