-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexes.go
226 lines (194 loc) · 6.26 KB
/
hexes.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package hexes
import (
"io"
"os"
"unicode/utf8"
runeWidth "github.com/mattn/go-runewidth"
"golang.org/x/term"
)
// Gives an abstraction to render text in any position in the terminal.
type Renderer struct {
Lines [][]rune // (RO) The virtual output characters.
Attributes [][]Attribute // (RO) The virtual output Attributes.
Rows int // (RO) The amount of rows in virtual output.
Cols int // (RO) The amount of columns in virtual output.
CursorRow int // (RO) The row the cursor is in the terminal.
CursorCol int // (RO) The column the cursor is in the terminal.
CurrentAttribute Attribute // (RO) The terminal's current attribute.
DefaultAttribute Attribute // (RO) The preferred default attribute of the renderer.
Out io.Writer // (RO) The default writer to send terminal data to.
In io.Reader // (RO) The default reader to get data from.
oldTermState *term.State
}
// Creates a new Renderer using in (e.g. os.Stdin) as input and
// out (e.g. os.Stdout) as output.
func New(in io.Reader, out io.Writer) *Renderer {
return &Renderer{DefaultAttribute: NORMAL, Out: out, In: in}
}
// Initializes and configures the Renderer and user's terminal.
func (r *Renderer) Start() {
oldTermState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic("Could not set terminal to raw mode: " + err.Error())
}
r.oldTermState = oldTermState
r.write([]byte("\033[?25l")) // Hide cursor
r.updateRowsAndCols()
for i := 0; i < r.Rows; i++ {
line := []rune{}
attributes := []Attribute{}
for j := 0; j < r.Cols; j++ {
line = append(line, ' ')
attributes = append(attributes, r.DefaultAttribute)
}
r.Lines = append(r.Lines, line)
r.Attributes = append(r.Attributes, attributes)
}
r.CurrentAttribute = r.DefaultAttribute
r.Refresh()
}
func (r *Renderer) updateRowsAndCols() {
cols, rows, err := term.GetSize(int(os.Stdin.Fd()))
r.Rows = rows
r.Cols = cols
if err != nil {
panic("Could not get terminal size")
}
}
func (r *Renderer) resizeLinesAndAttributes() {
currentRows := len(r.Lines)
newRows := r.Rows
currentCols := len(r.Lines[0])
newCols := r.Cols
if newCols < currentCols {
for i := 0; i < currentRows; i++ {
r.Lines[i] = r.Lines[i][:newCols]
r.Attributes[i] = r.Attributes[i][:newCols]
}
} else if newCols > currentCols {
for i := 0; i < currentRows; i++ {
for j := 0; j < newCols - currentCols; j++ {
r.Lines[i] = append(r.Lines[i], ' ')
r.Attributes[i] = append(r.Attributes[i], r.DefaultAttribute)
}
}
}
if newRows < currentRows {
r.Lines = r.Lines[:newRows]
r.Attributes = r.Attributes[:newRows]
} else if newRows > currentRows {
for i := 0; i < newCols - currentRows; i++ {
row := []rune{}
attributes := []Attribute{}
for j := 0; j < newCols; j++ {
row = append(row, ' ')
attributes = append(attributes, r.DefaultAttribute)
}
r.Lines = append(r.Lines, row)
r.Attributes = append(r.Attributes, attributes)
}
}
}
func (r *Renderer) write(data []byte) {
r.Out.Write(data)
}
var tmpBuf = make([]byte, 4)
func (r *Renderer) writeRune(chr rune) {
length := utf8.EncodeRune(tmpBuf, chr)
r.Out.Write(tmpBuf[:length])
}
// Redraws virtual output to the terminal, handling resizes.
func (r *Renderer) Refresh() {
r.updateRowsAndCols()
r.resizeLinesAndAttributes()
r.SetAttribute(r.DefaultAttribute)
r.write([]byte("\033[H")) // Move to top left corner
r.CursorCol = 0
r.CursorRow = 0
r.write([]byte("\033[J")) // Clear to end of screen
r.redraw()
}
func (r *Renderer) redraw() {
for row := 0; row < r.Rows; row++ {
for col := 0; col < r.Cols; col++ {
r.MoveCursor(row, col)
r.write(r.Attributes[row][col])
r.writeRune(r.Lines[row][col])
r.CursorCol++
}
}
}
// Moves terminal cursor to a position (0 indexed).
func (r *Renderer) MoveCursor(row, col int) {
// NOTE: This optimization doesn't always work, as some unicode characters
// are 2 wide even if using the 'width' package to narrow them
if r.CursorRow == row && r.CursorCol == col {
return
}
r.CursorRow = row
r.CursorCol = col
r.moveTerminalCursor(row + 1, col + 1)
}
var baseSequence = []byte{27, '[', 0, 0, 0, ';', 0, 0, 0, 'H'}
var tmpSequence = []byte{27, '[', 0, 0, 0, ';', 0, 0, 0, 'H'}
func (r *Renderer) moveTerminalCursor(row, col int) {
// 0 1 2 3 4 5 6 7 8 9
// ESC ROW COL
copy(tmpSequence, baseSequence)
byteToAscii(tmpSequence[2:], byte(row))
byteToAscii(tmpSequence[6:], byte(col))
r.write(tmpSequence)
}
// Sets the cell at row, col (0 indexed) to the character given.
func (r *Renderer) Set(row, col int, value rune) {
if (
row > r.Rows - 1 ||
col > r.Cols - 1 ||
(r.Lines[row][col] == value && &r.Attributes[row][col][0] == &r.CurrentAttribute[0])) {
return
}
var oldWidth int
width := runeWidth.RuneWidth(value)
if width == 2 {
oldWidth = runeWidth.RuneWidth(r.Lines[row][col])
}
r.MoveCursor(row, col)
r.Lines[row][col] = value
r.Attributes[row][col] = r.CurrentAttribute
r.writeRune(value)
r.CursorCol += width
if width < oldWidth && col < r.Cols - 1 {
r.MoveCursor(row, col+1)
r.SetAttribute(r.Attributes[row][col+1])
r.writeRune(r.Lines[row][col+1])
}
}
// Sets the cells starting at row, col (0 indexed) to value,
// accounting for wide characters.
func (r *Renderer) SetString(row, col int, value string) {
for _, chr := range value {
r.Set(row, col, chr)
col += runeWidth.RuneWidth(chr)
}
}
// Sets the Attribute of the text being written.
func (r *Renderer) SetAttribute(attribute Attribute) {
r.CurrentAttribute = attribute
r.write(r.CurrentAttribute)
}
// Sets the preferred Attribute with which to prepend new ones.
func (r *Renderer) SetDefaultAttribute(attribute Attribute) {
r.DefaultAttribute = attribute
}
// Creates a new attribute based on the default attribute.
func (r *Renderer) NewAttribute(attributes... Attribute) Attribute {
return Join(r.DefaultAttribute, Join(attributes...))
}
// Restores terminal to default state.
func (r *Renderer) End() {
term.Restore(int(os.Stdin.Fd()), r.oldTermState)
r.write([]byte("\033[?25h")) // Show cursor
r.write(NORMAL)
r.write([]byte("\033[H")) // Move to top left corner
r.write([]byte("\033[J")) // Clear to end
}