Debian Codegraph @

debian/vendor/github.com/micro-editor/tcell/v2/terminfo/dynamic/dynamic.go

raw
1 // Copyright 2019 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use file except in compliance with the License.
5 // You may obtain a copy of the license at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // The dynamic package is used to generate a terminal description dynamically,
16 // using infocmp. This is really a method of last resort, as the performance
17 // will be slow, and it requires a working infocmp. But, the hope is that it
18 // will assist folks who have to deal with a terminal description that isn't
19 // already built in. This requires infocmp to be in the user's path, and to
20 // support reasonably the -1 option.
21
22 package dynamic
23
24 import (
25 "bytes"
26 "errors"
27 "os"
28 "os/exec"
29 "regexp"
30 "strconv"
31 "strings"
32
33 "github.com/micro-editor/tcell/v2/terminfo"
34 )
35
36 type termcap struct {
37 name string
38 desc string
39 aliases []string
40 bools map[string]bool
41 nums map[string]int
42 strs map[string]string
43 }
44
45 func (tc *termcap) getnum(s string) int {
46 return (tc.nums[s])
47 }
48
49 func (tc *termcap) getflag(s string) bool {
50 return (tc.bools[s])
51 }
52
53 func (tc *termcap) getstr(s string) string {
54 return (tc.strs[s])
55 }
56
57 const (
58 none = iota
59 control
60 escaped
61 )
62
63 var errNotAddressable = errors.New("terminal not cursor addressable")
64
65 func unescape(s string) string {
66 // Various escapes are in \x format. Control codes are
67 // encoded as ^M (carat followed by ASCII equivalent).
68 // escapes are: \e, \E - escape
69 // \0 NULL, \n \l \r \t \b \f \s for equivalent C escape.
70 buf := &bytes.Buffer{}
71 esc := none
72
73 for i := 0; i < len(s); i++ {
74 c := s[i]
75 switch esc {
76 case none:
77 switch c {
78 case '\\':
79 esc = escaped
80 case '^':
81 esc = control
82 default:
83 buf.WriteByte(c)
84 }
85 case control:
86 buf.WriteByte(c ^ 1<<6)
87 esc = none
88 case escaped:
89 switch c {
90 case 'E', 'e':
91 buf.WriteByte(0x1b)
92 case '0', '1', '2', '3', '4', '5', '6', '7':
93 if i+2 < len(s) && s[i+1] >= '0' && s[i+1] <= '7' && s[i+2] >= '0' && s[i+2] <= '7' {
94 buf.WriteByte(((c - '0') * 64) + ((s[i+1] - '0') * 8) + (s[i+2] - '0'))
95 i = i + 2
96 } else if c == '0' {
97 buf.WriteByte(0)
98 }
99 case 'n':
100 buf.WriteByte('\n')
101 case 'r':
102 buf.WriteByte('\r')
103 case 't':
104 buf.WriteByte('\t')
105 case 'b':
106 buf.WriteByte('\b')
107 case 'f':
108 buf.WriteByte('\f')
109 case 's':
110 buf.WriteByte(' ')
111 default:
112 buf.WriteByte(c)
113 }
114 esc = none
115 }
116 }
117 return (buf.String())
118 }
119
120 func (tc *termcap) setupterm(name string) error {
121 cmd := exec.Command("infocmp", "-1", name)
122 output := &bytes.Buffer{}
123 cmd.Stdout = output
124
125 tc.strs = make(map[string]string)
126 tc.bools = make(map[string]bool)
127 tc.nums = make(map[string]int)
128
129 if err := cmd.Run(); err != nil {
130 return err
131 }
132
133 // Now parse the output.
134 // We get comment lines (starting with "#"), followed by
135 // a header line that looks like "<name>|<alias>|...|<desc>"
136 // then capabilities, one per line, starting with a tab and ending
137 // with a comma and newline.
138 lines := strings.Split(output.String(), "\n")
139 for len(lines) > 0 && strings.HasPrefix(lines[0], "#") {
140 lines = lines[1:]
141 }
142
143 // Ditch trailing empty last line
144 if lines[len(lines)-1] == "" {
145 lines = lines[:len(lines)-1]
146 }
147 header := lines[0]
148 if strings.HasSuffix(header, ",") {
149 header = header[:len(header)-1]
150 }
151 names := strings.Split(header, "|")
152 tc.name = names[0]
153 names = names[1:]
154 if len(names) > 0 {
155 tc.desc = names[len(names)-1]
156 names = names[:len(names)-1]
157 }
158 tc.aliases = names
159 for _, val := range lines[1:] {
160 if (!strings.HasPrefix(val, "\t")) ||
161 (!strings.HasSuffix(val, ",")) {
162 return (errors.New("malformed infocmp: " + val))
163 }
164
165 val = val[1:]
166 val = val[:len(val)-1]
167
168 if k := strings.SplitN(val, "=", 2); len(k) == 2 {
169 tc.strs[k[0]] = unescape(k[1])
170 } else if k := strings.SplitN(val, "#", 2); len(k) == 2 {
171 u, err := strconv.ParseUint(k[1], 0, 0)
172 if err != nil {
173 return (err)
174 }
175 tc.nums[k[0]] = int(u)
176 } else {
177 tc.bools[val] = true
178 }
179 }
180 return nil
181 }
182
183 // LoadTerminfo creates a Terminfo by for named terminal by attempting to parse
184 // the output from infocmp. This returns the terminfo entry, a description of
185 // the terminal, and either nil or an error.
186 func LoadTerminfo(name string) (*terminfo.Terminfo, string, error) {
187 var tc termcap
188 if err := tc.setupterm(name); err != nil {
189 if err != nil {
190 return nil, "", err
191 }
192 }
193 t := &terminfo.Terminfo{}
194 // If this is an alias record, then just emit the alias
195 t.Name = tc.name
196 if t.Name != name {
197 return t, "", nil
198 }
199 t.Modifiers = terminfo.ModifiersDynamic
200 t.Aliases = tc.aliases
201 t.Colors = tc.getnum("colors")
202 t.Columns = tc.getnum("cols")
203 t.Lines = tc.getnum("lines")
204 t.Bell = tc.getstr("bel")
205 t.Clear = tc.getstr("clear")
206 t.EnterCA = tc.getstr("smcup")
207 t.ExitCA = tc.getstr("rmcup")
208 t.ShowCursor = tc.getstr("cnorm")
209 t.HideCursor = tc.getstr("civis")
210 t.AttrOff = tc.getstr("sgr0")
211 t.Underline = tc.getstr("smul")
212 t.Bold = tc.getstr("bold")
213 t.Blink = tc.getstr("blink")
214 t.Dim = tc.getstr("dim")
215 t.Italic = tc.getstr("sitm")
216 t.Reverse = tc.getstr("rev")
217 t.EnterKeypad = tc.getstr("smkx")
218 t.ExitKeypad = tc.getstr("rmkx")
219 t.SetFg = tc.getstr("setaf")
220 t.SetBg = tc.getstr("setab")
221 t.SetCursor = tc.getstr("cup")
222 t.CursorBack1 = tc.getstr("cub1")
223 t.CursorUp1 = tc.getstr("cuu1")
224 t.KeyF1 = tc.getstr("kf1")
225 t.KeyF2 = tc.getstr("kf2")
226 t.KeyF3 = tc.getstr("kf3")
227 t.KeyF4 = tc.getstr("kf4")
228 t.KeyF5 = tc.getstr("kf5")
229 t.KeyF6 = tc.getstr("kf6")
230 t.KeyF7 = tc.getstr("kf7")
231 t.KeyF8 = tc.getstr("kf8")
232 t.KeyF9 = tc.getstr("kf9")
233 t.KeyF10 = tc.getstr("kf10")
234 t.KeyF11 = tc.getstr("kf11")
235 t.KeyF12 = tc.getstr("kf12")
236 t.KeyF13 = tc.getstr("kf13")
237 t.KeyF14 = tc.getstr("kf14")
238 t.KeyF15 = tc.getstr("kf15")
239 t.KeyF16 = tc.getstr("kf16")
240 t.KeyF17 = tc.getstr("kf17")
241 t.KeyF18 = tc.getstr("kf18")
242 t.KeyF19 = tc.getstr("kf19")
243 t.KeyF20 = tc.getstr("kf20")
244 t.KeyF21 = tc.getstr("kf21")
245 t.KeyF22 = tc.getstr("kf22")
246 t.KeyF23 = tc.getstr("kf23")
247 t.KeyF24 = tc.getstr("kf24")
248 t.KeyF25 = tc.getstr("kf25")
249 t.KeyF26 = tc.getstr("kf26")
250 t.KeyF27 = tc.getstr("kf27")
251 t.KeyF28 = tc.getstr("kf28")
252 t.KeyF29 = tc.getstr("kf29")
253 t.KeyF30 = tc.getstr("kf30")
254 t.KeyF31 = tc.getstr("kf31")
255 t.KeyF32 = tc.getstr("kf32")
256 t.KeyF33 = tc.getstr("kf33")
257 t.KeyF34 = tc.getstr("kf34")
258 t.KeyF35 = tc.getstr("kf35")
259 t.KeyF36 = tc.getstr("kf36")
260 t.KeyF37 = tc.getstr("kf37")
261 t.KeyF38 = tc.getstr("kf38")
262 t.KeyF39 = tc.getstr("kf39")
263 t.KeyF40 = tc.getstr("kf40")
264 t.KeyF41 = tc.getstr("kf41")
265 t.KeyF42 = tc.getstr("kf42")
266 t.KeyF43 = tc.getstr("kf43")
267 t.KeyF44 = tc.getstr("kf44")
268 t.KeyF45 = tc.getstr("kf45")
269 t.KeyF46 = tc.getstr("kf46")
270 t.KeyF47 = tc.getstr("kf47")
271 t.KeyF48 = tc.getstr("kf48")
272 t.KeyF49 = tc.getstr("kf49")
273 t.KeyF50 = tc.getstr("kf50")
274 t.KeyF51 = tc.getstr("kf51")
275 t.KeyF52 = tc.getstr("kf52")
276 t.KeyF53 = tc.getstr("kf53")
277 t.KeyF54 = tc.getstr("kf54")
278 t.KeyF55 = tc.getstr("kf55")
279 t.KeyF56 = tc.getstr("kf56")
280 t.KeyF57 = tc.getstr("kf57")
281 t.KeyF58 = tc.getstr("kf58")
282 t.KeyF59 = tc.getstr("kf59")
283 t.KeyF60 = tc.getstr("kf60")
284 t.KeyF61 = tc.getstr("kf61")
285 t.KeyF62 = tc.getstr("kf62")
286 t.KeyF63 = tc.getstr("kf63")
287 t.KeyF64 = tc.getstr("kf64")
288 t.KeyInsert = tc.getstr("kich1")
289 t.KeyDelete = tc.getstr("kdch1")
290 t.KeyBackspace = tc.getstr("kbs")
291 t.KeyHome = tc.getstr("khome")
292 t.KeyEnd = tc.getstr("kend")
293 t.KeyUp = tc.getstr("kcuu1")
294 t.KeyDown = tc.getstr("kcud1")
295 t.KeyRight = tc.getstr("kcuf1")
296 t.KeyLeft = tc.getstr("kcub1")
297 t.KeyPgDn = tc.getstr("knp")
298 t.KeyPgUp = tc.getstr("kpp")
299 t.KeyBacktab = tc.getstr("kcbt")
300 t.KeyExit = tc.getstr("kext")
301 t.KeyCancel = tc.getstr("kcan")
302 t.KeyPrint = tc.getstr("kprt")
303 t.KeyHelp = tc.getstr("khlp")
304 t.KeyClear = tc.getstr("kclr")
305 t.AltChars = tc.getstr("acsc")
306 t.EnterAcs = tc.getstr("smacs")
307 t.ExitAcs = tc.getstr("rmacs")
308 t.EnableAcs = tc.getstr("enacs")
309 t.Mouse = tc.getstr("kmous")
310 t.KeyShfRight = tc.getstr("kRIT")
311 t.KeyShfLeft = tc.getstr("kLFT")
312 t.KeyShfHome = tc.getstr("kHOM")
313 t.KeyShfEnd = tc.getstr("kEND")
314
315 // Terminfo lacks descriptions for a bunch of modified keys,
316 // but modern XTerm and emulators often have them. Let's add them,
317 // if the shifted right and left arrows are defined.
318 if t.KeyShfRight == "\x1b[1;2C" && t.KeyShfLeft == "\x1b[1;2D" {
319 t.KeyShfUp = "\x1b[1;2A"
320 t.KeyShfDown = "\x1b[1;2B"
321 t.KeyMetaUp = "\x1b[1;9A"
322 t.KeyMetaDown = "\x1b[1;9B"
323 t.KeyMetaRight = "\x1b[1;9C"
324 t.KeyMetaLeft = "\x1b[1;9D"
325 t.KeyAltUp = "\x1b[1;3A"
326 t.KeyAltDown = "\x1b[1;3B"
327 t.KeyAltRight = "\x1b[1;3C"
328 t.KeyAltLeft = "\x1b[1;3D"
329 t.KeyCtrlUp = "\x1b[1;5A"
330 t.KeyCtrlDown = "\x1b[1;5B"
331 t.KeyCtrlRight = "\x1b[1;5C"
332 t.KeyCtrlLeft = "\x1b[1;5D"
333 t.KeyAltShfUp = "\x1b[1;4A"
334 t.KeyAltShfDown = "\x1b[1;4B"
335 t.KeyAltShfRight = "\x1b[1;4C"
336 t.KeyAltShfLeft = "\x1b[1;4D"
337
338 t.KeyMetaShfUp = "\x1b[1;10A"
339 t.KeyMetaShfDown = "\x1b[1;10B"
340 t.KeyMetaShfRight = "\x1b[1;10C"
341 t.KeyMetaShfLeft = "\x1b[1;10D"
342
343 t.KeyCtrlShfUp = "\x1b[1;6A"
344 t.KeyCtrlShfDown = "\x1b[1;6B"
345 t.KeyCtrlShfRight = "\x1b[1;6C"
346 t.KeyCtrlShfLeft = "\x1b[1;6D"
347
348 t.KeyShfPgUp = "\x1b[5;2~"
349 t.KeyShfPgDn = "\x1b[6;2~"
350 }
351 // And also for Home and End
352 if t.KeyShfHome == "\x1b[1;2H" && t.KeyShfEnd == "\x1b[1;2F" {
353 t.KeyCtrlHome = "\x1b[1;5H"
354 t.KeyCtrlEnd = "\x1b[1;5F"
355 t.KeyAltHome = "\x1b[1;9H"
356 t.KeyAltEnd = "\x1b[1;9F"
357 t.KeyCtrlShfHome = "\x1b[1;6H"
358 t.KeyCtrlShfEnd = "\x1b[1;6F"
359 t.KeyAltShfHome = "\x1b[1;4H"
360 t.KeyAltShfEnd = "\x1b[1;4F"
361 t.KeyMetaShfHome = "\x1b[1;10H"
362 t.KeyMetaShfEnd = "\x1b[1;10F"
363
364 }
365
366 // If the terminal has all the 'standard' Xterm defines, use ModifiersXTerm to reconstruct
367 // ALL XTerm sequences dynamically
368 if t.KeyShfHome == "\x1b[1;2H" && t.KeyShfEnd == "\x1b[1;2F" && t.KeyShfRight == "\x1b[1;2C" && t.KeyShfLeft == "\x1b[1;2D" {
369 t.Modifiers = terminfo.ModifiersXTerm
370 }
371
372 // And the same thing for rxvt and workalikes (Eterm, aterm, etc.)
373 // It seems that urxvt at least send escaped as ALT prefix for these,
374 // although some places seem to indicate a separate ALT key sesquence.
375 if t.KeyShfRight == "\x1b[c" && t.KeyShfLeft == "\x1b[d" {
376 t.KeyShfUp = "\x1b[a"
377 t.KeyShfDown = "\x1b[b"
378 t.KeyCtrlUp = "\x1b[Oa"
379 t.KeyCtrlDown = "\x1b[Ob"
380 t.KeyCtrlRight = "\x1b[Oc"
381 t.KeyCtrlLeft = "\x1b[Od"
382 }
383 if t.KeyShfHome == "\x1b[7$" && t.KeyShfEnd == "\x1b[8$" {
384 t.KeyCtrlHome = "\x1b[7^"
385 t.KeyCtrlEnd = "\x1b[8^"
386 }
387
388 // If the kmous entry is present, then we need to record the
389 // the codes to enter and exit mouse mode. Sadly, this is not
390 // part of the terminfo databases anywhere that I've found, but
391 // is an extension. The escapedape codes are documented in the XTerm
392 // manual, and all terminals that have kmous are expected to
393 // use these same codes, unless explicitly configured otherwise
394 // vi XM. Note that in any event, we only known how to parse either
395 // x11 or SGR mouse events -- if your terminal doesn't support one
396 // of these two forms, you maybe out of luck.
397 t.MouseMode = tc.getstr("XM")
398 if t.Mouse != "" && t.MouseMode == "" {
399 // we anticipate that all xterm mouse tracking compatible
400 // terminals understand mouse tracking (1000), but we hope
401 // that those that don't understand button-event tracking (1002)
402 // will at least ignore it. Likewise we hope that terminals
403 // that don't understand SGR reporting (1006) just ignore it.
404 t.MouseMode = "%?%p1%{1}%=%t%'h'%Pa%e%'l'%Pa%;" +
405 "\x1b[?1000%ga%c\x1b[?1002%ga%c\x1b[?1006%ga%c"
406 }
407
408 // We only support colors in ANSI 8 or 256 color mode.
409 if t.Colors < 8 || t.SetFg == "" {
410 t.Colors = 0
411 }
412 if t.SetCursor == "" {
413 return nil, "", errNotAddressable
414 }
415
416 // For padding, we lookup the pad char. If that isn't present,
417 // and npc is *not* set, then we assume a null byte.
418 t.PadChar = tc.getstr("pad")
419 if t.PadChar == "" {
420 if !tc.getflag("npc") {
421 t.PadChar = "\u0000"
422 }
423 }
424
425 // For terminals that use "standard" SGR sequences, lets combine the
426 // foreground and background together.
427 if strings.HasPrefix(t.SetFg, "\x1b[") &&
428 strings.HasPrefix(t.SetBg, "\x1b[") &&
429 strings.HasSuffix(t.SetFg, "m") &&
430 strings.HasSuffix(t.SetBg, "m") {
431 fg := t.SetFg[:len(t.SetFg)-1]
432 r := regexp.MustCompile("%p1")
433 bg := r.ReplaceAllString(t.SetBg[2:], "%p2")
434 t.SetFgBg = fg + ";" + bg
435 }
436
437 addtruecolor := false
438 switch os.Getenv("COLORTERM") {
439 case "truecolor", "24bit", "24-bit":
440 addtruecolor = true
441 }
442 switch os.Getenv("TCELL_TRUECOLOR") {
443 case "":
444 case "disable":
445 addtruecolor = false
446 default:
447 addtruecolor = true
448 }
449
450 // If the user has requested 24-bit color with $COLORTERM, then
451 // amend the value (unless already present). This means we don't
452 // need to have a value present.
453 if addtruecolor &&
454 t.SetFgBgRGB == "" &&
455 t.SetFgRGB == "" &&
456 t.SetBgRGB == "" {
457
458 // Supply vanilla ISO 8613-6:1994 24-bit color sequences.
459 t.SetFgRGB = "\x1b[38;2;%p1%d;%p2%d;%p3%dm"
460 t.SetBgRGB = "\x1b[48;2;%p1%d;%p2%d;%p3%dm"
461 t.SetFgBgRGB = "\x1b[38;2;%p1%d;%p2%d;%p3%d;" +
462 "48;2;%p4%d;%p5%d;%p6%dm"
463 }
464
465 return t, tc.desc, nil
466 }