/
index.js
157 lines (123 loc) · 3.24 KB
/
index.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
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
/**
* Module dependencies.
*/
var ansi = require('ansi');
var Canvas = require('canvas');
/**
* Module exports.
*/
module.exports = term;
/**
* Terminal <canvas>.
* The `width` and `height` properties are automated based off of the TTY's
* current window size.
*
* @api public
*/
function term (opts) {
if (!opts) opts = {};
var stream = opts.stream || process.stdout;
var small = null == opts.small ? true : !!opts.small;
var pixelHeight = small ? 0.5 : 1;
var pixelWidth = small ? 1 : 2;
// create <canvas> instance
var canvas = new Canvas(stream.columns / pixelWidth, stream.rows / pixelHeight);
canvas.render = render;
canvas.stream = stream;
canvas.small = small;
// cached "context"
canvas.renderCtx = canvas.getContext('2d');
// handle the "resize" event
stream.on('resize', function () {
canvas.width = stream.columns / pixelWidth;
canvas.height = stream.rows / pixelHeight;
});
return canvas;
}
/**
* Renders the <canvas> current context to the TTY.
*
* @api public
*/
function render () {
var cursor = ansi(this.stream);
// retain calls in memory until `flush()` call
cursor.buffer();
// erase everything on the screen
cursor.eraseData(2);
// go to the top left origin (1-indexed, not 0...)
cursor.goto(1, 1);
// render the current <canvas> contents to the TTY
var ctx = this.renderCtx;
var small = this.small;
var w = this.width;
var h = this.height;
var alphaThreshold = 0;
var data = ctx.getImageData(0, 0, w, h).data;
var r, g, b, alpha;
var topBlank, bottomBlank;
var i = 0;
for (var y = 0; y < h; y++) {
// beginning of the row
cursor.bg.reset();
cursor.write('\n');
for (var x = 0; x < w; x++) {
// in `small` mode, we have to render 2 rows at a time, where the top row
// is the background color, and the bottom row is the foreground color
i = ((y * w) + x) * 4;
// top row
r = data[i];
g = data[i+1];
b = data[i+2];
alpha = data[i+3];
if (alpha > alphaThreshold) {
cursor.bg.rgb(r, g, b);
topBlank = false;
} else {
cursor.bg.reset();
topBlank = true;
}
if (small) {
// bottom row
// go to the next row
i = (((y + 1) * w) + x) * 4;
r = data[i];
g = data[i+1];
b = data[i+2];
alpha = data[i+3];
if (alpha > alphaThreshold) {
cursor.fg.rgb(r, g, b);
bottomBlank = false;
} else {
cursor.fg.reset();
bottomBlank = true;
}
}
if (small && bottomBlank && !topBlank) {
// swapping fg and bg for this pixel since we're gonna use a "top
// half" instead of the usual "bottom half"
i = ((y * w) + x) * 4;
// top row
r = data[i];
g = data[i+1];
b = data[i+2];
cursor.bg.reset();
cursor.fg.rgb(r, g, b);
}
// write the pixel
if (!small) {
cursor.write(' ');
} else if (topBlank && bottomBlank) {
cursor.write(' ');
} else if (bottomBlank) {
cursor.write('▀');
} else {
cursor.write('▄');
}
}
if (small) y++;
}
cursor.fg.reset();
cursor.bg.reset();
cursor.flush();
}