Files
webshell/src/program/Ls.ts
2026-05-21 23:42:14 +02:00

40 lines
1.4 KiB
TypeScript

import { Item } from '../fs/Item'
import type { SimpleStream } from '../utils/SimpleStream'
import { Program } from './Program'
export class Ls extends Program {
constructor() {
super()
}
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
// open the target directory, default to workdir
const item = args[1]
? await Item.openDir(Item.NormalizePath(
args[1].startsWith('/')
? args[1]
: `${workdir.GetPath()}/${args[1]}`
))
: workdir
if (args[1] && !item.IsDirectory()) {
stdout.emit("ls: error: the provided path is not a directory\n")
return 1
}
if (!(await item.Exists())) {
stdout.emit(`ls: error: path ${item.GetPath()} doesn't exist\n`)
return 2
}
stdout.emit(`-> Listing contents of item: '${item.GetPath()}'\n`)
stdout.emit(` [ drwx name ]\n`)
const items = await item.List()
items.forEach(entry => {
stdout.emit(` | ${`${(entry.IsDirectory() ? 'd' : '-')}${(entry.IsReadable() ? 'r' : '-')}${(entry.IsWritable() ? 'w' : '-')}${(entry.IsExecutable() ? 'x' : '-')}`.padEnd(8, ' ')} ${entry.IsDirectory() ? '\x1B[0;30m\x1B[47m' : ''}${entry.GetName()}\x1B[0m\n`)
})
return 0
}
}