2.9 KiB
2.9 KiB
Implement the sl ("steam locomotive") command from Linux in TypeScript. I want an exact replica of the original Linux command. Strictly follow the following APIs and concepts:
- class SimpleStream
- on(listener: (data: T) => any): void
- Attaches a listener to the stream
- param listener -> the function to call when data is streamed
- once(listener: (data: T) => any): void
- Attaches a one-time listener to the stream
- param listener -> the function to call when data is streamed
- wait(): Promise
- Patiently waits until new data is streamed, then returns it
- return a promise for a single chunk of streamed data
- off(listener: Function): void
- Removes a listener from the stream
- param listener -> the function to remove
- emit(data: T): void
- Streams data
- param data -> the data to stream
- on(listener: (data: T) => any): void
- Escape codes
- Escape codes can be formatted using one of the following syntaxes:
\{code letter}-> for short codes like \n or \f\0{code id};{first parameter};{second parameter};{...parameters}\0-> for longer codes with complex parameters like cursor movement codes- A semicolon right after or before a null terminator (
\0) inside of an escape code is a syntax error
- Supported escape codes:
- Short codes (
-> )- n -> line feed (new line)
- f -> form feed (new page)
- Longer codes (
,,<...parameters> -> )- cmr,{delta x},{delta y} -> moves the cursor relative to its current position by the specified amounts
- cma,{absolute x},{absolute y} -> moves the cursor absolute in the terminal coordinate space
- Short codes (
- Example usage:
stdout.emit("\\cma;3;2\\")- sets the cursor position to the specified XY coordinates - x=3, y=2
- Escape codes can be formatted using one of the following syntaxes:
- stdout/stdin - SimpleStream instances
- they function as input/output interfaces to send text between the shell and other programs
- stdout allows for formatting using the above mentioned Escape codes
- Example usage:
stdout.emit("Hello world!\n")- streams the text to the stdout stream, works like printf in Cstdin.once(data => stdout.emit(`Received data: ${data}`))- creates a single use listener in the stdin stream and prints out the received data
- abstract class Program
- abstract Exec(stdin: SimpleStream, stdout: SimpleStream, workdir: Item, args: string[]): Promise
- Entry point for a program
- param stdin -> input stream
- param stdout -> output stream
- param workdir -> Working directory filesystem item
- param args -> arguments array, including the executed program name from the shell
- for example for echo command:
[ "echo", "test", "one", "two", "three" ]
- for example for echo command:
- returns a posix-like exit code
- abstract Exec(stdin: SimpleStream, stdout: SimpleStream, workdir: Item, args: string[]): Promise