feat: add some utilities

This commit is contained in:
2026-03-26 22:33:04 +01:00
parent 21a1a34309
commit 983cf59476
10 changed files with 119 additions and 27 deletions

View File

@@ -1,11 +1,14 @@
// Web-Uno Shell
// the best name I could come up with lmao
import { ClearExec } from '../program/clear'
import { Clear } from '../program/Clear'
import { Eval } from '../program/Eval'
import { Loadprg } from '../program/Loadprg'
import { Lsprg } from '../program/Lsprg'
import { Program } from '../program/Program'
import { Terminal } from '../terminal/Terminal'
import { EventBroadcaster } from '../utils/EventBroadcaster'
import { SimpleStream } from '../utils/SimpleStream'
import { ControlCode } from './ControlCode'
import { Shell } from './Shell'
export class Wush extends Shell {
@@ -23,6 +26,8 @@ export class Wush extends Shell {
readonly stdin: SimpleStream<string>
readonly stdout: SimpleStream<string>
private programs: { [name: string]: Program } = {}
constructor(broadcaster: EventBroadcaster, terminal: Terminal) {
super(broadcaster, terminal)
@@ -36,10 +41,28 @@ export class Wush extends Shell {
this.HandleKeyInput(key, isCharacter),
)
// load core programs
this.programs['clear'] = new Clear()
this.programs['eval'] = new Eval()
this.programs['loadprg'] = new Loadprg(this)
this.programs['lsprg'] = new Lsprg(this)
this.stdout.on(data => this.WriteEscapedString(data))
this.Prompt()
}
GetPrograms(): { [name: string]: Program } {
return this.programs
}
LoadProgram(program: Program, name: string) {
this.programs[name] = program
}
UnloadProgram(name: string) {
delete this.programs[name]
}
Prompt() {
this.terminal.Write(`hi [${this.execExitCode}] -> `)
}
@@ -53,6 +76,8 @@ export class Wush extends Shell {
buf.forEach((char, i) => {
if (this.ProcessControlCode(char)) {
buf.splice(i, 1)
} else {
this.terminal.Write(char)
}
})
}
@@ -90,16 +115,19 @@ export class Wush extends Shell {
this.execExitCode = -1
if (args[0] === 'clear') {
ClearExec(this.stdin, this.stdout)
if (this.programs[args[0]] instanceof Program) {
this.programs[args[0]].Exec(this.stdin, this.stdout, args)
.then(code => {
this.execExitCode = code != -1 ? code : -2
})
.catch(() => {
this.WriteEscapedString("lol")
})
.finally(() => {
// check if the exec actually exited with an exit code
// and if not, set it to -2 to indicate that it didn't exit with a valid code
if (this.execExitCode != -1)
this.execExitCode == -2
if (this.execExitCode === -1)
this.execExitCode = -2
// this.terminal.Write(`The program exited with exit code ${this.execExitCode}.`)
this.Prompt()
@@ -109,7 +137,7 @@ export class Wush extends Shell {
// don't print anything if there are no arguments
if (args[0].length !== 0) {
this.terminal.Write(`Program ${args[0]} was not found.`)
this.terminal.Write(`wush: unknown command: ${args[0]}.`)
this.terminal.MoveCursor(0, 1, { x: true, y: false })
}
@@ -122,6 +150,9 @@ export class Wush extends Shell {
case '\f':
this.terminal.NewPage()
return true
case '\n':
this.terminal.MoveCursor(0, 1, { x: true, y: false })
return true
default:
return false
}