sync: sync wip stuff
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import type { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Clear extends Program {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: string[]): Promise<number> {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, ___: string[]): Promise<number> {
|
||||
stdout.emit('\f')
|
||||
|
||||
return 0
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import type { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Eval extends Program {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, args: string[]): Promise<number> {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, _workdir: Item, args: string[]): Promise<number> {
|
||||
const javascript = args.slice(1).join(' ')
|
||||
|
||||
try {
|
||||
// todo: pass workdir to the program
|
||||
eval(javascript)
|
||||
} catch (e) {
|
||||
stdout.emit(`${String(e)}\n`)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { GetCurrentTerminal, WEBSHELL_VERSION } from '../app'
|
||||
import type { Item } from '../fs/Item'
|
||||
import { Terminal } from '../terminal/Terminal'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Info extends Program {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: string[]): Promise<number> {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, ___: string[]): Promise<number> {
|
||||
stdout.emit(`Webshell v${WEBSHELL_VERSION}\n`)
|
||||
stdout.emit(`Terminal v${Terminal.Version}\n`)
|
||||
stdout.emit(`Running ${GetCurrentTerminal().GetShell()?.Name} v${GetCurrentTerminal().GetShell()?.Version}\n`)
|
||||
|
||||
@@ -1,32 +1,30 @@
|
||||
import type { Wush } from '../shell/Wush'
|
||||
import type { Item } from '../fs/Item'
|
||||
import type { Shell } from '../shell/Shell'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Loadprg extends Program {
|
||||
private wush: Wush
|
||||
private shell: Shell
|
||||
|
||||
constructor(wush: Wush) {
|
||||
constructor(shell: Shell) {
|
||||
super()
|
||||
this.wush = wush
|
||||
this.shell = shell
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, args: string[]): Promise<number> {
|
||||
// stdout.emit('Not implemented yet.\n')
|
||||
// return 0
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
|
||||
const javascript = args.slice(2).join(' ')
|
||||
|
||||
try {
|
||||
const exec: Function = eval(javascript)
|
||||
|
||||
const program = class extends Program {
|
||||
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, args: string[]): Promise<number> {
|
||||
exec(stdin, stdout, args)
|
||||
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
exec(stdin, stdout, workdir, args)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
this.wush.LoadProgram(new program(), args[1])
|
||||
this.shell.LoadProgram(new program(), args[1])
|
||||
} catch (e) {
|
||||
stdout.emit(`${String(e)}\n`)
|
||||
stdout.emit(`${String((e as Error).stack)}\n`)
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
import { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
import { File } from '../fs/File'
|
||||
import { Directory } from '../fs/Directory'
|
||||
|
||||
export class Ls extends Program {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Directory, __: string[]): Promise<number> {
|
||||
new Directory('/etc/system/idk')
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
if (!(new Item(args[1]).IsDirectory())) {
|
||||
stdout.emit("ls: error: the provided path is not a directory")
|
||||
return 1
|
||||
}
|
||||
|
||||
stdout.emit(`item: '${workdir.GetPath()}'\n`)
|
||||
stdout.emit(`index attr name\n`)
|
||||
workdir.List().forEach((entry, i) => {
|
||||
stdout.emit(`${i.toString().padEnd(8, ' ')} ${''.padEnd(4, '-').padEnd(8, ' ')} '${entry.GetName()}'\n`)
|
||||
})
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { Item } from '../fs/Item'
|
||||
import type { Wush } from '../shell/Wush'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
@@ -10,7 +11,7 @@ export class Lsprg extends Program {
|
||||
this.wush = wush
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: string[]): Promise<number> {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, ___: string[]): Promise<number> {
|
||||
for (const program in this.wush.GetPrograms())
|
||||
stdout.emit(`${program}\n`)
|
||||
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import type { Item } from "../fs/Item"
|
||||
import type { SimpleStream } from "../utils/SimpleStream"
|
||||
|
||||
export abstract class Program {
|
||||
abstract Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, args: string[]): Promise<number>
|
||||
abstract Exec(
|
||||
stdin: SimpleStream<string>,
|
||||
stdout: SimpleStream<string>,
|
||||
workdir: Item,
|
||||
args: string[]
|
||||
): Promise<number>
|
||||
}
|
||||
|
||||
0
src/program/Rm.ts
Normal file
0
src/program/Rm.ts
Normal file
119
src/program/Sl.ts
Normal file
119
src/program/Sl.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
// --- `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))
|
||||
}
|
||||
}
|
||||
25
src/program/Touch.ts
Normal file
25
src/program/Touch.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Item } from '../fs/Item'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Touch extends Program {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
stdout.emit(`touching children, please wait...\n`)
|
||||
|
||||
const file = new Item(args[1])
|
||||
stdout.emit(`does ${args[1]} exist? ${file.Exists()}\n`)
|
||||
|
||||
if (file.Exists()) {
|
||||
stdout.emit("touch: the file already exists.\n")
|
||||
return 1
|
||||
}
|
||||
|
||||
file.Create()
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user