-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
102 lines (77 loc) · 1.58 KB
/
text.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
package codeview
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
"image/color"
"strings"
)
type TextEntry struct {
Text string
Color int
}
type Text struct {
Lines [][]TextEntry
}
func NewText(text string, lineParser func(string) []TextEntry) Text {
lines, more := _splitText(text)
t := Text{
Lines: make([][]TextEntry, len(lines)),
}
for i, line := range lines {
parsed := lineParser(line)
cleaned := make([]TextEntry, 0)
length := 0
for _, entry := range parsed {
l := len(entry.Text)
if length+l > 50 {
l = 50 - length
entry.Text = entry.Text[:l]
cleaned = append(cleaned, entry)
break
}
cleaned = append(cleaned, entry)
length += l
}
t.Lines[i] = cleaned
}
if more > 0 {
t.Lines = append(t.Lines, []TextEntry{
{
Text: "... " + _fmtNumber(more) + " more",
Color: ColorComment,
},
})
}
return t
}
func (e TextEntry) GetColor(theme Theme) color.RGBA {
switch e.Color {
case ColorDefault:
return theme.Default
case ColorToken:
return theme.Token
case ColorValue:
return theme.Value
case ColorString:
return theme.String
case ColorComment:
return theme.Comment
default:
return theme.Default
}
}
func _splitText(text string) ([]string, int) {
text = strings.ReplaceAll(text, "\t", " ")
text = strings.ReplaceAll(text, "\r\n", "\n")
lines := strings.Split(text, "\n")
more := 0
if len(lines) > 14 {
more = len(lines) - 13
lines = lines[:13]
}
return lines, more
}
func _fmtNumber(n int) string {
p := message.NewPrinter(language.English)
return p.Sprintf("%d", n)
}