-
Notifications
You must be signed in to change notification settings - Fork 12
/
terminfo.go
194 lines (182 loc) · 4.73 KB
/
terminfo.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Package terminfo implements reading terminfo files in pure go.
package terminfo
import (
"errors"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"github.com/nhooyr/terminfo/caps"
)
// Terminfo describes a terminal's capabilities.
type Terminfo struct {
Names []string
Bools [caps.BoolCount]bool
Numbers [caps.NumberCount]int16
Strings [caps.StringCount]string
ExtBools map[string]bool
ExtNumbers map[string]int16
ExtStrings map[string]string
}
// Terminfo cache.
var (
dbMutex sync.RWMutex
db = make(map[string]*Terminfo)
)
// LoadEnv calls Load with the name as $TERM.
func LoadEnv() (*Terminfo, error) {
return Load(os.Getenv("TERM"))
}
// Returned when no name is provided to Load.
var ErrEmptyTerm = errors.New("terminfo: empty term name")
// Load follows the behavior described in terminfo(5) to find correct the terminfo file
// using the name, reads the file and then returns a Terminfo struct that describes the file.
func Load(name string) (ti *Terminfo, err error) {
if name == "" {
return nil, ErrEmptyTerm
}
dbMutex.RLock()
ti, ok := db[name]
dbMutex.RUnlock()
if ok {
return
}
if terminfo := os.Getenv("TERMINFO"); terminfo != "" {
return openDir(terminfo, name)
}
if home := os.Getenv("HOME"); home != "" {
ti, err = openDir(home+"/.terminfo", name)
if err == nil {
return
}
}
if dirs := os.Getenv("TERMINFO_DIRS"); dirs != "" {
for _, dir := range strings.Split(dirs, ":") {
if dir == "" {
dir = "/usr/share/terminfo"
}
ti, err = openDir(dir, name)
if err == nil {
return
}
}
}
for _, dir := range []string{"/etc/terminfo", "/lib/terminfo"} {
ti, err = openDir(dir, name)
if err == nil {
return
}
}
return openDir("/usr/share/terminfo", name)
}
// openDir reads the Terminfo file specified by the dir and name.
func openDir(dir, name string) (*Terminfo, error) {
// Try typical *nix path.
b, err := ioutil.ReadFile(dir + "/" + name[0:1] + "/" + name)
if err != nil {
// Fallback to the darwin specific path.
b, err = ioutil.ReadFile(dir + "/" + strconv.FormatUint(uint64(name[0]), 16) + "/" + name)
if err != nil {
return nil, err
}
}
r := &decoder{buf: b}
if err = r.unmarshal(); err != nil {
return nil, err
}
// Cache the Terminfo struct.
dbMutex.Lock()
for _, n := range r.ti.Names {
db[n] = r.ti
}
dbMutex.Unlock()
return r.ti, nil
}
// Color takes a foreground and background color and returns string
// that sets them for this terminal.
// TODO redo with styles integer
func (ti *Terminfo) Color(fg, bg int) (rv string) {
maxColors := int(ti.Numbers[caps.MaxColors])
// Map bright colors to lower versions if the color table only holds 8.
if maxColors == 8 {
if fg > 7 && fg < 16 {
fg -= 8
}
if bg > 7 && bg < 16 {
bg -= 8
}
}
if maxColors > fg && fg >= 0 {
rv += ti.Parm(caps.SetAForeground, fg)
}
if maxColors > bg && bg >= 0 {
rv += ti.Parm(caps.SetABackground, bg)
}
return
}
// Parm calls the function Parm with the string in ti.Strings at
// i and the variadic arguments.
func (ti *Terminfo) Parm(i int, p ...interface{}) string {
return Parm(ti.Strings[i], p...)
}
// Puts emits the string to the writer, but expands inline padding
// indications (of the form $<[delay]> where [delay] is msec) to
// a suitable number of padding characters (usually null bytes) based
// upon the supplied baud. At high baud rates, more padding characters
// will be inserted.
func (ti *Terminfo) Puts(w io.Writer, s string, lines, baud int) {
for {
start := strings.Index(s, "$<")
if start == -1 {
// Most strings don't need padding, which is good news!
io.WriteString(w, s)
return
}
io.WriteString(w, s[:start])
s = s[start+2:]
end := strings.Index(s, ">")
if end == -1 {
// Unterminated... just emit bytes unadulterated.
io.WriteString(w, "$<"+s)
return
}
val := s[:end]
s = s[end+1:]
var ms int
var dot, mandatory, asterisk bool
unit := 1000
for _, ch := range val {
if ch >= '0' && ch <= '9' {
ms = (ms * 10) + int(ch-'0')
if dot {
unit *= 10
}
} else if ch == '.' && !dot {
dot = true
} else if ch == '*' && !asterisk {
ms *= lines
asterisk = true
} else if ch == '/' {
mandatory = true
} else {
break
}
}
n := ((baud / 8) / unit) * ms
pad := ti.Strings[caps.PadChar]
b := make([]byte, len(pad)*n)
for bp := copy(b, pad); bp < len(b); bp *= 2 {
copy(b[bp:], b[:bp])
}
if (!ti.Bools[caps.XonXoff] && baud > int(ti.Numbers[caps.PaddingBaudRate])) || mandatory {
w.Write(b)
}
}
}
// Goto returns a string suitable for addressing the cursor at the given
// row and column. The origin 0, 0 is in the upper left corner of the screen.
func (ti *Terminfo) Goto(row, col int) string {
return ti.Parm(caps.CursorAddress, row, col)
}