-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.ts
47 lines (40 loc) · 1.06 KB
/
terminal.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
import { stdin } from 'process';
import chalk from 'chalk';
import Game from './';
import { getColor } from './colors';
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
type ChalkColor = [red: number, green: number, blue: number];
function logGame() {
const printArr = Game.field.map((row) => {
// TODO fix number padding
return row.map((cell) => {
const colors = getColor(cell);
return chalk.hex(colors.color).bgHex(colors.background).bold(` ${cell}`.slice(-4))
});
});
process.stdout.write('\x1b[H\x1b[2J')
process.stdout.write(`${Game.score} \n`);
process.stdout.write(printArr.join('\n'));
}
logGame();
stdin.on('data', (key: string) => {
if (key == '\u001B\u005B\u0041') {
Game.onSwipeTop();
logGame();
}
if (key == '\u001B\u005B\u0043') {
Game.onSwipeRight();
logGame();
}
if (key == '\u001B\u005B\u0042') {
Game.onSwipeBottom();
logGame();
}
if (key == '\u001B\u005B\u0044') {
Game.onSwipeLeft();
logGame();
}
if (key == '\u0003') { process.exit(); } // ctrl-c
});