chore: fix path syntax in core programs
This commit is contained in:
@@ -15,7 +15,7 @@ export class Item {
|
||||
private children: string[] = []
|
||||
|
||||
private constructor(path: string) {
|
||||
this.path = this.normalizePath(path)
|
||||
this.path = Item.NormalizePath(path)
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -222,7 +222,7 @@ export class Item {
|
||||
return this.path
|
||||
}
|
||||
|
||||
private normalizePath(path: string): string {
|
||||
public static NormalizePath(path: string): string {
|
||||
if (!path.startsWith('/')) path = '/' + path
|
||||
|
||||
// Resolve . and .. segments.
|
||||
@@ -310,8 +310,6 @@ export class Item {
|
||||
.get(this.storageKey)
|
||||
|
||||
request.onsuccess = () => {
|
||||
console.log(request.result)
|
||||
|
||||
const parsed = request.result as ItemPayload | undefined
|
||||
if (parsed) {
|
||||
this.isDirectory = parsed.isDirectory
|
||||
@@ -319,6 +317,7 @@ export class Item {
|
||||
this.data = parsed.data
|
||||
this.children = parsed.children
|
||||
}
|
||||
|
||||
resolve()
|
||||
}
|
||||
request.onerror = () => reject(request.error)
|
||||
|
||||
@@ -3,21 +3,26 @@ import type { SimpleStream } from '../utils/SimpleStream'
|
||||
import { Program } from './Program'
|
||||
|
||||
export class Cat extends Program {
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
|
||||
let item: Item = await Item.open(args[1])
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
if (args.length < 2) {
|
||||
stdout.emit("cat: error: missing path argument\n")
|
||||
return 1
|
||||
}
|
||||
|
||||
const item = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||
|
||||
if (!(await item.Exists())) {
|
||||
stdout.emit(`cat: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||
return 1
|
||||
return 2
|
||||
}
|
||||
|
||||
if (item.IsDirectory()) {
|
||||
stdout.emit(`cat: error: can't read data from a directory; item ${item.GetPath()} is a directory.\n`)
|
||||
return 2
|
||||
return 3
|
||||
}
|
||||
|
||||
stdout.emit(`${item.ReadData()}`)
|
||||
stdout.emit('[ EOF ]\n')
|
||||
stdout.emit('<- EOF\n')
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -12,10 +12,14 @@ export class Cd extends Program {
|
||||
}
|
||||
|
||||
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(args[1].startsWith('/')
|
||||
? await Item.openDir(Item.NormalizePath(
|
||||
args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
||||
: `${workdir.GetPath()}/${args[1]}`
|
||||
))
|
||||
: workdir
|
||||
|
||||
if (args[1] && !item.IsDirectory()) {
|
||||
|
||||
@@ -8,27 +8,6 @@ export class Echo extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, __: Item, args: string[]): Promise<number> {
|
||||
// if (args.length < 2) {
|
||||
// stdout.emit("echo: error: missing path argument\n")
|
||||
// return 1
|
||||
// }
|
||||
|
||||
// const item = await Item.open(args[1].startsWith('/')
|
||||
// ? args[1]
|
||||
// : `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
||||
|
||||
// if (!(await item.Exists())) {
|
||||
// stdout.emit(`echo: error: item ${item.GetPath()} doesn't exist.\n`)
|
||||
// return 2
|
||||
// }
|
||||
|
||||
// if (item.IsDirectory()) {
|
||||
// stdout.emit(`echo: error: can't write data to a directory; item ${item.GetPath()} is a directory.\n`)
|
||||
// return 3
|
||||
// }
|
||||
|
||||
// await item.Write(args.slice(2).join(' '))
|
||||
|
||||
stdout.emit(args.slice(1).join(' ') + '\n')
|
||||
|
||||
return 0
|
||||
|
||||
@@ -8,10 +8,14 @@ export class Ls extends Program {
|
||||
}
|
||||
|
||||
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(args[1].startsWith('/')
|
||||
? await Item.openDir(Item.NormalizePath(
|
||||
args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
||||
: `${workdir.GetPath()}/${args[1]}`
|
||||
))
|
||||
: workdir
|
||||
|
||||
if (args[1] && !item.IsDirectory()) {
|
||||
@@ -28,7 +32,7 @@ export class Ls extends Program {
|
||||
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.GetName()}'\n`)
|
||||
stdout.emit(` | ${`${(entry.IsDirectory() ? 'd' : '-')}${(entry.IsReadable() ? 'r' : '-')}${(entry.IsWritable() ? 'w' : '-')}${(entry.IsExecutable() ? 'x' : '-')}`.padEnd(8, ' ')} ${entry.GetName()}\n`)
|
||||
})
|
||||
|
||||
return 0
|
||||
|
||||
@@ -13,11 +13,8 @@ export class Mkdir extends Program {
|
||||
return 1
|
||||
}
|
||||
|
||||
const item = await Item.openDir(args[1].startsWith('/')
|
||||
? args[1]
|
||||
: `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`)
|
||||
const item = await Item.openDir(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||
|
||||
stdout.emit(`does ${args[1]} exist? ${await item.Exists()}\n`)
|
||||
|
||||
if (await item.Exists()) {
|
||||
stdout.emit(`mkdir: directory ${item.GetPath()} already exists.\n`)
|
||||
|
||||
@@ -30,7 +30,7 @@ export class Rm extends Program {
|
||||
return 1
|
||||
}
|
||||
|
||||
stdout.emit(`removing: '${item.GetPath()}'\n`)
|
||||
stdout.emit(`-> removing: '${item.GetPath()}'\n`)
|
||||
|
||||
await item.Delete()
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ export class Touch extends Program {
|
||||
}
|
||||
|
||||
async Exec(_: SimpleStream<string>, stdout: SimpleStream<string>, workdir: Item, args: string[]): Promise<number> {
|
||||
stdout.emit(`touching children, please wait...\n`)
|
||||
if (args.length < 2) {
|
||||
stdout.emit("touch: error: missing path argument\n")
|
||||
return 1
|
||||
}
|
||||
|
||||
const path = args.slice(1).join()
|
||||
const item = await Item.open(path.includes('/') ? path : `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${path}`)
|
||||
stdout.emit(`does ${item.GetPath()} exist? ${await item.Exists()}\n`)
|
||||
const item = await Item.open(Item.NormalizePath(args[1].startsWith('/') ? args[1] : `${workdir.GetPath()}/${args[1]}`))
|
||||
|
||||
if (await item.Exists()) {
|
||||
stdout.emit("touch: the file already exists.\n")
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
@use "colors.scss" as colors;
|
||||
|
||||
#cursor {
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 1px;
|
||||
height: 20px;
|
||||
position: absolute;
|
||||
background: colors.$terminal-white;
|
||||
|
||||
top: 0;
|
||||
left: 0;
|
||||
|
||||
position: absolute;
|
||||
|
||||
backdrop-filter: invert(100%) saturate(0);
|
||||
transition: .2s cubic-bezier(0, 1, 0.3, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user