Compare commits
7 Commits
646a9f6b7a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16c81e5d07 | ||
|
|
085cec8dc0 | ||
|
|
071d4e3aa4 | ||
|
|
d76119918d | ||
|
|
3798a53395 | ||
|
|
4777552106 | ||
|
|
62038c2814 |
@@ -5,7 +5,7 @@ import { Terminal } from './terminal/Terminal'
|
||||
import { EventBroadcaster } from './utils/EventBroadcaster'
|
||||
|
||||
export const WEBSHELL_VERSION = "0.2.1"
|
||||
export const VERSION_COMMENT: string | null = "hi"
|
||||
export const INFO_COMMENT: string | null = null
|
||||
|
||||
// initialize object store for webfs
|
||||
let WebfsDatabase: IDBDatabase | null = null
|
||||
|
||||
@@ -172,7 +172,7 @@ export class Item {
|
||||
return this.path === '/' ? '/' : this.path.substring(this.path.lastIndexOf('/') + 1)
|
||||
}
|
||||
|
||||
ReadData(): string | null {
|
||||
GetData(): string | null {
|
||||
return this.data
|
||||
}
|
||||
|
||||
|
||||
@@ -9,20 +9,23 @@ export class Cat extends Program {
|
||||
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())) {
|
||||
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||
try {
|
||||
item = await Item.open(path)
|
||||
} catch (err) {
|
||||
stdout.emit(`cat: error: item ${path} is a directory\n`)
|
||||
return 2
|
||||
}
|
||||
|
||||
if (item.IsDirectory()) {
|
||||
stdout.emit(`cat: error: can't read data from a directory; item ${item.GetPath()} is a directory.\n`)
|
||||
if (!(await item.Exists())) {
|
||||
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||
return 3
|
||||
}
|
||||
|
||||
stdout.emit(`${item.ReadData()}`)
|
||||
stdout.emit('<- EOF\n')
|
||||
stdout.emit(`${item.GetData()}`)
|
||||
stdout.emit('\x1B[0;30m\x1B[47m EOF \x1B[0m\n')
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ export class Edit extends Program {
|
||||
let filePath = resolvePath(args[1])
|
||||
let file = await Item.open(filePath, '')
|
||||
let fileExists = await file.Exists()
|
||||
let lines = this.normalizeLines(file.ReadData())
|
||||
let lines = this.normalizeLines(file.GetData())
|
||||
let dirty = false
|
||||
|
||||
let cursorLine = 0
|
||||
@@ -93,7 +93,7 @@ export class Edit extends Program {
|
||||
const resolved = resolvePath(path)
|
||||
const nextFile = await Item.open(resolved, '')
|
||||
const exists = await nextFile.Exists()
|
||||
const content = nextFile.ReadData()
|
||||
const content = nextFile.GetData()
|
||||
|
||||
filePath = resolved
|
||||
file = nextFile
|
||||
|
||||
@@ -3,17 +3,33 @@ import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Eval extends Program {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, _workdir: Item, args: string[]): Promise<number> {
|
||||
const javascript = args.slice(1).join(' ')
|
||||
async Exec(stdin: SimpleStream<string>, stdout: SimpleStream<string>, _workdir: Item, args: string[]): Promise<number> {
|
||||
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 {
|
||||
// todo: pass workdir to the program
|
||||
eval(javascript)
|
||||
} catch (e) {
|
||||
stdout.emit(`${String(e)}\n`)
|
||||
this.RunJs(source, stdout)
|
||||
} catch {
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
private RunJs(javascript: string, stdout: SimpleStream<string>) {
|
||||
try {
|
||||
// todo: pass workdir to the program
|
||||
eval(javascript)
|
||||
} catch (e) {
|
||||
stdout.emit(`${String(e)}\n`)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GetCurrentTerminal, WEBSHELL_VERSION, VERSION_COMMENT } from '../app'
|
||||
import { GetCurrentTerminal, WEBSHELL_VERSION, INFO_COMMENT } from '../app'
|
||||
import type { Item } from '../fs/Item'
|
||||
import { Terminal } from '../terminal/Terminal'
|
||||
import type { SimpleStream } from '../utils/SimpleStream'
|
||||
@@ -6,13 +6,24 @@ import { Program } from './Program'
|
||||
|
||||
export class Info extends Program {
|
||||
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`)
|
||||
stdout.emit('\n')
|
||||
stdout.emit(" --> binekrasik's Webshell / ->\n\n")
|
||||
|
||||
// print a comment for the current release if any
|
||||
if (VERSION_COMMENT)
|
||||
stdout.emit(`-> ${VERSION_COMMENT}\n`)
|
||||
stdout.emit(" Webshell and its components are licensed under the Apache 2.0 license unless stated otherwise.\n")
|
||||
stdout.emit(" Source available at https://git.martinpetr.dev/binekrasik/webshell.\n\n\n")
|
||||
|
||||
stdout.emit(" --> Versions / ->\n\n")
|
||||
stdout.emit(` Webshell ${WEBSHELL_VERSION}\n`)
|
||||
stdout.emit(` Terminal ${Terminal.Version}\n`)
|
||||
stdout.emit(` Shell '${GetCurrentTerminal().GetShell()?.Name}' version ${GetCurrentTerminal().GetShell()?.Version}\n\n\n`)
|
||||
|
||||
// print a comment for the current release (if any)
|
||||
if (INFO_COMMENT) {
|
||||
stdout.emit(" --> Version comment / ->\n\n")
|
||||
stdout.emit(` ${INFO_COMMENT}\n\n\n`)
|
||||
}
|
||||
|
||||
stdout.emit(" - Copyright (C) 2026 binekrasik -\n\n")
|
||||
|
||||
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 { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
@@ -11,19 +11,59 @@ export class Loadprg extends Program {
|
||||
this.shell = shell
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
|
||||
const javascript = args.slice(2).join(' ')
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
const path = Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`)
|
||||
let file: Item
|
||||
|
||||
try {
|
||||
const program = eval(javascript)
|
||||
|
||||
this.shell.LoadProgram(new program(), args[1])
|
||||
} catch (e) {
|
||||
stdout.emit(`${String(e)}\n`)
|
||||
stdout.emit(`${String((e as Error).stack)}\n`)
|
||||
return 1
|
||||
file = await Item.open(path)
|
||||
} catch (err) {
|
||||
stdout.emit(`loadprg: error: item ${path} is a directory\n`)
|
||||
return 2
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
* Provides necessary parsing functions
|
||||
*/
|
||||
export class InputParser {
|
||||
static Tokenize(input: string) {
|
||||
// static Tokenize(input: string) {
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
static Parse(tokens: string[]) {
|
||||
// static Parse(tokens: string[]) {
|
||||
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
import { Terminal } from '../../terminal/Terminal'
|
||||
import type { CursorPosition } from '../../terminal/CursorProperties'
|
||||
import { EventBroadcaster } from '../../utils/EventBroadcaster'
|
||||
import { SimpleStream } from '../../utils/SimpleStream'
|
||||
import { Shell } from '../Shell'
|
||||
import { Item } from '../../fs/Item'
|
||||
import { Environment } from './Environment'
|
||||
import { InputManager } from './InputManager'
|
||||
// Web-Uno Shell
|
||||
// the best name I could come up with lol
|
||||
|
||||
// import programs
|
||||
import { Clear } from '../../program/Clear'
|
||||
import { Eval } from '../../program/Eval'
|
||||
import { Loadprg } from '../../program/Loadprg'
|
||||
import { Lsprg } from '../../program/Lsprg'
|
||||
import { Info } from '../../program/Info'
|
||||
import { Program } from '../../program/Program'
|
||||
import { Edit } from '../../program/Edit'
|
||||
import { Mv } from '../../program/Mv'
|
||||
import { Terminal } from '../../terminal/Terminal'
|
||||
import type { CursorPosition } from '../../terminal/CursorProperties'
|
||||
import { EventBroadcaster } from '../../utils/EventBroadcaster'
|
||||
import { SimpleStream } from '../../utils/SimpleStream'
|
||||
import { Shell } from '../Shell'
|
||||
import { Ls } from '../../program/Ls'
|
||||
import { Item } from '../../fs/Item'
|
||||
import { Touch } from '../../program/Touch'
|
||||
import { Sl } from '../../program/Sl'
|
||||
import { Rm } from '../../program/Rm'
|
||||
@@ -27,15 +25,13 @@ import { Mkdir } from '../../program/Mkdir'
|
||||
import { Cd } from '../../program/Cd'
|
||||
import { Printf } from '../../program/Printf'
|
||||
import { Pwd } from '../../program/Pwd'
|
||||
import { Ls } from '../../program/Ls'
|
||||
import { Cp } from '../../program/Cp'
|
||||
import { Environment } from './Environment'
|
||||
import { InputManager } from './InputManager'
|
||||
import { Edit } from '../../program/Edit'
|
||||
import { Mv } from '../../program/Mv'
|
||||
import { Tree } from '../../program/Tree'
|
||||
import { Cp } from '../../program/Cp'
|
||||
|
||||
/**
|
||||
* Web-Uno Shell
|
||||
* - the best name I could come up with lol
|
||||
* @description serves as the default shell idk
|
||||
*/
|
||||
export class Wush extends Shell {
|
||||
public readonly Version = "0.3.2"
|
||||
public readonly Name = "wush"
|
||||
@@ -693,7 +689,7 @@ export class Wush extends Shell {
|
||||
if (item.IsDirectory())
|
||||
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) => {
|
||||
@@ -910,7 +906,6 @@ export class Wush extends Shell {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private GetWrapWidth(): number {
|
||||
const width = Math.floor(this.terminal.GetWidthCells())
|
||||
if (!Number.isFinite(width) || width <= 0)
|
||||
|
||||
Reference in New Issue
Block a user