-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
94 lines (80 loc) · 1.32 KB
/
types.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
package unixcsl
import (
"bytes"
"strconv"
)
type TkType int
const (
TkBytes TkType = iota
TkCtl
TkESC
TkCSI
)
func (t TkType)String()(string){
switch t {
case TkBytes: return "BTS"
case TkCtl: return "CTL"
case TkESC: return "ESC"
case TkCSI: return "CSI"
}
return "UKN"
}
type Token struct{
T TkType
V interface{}
}
func (tk *Token)Bytes()([]byte){
switch tk.T {
case TkBytes:
return tk.V.([]byte)
case TkCtl:
return []byte{tk.V.(byte)}
case TkESC:
return tk.V.(*ESCSeq).Bytes()
case TkCSI:
return tk.V.(*CSISeq).Bytes()
}
return nil
}
type ESCSeq struct{
Ch byte
Payload byte
}
func (s *ESCSeq)Bytes()([]byte){
if s.Payload == 0 {
return []byte{s.Ch}
}
return []byte{s.Ch, s.Payload}
}
type CSISeq struct{
Ch byte
Args []int
}
func NewCSI(ch byte, args ...int)(*CSISeq){
return &CSISeq{
Ch: ch,
Args: args,
}
}
func (s *CSISeq)Bytes()([]byte){
buf := bytes.NewBuffer(make([]byte, 0, 2 + len(s.Args) * 3))
buf.Write(CSI_SEQ)
if len(s.Args) > 0 {
buf.WriteString(strconv.Itoa(s.Args[0]))
for _, a := range s.Args[1:] {
buf.WriteByte(';')
buf.WriteString(strconv.Itoa(a))
}
}
buf.WriteByte(s.Ch)
return buf.Bytes()
}
func (s *CSISeq)ArgDef(i int, def int)(int){
if i >= len(s.Args) {
return def
}
return s.Args[i]
}
func (s *CSISeq)Arg(i int)(int){
return s.ArgDef(i, 0)
}