diff --git a/src/fs/Item.ts b/src/fs/Item.ts index 362bd19..9e59fd0 100644 --- a/src/fs/Item.ts +++ b/src/fs/Item.ts @@ -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) diff --git a/src/program/Cat.ts b/src/program/Cat.ts index e94f27b..cce5c39 100644 --- a/src/program/Cat.ts +++ b/src/program/Cat.ts @@ -3,21 +3,26 @@ import type { SimpleStream } from '../utils/SimpleStream' import { Program } from './Program' export class Cat extends Program { - async Exec(_: SimpleStream, stdout: SimpleStream, __: Item, args: string[]): Promise { - let item: Item = await Item.open(args[1]) + async Exec(_: SimpleStream, stdout: SimpleStream, workdir: Item, args: string[]): Promise { + 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 } diff --git a/src/program/Cd.ts b/src/program/Cd.ts index 26facb6..91435d3 100644 --- a/src/program/Cd.ts +++ b/src/program/Cd.ts @@ -12,10 +12,14 @@ export class Cd extends Program { } async Exec(_: SimpleStream, stdout: SimpleStream, workdir: Item, args: string[]): Promise { + // open the target directory, default to workdir + const item = args[1] - ? await Item.openDir(args[1].startsWith('/') - ? args[1] - : `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`) + ? await Item.openDir(Item.NormalizePath( + args[1].startsWith('/') + ? args[1] + : `${workdir.GetPath()}/${args[1]}` + )) : workdir if (args[1] && !item.IsDirectory()) { diff --git a/src/program/Echo.ts b/src/program/Echo.ts index 754ff7f..b4f33ae 100644 --- a/src/program/Echo.ts +++ b/src/program/Echo.ts @@ -8,27 +8,6 @@ export class Echo extends Program { } async Exec(_: SimpleStream, stdout: SimpleStream, __: Item, args: string[]): Promise { - // 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 diff --git a/src/program/Ls.ts b/src/program/Ls.ts index a1476a3..9d30a9b 100644 --- a/src/program/Ls.ts +++ b/src/program/Ls.ts @@ -8,10 +8,14 @@ export class Ls extends Program { } async Exec(_: SimpleStream, stdout: SimpleStream, workdir: Item, args: string[]): Promise { + // open the target directory, default to workdir + const item = args[1] - ? await Item.openDir(args[1].startsWith('/') - ? args[1] - : `${workdir.GetPath()}${workdir.GetPath().endsWith('/') ? '' : '/'}${args[1]}`) + ? await Item.openDir(Item.NormalizePath( + args[1].startsWith('/') + ? args[1] + : `${workdir.GetPath()}/${args[1]}` + )) : workdir if (args[1] && !item.IsDirectory()) { @@ -25,10 +29,10 @@ export class Ls extends Program { } stdout.emit(`-> Listing contents of item: '${item.GetPath()}'\n`) - stdout.emit(` [ drwx name ]\n`) + 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 diff --git a/src/program/Mkdir.ts b/src/program/Mkdir.ts index 49be8fd..82cc2bb 100644 --- a/src/program/Mkdir.ts +++ b/src/program/Mkdir.ts @@ -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`) diff --git a/src/program/Rm.ts b/src/program/Rm.ts index f6607ed..80dea36 100644 --- a/src/program/Rm.ts +++ b/src/program/Rm.ts @@ -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() diff --git a/src/program/Touch.ts b/src/program/Touch.ts index 9b655e4..abe8a12 100644 --- a/src/program/Touch.ts +++ b/src/program/Touch.ts @@ -8,11 +8,12 @@ export class Touch extends Program { } async Exec(_: SimpleStream, stdout: SimpleStream, workdir: Item, args: string[]): Promise { - 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") diff --git a/src/styles/cursor.scss b/src/styles/cursor.scss index 17fccc1..0a5a290 100644 --- a/src/styles/cursor.scss +++ b/src/styles/cursor.scss @@ -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); }