| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"sort" |
| 11 |
"strings" |
| 12 |
"unicode" |
| 13 |
|
| 14 |
"github.com/xo/terminfo" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
flagTerm = flag.String("term", os.Getenv("TERM"), "term name") |
| 19 |
flagExtended = flag.Bool("x", false, "extended options") |
| 20 |
) |
| 21 |
|
| 22 |
func main() { |
| 23 |
flag.Parse() |
| 24 |
|
| 25 |
ti, err := terminfo.Load(*flagTerm) |
| 26 |
if err != nil { |
| 27 |
log.Fatal(err) |
| 28 |
} |
| 29 |
|
| 30 |
fmt.Printf("#\tReconstructed via %s from file: %s\n", strings.TrimPrefix(os.Args[0], "./"), ti.File) |
| 31 |
fmt.Printf("%s,\n", strings.TrimSpace(strings.Join(ti.Names, "|"))) |
| 32 |
|
| 33 |
process(ti.BoolCaps, ti.ExtBoolCaps, ti.BoolsM, terminfo.BoolCapName, nil) |
| 34 |
process( |
| 35 |
ti.NumCaps, ti.ExtNumCaps, ti.NumsM, terminfo.NumCapName, |
| 36 |
func(v interface{}) string { return fmt.Sprintf("#%d", v) }, |
| 37 |
) |
| 38 |
process( |
| 39 |
ti.StringCaps, ti.ExtStringCaps, ti.StringsM, terminfo.StringCapName, |
| 40 |
func(v interface{}) string { return "=" + escape(v.([]byte)) }, |
| 41 |
) |
| 42 |
} |
| 43 |
|
| 44 |
func process(x, y interface{}, m map[int]bool, name func(int) string, mask func(interface{}) string) { |
| 45 |
printIt(x, m, name, mask) |
| 46 |
if *flagExtended { |
| 47 |
printIt(y, nil, name, mask) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func printIt(z interface{}, m map[int]bool, name func(int) string, mask func(interface{}) string) { |
| 54 |
var names []string |
| 55 |
x := make(map[string]string) |
| 56 |
switch v := z.(type) { |
| 57 |
case func() map[string]bool: |
| 58 |
for n, a := range v() { |
| 59 |
if !a { |
| 60 |
continue |
| 61 |
} |
| 62 |
var f string |
| 63 |
if mask != nil { |
| 64 |
f = mask(a) |
| 65 |
} |
| 66 |
x[n], names = f, append(names, n) |
| 67 |
} |
| 68 |
|
| 69 |
case func() map[string]int: |
| 70 |
for n, a := range v() { |
| 71 |
if a < 0 { |
| 72 |
continue |
| 73 |
} |
| 74 |
var f string |
| 75 |
if mask != nil { |
| 76 |
f = mask(a) |
| 77 |
} |
| 78 |
x[n], names = f, append(names, n) |
| 79 |
} |
| 80 |
|
| 81 |
case func() map[string][]byte: |
| 82 |
for n, a := range v() { |
| 83 |
if a == nil { |
| 84 |
continue |
| 85 |
} |
| 86 |
var f string |
| 87 |
if mask != nil { |
| 88 |
f = mask(a) |
| 89 |
} |
| 90 |
if n == "acs_chars" && strings.TrimSpace(strings.TrimPrefix(f, "=")) == "" { |
| 91 |
continue |
| 92 |
} |
| 93 |
x[n], names = f, append(names, n) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
for i := range m { |
| 99 |
n := name(i) |
| 100 |
x[n], names = "@", append(names, n) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
sort.Strings(names) |
| 105 |
for _, n := range names { |
| 106 |
fmt.Printf("\t%s%s,\n", n, x[n]) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func peek(b []byte, pos, length int) byte { |
| 112 |
if pos < length { |
| 113 |
return b[pos] |
| 114 |
} |
| 115 |
return 0 |
| 116 |
} |
| 117 |
|
| 118 |
func isprint(b byte) bool { |
| 119 |
return unicode.IsPrint(rune(b)) |
| 120 |
} |
| 121 |
|
| 122 |
func realprint(b byte) bool { |
| 123 |
return b < 127 && isprint(b) |
| 124 |
} |
| 125 |
|
| 126 |
func iscntrl(b byte) bool { |
| 127 |
return unicode.IsControl(rune(b)) |
| 128 |
} |
| 129 |
|
| 130 |
func realctl(b byte) bool { |
| 131 |
return b < 127 && iscntrl(b) |
| 132 |
} |
| 133 |
|
| 134 |
func isdigit(b byte) bool { |
| 135 |
return unicode.IsDigit(rune(b)) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func escape(buf []byte) string { |
| 140 |
length := len(buf) |
| 141 |
if length == 0 { |
| 142 |
return "" |
| 143 |
} |
| 144 |
|
| 145 |
var s []byte |
| 146 |
islong := length > 3 |
| 147 |
for i := 0; i < length; i++ { |
| 148 |
ch := buf[i] |
| 149 |
switch { |
| 150 |
case ch == '%' && realprint(peek(buf, i+1, length)): |
| 151 |
s = append(s, buf[i], buf[i+1]) |
| 152 |
i++ |
| 153 |
|
| 154 |
case ch == 128: |
| 155 |
s = append(s, '\\', '0') |
| 156 |
|
| 157 |
case ch == '\033': |
| 158 |
s = append(s, '\\', 'E') |
| 159 |
|
| 160 |
case realprint(ch) && ch != ',' && ch != ':' && ch != '!' && ch != '^': |
| 161 |
s = append(s, ch) |
| 162 |
|
| 163 |
case ch == '\r' && (islong || (i == length-1 && length > 2)): |
| 164 |
s = append(s, '\\', 'r') |
| 165 |
|
| 166 |
case ch == '\n' && islong: |
| 167 |
s = append(s, '\\', 'n') |
| 168 |
|
| 169 |
case realctl(ch) && ch != '\\' && (!islong || isdigit(peek(buf, i+1, length))): |
| 170 |
s = append(s, '^', ch+'@') |
| 171 |
|
| 172 |
default: |
| 173 |
s = append(s, []byte(fmt.Sprintf("\\%03o", ch))...) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return string(s) |
| 178 |
} |