website: handle CSI sequences

This commit is contained in:
Mitchell Hashimoto
2023-10-05 22:18:06 -07:00
parent 861845e6df
commit 3226576f29
2 changed files with 32 additions and 9 deletions

View File

@ -2,6 +2,6 @@ import VTSequence from "@/components/VTSequence";
# Cursor Backword (CUB) # Cursor Backword (CUB)
<VTSequence sequence={["CSI", "n", "D"]} /> <VTSequence sequence={["CSI", "Pn", "D"]} />
TODO TODO

View File

@ -1,21 +1,43 @@
// Draw a diagram showing the VT sequence. // Draw a diagram showing the VT sequence.
//
// There are some special sequence elements that can be used:
//
// - CSI will be replaced with ESC [.
// - Pn will be considered a parameter
//
export default function VTSequence({ export default function VTSequence({
sequence, sequence,
}: { }: {
sequence: string | [string]; sequence: string | [string];
}) { }) {
// TODO: handle sequence array let arr: [string] = typeof sequence === "string" ? [sequence] : sequence;
const elem = typeof sequence === "string" ? sequence : sequence[0];
const specialChar = special[elem] ?? 0; if (arr[0] === "CSI") {
const hex = specialChar.toString(16).padStart(2, "0"); arr.shift();
arr.unshift("ESC", "[");
}
return ( return (
<div className="flex my-2.5"> <div className="flex my-2.5">
<div className="border shrink px-1 grid grid-rows-2 grid-cols-1 text-center"> {arr.map((elem, i) => (
<div>0x{hex}</div> <div className="shrink">
<div>{sequence}</div> <VTElem key={`${i}${elem}`} elem={elem} />
</div> </div>
))}
</div>
);
}
function VTElem({ elem }: { elem: string }) {
const param = elem === "Pn";
elem = param ? elem[1] : elem;
const specialChar = special[elem] ?? elem.charCodeAt(0);
const hex = specialChar.toString(16).padStart(2, "0").toUpperCase();
return (
<div className="border px-1 grid grid-rows-2 grid-cols-1 text-center">
<div>0x{hex}</div>
<div>{elem}</div>
</div> </div>
); );
} }
@ -23,4 +45,5 @@ export default function VTSequence({
const special: { [key: string]: number } = { const special: { [key: string]: number } = {
BEL: 0x07, BEL: 0x07,
BS: 0x08, BS: 0x08,
ESC: 0x1b,
}; };