Compare commits
9 Commits
e0269c9b6a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16c81e5d07 | ||
|
|
085cec8dc0 | ||
|
|
071d4e3aa4 | ||
|
|
d76119918d | ||
|
|
3798a53395 | ||
|
|
4777552106 | ||
|
|
62038c2814 | ||
|
|
646a9f6b7a | ||
|
|
255cc6a858 |
@@ -5,7 +5,7 @@ import { Terminal } from './terminal/Terminal'
|
|||||||
import { EventBroadcaster } from './utils/EventBroadcaster'
|
import { EventBroadcaster } from './utils/EventBroadcaster'
|
||||||
|
|
||||||
export const WEBSHELL_VERSION = "0.2.1"
|
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
|
// initialize object store for webfs
|
||||||
let WebfsDatabase: IDBDatabase | null = null
|
let WebfsDatabase: IDBDatabase | null = null
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
59
src/program/Cp.ts
Normal file
59
src/program/Cp.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import { Item } from '../fs/Item'
|
||||||
|
import type { SimpleStream } from '../utils/SimpleStream'
|
||||||
|
import { Program } from './Program'
|
||||||
|
|
||||||
|
export class Cp extends Program {
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||||
|
if (args.length < 3) {
|
||||||
|
stdout.emit("cp: error: missing the first and/or second path arguments\n")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
let item1: Item
|
||||||
|
let item2: Item
|
||||||
|
|
||||||
|
let destIsDir = false
|
||||||
|
|
||||||
|
// figure out if the items are files or directories
|
||||||
|
try {
|
||||||
|
item1 = await Item.openDir(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||||
|
} catch {
|
||||||
|
item1 = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
item2 = await Item.open(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
||||||
|
} catch {
|
||||||
|
item2 = await Item.openDir(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
||||||
|
destIsDir = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await item1.Exists()) {
|
||||||
|
stdout.emit(`cp: error: source item ${item1.GetPath()} does not exist.\n`)
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await item2.Exists() && !destIsDir) {
|
||||||
|
stdout.emit(`cp: error: destination item ${item2.GetPath()} already exists.\n`)
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
// either copy the item into a destination directory or create a new copy
|
||||||
|
if (destIsDir) {
|
||||||
|
const destChild = await Item.open(Item.NormalizePath(`${item2.GetPath()}/${item1.GetName()}`))
|
||||||
|
await item1.Copy(destChild)
|
||||||
|
stdout.emit(`-> copied ${item1.GetPath()} -> ${destChild.GetPath()}\n`)
|
||||||
|
} else {
|
||||||
|
await item2.Create()
|
||||||
|
await item1.Copy(item2)
|
||||||
|
|
||||||
|
stdout.emit(`-> copied ${item1.GetPath()} -> ${item2.GetPath()}\n`)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -3,17 +3,33 @@ import type { SimpleStream } from '../utils/SimpleStream'
|
|||||||
import { Program } from './Program'
|
import { Program } from './Program'
|
||||||
|
|
||||||
export class Eval extends Program {
|
export class Eval extends Program {
|
||||||
async Exec(_: 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> {
|
||||||
const javascript = 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 {
|
try {
|
||||||
// todo: pass workdir to the program
|
this.RunJs(source, stdout)
|
||||||
eval(javascript)
|
} catch {
|
||||||
} catch (e) {
|
|
||||||
stdout.emit(`${String(e)}\n`)
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
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 type { Item } from '../fs/Item'
|
||||||
import { Terminal } from '../terminal/Terminal'
|
import { Terminal } from '../terminal/Terminal'
|
||||||
import type { SimpleStream } from '../utils/SimpleStream'
|
import type { SimpleStream } from '../utils/SimpleStream'
|
||||||
@@ -6,13 +6,24 @@ import { Program } from './Program'
|
|||||||
|
|
||||||
export class Info extends Program {
|
export class Info extends Program {
|
||||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, ___: string[]): Promise<number> {
|
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, ___: string[]): Promise<number> {
|
||||||
stdout.emit(`Webshell v${WEBSHELL_VERSION}\n`)
|
stdout.emit('\n')
|
||||||
stdout.emit(`Terminal v${Terminal.Version}\n`)
|
stdout.emit(" --> binekrasik's Webshell / ->\n\n")
|
||||||
stdout.emit(`Running ${GetCurrentTerminal().GetShell()?.Name} v${GetCurrentTerminal().GetShell()?.Version}\n`)
|
|
||||||
|
|
||||||
// print a comment for the current release if any
|
stdout.emit(" Webshell and its components are licensed under the Apache 2.0 license unless stated otherwise.\n")
|
||||||
if (VERSION_COMMENT)
|
stdout.emit(" Source available at https://git.martinpetr.dev/binekrasik/webshell.\n\n\n")
|
||||||
stdout.emit(`-> ${VERSION_COMMENT}\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
|
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,19 +11,59 @@ export class Loadprg extends Program {
|
|||||||
this.shell = shell
|
this.shell = shell
|
||||||
}
|
}
|
||||||
|
|
||||||
async Exec(_: 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
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ export class Ls extends Program {
|
|||||||
|
|
||||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||||
// open the target directory, default to workdir
|
// open the target directory, default to workdir
|
||||||
|
|
||||||
const item = args[1]
|
const item = args[1]
|
||||||
? await Item.openDir(Item.NormalizePath(
|
? await Item.openDir(Item.NormalizePath(
|
||||||
args[1].startsWith('/')
|
args[1].startsWith('/')
|
||||||
@@ -32,7 +31,7 @@ export class Ls extends Program {
|
|||||||
stdout.emit(` [ drwx name ]\n`)
|
stdout.emit(` [ drwx name ]\n`)
|
||||||
const items = await item.List()
|
const items = await item.List()
|
||||||
items.forEach(entry => {
|
items.forEach(entry => {
|
||||||
stdout.emit(` | ${`${(entry.IsDirectory() ? 'd' : '-')}${(entry.IsReadable() ? 'r' : '-')}${(entry.IsWritable() ? 'w' : '-')}${(entry.IsExecutable() ? 'x' : '-')}`.padEnd(8, ' ')} ${entry.GetName()}\n`)
|
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
|
return 0
|
||||||
|
|||||||
@@ -16,30 +16,46 @@ export class Mv extends Program {
|
|||||||
let item1: Item
|
let item1: Item
|
||||||
let item2: Item
|
let item2: Item
|
||||||
|
|
||||||
|
let destIsDir = false
|
||||||
|
|
||||||
|
// figure out if the items are files or directories
|
||||||
try {
|
try {
|
||||||
item1 = await Item.openDir(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
item1 = await Item.openDir(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||||
item2 = await Item.openDir(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
|
||||||
} catch {
|
} catch {
|
||||||
item1 = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
item1 = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
item2 = await Item.open(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
item2 = await Item.open(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
||||||
|
} catch {
|
||||||
|
item2 = await Item.openDir(Item.NormalizePath(args[2].startsWith('/') ? args[2] : `${workdir.GetPath()}/${args[2]}`))
|
||||||
|
destIsDir = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await item1.Exists()) {
|
if (!await item1.Exists()) {
|
||||||
stdout.emit(`mv: error: source directory ${item1.GetPath()} does not exist.\n`)
|
stdout.emit(`mv: error: source item ${item1.GetPath()} does not exist.\n`)
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await item2.Exists()) {
|
if (await item2.Exists() && !destIsDir) {
|
||||||
stdout.emit(`mv: error: destination directory ${item2.GetPath()} already exists.\n`)
|
stdout.emit(`mv: error: destination item ${item2.GetPath()} already exists.\n`)
|
||||||
return 2
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
await item2.Create()
|
// either move the file into a destination directory or move it to a new path
|
||||||
await item1.Copy(item2)
|
if (destIsDir) {
|
||||||
|
const destChild = await Item.open(Item.NormalizePath(`${item2.GetPath()}/${item1.GetName()}`))
|
||||||
|
await item1.Copy(destChild)
|
||||||
|
stdout.emit(`-> moved ${item1.GetPath()} -> ${destChild.GetPath()}\n`)
|
||||||
|
} else {
|
||||||
|
await item2.Create()
|
||||||
|
await item1.Copy(item2)
|
||||||
|
|
||||||
|
stdout.emit(`-> moved ${item1.GetPath()} -> ${item2.GetPath()}\n`)
|
||||||
|
}
|
||||||
|
|
||||||
await item1.Delete()
|
await item1.Delete()
|
||||||
|
|
||||||
stdout.emit(`-> moved ${item1.GetPath()} -> ${item2.GetPath()}\n`)
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
src/program/Tree.ts
Normal file
45
src/program/Tree.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { Item } from '../fs/Item'
|
||||||
|
import type { SimpleStream } from '../utils/SimpleStream'
|
||||||
|
import { Program } from './Program'
|
||||||
|
|
||||||
|
export class Tree 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("tree: error: the provided path is not a directory\n")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(await item.Exists())) {
|
||||||
|
stdout.emit(`tree: error: path ${item.GetPath()} doesn't exist\n`)
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout.emit(`-> Tree of item: '${item.GetPath()}'\n`)
|
||||||
|
await this.printTree(stdout, item, 1)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
private async printTree(stdout: SimpleStream<string>, item: Item, depth: number): Promise<void> {
|
||||||
|
const items = await item.List()
|
||||||
|
for (const item of items) {
|
||||||
|
stdout.emit(` ${' |'.repeat(depth)}--+ ${item.IsDirectory() ? '\x1B[0;30m\x1B[47m' : ''}${item.GetName()}\x1B[0m\n`)
|
||||||
|
|
||||||
|
if (item.IsDirectory())
|
||||||
|
await this.printTree(stdout, item, depth + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -140,6 +140,14 @@ export class InputManager {
|
|||||||
HandleKeyInput(key: string, isCharacter: boolean) {
|
HandleKeyInput(key: string, isCharacter: boolean) {
|
||||||
// handle special input
|
// handle special input
|
||||||
switch (key.toUpperCase()) {
|
switch (key.toUpperCase()) {
|
||||||
|
case 'C':
|
||||||
|
if (InputManager.HasModifierCombo(this.modifierKeys, ['ctrl'])) {
|
||||||
|
document.execCommand('copy')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
|
||||||
case 'R':
|
case 'R':
|
||||||
if (InputManager.HasModifierCombo(this.modifierKeys, ['ctrl'])) {
|
if (InputManager.HasModifierCombo(this.modifierKeys, ['ctrl'])) {
|
||||||
location.reload()
|
location.reload()
|
||||||
@@ -149,8 +157,12 @@ export class InputManager {
|
|||||||
break
|
break
|
||||||
|
|
||||||
case 'F5':
|
case 'F5':
|
||||||
location.reload()
|
if (InputManager.HasModifierCombo(this.modifierKeys, ['alt'])) {
|
||||||
return
|
location.reload()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// redirect input to a running program instead
|
// redirect input to a running program instead
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
* Provides necessary parsing functions
|
* Provides necessary parsing functions
|
||||||
*/
|
*/
|
||||||
export class InputParser {
|
export class InputParser {
|
||||||
static Tokenize(input: string) {
|
// static Tokenize(input: string) {
|
||||||
|
|
||||||
}
|
// }
|
||||||
|
|
||||||
static Parse(tokens: string[]) {
|
// static Parse(tokens: string[]) {
|
||||||
|
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -6,6 +6,24 @@ import type { CursorPosition, CursorStyle } from './CursorProperties'
|
|||||||
|
|
||||||
export class Terminal {
|
export class Terminal {
|
||||||
public static readonly Version = "0.2.2"
|
public static readonly Version = "0.2.2"
|
||||||
|
private static readonly AnsiColors = [
|
||||||
|
'#000000',
|
||||||
|
'#800000',
|
||||||
|
'#008000',
|
||||||
|
'#808000',
|
||||||
|
'#000080',
|
||||||
|
'#800080',
|
||||||
|
'#008080',
|
||||||
|
'#ffffff',
|
||||||
|
'#808080',
|
||||||
|
'#ff0000',
|
||||||
|
'#00ff00',
|
||||||
|
'#ffff00',
|
||||||
|
'#0000ff',
|
||||||
|
'#ff00ff',
|
||||||
|
'#00ffff',
|
||||||
|
'#ffffff',
|
||||||
|
]
|
||||||
|
|
||||||
private terminal: HTMLElement
|
private terminal: HTMLElement
|
||||||
private cursor: HTMLElement
|
private cursor: HTMLElement
|
||||||
@@ -16,6 +34,9 @@ export class Terminal {
|
|||||||
private lineElements: HTMLElement[] = []
|
private lineElements: HTMLElement[] = []
|
||||||
private lineWrapped: boolean[] = []
|
private lineWrapped: boolean[] = []
|
||||||
private lineStyles: Map<number, Map<number, string>> = new Map()
|
private lineStyles: Map<number, Map<number, string>> = new Map()
|
||||||
|
private currentForeground: string | null = null
|
||||||
|
private currentBackground: string | null = null
|
||||||
|
private currentCellStyle: string | null = null
|
||||||
private scrollPending = false
|
private scrollPending = false
|
||||||
private cursorRange: Range | null = null
|
private cursorRange: Range | null = null
|
||||||
|
|
||||||
@@ -76,6 +97,7 @@ export class Terminal {
|
|||||||
const width = this.GetWrapWidth()
|
const width = this.GetWrapWidth()
|
||||||
let row = this.cursorPosition.row
|
let row = this.cursorPosition.row
|
||||||
let col = this.cursorPosition.col
|
let col = this.cursorPosition.col
|
||||||
|
const style = this.currentCellStyle
|
||||||
|
|
||||||
while (col >= width) {
|
while (col >= width) {
|
||||||
this.EnsureLine(row)
|
this.EnsureLine(row)
|
||||||
@@ -112,6 +134,7 @@ export class Terminal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.lines[row] = line
|
this.lines[row] = line
|
||||||
|
this.ApplyStyleRange(row, col, chunk.length, style)
|
||||||
this.UpdateLine(row)
|
this.UpdateLine(row)
|
||||||
|
|
||||||
remaining = remaining.slice(chunk.length)
|
remaining = remaining.slice(chunk.length)
|
||||||
@@ -157,6 +180,7 @@ export class Terminal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.lines[row] = line
|
this.lines[row] = line
|
||||||
|
this.ApplyStyleRange(row, col, 1, this.currentCellStyle)
|
||||||
this.UpdateLine(row)
|
this.UpdateLine(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,6 +224,102 @@ export class Terminal {
|
|||||||
this.UpdateCursor()
|
this.UpdateCursor()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ApplyCellStyleCodes(values: number[]) {
|
||||||
|
const params = values.length === 0 ? [0] : values
|
||||||
|
let foreground = this.currentForeground
|
||||||
|
let background = this.currentBackground
|
||||||
|
|
||||||
|
let i = 0
|
||||||
|
while (i < params.length) {
|
||||||
|
const code = params[i]
|
||||||
|
if (!Number.isFinite(code)) {
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === 0) {
|
||||||
|
foreground = null
|
||||||
|
background = null
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === 39) {
|
||||||
|
foreground = null
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === 49) {
|
||||||
|
background = null
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code >= 30 && code <= 37) {
|
||||||
|
foreground = Terminal.AnsiColors[code - 30]
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code >= 90 && code <= 97) {
|
||||||
|
foreground = Terminal.AnsiColors[code - 90 + 8]
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code >= 40 && code <= 47) {
|
||||||
|
background = Terminal.AnsiColors[code - 40]
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code >= 100 && code <= 107) {
|
||||||
|
background = Terminal.AnsiColors[code - 100 + 8]
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === 38 || code === 48) {
|
||||||
|
const isForeground = code === 38
|
||||||
|
const mode = params[i + 1]
|
||||||
|
if (mode === 2) {
|
||||||
|
const red = params[i + 2]
|
||||||
|
const green = params[i + 3]
|
||||||
|
const blue = params[i + 4]
|
||||||
|
if (Number.isFinite(red) && Number.isFinite(green) && Number.isFinite(blue)) {
|
||||||
|
const color = Terminal.ToRgbColor(red, green, blue)
|
||||||
|
if (isForeground)
|
||||||
|
foreground = color
|
||||||
|
else
|
||||||
|
background = color
|
||||||
|
}
|
||||||
|
i += 5
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode === 5) {
|
||||||
|
const index = params[i + 2]
|
||||||
|
const color = Terminal.GetAnsi256Color(index)
|
||||||
|
if (color) {
|
||||||
|
if (isForeground)
|
||||||
|
foreground = color
|
||||||
|
else
|
||||||
|
background = color
|
||||||
|
}
|
||||||
|
i += 3
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
this.currentForeground = foreground
|
||||||
|
this.currentBackground = background
|
||||||
|
this.UpdateCurrentStyle()
|
||||||
|
}
|
||||||
|
|
||||||
InsertCell(char: string) {
|
InsertCell(char: string) {
|
||||||
const width = this.GetWrapWidth()
|
const width = this.GetWrapWidth()
|
||||||
let row = this.cursorPosition.row
|
let row = this.cursorPosition.row
|
||||||
@@ -220,6 +340,7 @@ export class Terminal {
|
|||||||
|
|
||||||
line = line.slice(0, col) + char[0] + line.slice(col)
|
line = line.slice(0, col) + char[0] + line.slice(col)
|
||||||
this.lines[row] = line
|
this.lines[row] = line
|
||||||
|
this.ApplyInsertStyle(row, col, this.currentCellStyle)
|
||||||
this.UpdateLine(row)
|
this.UpdateLine(row)
|
||||||
this.ReflowFromRow(row)
|
this.ReflowFromRow(row)
|
||||||
}
|
}
|
||||||
@@ -245,6 +366,7 @@ export class Terminal {
|
|||||||
|
|
||||||
line = line.slice(0, removeIndex) + line.slice(removeIndex + 1)
|
line = line.slice(0, removeIndex) + line.slice(removeIndex + 1)
|
||||||
this.lines[row] = line
|
this.lines[row] = line
|
||||||
|
this.ApplyRemoveStyle(row, removeIndex)
|
||||||
this.UpdateLine(row)
|
this.UpdateLine(row)
|
||||||
this.ReflowFromRow(row)
|
this.ReflowFromRow(row)
|
||||||
}
|
}
|
||||||
@@ -514,6 +636,119 @@ export class Terminal {
|
|||||||
this.UpdateLine(renderRow)
|
this.UpdateLine(renderRow)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private UpdateCurrentStyle() {
|
||||||
|
const styles: string[] = []
|
||||||
|
if (this.currentForeground)
|
||||||
|
styles.push(`color: ${this.currentForeground}`)
|
||||||
|
if (this.currentBackground)
|
||||||
|
styles.push(`background-color: ${this.currentBackground}`)
|
||||||
|
|
||||||
|
this.currentCellStyle = styles.length > 0 ? styles.join('; ') : null
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApplyStyleRange(row: number, startCol: number, length: number, style: string | null) {
|
||||||
|
if (!Number.isFinite(row) || !Number.isFinite(startCol) || length <= 0)
|
||||||
|
return
|
||||||
|
|
||||||
|
let rowStyles = this.lineStyles.get(row)
|
||||||
|
if (!rowStyles && !style)
|
||||||
|
return
|
||||||
|
|
||||||
|
if (!rowStyles) {
|
||||||
|
rowStyles = new Map()
|
||||||
|
this.lineStyles.set(row, rowStyles)
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = Math.max(0, Math.floor(startCol))
|
||||||
|
const end = start + Math.max(0, Math.floor(length))
|
||||||
|
for (let col = start; col < end; col++) {
|
||||||
|
if (style)
|
||||||
|
rowStyles.set(col, style)
|
||||||
|
else
|
||||||
|
rowStyles.delete(col)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rowStyles.size === 0)
|
||||||
|
this.lineStyles.delete(row)
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApplyInsertStyle(row: number, col: number, style: string | null) {
|
||||||
|
const rowStyles = this.lineStyles.get(row)
|
||||||
|
if (!rowStyles && !style)
|
||||||
|
return
|
||||||
|
|
||||||
|
const updated = new Map<number, string>()
|
||||||
|
if (rowStyles) {
|
||||||
|
for (const [index, cssText] of rowStyles) {
|
||||||
|
if (index >= col)
|
||||||
|
updated.set(index + 1, cssText)
|
||||||
|
else
|
||||||
|
updated.set(index, cssText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (style)
|
||||||
|
updated.set(col, style)
|
||||||
|
|
||||||
|
if (updated.size > 0)
|
||||||
|
this.lineStyles.set(row, updated)
|
||||||
|
else
|
||||||
|
this.lineStyles.delete(row)
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApplyRemoveStyle(row: number, col: number) {
|
||||||
|
const rowStyles = this.lineStyles.get(row)
|
||||||
|
if (!rowStyles)
|
||||||
|
return
|
||||||
|
|
||||||
|
const updated = new Map<number, string>()
|
||||||
|
for (const [index, cssText] of rowStyles) {
|
||||||
|
if (index === col)
|
||||||
|
continue
|
||||||
|
if (index > col)
|
||||||
|
updated.set(index - 1, cssText)
|
||||||
|
else
|
||||||
|
updated.set(index, cssText)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updated.size > 0)
|
||||||
|
this.lineStyles.set(row, updated)
|
||||||
|
else
|
||||||
|
this.lineStyles.delete(row)
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ToRgbColor(red: number, green: number, blue: number): string {
|
||||||
|
const clamp = (value: number) => Math.min(255, Math.max(0, Math.round(value)))
|
||||||
|
const r = clamp(red)
|
||||||
|
const g = clamp(green)
|
||||||
|
const b = clamp(blue)
|
||||||
|
return `rgb(${r}, ${g}, ${b})`
|
||||||
|
}
|
||||||
|
|
||||||
|
private static GetAnsi256Color(code: number): string | null {
|
||||||
|
if (!Number.isFinite(code))
|
||||||
|
return null
|
||||||
|
|
||||||
|
const index = Math.floor(code)
|
||||||
|
if (index < 0 || index > 255)
|
||||||
|
return null
|
||||||
|
|
||||||
|
if (index < 16)
|
||||||
|
return Terminal.AnsiColors[index]
|
||||||
|
|
||||||
|
if (index <= 231) {
|
||||||
|
const offset = index - 16
|
||||||
|
const r = Math.floor(offset / 36)
|
||||||
|
const g = Math.floor((offset % 36) / 6)
|
||||||
|
const b = offset % 6
|
||||||
|
const levels = [0, 95, 135, 175, 215, 255]
|
||||||
|
return `rgb(${levels[r]}, ${levels[g]}, ${levels[b]})`
|
||||||
|
}
|
||||||
|
|
||||||
|
const grayscale = 8 + (index - 232) * 10
|
||||||
|
return `rgb(${grayscale}, ${grayscale}, ${grayscale})`
|
||||||
|
}
|
||||||
|
|
||||||
private GetCursorXOffset(): number {
|
private GetCursorXOffset(): number {
|
||||||
const row = this.cursorPosition.row
|
const row = this.cursorPosition.row
|
||||||
const col = this.cursorPosition.col
|
const col = this.cursorPosition.col
|
||||||
|
|||||||
Reference in New Issue
Block a user