-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_segment.go
More file actions
104 lines (85 loc) · 2.1 KB
/
Copy pathtext_segment.go
File metadata and controls
104 lines (85 loc) · 2.1 KB
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
package fasttui
import (
"unicode/utf8"
"github.com/mattn/go-runewidth"
)
// Configure runewidth to treat ambiguous-width characters as narrow (width 1)
// This matches the behavior of most modern terminals
var runewidthCondition = runewidth.NewCondition()
func init() {
// Set EastAsianWidth to false to treat ambiguous characters (like box drawing
// characters ─│┌┐└┘ and arrows ↑↓←→) as width 1 instead of width 2.
// This matches how most terminals actually render these characters.
runewidthCondition.EastAsianWidth = false
}
type segmentType int
const (
segmentTypeAnsi segmentType = iota
segmentTypeGrapheme
)
type textSegment struct {
segType segmentType
value string
}
type SliceResult struct {
text string
width int
}
func GetSegmenter() any {
return nil
}
func isPureZeroWidthRune(r rune) bool {
return r >= 0x200B && r <= 0x200D || r == 0xFEFF
}
func isPureZeroWidthString(s string) bool {
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
if !isPureZeroWidthRune(r) {
return false
}
i += size
}
return true
}
func isCombiningMark(r rune) bool {
return r >= 0x0300 && r <= 0x036F ||
r >= 0x1AB0 && r <= 0x1AFF ||
r >= 0x1DC0 && r <= 0x1DFF ||
r >= 0x20D0 && r <= 0x20FF ||
r >= 0xFE20 && r <= 0xFE2F
}
// GraphemeWidth calculates the display width of a grapheme cluster
// It handles:
// - Zero-width characters (zero-width joiners, zero-width spaces)
// - Combining marks (counted with their base character)
// - Emoji (typically width 2)
// - East Asian characters (width 2 for fullwidth, 1 for halfwidth)
// - Regular ASCII (width 1)
func GraphemeWidth(s string) int {
if len(s) == 0 {
return 0
}
if isPureZeroWidthString(s) {
return 0
}
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError {
return 0
}
if isCombiningMark(r) {
return 0
}
width := runewidthCondition.RuneWidth(r)
for i := size; i < len(s); {
r, n := utf8.DecodeRuneInString(s[i:])
if isCombiningMark(r) {
i += n
continue
}
if r >= 0xFF00 && r <= 0xFFEF {
width += runewidthCondition.RuneWidth(r)
}
i += n
}
return width
}