forked from littledivy/drawille
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawille.ts
103 lines (90 loc) · 2.48 KB
/
drawille.ts
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
import { Buffer } from "https://deno.land/std/node/buffer.ts";
const map = [
[0x1, 0x8],
[0x2, 0x10],
[0x4, 0x20],
[0x40, 0x80],
];
class Canvas {
private _width: number = 0;
private _height: number = 0;
public content: Buffer;
constructor(width?: number, height?: number) {
// XXX(UNSTABLE): This is a Deno unstable feature.
let { columns, rows } = Deno.consoleSize(Deno.stdout.rid);
this.width = width || columns * 2 - 2;
this.height = height || rows * 4;
this.content = Buffer.alloc((this.width * this.height) / 8);
}
set width(width: number) {
this._width = Math.floor(width / 2) * 2;
this.content = Buffer.alloc((this.width * this.height) / 8);
this.clear();
}
get width() {
return this._width;
}
get height() {
return this._height;
}
set height(height: number) {
this._height = Math.floor(height / 4) * 4;
this.content = Buffer.alloc((this.width * this.height) / 8);
this.clear();
}
clear() {
this.content.fill(0);
}
frame(delimiter: string = "\n"): string {
const frameWidth = this.width / 2;
const result = this.content.reduce(
(acc: string[], cur: number, i: number) => {
if (i % frameWidth === 0) {
acc.push(delimiter);
}
acc.push(cur ? String.fromCharCode(0x2800 + cur) : " ");
return acc;
},
[],
);
result.push(delimiter);
return result.join("");
}
set(x: number, y: number) {
if (!(x >= 0 && x < this.width && y >= 0 && y < this.height)) {
return;
}
x = Math.floor(x);
y = Math.floor(y);
const nx = Math.floor(x / 2);
const ny = Math.floor(y / 4);
const coord = nx + (this.width / 2) * ny;
const mask = map[y % 4][x % 2];
this.content[coord] |= mask;
}
unset(x: number, y: number) {
if (!(x >= 0 && x < this.width && y >= 0 && y < this.height)) {
return;
}
x = Math.floor(x);
y = Math.floor(y);
const nx = Math.floor(x / 2);
const ny = Math.floor(y / 4);
const coord = nx + (this.width / 2) * ny;
const mask = map[y % 4][x % 2];
this.content[coord] &= ~mask;
}
toggle(x: number, y: number) {
if (!(x >= 0 && x < this.width && y >= 0 && y < this.height)) {
return;
}
x = Math.floor(x);
y = Math.floor(y);
const nx = Math.floor(x / 2);
const ny = Math.floor(y / 4);
const coord = nx + (this.width / 2) * ny;
const mask = map[y % 4][x % 2];
this.content[coord] ^= mask;
}
}
export default Canvas;