120 lines
4.9 KiB
TypeScript
120 lines
4.9 KiB
TypeScript
// --- `sl` Command Implementation ---
|
|
|
|
import type { Item } from "../fs/Item"
|
|
import type { SimpleStream } from "../utils/SimpleStream"
|
|
import { Program } from "./Program"
|
|
|
|
export class Sl extends Program {
|
|
// The classic D51 locomotive ASCII art with 3 frames of wheel animation.
|
|
// Notice the trailing space on each line: this acts as an automatic "eraser"
|
|
// for the previous frame as the train moves left!
|
|
private static readonly D51_FRAMES: string[][] = [
|
|
[
|
|
' ==== ________ ___________ ',
|
|
' _D _| |_______/ \\__I_I_____===__|_________| ',
|
|
' |(_)--- | H\\________/ | | =|___ ___| ',
|
|
' / | | H | | | | ||_| |_|| ',
|
|
' | | | H |__--------------------| [___] | ',
|
|
' | ________|___H__/__|_____/[][]~\\_______| | ',
|
|
' |/ | |-----------I_____I [][] [] D |=======| ',
|
|
'__/ =| o |=-O=====O=====O=====O \\ ____Y___________| ',
|
|
' |/-=|___|= || || || |_____/~\\___/ ',
|
|
' \\_/ \\__/ \\__/ \\__/ \\__/ \\_/ ',
|
|
],
|
|
[
|
|
' ==== ________ ___________ ',
|
|
' _D _| |_______/ \\__I_I_____===__|_________| ',
|
|
' |(_)--- | H\\________/ | | =|___ ___| ',
|
|
' / | | H | | | | ||_| |_|| ',
|
|
' | | | H |__--------------------| [___] | ',
|
|
' | ________|___H__/__|_____/[][]~\\_______| | ',
|
|
' |/ | |-----------I_____I [][] [] D |=======| ',
|
|
'__/ =| o |=-~O====O====O====O~ \\ ____Y___________| ',
|
|
' |/-=|___|= || || || |_____/~\\___/ ',
|
|
' \\_/ \\__/ \\__/ \\__/ \\__/ \\_/ ',
|
|
],
|
|
[
|
|
' ==== ________ ___________ ',
|
|
' _D _| |_______/ \\__I_I_____===__|_________| ',
|
|
' |(_)--- | H\\________/ | | =|___ ___| ',
|
|
' / | | H | | | | ||_| |_|| ',
|
|
' | | | H |__--------------------| [___] | ',
|
|
' | ________|___H__/__|_____/[][]~\\_______| | ',
|
|
' |/ | |-----------I_____I [][] [] D |=======| ',
|
|
'__/ =| o |=-~~O===O===O===O~~ \\ ____Y___________| ',
|
|
' |/-=|___|= || || || |_____/~\\___/ ',
|
|
' \\_/ \\__/ \\__/ \\__/ \\__/ \\_/ ',
|
|
],
|
|
]
|
|
|
|
public async Exec(
|
|
_: SimpleStream<string>,
|
|
stdout: SimpleStream<string>,
|
|
__: Item,
|
|
args: string[],
|
|
): Promise<number> {
|
|
// Original `sl` behavior flags
|
|
const isFly = args.includes('-F')
|
|
|
|
// Terminal size assumptions since they aren't provided by the API
|
|
const termWidth = 100
|
|
const trainWidth = Sl.D51_FRAMES[0][0].length
|
|
|
|
// The train starts off-screen to the right and ends completely off-screen to the left
|
|
const startX = termWidth
|
|
const endX = -trainWidth
|
|
|
|
// Clear screen (new page using form feed)
|
|
stdout.emit('\f')
|
|
|
|
let frameIdx = 0
|
|
for (let x = startX; x >= endX; x--) {
|
|
const frame = Sl.D51_FRAMES[frameIdx % Sl.D51_FRAMES.length]
|
|
|
|
// If the `-F` flag is passed, the train "flies" upwards as it moves forward
|
|
let startY = isFly ? Math.floor(x / 4) + 2 : 5
|
|
|
|
for (let y = 0; y < frame.length; y++) {
|
|
const line = frame[y]
|
|
let outLine = line
|
|
let cursorX = x
|
|
|
|
// When the train hits the left edge, we must truncate the string
|
|
// and lock the drawing cursor to X=0 to prevent terminal wrapping artifacts
|
|
if (x < 0) {
|
|
cursorX = 0
|
|
outLine = line.substring(-x)
|
|
}
|
|
|
|
const cursorY = startY + y
|
|
|
|
// Only render if within vertical bounds and there's text left to draw
|
|
if (cursorY >= 0 && outLine.length > 0) {
|
|
// Send absolute cursor positioning sequence
|
|
stdout.emit(`\0cma;${cursorX};${cursorY}\0`)
|
|
// Render the frame line
|
|
stdout.emit(outLine)
|
|
}
|
|
}
|
|
|
|
// Artificial delay to pace the animation
|
|
await this.sleep(40)
|
|
frameIdx++
|
|
|
|
console.log(frameIdx)
|
|
}
|
|
|
|
// Return the cursor back to a safe position to give shell control back seamlessly
|
|
stdout.emit(`\0cma;0;20\0\n`)
|
|
|
|
return 0 // POSIX successful exit code
|
|
}
|
|
|
|
/**
|
|
* Utility method to pause execution to pace the animation frames.
|
|
*/
|
|
private sleep(ms: number): Promise<void> {
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
}
|
|
}
|