-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-keypress-events.ts
110 lines (93 loc) · 3.3 KB
/
2-keypress-events.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
104
105
106
107
108
109
110
import { emitKeypressEvents } from 'node:readline';
import { Scrollable } from '../src/index';
import chalk from 'chalk';
console.clear();
const size = { width: 15, height: 6 };
const catBox = new Scrollable({
content: `
/\\_/\\
(${chalk.greenBright(' o') + chalk.red('.') + chalk.greenBright('o ')})
`,
start: { x: 6, y: 2 },
size,
wrapOptions: { trim: false }
})
.scroll(-1) // Center vertically.
.print();
const cowBox = new Scrollable({
content: `
${chalk.yellow('((...))')}
( O O )
\\ /
${chalk.magenta('(`_`)')}
`,
start: { x: 30, y: 2 },
size,
wrapOptions: { trim: false }
}).print();
const storyBox = new Scrollable({
content:
'The cat and the cow where playing hide and seek. The cat was hiding in the barn, but the cow was too big to fit inside. \n' +
'The cow was hiding in the field, but the cat was too small to see over the grass. \n' +
'The cat was looking for the cow, and the cow was looking for the cat. \n' +
'They stumbled upon a ' + chalk.magenta('magical portal') + ' in the middle of the field. ' +
'Curiosity getting the best of them, they jumped in together. ' +
'Inside, they found a talking mushroom that offered them three wishes each. ' +
'The cat wished for a jetpack, the cow wished for wings, and they zoomed off into the sky. ' +
'Soon, they encountered a UFO piloted by alien chefs who challenged them to a cooking competition. ' +
'The cat whipped up intergalactic lasagna, and the cow produced flying milkshakes. ' +
'The judges declared them winners, and they were crowned ' +
chalk.italic.yellow('rulers of the galaxy.'),
start: { x: 5, y: 10 },
size: { width: 40, height: 3 }
}).print();
printBoxBorders(catBox);
printBoxBorders(cowBox);
process.stdout.cursorTo(0, 15);
console.log('Up [↑] and down [↓] arrow keys to scroll. [Q] to quit');
// Handle key press events
emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
if (key.name == 'up') {
cowBox.scroll(-1).print();
catBox.scroll(1).print();
storyBox.scroll(1).print();
process.stdout.cursorTo(0, 15);
}
if (key.name == 'down') {
cowBox.scroll(1).print();
catBox.scroll(-1).print();
storyBox.scroll(1).print();
process.stdout.cursorTo(0, 15);
}
if (key.name == 'q') {
process.exit();
}
});
function printBoxBorders(box: Scrollable) {
const { stdout } = process;
const { size, start } = box.options;
const height = size.height + 2;
const width = size.width + 2;
const x = start.x - 1;
const y = start.y - 1;
stdout.cursorTo(x, y);
console.log(Array(width).fill('─').join(''));
stdout.cursorTo(x, y + height - 1);
console.log(Array(width).fill('─').join(''));
for (let i = 0; i < height - 1; i++) {
stdout.cursorTo(x, y + i);
console.log('│');
stdout.cursorTo(x + width - 1, y + i);
console.log('│');
}
stdout.cursorTo(x, y);
console.log('╭');
stdout.cursorTo(x + width - 1, y);
console.log('╮');
stdout.cursorTo(x, y + height - 1);
console.log('╰');
stdout.cursorTo(x + width - 1, y + height - 1);
console.log('╯');
}