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
|
// Application infocmp should have the same output as the standard Unix infocmp
// -1 -L output.
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"unicode"
"github.com/xo/terminfo"
)
var (
flagTerm = flag.String("term", os.Getenv("TERM"), "term name")
flagExtended = flag.Bool("x", false, "extended options")
)
func main() {
flag.Parse()
ti, err := terminfo.Load(*flagTerm)
if err != nil {
log.Fatal(err)
}
fmt.Printf("#\tReconstructed via %s from file: %s\n", strings.TrimPrefix(os.Args[0], "./"), ti.File)
fmt.Printf("%s,\n", strings.TrimSpace(strings.Join(ti.Names, "|")))
process(ti.BoolCaps, ti.ExtBoolCaps, ti.BoolsM, terminfo.BoolCapName, nil)
process(
ti.NumCaps, ti.ExtNumCaps, ti.NumsM, terminfo.NumCapName,
func(v interface{}) string { return fmt.Sprintf("#%d", v) },
)
process(
ti.StringCaps, ti.ExtStringCaps, ti.StringsM, terminfo.StringCapName,
func(v interface{}) string { return "=" + escape(v.([]byte)) },
)
}
func process(x, y interface{}, m map[int]bool, name func(int) string, mask func(interface{}) string) {
printIt(x, m, name, mask)
if *flagExtended {
printIt(y, nil, name, mask)
}
}
// process walks the values in z, adding missing elements in m. a mask func can
// be provided to format the values in z.
func printIt(z interface{}, m map[int]bool, name func(int) string, mask func(interface{}) string) {
var names []string
x := make(map[string]string)
switch v := z.(type) {
case func() map[string]bool:
for n, a := range v() {
if !a {
continue
}
var f string
if mask != nil {
f = mask(a)
}
x[n], names = f, append(names, n)
}
case func() map[string]int:
for n, a := range v() {
if a < 0 {
continue
}
var f string
if mask != nil {
f = mask(a)
}
x[n], names = f, append(names, n)
}
case func() map[string][]byte:
for n, a := range v() {
if a == nil {
continue
}
var f string
if mask != nil {
f = mask(a)
}
if n == "acs_chars" && strings.TrimSpace(strings.TrimPrefix(f, "=")) == "" {
continue
}
x[n], names = f, append(names, n)
}
}
// add missing
for i := range m {
n := name(i)
x[n], names = "@", append(names, n)
}
// sort and print
sort.Strings(names)
for _, n := range names {
fmt.Printf("\t%s%s,\n", n, x[n])
}
}
// peek peeks a byte.
func peek(b []byte, pos, length int) byte {
if pos < length {
return b[pos]
}
return 0
}
func isprint(b byte) bool {
return unicode.IsPrint(rune(b))
}
func realprint(b byte) bool {
return b < 127 && isprint(b)
}
func iscntrl(b byte) bool {
return unicode.IsControl(rune(b))
}
func realctl(b byte) bool {
return b < 127 && iscntrl(b)
}
func isdigit(b byte) bool {
return unicode.IsDigit(rune(b))
}
// logic taken from _nc_tic_expand from ncurses-6.0/ncurses/tinfo/comp_expand.c
func escape(buf []byte) string {
length := len(buf)
if length == 0 {
return ""
}
var s []byte
islong := length > 3
for i := 0; i < length; i++ {
ch := buf[i]
switch {
case ch == '%' && realprint(peek(buf, i+1, length)):
s = append(s, buf[i], buf[i+1])
i++
case ch == 128:
s = append(s, '\\', '0')
case ch == '\033':
s = append(s, '\\', 'E')
case realprint(ch) && ch != ',' && ch != ':' && ch != '!' && ch != '^':
s = append(s, ch)
case ch == '\r' && (islong || (i == length-1 && length > 2)):
s = append(s, '\\', 'r')
case ch == '\n' && islong:
s = append(s, '\\', 'n')
case realctl(ch) && ch != '\\' && (!islong || isdigit(peek(buf, i+1, length))):
s = append(s, '^', ch+'@')
default:
s = append(s, []byte(fmt.Sprintf("\\%03o", ch))...)
}
}
return string(s)
}
|