blob: 7272d91677063372c9f63ae915c16541f42a2b80 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001package vtclean
2
3type char struct {
4 char byte
5 vt100 []byte
6}
7
8func chars(p []byte) []char {
9 tmp := make([]char, len(p))
10 for i, v := range p {
11 tmp[i].char = v
12 }
13 return tmp
14}
15
16type lineEdit struct {
17 buf []char
18 pos, size int
19 vt100 []byte
20}
21
22func newLineEdit(length int) *lineEdit {
23 return &lineEdit{buf: make([]char, length)}
24}
25
26func (l *lineEdit) Vt100(p []byte) {
27 l.vt100 = p
28}
29
30func (l *lineEdit) Move(x int) {
31 if x < 0 && l.pos <= -x {
32 l.pos = 0
33 } else if x > 0 && l.pos+x > l.size {
34 l.pos = l.size
35 } else {
36 l.pos += x
37 }
38}
39
40func (l *lineEdit) Write(p []byte) {
41 c := chars(p)
42 if len(c) > 0 {
43 c[0].vt100 = l.vt100
44 l.vt100 = nil
45 }
46 if len(l.buf)-l.pos < len(c) {
47 l.buf = append(l.buf[:l.pos], c...)
48 } else {
49 copy(l.buf[l.pos:], c)
50 }
51 l.pos += len(c)
52 if l.pos > l.size {
53 l.size = l.pos
54 }
55}
56
57func (l *lineEdit) Insert(p []byte) {
58 c := chars(p)
59 if len(c) > 0 {
60 c[0].vt100 = l.vt100
61 l.vt100 = nil
62 }
63 l.size += len(c)
64 c = append(c, l.buf[l.pos:]...)
65 l.buf = append(l.buf[:l.pos], c...)
66}
67
68func (l *lineEdit) Delete(n int) {
69 most := l.size - l.pos
70 if n > most {
71 n = most
72 }
73 copy(l.buf[l.pos:], l.buf[l.pos+n:])
74 l.size -= n
75}
76
77func (l *lineEdit) Clear() {
78 for i := 0; i < len(l.buf); i++ {
79 l.buf[i].char = ' '
80 }
81}
82func (l *lineEdit) ClearLeft() {
83 for i := 0; i < l.pos+1; i++ {
84 l.buf[i].char = ' '
85 }
86}
87func (l *lineEdit) ClearRight() {
88 l.size = l.pos
89}
90
91func (l *lineEdit) Bytes() []byte {
92 length := 0
93 buf := l.buf[:l.size]
94 for _, v := range buf {
95 length += 1 + len(v.vt100)
96 }
97 tmp := make([]byte, 0, length)
98 for _, v := range buf {
99 tmp = append(tmp, v.vt100...)
100 tmp = append(tmp, v.char)
101 }
102 return tmp
103}
104
105func (l *lineEdit) String() string {
106 return string(l.Bytes())
107}