Skip to content

Commit 5c9cb68

Browse files
committed
(docs) add star wars command
1 parent 2abd785 commit 5c9cb68

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

docs/src/components/Terminal/index.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import figlet from './figlet';
3030
import { jargon, jargon_init } from './jargon';
3131
import cal from './cal';
3232
import chuck_norris from './chuck-norris';
33+
import star_wars from './star-wars';
3334

3435
const languages = [
3536
'markdown',
@@ -69,6 +70,7 @@ const commands = {
6970
jargon,
7071
cal,
7172
'chuck-norris': chuck_norris,
73+
'star-wars': star_wars,
7274
help,
7375
theme,
7476
record,
@@ -225,6 +227,9 @@ export default function Interpreter(): JSX.Element {
225227
<li>
226228
<button onClick={exec('chuck-norris')}>chuck-norris</button>
227229
</li>
230+
<li>
231+
<button onClick={exec('star-wars')}>star-wars</button>
232+
</li>
228233
<li>
229234
<button onClick={exec('fortune | cowsay | lolcat')}>cowsay</button>
230235
</li>
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { JQueryTerminal } from 'jquery.terminal';
2+
3+
export default async function command(this: JQueryTerminal, delay_arg = '50') {
4+
const $ = (globalThis as any).$ as any;;
5+
if (this.cols() < 67) {
6+
this.error('not enough width to run the star wars');
7+
return;
8+
}
9+
let delay = parseInt(delay_arg);
10+
const sw_frames: string[][] = globalThis.sw_frames = (globalThis as any).sw_frames ?? [];
11+
if (!globalThis.star_wars) {
12+
await $.getScript('https://cdn.jsdelivr.net/gh/jcubic/static@master/js/star_wars.js');
13+
const star_wars = globalThis.star_wars;
14+
const LINES_PER_FRAME = 14;
15+
const lines = star_wars.length;
16+
for (let i = 0; i < lines; i += LINES_PER_FRAME) {
17+
sw_frames.push(star_wars.slice(i, i + LINES_PER_FRAME));
18+
}
19+
}
20+
let stop = false;
21+
function play(term: JQueryTerminal) {
22+
let i = 0;
23+
let next_delay: number;
24+
(function display() {
25+
if (i == sw_frames.length) {
26+
i = 0;
27+
}
28+
if (!stop) {
29+
term.clear();
30+
if (sw_frames[i][0].match(/[0-9]+/)) {
31+
next_delay = parseInt(sw_frames[i][0]) * delay;
32+
} else {
33+
next_delay = delay;
34+
}
35+
term.echo(sw_frames[i++].slice(1).join('\n')+'\n');
36+
setTimeout(display, next_delay);
37+
}
38+
})();
39+
}
40+
const view: JQueryTerminal.View = this.export_view();
41+
function exit() {
42+
stop = true;
43+
this.cmd().show();
44+
this.import_view(view);
45+
}
46+
this.push($.noop, {
47+
onExit: exit,
48+
keymap: {
49+
'Q': exit,
50+
'CTRL+C': exit,
51+
'ESCAPE': exit
52+
},
53+
prompt: ''
54+
});
55+
this.cmd().hide();
56+
play(this);
57+
}

0 commit comments

Comments
 (0)