-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.js
69 lines (57 loc) · 2.06 KB
/
display.js
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
const text = require('./text.js')
class Buff {
// this class buffers a slimp3 screen of any size
// the it stores the display data in a single buffer, and offers methods for randomly writing to it
// Buff.visual() will return a string with the display data, and Buff.nice() will return a boxed representation
constructor(row_size, row_count) {
this.size = row_size
this.count = row_count
this.buff = null
this.make_clear()
}
// recreates the buffer, and fills it with spaces
make_clear() {
this.clear_row = ' '.repeat(this.size)
this.buff = Buffer.allocUnsafe(this.size*this.count)
this.buff.fill(' ')
}
// fills the buffer with spaces
clear() {
this.buff.fill(' ')
}
// returns the index of a Letter on the display
indexLixel(x,y){
if (x>=this.size) { throw new Error(`x-coord: ${x} out of range (max: ${this.size-1})`)}
if (y>=this.count) { throw new Error(`y-coord: ${y} out of range (max: ${this.count-1})`)}
return (this.size*y)+x
}
// write a string to the buffer
write(x, y, str) {
this.buff.write(str, this.indexLixel(x,y), str.length, 'ascii')
}
// returns the buffer with ascii encoding
print() {
return this.buff.toString('ascii')
}
// reads the buffer into a string, with newlines
visual() {
var result = []
for (var offset = 0; (offset+this.size) <= this.buff.length; offset += this.size) {
result.push(this.buff.slice(offset, offset+this.size).toString())
}
return result
}
// reads the buffer into a string, with a nice box
nice() {
return '┏' + text.alignCenter('[Slimp3]', this.size, '━') + '┓\n┃' + this.visual().join('┃\n┃') + '┃\n┗' + '━'.repeat(this.size) + '┛'
}
// gets the offset needed for centering text (given the length of the text)
centerOffset(text_length) {
return Math.max(Math.floor((this.size/2) - (text_length/2)), 0)
}
// gets the offset needed for aligning text to the right (given the length of the text)
rightOffset(text_length){
return this.size - text_length
}
}
exports.Buff = Buff