chore: rewrite loadprg & fix some issues
This commit is contained in:
@@ -172,7 +172,7 @@ export class Item {
|
|||||||
return this.path === '/' ? '/' : this.path.substring(this.path.lastIndexOf('/') + 1)
|
return this.path === '/' ? '/' : this.path.substring(this.path.lastIndexOf('/') + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadData(): string | null {
|
GetData(): string | null {
|
||||||
return this.data
|
return this.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,20 +9,23 @@ export class Cat extends Program {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
const item = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
const path = Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`)
|
||||||
|
let item: Item
|
||||||
|
|
||||||
if (!(await item.Exists())) {
|
try {
|
||||||
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
|
item = await Item.open(path)
|
||||||
|
} catch (err) {
|
||||||
|
stdout.emit(`cat: error: item ${path} is a directory\n`)
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.IsDirectory()) {
|
if (!(await item.Exists())) {
|
||||||
stdout.emit(`cat: error: can't read data from a directory; item ${item.GetPath()} is a directory.\n`)
|
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||||
return 3
|
return 3
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.emit(`${item.ReadData()}`)
|
stdout.emit(`${item.GetData()}`)
|
||||||
stdout.emit('<- EOF\n')
|
stdout.emit('\x1B[0;30m\x1B[47m EOF \x1B[0m\n')
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export class Edit extends Program {
|
|||||||
let filePath = resolvePath(args[1])
|
let filePath = resolvePath(args[1])
|
||||||
let file = await Item.open(filePath, '')
|
let file = await Item.open(filePath, '')
|
||||||
let fileExists = await file.Exists()
|
let fileExists = await file.Exists()
|
||||||
let lines = this.normalizeLines(file.ReadData())
|
let lines = this.normalizeLines(file.GetData())
|
||||||
let dirty = false
|
let dirty = false
|
||||||
|
|
||||||
let cursorLine = 0
|
let cursorLine = 0
|
||||||
@@ -93,7 +93,7 @@ export class Edit extends Program {
|
|||||||
const resolved = resolvePath(path)
|
const resolved = resolvePath(path)
|
||||||
const nextFile = await Item.open(resolved, '')
|
const nextFile = await Item.open(resolved, '')
|
||||||
const exists = await nextFile.Exists()
|
const exists = await nextFile.Exists()
|
||||||
const content = nextFile.ReadData()
|
const content = nextFile.GetData()
|
||||||
|
|
||||||
filePath = resolved
|
filePath = resolved
|
||||||
file = nextFile
|
file = nextFile
|
||||||
|
|||||||
@@ -4,7 +4,21 @@ import { Program } from './Program'
|
|||||||
|
|
||||||
export class Eval extends Program {
|
export class Eval extends Program {
|
||||||
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, _workdir: Item, args: string[]): Promise<number> {
|
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, _workdir: Item, args: string[]): Promise<number> {
|
||||||
let javascript: string = args.slice(1).join(' ')
|
const inlineArgs = args.slice(1)
|
||||||
|
const source = inlineArgs.length > 0
|
||||||
|
? inlineArgs.join(' ')
|
||||||
|
: await stdin.wait()
|
||||||
|
|
||||||
|
if (source.trim().length === 0) {
|
||||||
|
stdout.emit('eval: error: missing javascript input\n')
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.RunJs(source, stdout)
|
||||||
|
} catch {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { Item } from '../fs/Item'
|
import { Item } from '../fs/Item'
|
||||||
import type { Shell } from '../shell/Shell'
|
import type { Shell } from '../shell/Shell'
|
||||||
import type { SimpleStream } from '../utils/SimpleStream'
|
import type { SimpleStream } from '../utils/SimpleStream'
|
||||||
import { Program } from './Program'
|
import { Program } from './Program'
|
||||||
@@ -11,21 +11,59 @@ export class Loadprg extends Program {
|
|||||||
this.shell = shell
|
this.shell = shell
|
||||||
}
|
}
|
||||||
|
|
||||||
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
|
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||||
const javascript = args.slice(2).join(' ')
|
const path = Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`)
|
||||||
|
let file: Item
|
||||||
stdin.on(data => stdout.emit(`loadprg stdin: ${data}\x1B[0;30m\x1B[47m<- EOF\x1B[0m\n`))
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const program = eval(javascript)
|
file = await Item.open(path)
|
||||||
|
} catch (err) {
|
||||||
this.shell.LoadProgram(new program(), args[1])
|
stdout.emit(`loadprg: error: item ${path} is a directory\n`)
|
||||||
} catch (e) {
|
return 2
|
||||||
stdout.emit(`${String(e)}\n`)
|
|
||||||
stdout.emit(`${String((e as Error).stack)}\n`)
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!(await file.Exists())) {
|
||||||
|
stdout.emit(`loadprg: error: item ${file.GetPath()} doesn't exist.\n`)
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
|
||||||
|
let programName: string | null = null
|
||||||
|
|
||||||
|
// check if we have a name argument
|
||||||
|
// use the file name if the program name wasn't explicitly specified
|
||||||
|
const nameArgIndex = args.findIndex(value => value === "--name")
|
||||||
|
if (nameArgIndex !== -1 && args[nameArgIndex + 1]) {
|
||||||
|
programName = args[nameArgIndex + 1]
|
||||||
|
} else programName = file.GetName()
|
||||||
|
|
||||||
|
// read the file
|
||||||
|
const javascript = file.GetData()
|
||||||
|
if (!javascript) {
|
||||||
|
stdout.emit(`loadprg: error: could not read the program data\n`)
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the program
|
||||||
|
type EntrypointFunction = InstanceType<typeof Program>['Exec']
|
||||||
|
let programEntrypoint: EntrypointFunction
|
||||||
|
|
||||||
|
try {
|
||||||
|
// due to some inconveniences and limitations,
|
||||||
|
// we only allow for loading the program main method
|
||||||
|
// instead of an entire program class.
|
||||||
|
programEntrypoint = new Function("stdin", "stdout", "workdir", "args", javascript) as EntrypointFunction
|
||||||
|
} catch (err) {
|
||||||
|
stdout.emit(`loadprg: error: could not load the program: ${err}\n`)
|
||||||
|
stdout.emit(`-> Stacktrace: ${(err as Error).stack}\n`)
|
||||||
|
return 4
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a wrapper class for the program
|
||||||
|
const wrapper: Program = new (class ProgramWrapper extends Program { Exec = programEntrypoint })
|
||||||
|
|
||||||
|
this.shell.LoadProgram(wrapper, programName)
|
||||||
|
stdout.emit(`-> Loaded program from ${file.GetPath()} as ${programName}\n`)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ import { Environment } from './Environment'
|
|||||||
import { InputManager } from './InputManager'
|
import { InputManager } from './InputManager'
|
||||||
import { Edit } from '../../program/Edit'
|
import { Edit } from '../../program/Edit'
|
||||||
import { Mv } from '../../program/Mv'
|
import { Mv } from '../../program/Mv'
|
||||||
|
import { Tree } from '../../program/Tree'
|
||||||
|
import { Cp } from '../../program/Cp'
|
||||||
|
|
||||||
export class Wush extends Shell {
|
export class Wush extends Shell {
|
||||||
public readonly Version = "0.3.2"
|
public readonly Version = "0.3.2"
|
||||||
@@ -96,6 +98,8 @@ export class Wush extends Shell {
|
|||||||
this.programs['printf'] = new Printf()
|
this.programs['printf'] = new Printf()
|
||||||
this.programs['edit'] = new Edit()
|
this.programs['edit'] = new Edit()
|
||||||
this.programs['mv'] = new Mv()
|
this.programs['mv'] = new Mv()
|
||||||
|
this.programs['cp'] = new Cp()
|
||||||
|
this.programs['tree'] = new Tree()
|
||||||
|
|
||||||
// reset exit code
|
// reset exit code
|
||||||
this.SetExitCode(0)
|
this.SetExitCode(0)
|
||||||
@@ -685,7 +689,7 @@ export class Wush extends Shell {
|
|||||||
if (item.IsDirectory())
|
if (item.IsDirectory())
|
||||||
throw new Error(`input file ${resolved} is a directory`)
|
throw new Error(`input file ${resolved} is a directory`)
|
||||||
|
|
||||||
return { stream: new SimpleStream<string>(), data: item.ReadData() ?? '' }
|
return { stream: new SimpleStream<string>(), data: item.GetData() ?? '' }
|
||||||
}
|
}
|
||||||
|
|
||||||
const openOutputRedirect = async (path: string, append: boolean) => {
|
const openOutputRedirect = async (path: string, append: boolean) => {
|
||||||
@@ -894,11 +898,14 @@ export class Wush extends Shell {
|
|||||||
this.terminal.NewPage()
|
this.terminal.NewPage()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
case 'm': {
|
||||||
|
this.terminal.ApplyCellStyleCodes(values)
|
||||||
|
return true
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private GetWrapWidth(): number {
|
private GetWrapWidth(): number {
|
||||||
const width = Math.floor(this.terminal.GetWidthCells())
|
const width = Math.floor(this.terminal.GetWidthCells())
|
||||||
if (!Number.isFinite(width) || width <= 0)
|
if (!Number.isFinite(width) || width <= 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user