Skip to content

Commit

Permalink
(docs) add theme command
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Sep 24, 2024
1 parent 379bd8f commit 3294be4
Show file tree
Hide file tree
Showing 4 changed files with 1,670 additions and 4 deletions.
10 changes: 6 additions & 4 deletions docs/src/components/Terminal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import github from './github';
import echo from './echo';
import source from './source';
import record from './record';
import theme from './theme';

const terminal_scripts = [
'https://cdn.jsdelivr.net/npm/jquery',
'https://cdn.jsdelivr.net/combine/gh/jcubic/jquery.terminal@devel/js/jquery.terminal.min.js,npm/js-polyfills/keyboard.js,gh/jcubic/jquery.terminal@99526e255/js/less.js,npm/jquery.terminal/js/xml_formatting.js'
];

function command(term: RefObject<JQueryTerminal>) {
const options = { typing: true, delay: 100 };
return (command: string) => () => {
const options = { typing: true, delay: 50 };
return (command: string, silent: boolean = false) => () => {
setTimeout(() => {
term.current.focus().exec(command, options);
term.current.focus().exec(command, { ...options, silent });
}, 0);
};
}
Expand All @@ -57,6 +58,7 @@ export default function Interpreter(): JSX.Element {
github,
source,
echo,
theme,
record,
size(num: string) {
this.css('--size', num);
Expand Down Expand Up @@ -102,7 +104,7 @@ export default function Interpreter(): JSX.Element {
</button>
</li>
<li>
<button onClick={exec('size 1')}>size</button>
<button onClick={exec('theme', true)}>theme</button>
</li>
</ul>
</div>
Expand Down
14 changes: 14 additions & 0 deletions docs/src/components/Terminal/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { JQueryTerminal } from 'jquery.terminal';
import themes from '@site/themes.json';
import { shuffle } from '@site/src/utils';

let index = 0;
shuffle(themes);

export default function theme(this: JQueryTerminal) {
const { background, foreground } = themes[index++ % themes.length];
this.css({
'--background': background,
'--color': foreground
});
}
13 changes: 13 additions & 0 deletions docs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@ export function omap<T extends unknown, U extends unknown>(object: Record<string
});
return Object.fromEntries(entries);
}

// ref: https://rosettacode.org/wiki/Knuth_shuffle#JavaScript
export function shuffle<T>(arr: T[]) {
let rand: number, temp: T;

for (let i = arr.length - 1; i > 0; i -= 1) {
rand = Math.floor((i + 1) * Math.random());//get random between zero and i (inclusive)
temp = arr[rand];
arr[rand] = arr[i]; //swap i (last element) with random element.
arr[i] = temp;
}
return arr;
}
Loading

0 comments on commit 3294be4

Please sign in to comment.