Repository navigation
Expand file tree
/
Copy pathps-player.html
More file actions
51 lines (51 loc) · 2.33 KB
/
Copy pathps-player.html
File metadata and controls
51 lines (51 loc) · 2.33 KB
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
<script>
// Looping PowerShell demos on windows-setup.qmd: each <div class="ps-demo"> holds a JSON
// script in data-steps (a list of {cmd, out}) and replays it forever: type the command,
// press Enter, print the output, pause, start again. A picture, so nothing here runs a
// command. With reduced motion requested the finished screen shows; without JavaScript the
// window stays empty, and the command and "You should see" box beside it carry the step.
(() => {
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches;
document.querySelectorAll(".ps-demo").forEach((demo) => {
const steps = JSON.parse(demo.dataset.steps);
const prompt = demo.dataset.prompt;
const pre = demo.querySelector("pre");
const esc = (s) => s.replace(/[&<>]/g, (c) => ({ "&": "&", "<": "<", ">": ">" }[c]));
const line = (typed, cursor) =>
`<span class="pp">${esc(prompt)}</span> <b>${esc(typed)}</b>${cursor ? '<span class="cursor">█</span>' : ""}`;
const frame = (done, typed, showOut) => {
let html = "";
done.forEach((s) => { html += line(s.cmd) + "\n" + s.out.map((o) => esc(o) + "\n").join(""); });
if (typed !== null) html += line(typed, true) + "\n";
pre.innerHTML = html + (showOut ?? "");
};
const finished = () => { frame(steps, null); pre.innerHTML += line("", true); };
if (reduce) return finished();
let visible = true;
new IntersectionObserver(([e]) => { visible = e.isIntersecting; }).observe(demo);
const idle = async () => { while (!visible) await sleep(300); };
(async () => {
for (;;) {
const done = [];
for (const s of steps) {
await idle();
frame(done, "");
await sleep(700);
for (let i = 1; i <= s.cmd.length; i++) { frame(done, s.cmd.slice(0, i)); await sleep(45 + Math.random() * 45); }
await sleep(450);
pre.innerHTML = pre.innerHTML.replace(/<span class="cursor">.*?<\/span>/, "");
for (let n = 1; n <= s.out.length; n++) {
frame([...done, { cmd: s.cmd, out: s.out.slice(0, n) }], null);
await sleep(s.pace ?? 350);
}
done.push(s);
}
frame(done, "");
await sleep(3500);
}
})();
demo.dataset.ready = "1";
});
})();
</script>