-
Notifications
You must be signed in to change notification settings - Fork 16
/
man.go
178 lines (167 loc) · 6.09 KB
/
man.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
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
package main
import (
"strings"
"unicode"
"github.com/xyproto/vt100"
)
func handleManPageEscape(s string) string {
var lineRunes []rune
for _, r := range s {
if r == 0x8 {
// Encountered ^H
// Pop the last appended rune and continue
lineRunes = lineRunes[:len(lineRunes)-1]
continue
}
lineRunes = append(lineRunes, r)
}
return string(lineRunes)
}
func (e *Editor) manPageHighlight(line, programName string, firstLine, lastLine bool) string {
line = handleManPageEscape(line)
var (
normal = e.Foreground
off = vt100.Stop()
trimmedLine = strings.TrimSpace(line)
hasWords = HasWords(trimmedLine)
innerSpaceCount = strings.Count(trimmedLine, " ")
)
if strings.Count(trimmedLine, " ") > 10 && (firstLine || lastLine) { // first and last line
return e.CommentColor.Get(line)
} else if strings.ToUpper(trimmedLine) == trimmedLine && !strings.HasPrefix(trimmedLine, "-") && hasWords && !strings.HasPrefix(line, " ") { // a sub-section header
return e.ManSectionColor.Get(line)
} else if strings.HasPrefix(trimmedLine, "-") { // a flag or parameter
var rs []rune
rs = append(rs, []rune(e.MarkdownTextColor.String())...)
inFlag := false
spaceCount := 0
foundLetter := false
prevR := ' '
for _, r := range line {
if strings.HasPrefix(trimmedLine, "-") && strings.Count(line, "-") >= 1 && strings.Count(trimmedLine, " ") <= 1 {
// One or two command line options, color them differently
return e.MenuArrowColor.Get(line)
}
if !foundLetter && (unicode.IsLetter(r) || r == '_') {
foundLetter = true
}
if r == ' ' {
spaceCount++
if innerSpaceCount > 8 {
inFlag = false
}
} else {
spaceCount = 0
}
if r != ' ' && (prevR == ' ' || prevR == '-') && (r == '-' || r == '[' || r == '_') && (prevR == '-' || !inFlag) {
inFlag = true
rs = append(rs, []rune(off+e.MenuArrowColor.String())...)
rs = append(rs, r)
} else if (prevR == ' ' || prevR == '-') && (r == '-' || r == '[' || r == ']' || r == '_') && inFlag {
rs = append(rs, r)
} else if inFlag { // Color the rest of the flag text in the textColor color (LightBlue)
inFlag = false
rs = append(rs, []rune(off+e.MarkdownTextColor.String())...)
rs = append(rs, r)
} else if foundLetter && spaceCount > 2 { // Color the rest of the line in the foreground color (LightGreen)
rs = append(rs, []rune(off+normal.String())...)
rs = append(rs, r)
} else if r == ']' { // Color the rest of the line in the comment color (DarkGray)
rs = append(rs, []rune(off+e.CommentColor.String())...)
rs = append(rs, r)
} else {
rs = append(rs, r)
}
prevR = r
}
rs = append(rs, []rune(off)...)
return string(rs)
} else if allUpper(trimmedLine) {
return e.MarkdownTextColor.Get(line)
} else { // regular text, but highlight numbers (and hex numbers, if the number starts with a digit) + highlight "@"
var (
rs []rune
prevRune rune
inDigits bool
inWord bool
inAngles bool
nextRune rune
)
rs = append(rs, []rune(normal.String())...)
hasAlpha := strings.Contains(trimmedLine, "@")
lineRunes := []rune(line)
for i, r := range line {
if (i + 1) < len(lineRunes) {
nextRune = lineRunes[i+1]
} else {
nextRune = ' '
}
if (unicode.IsLetter(r) || r == '_') && !inWord {
inWord = true
} else if inWord && !unicode.IsLetter(r) && !hexDigit(r) {
inWord = false
}
if !inAngles && r == '<' {
inAngles = true
} else if inAngles && r == '>' {
inAngles = false
}
if !inWord && unicode.IsDigit(r) && !inDigits {
inDigits = true
rs = append(rs, []rune(off+e.ItalicsColor.String())...)
rs = append(rs, r)
} else if inDigits && hexDigit(r) {
rs = append(rs, r)
} else if !inWord && inDigits {
inDigits = false
rs = append(rs, []rune(off+normal.String())...)
rs = append(rs, r)
} else if !inWord && (r == '*' || r == '$' || r == '%' || r == '!' || r == '/' || r == '=' || r == '-') {
rs = append(rs, []rune(off+e.MenuArrowColor.String())...)
rs = append(rs, r)
} else if r == '@' { // color @ gray and the rest of the string white
rs = append(rs, []rune(off+e.CommentColor.String())...)
rs = append(rs, r)
rs = append(rs, []rune(off+e.ItalicsColor.String())...)
} else if hasAlpha && r == '<' { // color < gray and the rest of the string white
rs = append(rs, []rune(off+e.CommentColor.String())...)
rs = append(rs, r)
rs = append(rs, []rune(off+e.ItalicsColor.String())...)
} else if hasAlpha && r == '>' { // color > gray and the rest of the string normal
rs = append(rs, []rune(off+e.CommentColor.String())...)
rs = append(rs, r)
rs = append(rs, []rune(off+normal.String())...)
} else if inAngles || r == '>' {
rs = append(rs, []rune(off+e.ItalicsColor.String())...)
rs = append(rs, r)
} else if inWord && unicode.IsUpper(prevRune) && ((unicode.IsUpper(r) && unicode.IsLetter(nextRune)) || (unicode.IsLower(r) && unicode.IsUpper(prevRune) && !unicode.IsLetter(nextRune))) {
if unicode.IsUpper(r) {
// This is for the leading and trailing letter of uppercase words
rs = append(rs, []rune(off+e.ImageColor.String())...)
} else {
rs = append(rs, []rune(off+e.MarkdownTextColor.String())...)
}
rs = append(rs, r)
} else if inWord && (unicode.IsUpper(r) || (unicode.IsUpper(prevRune) && unicode.IsLetter(r))) {
if !unicode.IsLower(r) && (((unicode.IsUpper(nextRune) || nextRune == ' ') && unicode.IsLetter(prevRune)) || unicode.IsUpper(nextRune) || !unicode.IsLetter(nextRune)) {
// This is for the center letters of uppercase words
rs = append(rs, []rune(off+e.ImageColor.String())...)
} else {
rs = append(rs, []rune(off+e.MarkdownTextColor.String())...)
}
rs = append(rs, r)
} else if inWord && unicode.IsUpper(r) {
rs = append(rs, []rune(off+e.ImageColor.String())...)
rs = append(rs, r)
} else if !inWord || !unicode.IsUpper(r) {
rs = append(rs, []rune(off+e.MarkdownTextColor.String())...)
rs = append(rs, r)
} else {
rs = append(rs, r)
}
prevRune = r
}
rs = append(rs, []rune(off)...)
return string(rs)
}
}