Debian Codegraph @

input/termcap.go

raw
1 package input
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "strings"
7 )
8
9 // CapabilityEvent represents a Termcap/Terminfo response event. Termcap
10 // responses are generated by the terminal in response to RequestTermcap
11 // (XTGETTCAP) requests.
12 //
13 // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
14 type CapabilityEvent string
15
16 func parseTermcap(data []byte) CapabilityEvent {
17 // XTGETTCAP
18 if len(data) == 0 {
19 return CapabilityEvent("")
20 }
21
22 var tc strings.Builder
23 split := bytes.Split(data, []byte{';'})
24 for _, s := range split {
25 parts := bytes.SplitN(s, []byte{'='}, 2)
26 if len(parts) == 0 {
27 return CapabilityEvent("")
28 }
29
30 name, err := hex.DecodeString(string(parts[0]))
31 if err != nil || len(name) == 0 {
32 continue
33 }
34
35 var value []byte
36 if len(parts) > 1 {
37 value, err = hex.DecodeString(string(parts[1]))
38 if err != nil {
39 continue
40 }
41 }
42
43 if tc.Len() > 0 {
44 tc.WriteByte(';')
45 }
46 tc.WriteString(string(name))
47 if len(value) > 0 {
48 tc.WriteByte('=')
49 tc.WriteString(string(value))
50 }
51 }
52
53 return CapabilityEvent(tc.String())
54 }