forked from tiborvass/uniline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
151 lines (130 loc) · 3.54 KB
/
utils.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
// This file is an attempt to work with []byte and []rune at the same time while
// preserving information about character width in a terminal.
//
// TODO: improve on this poor design.
package uniline
import "github.com/shinichy/go-wcwidth"
// char represents a character in the terminal screen
// Its size is defined as follows:
// - 1 rune
// - len(char.b) bytes
// - char.colLen terminal columns
type char struct {
p []byte
r rune
colLen int
}
func charFromRune(r rune) char {
return char{[]byte(string(r)), r, wcwidth.WcwidthUcs(r)}
}
func (c char) Clone() char {
b := make([]byte, len(c.p))
copy(b, c.p)
c.p = b
return c
}
// text represents a sequence of characters
// Its size is defined as follows:
// - len(text.chars) runes
// - len(text.bytes) bytes
// - text.colLen terminal columns
type text struct {
chars []char
bytes []byte
colLen int
}
func textFromString(s string) text {
t := text{chars: make([]char, 0, len(s)), bytes: make([]byte, 0, len(s))}
for _, r := range s {
c := charFromRune(r)
t.chars = append(t.chars, c)
t.bytes = append(t.bytes, c.p...)
t.colLen += c.colLen
}
return t
}
func (t text) AppendChar(c char) text {
return text{append(t.chars, c), append(t.bytes, c.p...), t.colLen + c.colLen}
}
func (t text) AppendText(n text) text {
return text{append(t.chars, n.chars...), append(t.bytes, n.bytes...), t.colLen + n.colLen}
}
func (t text) InsertCharAt(pos position, c char) text {
chars := make([]char, len(t.chars)+1)
copy(chars, t.chars[:pos.runes])
chars[pos.runes] = c
copy(chars[pos.runes+1:], t.chars[pos.runes:])
bytes := make([]byte, len(t.bytes)+len(c.p))
copy(bytes, t.bytes[:pos.bytes])
copy(bytes[pos.bytes:], c.p)
copy(bytes[pos.bytes+len(c.p):], t.bytes[pos.bytes:])
return text{chars, bytes, t.colLen + c.colLen}
}
func (t text) InsertTextAt(pos position, n text) text {
chars := make([]char, len(t.chars)+len(n.chars))
copy(chars, t.chars[:pos.runes])
copy(chars[pos.runes:], n.chars)
copy(chars[pos.runes+len(n.chars):], t.chars[pos.runes:])
bytes := make([]byte, len(t.bytes)+len(n.bytes))
copy(bytes, t.bytes[:pos.bytes])
copy(bytes[pos.bytes+len(n.bytes):], t.bytes[pos.bytes:])
copy(bytes[pos.bytes:], n.bytes)
return text{chars, bytes, t.colLen + n.colLen}
}
func (t text) RemoveCharAt(pos position) text {
c := t.chars[pos.runes]
t.bytes = append(t.bytes[:pos.bytes], t.bytes[pos.bytes+len(c.p):]...)
t.chars = append(t.chars[:pos.runes], t.chars[pos.runes+1:]...)
t.colLen -= c.colLen
return t
}
func (t text) Slice(segment ...position) text {
switch len(segment) {
case 1:
t.chars = t.chars[segment[0].runes:]
t.bytes = t.bytes[segment[0].bytes:]
t.colLen -= segment[0].columns
case 2:
t.chars = t.chars[segment[0].runes:segment[1].runes]
t.bytes = t.bytes[segment[0].bytes:segment[1].bytes]
t.colLen = segment[1].columns - segment[0].columns
default:
panic("Slice expects 1 or 2 position arguments")
}
return t
}
func (t text) Clone() text {
chars := make([]char, len(t.chars))
for i, c := range t.chars {
chars[i] = c.Clone()
}
t.chars = chars
b := make([]byte, len(t.bytes))
copy(b, t.bytes)
t.bytes = b
return t
}
func (t text) String() string {
return string(t.bytes)
}
type position struct {
bytes int
runes int
columns int
}
func (pos position) Add(chars ...char) position {
for _, c := range chars {
pos.runes++
pos.bytes += len(c.p)
pos.columns += c.colLen
}
return pos
}
func (pos position) Subtract(chars ...char) position {
for _, c := range chars {
pos.runes--
pos.bytes -= len(c.p)
pos.columns -= c.colLen
}
return pos
}