diff --git a/src/core/command_registry.ts b/src/core/command_registry.ts index 68663d8..22d2b1b 100644 --- a/src/core/command_registry.ts +++ b/src/core/command_registry.ts @@ -1,8 +1,10 @@ import type { CommandDescriptor } from "./command"; import { descriptor as cat } from "./commands/cat"; import { descriptor as cd } from "./commands/cd"; +import { descriptor as clear } from "./commands/clear"; import { descriptor as echo } from "./commands/echo"; import { descriptor as ls } from "./commands/ls"; +import { descriptor as pwd } from "./commands/pwd"; import { descriptor as testasync } from "./commands/testasync"; const reg = new Map(); @@ -12,6 +14,8 @@ reg.set(cd.name, cd); reg.set(testasync.name, testasync); reg.set(ls.name, ls); reg.set(cat.name, cat); +reg.set(pwd.name, pwd); +reg.set(clear.name, clear); Object.freeze(reg); export const COMMAND_REGISTRY: ReadonlyMap = reg; diff --git a/src/core/commands/__template__.ts b/src/core/commands/__template__.ts index efc4c13..bb4c487 100644 --- a/src/core/commands/__template__.ts +++ b/src/core/commands/__template__.ts @@ -6,7 +6,6 @@ import type { ShellError } from "@core/util/errors"; export const COMMAND_NAME = ""; -//Need to implement escape sequences async function main(io: CommandIO, state: ShellState, args: string[]): Promise> { const result: ResultSuccess = createSuccessResult(COMMAND_NAME); diff --git a/src/core/commands/clear.ts b/src/core/commands/clear.ts new file mode 100644 index 0000000..d822316 --- /dev/null +++ b/src/core/commands/clear.ts @@ -0,0 +1,23 @@ +import type { CommandDescriptor, CommandOutput } from "@core/command"; +import type { CommandIO } from "@core/command_io"; +import { createSuccessResult, type ResultSuccess } from "@core/result"; +import type { ShellState } from "@core/shell_state"; +import type { ShellError } from "@core/util/errors"; + +export const COMMAND_NAME = "clear"; + +async function main(io: CommandIO, state: ShellState, __args: string[]): Promise> { + io.writeStdout(new TextEncoder().encode("\x1b[2J\x1b[0;0H")); + + const result: ResultSuccess = createSuccessResult(COMMAND_NAME); + + return { + state: state, + result, + }; +} + +export const descriptor: CommandDescriptor = { + fn: main, + name: COMMAND_NAME, +}; diff --git a/src/core/commands/ls.ts b/src/core/commands/ls.ts index 6dd3b82..3a12f2d 100644 --- a/src/core/commands/ls.ts +++ b/src/core/commands/ls.ts @@ -7,52 +7,211 @@ import type { ShellState } from "@core/shell_state"; import type { ShellError } from "@core/util/errors"; import { resolvePath } from "@core/util/path_resolver"; import { SortTreeNodeBy, sortTreeNodes } from "@core/util/sort"; +import { concatUint8Arrays } from "@termos/util/concat"; import { convertToUnit } from "@termos/util/format_unit"; export const COMMAND_NAME = "ls"; async function main(io: CommandIO, state: ShellState, args: string[]): Promise> { const entries: { inode: number; path: string }[] = []; + const paths: string[] = []; const e = new TextEncoder(); - const flags: Set = new Set(); + let options = { + all: false, + almostAll: false, + long: false, + noSort: false, + noOwner: false, + noGroup: false, + sortBy: SortTreeNodeBy.NAME, + humanReadable: false, + f: false, + x: false, + timestamp: "atime", + reverse: false, + recursive: false, + quoteName: false, + appendIndicator: false, + numericUidGid: false, + literal: false, + inode: false, + si: false, + dereference: false, + fullTime: false, + }; - for (const arg of args) { + for (let arg of args) { + let val; if (arg.startsWith("--")) { - arg.slice(2); - flags.add(arg); + arg = arg.slice(2); + + if (arg.match(/\w+[=]{1}\w+/g)) { + const flag = arg.split("=")[0]; + val = arg.split("=")[1] ?? ""; + + if (flag === "time" && isValidTimestamp(val)) { + options.timestamp = val; + continue; + } + if (flag === "sort" && isValidSortMethod(val)) { + options.sortBy = val as SortTreeNodeBy; + continue; + } + return { + state, + result: createErrorResult("UsageError", `Invalid options`, {}, COMMAND_NAME), + }; + } + + switch (arg) { + case "all": + options.all = true; + break; + case "almost-all": + options.almostAll = true; + break; + case "no-group": + options.noGroup = true; + break; + case "human-readable": + options.humanReadable = true; + break; + case "reverse": + options.reverse = true; + break; + case "recursive": + options.recursive = true; + break; + case "quote-name": + options.quoteName = true; + break; + case "numeric-uid-gid": + options.numericUidGid = true; + break; + case "inode": + options.inode = true; + break; + case "dereference": + options.dereference = true; + break; + case "literal": + options.literal = true; + break; + case "si": + options.si = true; + options.humanReadable = true; + break; + case "full-time": + options.fullTime = true; + break; + case "help": + // handled separately + break; + default: + return { + state, + result: createErrorResult("UsageError", `Invalid options`, {}, COMMAND_NAME), + }; + break; + } } else if (arg.startsWith("-")) { - arg.slice(1); - flags.add(arg); + arg = arg.slice(1); + + for (const c of arg) { + switch (c) { + case "l": + options.long = true; + break; + case "a": + options.all = true; + break; + case "A": + options.almostAll = true; + break; + case "c": + options.timestamp = "ctime"; + break; + case "U": + options.noSort = true; + break; + case "g": + options.noOwner = true; + options.long = true; + break; + case "G": + options.noGroup = true; + break; + case "h": + options.humanReadable = true; + break; + case "f": + options.f = true; + options.noSort = true; + options.all = true; + break; + case "x": + options.x = true; + break; + case "X": + options.sortBy = SortTreeNodeBy.EXTENSION; + break; + case "u": + options.timestamp = "atime"; + break; + case "t": + options.sortBy = options.timestamp as SortTreeNodeBy; + break; + case "S": + options.sortBy = SortTreeNodeBy.SIZE; + break; + case "r": + options.reverse = true; + break; + case "R": + options.recursive = true; + break; + case "Q": + options.quoteName = true; + break; + case "p": + options.appendIndicator = true; + break; + case "o": + options.noGroup = true; + options.long = true; + break; + case "n": + options.numericUidGid = true; + break; + case "N": + options.literal = true; + break; + case "i": + options.inode = true; + break; + case "L": + options.dereference = true; + break; + case "m": + // -m: comma separated list (not in options yet) + break; + default: + return { + state, + result: createErrorResult("UsageError", `Invalid options`, {}, COMMAND_NAME), + }; + } + } + } else { + paths.push(arg); } } - const vars = flags.filter( - // Match at least 1 character between each side of a singular = character - f => - f.match(/\w+[=]{1}\w+/g) && - // Check if descriptors flags array contains an object with the name as the flags key and has takesValue set to true - descriptor.flags?.some(flag => flag.name === f.split("=")[0] && flag.takesValue === true), - ); - - const fSet = new Set(flags.filter(f => descriptor.flags?.some(flag => flag.name === f) && !vars.includes(f))); - - if (fSet.size + vars.length < flags.length) { - return { - state, - result: createErrorResult("UsageError", `Invalid options`, {}, COMMAND_NAME), - }; - } - const session = getCurrentSession(state); - const timeVar = vars.find(f => f.startsWith("time"))?.split("=")[1]; - const sortVar = vars.find(f => f.startsWith("sort"))?.split("=")[1]; - let sortBy: SortTreeNodeBy = sortVar && isValidSortMethod(sortVar) ? sortVar : SortTreeNodeBy.NAME; - let timestamp: SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME = - timeVar && isValidTimestamp(timeVar) ? timeVar : SortTreeNodeBy.ATIME; - - if (args.length === 0) { + // This needs fixing, separate paths into a separate array from args + if (paths.length === 0) { entries.push({ inode: session.cwd, path: "." }); } else { for (const arg of args) { @@ -72,7 +231,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise state.filesystem.getNode(c[1])!); - if (!fSet.has("f") || !fSet.has("U")) - children = sortTreeNodes(state, children, fSet.has("r") || fSet.has("reverse"), sortBy); + if (!options.noSort) children = sortTreeNodes(state, children, options.reverse, options.sortBy); - if (fSet.has("a") || fSet.has("A") || fSet.has("all") || fSet.has("almost-all") || fSet.has("f")) - children = children.filter(c => !c.name.startsWith(".")); + if (!options.all && !options.almostAll) children = children.filter(c => !c.name.startsWith(".")); - if (fSet.has("l") || fSet.has("g") || fSet.has("o")) { - longMode(io, state, children, fSet); - io.writeStdout(e.encode("\n")); + if (options.long) { + longMode(io, state, children); + //io.writeStdout(e.encode("\n")); } - if (fSet.has("R") || fSet.has("recursive")) { + if (options.recursive) { for (const child of children) { if (child.type === TreeNodeType.Directory) { let path; @@ -126,154 +277,172 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise c.inode.toString().length)); + + const maxSizeWidth = Math.max( + ...nodes.map(c => (c.type === TreeNodeType.File ? c.content.byteSize().toString().length : 4)), + ); + + const maxChildrenWidth = Math.max( + ...nodes.map(c => + c.type === TreeNodeType.Directory ? Object.entries(c.children).length.toString().length : 2, + ), + ); + + const namePadding = nodes.every(c => !c.name.includes(" ")); + + for (const node of nodes) { + //Inode + if (options.inode) { + const inode = node.inode.toString().padStart(maxInodeWidth, " "); + buff.push(e.encode(inode + " ")); + //io.writeStdout(e.encode(inode + " ")); + } + + // Mode + const mode: string = parseMode(node.mode); + + switch (node.type) { + case TreeNodeType.Directory: + buff.push(e.encode("d" + mode + " ")); + break; + case TreeNodeType.File: + buff.push(e.encode("-" + mode + " ")); + break; + } + + // Children amnt (later will be amnt of links) + const childAmnt = + node.type === TreeNodeType.Directory + ? `${Object.entries(node.children).length.toString().padStart(maxChildrenWidth, " ")} ` + : `${"10".padStart(maxChildrenWidth, " ")} `; + + buff.push(e.encode(childAmnt)); + + // owner group + if (options.noGroup) { + buff.push(e.encode(`${state.users.get(node.owner)?.username} `)); + } else if (options.noOwner) { + buff.push(e.encode(`${state.groups.get(node.group)?.groupname} `)); + } else if (options.numericUidGid) { + buff.push(e.encode(`${node.owner} ${node.group} `)); + } else { + buff.push( + e.encode(`${state.users.get(node.owner)?.username} ${state.groups.get(node.group)?.groupname} `), + ); + } + + //Size + // Holy abomination, pad the stringified bytesize number with whitespaces so the actual length of the string matches across all rows + // Sets the folder size to an arbitrary 4096, could change it later to smth else + let size; + if (options.humanReadable) { + const bytes = node.type === TreeNodeType.File ? node.content.byteSize() : 4096; + size = convertToUnit(bytes, 1, options.si ? 1000 : 1024); + } else { + size = + node.type === TreeNodeType.File + ? `${node.content.byteSize().toString().padStart(maxSizeWidth, " ")} ` + : `${"4096".padStart(maxSizeWidth, " ")} `; + } + + buff.push(e.encode(size)); + + //Timestamp + // for now only mtime, need a helper function for all the formatting later on + const date = getDateTimestamp(node, options.timestamp); + if (options.fullTime) { + buff.push( + // Month + 1 because January is 0 + e.encode( + `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()} `, + ), + ); + } else { + buff.push( + // Month + 1 because January is 0 + e.encode(`${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()} `), + ); + } + + //Name + if (options.quoteName) { + buff.push( + e.encode( + `"${node.type === TreeNodeType.Directory ? node.name + '"' + (options.appendIndicator ? "/" : "") : node.name + '"'}\n`, + ), + ); + } else { + const unpadded = `${node.name.includes(" ") && !options.literal ? "'" + node.name + "'" : node.name}`; + const name = `${!namePadding ? (unpadded.includes(" ") ? unpadded : " " + unpadded) : unpadded}`; + buff.push( + e.encode( + `${node.type === TreeNodeType.Directory ? name + (options.appendIndicator ? "/" : "") : name}\n`, + ), + ); + } + } + + const out = concatUint8Arrays(buff); + io.writeStdout(out); + } + + function getDateTimestamp(node: TreeNode, ts: string): Date { + if (ts === "atime") return new Date(node.atime); + if (ts === "ctime") return new Date(node.ctime); + if (ts === "mtime") return new Date(node.mtime); + return new Date(); + } + + function parseMode(mode: number) { + const owner = (mode >> 6) & 7; + const group = (mode >> 3) & 7; + const other = mode & 7; + + const map = (bits: number) => { + let s = ""; + s += bits & 4 ? "r" : "-"; + s += bits & 2 ? "w" : "-"; + s += bits & 1 ? "x" : "-"; + return s; + }; + + return map(owner) + map(group) + map(other); + } + + function isValidSortMethod(value: string): value is SortTreeNodeBy { + return Object.values(SortTreeNodeBy).includes(value as SortTreeNodeBy); + } + + function isValidTimestamp( + value: string, + ): value is SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME { + return Object.values(SortTreeNodeBy).includes( + value as SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME, + ); + } } //function normalMode(io: CommandIO, state: ShellState, flags: string[], args: string[]): void {} -function longMode(io: CommandIO, state: ShellState, nodes: TreeNode[], flags: Set): void { - const e = new TextEncoder(); - - if (flags.has("a") || flags.has("all") || flags.has("f")) { - const cwdNode = state.filesystem.getNode(nodes[0]!.parent)!; - const parentNode = state.filesystem.getNode(cwdNode.parent)!; - - const parent: TreeNode = { ...parentNode, name: ".." }; - nodes.unshift(parent); - - const cwd: TreeNode = { ...cwdNode, name: "." }; - nodes.unshift(cwd); - } - - // Calculate the string size of the biggerst node in the array; - // Sets 4 length for directories because the arbitrary size is 4096 later in the code - const maxInodeWidth = Math.max(...nodes.map(c => c.inode.toString().length)); - - const maxSizeWidth = Math.max( - ...nodes.map(c => (c.type === TreeNodeType.File ? c.content.byteSize().toString().length : 4)), - ); - - const maxChildrenWidth = Math.max( - ...nodes.map(c => - c.type === TreeNodeType.Directory ? Object.entries(c.children).length.toString().length : 2, - ), - ); - - for (const node of nodes) { - if (flags.has("i") || flags.has("inode")) { - const inode = node.inode.toString().padStart(maxInodeWidth, " "); - io.writeStdout(e.encode(inode + " ")); - } - - const mode: string = parseMode(node.mode); - - switch (node.type) { - case TreeNodeType.Directory: - io.writeStdout(e.encode("d" + mode + " ")); - break; - case TreeNodeType.File: - io.writeStdout(e.encode("-" + mode + " ")); - break; - } - - const childAmnt = - node.type === TreeNodeType.Directory - ? `${Object.entries(node.children).length.toString().padStart(maxChildrenWidth, " ")} ` - : `${"10".padStart(maxChildrenWidth, " ")} `; - - io.writeStdout(e.encode(childAmnt)); - - if (flags.has("o") || flags.has("G") || flags.has("no-group")) { - io.writeStdout(e.encode(`${state.users.get(node.owner)?.username} `)); - } else if (flags.has("g")) { - io.writeStdout(e.encode(`${state.groups.get(node.group)?.groupname} `)); - } else if (flags.has("n") || flags.has("numeric-uid-gid")) { - io.writeStdout(e.encode(`${node.owner} ${node.group} `)); - } else { - io.writeStdout( - e.encode(`${state.users.get(node.owner)?.username} ${state.groups.get(node.group)?.groupname} `), - ); - } - - // Holy abomination, pad the stringified bytesize number with whitespaces so the actual length of the string matches across all rows - // Sets the folder size to an arbitrary 4096, could change it later to smth else - let size; - if (flags.has("h") || flags.has("human-readable")) { - const bytes = node.type === TreeNodeType.File ? node.content.byteSize() : 4096; - size = convertToUnit(bytes, 1, flags.has("si") ? 1000 : 1024); - } else { - size = - node.type === TreeNodeType.File - ? `${node.content.byteSize().toString().padStart(maxSizeWidth, " ")} ` - : `${"4096".padStart(maxSizeWidth, " ")} `; - } - - io.writeStdout(e.encode(size)); - - // for now only mtime, need a helper function for all the formatting later on - const mtime = new Date(node.mtime); - if (flags.has("full-time")) { - io.writeStdout( - // Month + 1 because January is 0 - e.encode( - `${mtime.getFullYear()}-${mtime.getMonth() + 1}-${mtime.getDate()} ${mtime.getHours()}:${mtime.getMinutes()} `, - ), - ); - } else { - io.writeStdout( - // Month + 1 because January is 0 - e.encode(`${mtime.getMonth() + 1}-${mtime.getDate()} ${mtime.getHours()}:${mtime.getMinutes()} `), - ); - } - const appendSlash = flags.has("p"); - if (flags.has("Q") || flags.has("quote-name")) { - io.writeStdout( - e.encode( - `"${node.type === TreeNodeType.Directory ? node.name + '"' + (appendSlash ? "/" : "") : node.name + '"'}\n`, - ), - ); - } else if (!flags.has("N") || !flags.has("literal")) { - io.writeStdout( - e.encode( - `${node.type === TreeNodeType.Directory ? node.name + (appendSlash ? "/" : "") : node.name}\n`, - ), - ); - } else { - const name = `${node.name.includes(" ") ? "'" + node.name + "'" : node.name}`; - io.writeStdout( - e.encode(`${node.type === TreeNodeType.Directory ? name + (appendSlash ? "/" : "") : name}\n`), - ); - } - } -} - -function parseFlags() {} - -function parseMode(mode: number) { - const owner = (mode >> 6) & 7; - const group = (mode >> 3) & 7; - const other = mode & 7; - - const map = (bits: number) => { - let s = ""; - s += bits & 4 ? "r" : "-"; - s += bits & 2 ? "w" : "-"; - s += bits & 1 ? "x" : "-"; - return s; - }; - - return map(owner) + map(group) + map(other); -} - -function isValidSortMethod(value: string): value is SortTreeNodeBy { - return Object.values(SortTreeNodeBy).includes(value as SortTreeNodeBy); -} - -function isValidTimestamp(value: string): value is SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME { - return Object.values(SortTreeNodeBy).includes( - value as SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME, - ); -} - export const descriptor: CommandDescriptor = { fn: main, flags: [ diff --git a/src/core/commands/pwd.ts b/src/core/commands/pwd.ts new file mode 100644 index 0000000..c8d4c77 --- /dev/null +++ b/src/core/commands/pwd.ts @@ -0,0 +1,41 @@ +import type { CommandDescriptor, CommandOutput } from "@core/command"; +import type { CommandIO } from "@core/command_io"; +import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result"; +import { getCurrentSession } from "@core/session_manager"; +import type { ShellState } from "@core/shell_state"; +import type { InvalidArgumentsError, ShellError } from "@core/util/errors"; +import { resolveInode } from "@core/util/path_resolver"; + +export const COMMAND_NAME = "pwd"; + +//Need to implement escape sequences +async function main(io: CommandIO, state: ShellState, args: string[]): Promise> { + if (args.length > 0) { + return { + state, + result: createErrorResult( + "InvalidArguments", + `Expected aruments 0, got ${args.length}.`, + { expected: "pwd", actual: `pwd ${args.toString().replace(/[,]/g, " ")}` }, + COMMAND_NAME, + ), + } satisfies CommandOutput; + } + + const session = getCurrentSession(state); + const path = resolveInode(state, session.cwd); + + io.writeStdout(new TextEncoder().encode(path)); + + const result: ResultSuccess = createSuccessResult(COMMAND_NAME); + + return { + state: state, + result, + }; +} + +export const descriptor: CommandDescriptor = { + fn: main, + name: COMMAND_NAME, +}; diff --git a/src/core/util/path_resolver.ts b/src/core/util/path_resolver.ts index d18e6c0..0bfdcd6 100644 --- a/src/core/util/path_resolver.ts +++ b/src/core/util/path_resolver.ts @@ -28,6 +28,11 @@ export function resolvePath(state: ShellState, path: string): number { continue; } + if (name === "~") { + currentNode = state.filesystem.getNode(session.user.home) as DirectoryNode; + continue; + } + const inode = currentNode.children[name]; if (!inode) diff --git a/src/index.ts b/src/index.ts index 0b2b47c..b7032b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,28 +17,27 @@ import { createInitialShellState, type ShellState } from "@core/shell_state"; import { resolveInodeToHome } from "@core/util/path_resolver"; import { deserializeFileSystem } from "@shell/serializer"; import { Shell } from "@shell/shell"; -import { printTree } from "./util/tree_visualizer"; -const rawJson = `{"atime": 1786341244,"children": [{"atime": 1786341264,"children": [{"atime": 1786341330,"children": [],"content": null,"ctime": 1786341330,"group": 1000,"inode": 2,"mode": 493,"mtime": 1786341330,"name": "subdir1","owner": 1000,"parent": 1,"type": "directory"}],"content": null,"ctime": 1786341330,"group": 1000,"inode": 1,"mode": 493,"mtime": 1786341330,"name": "folder1","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341269,"children": [{"atime": 1786341344,"children": [],"content": null,"ctime": 1786341344,"group": 1000,"inode": 4,"mode": 493,"mtime": 1786341344,"name": "subdir2","owner": 1000,"parent": 3,"type": "directory"},{"atime": 1786341345,"children": [{"atime": 1786341356,"children": [],"content": [],"ctime": 1786341356,"group": 1000,"inode": 6,"mode": 420,"mtime": 1786341356,"name": "empty-foo","owner": 1000,"parent": 5,"type": "regular"}],"content": null,"ctime": 1786341356,"group": 1000,"inode": 5,"mode": 493,"mtime": 1786341356,"name": "subdir3","owner": 1000,"parent": 3,"type": "directory"}],"content": null,"ctime": 1786341345,"group": 1000,"inode": 3,"mode": 493,"mtime": 1786341345,"name": "folder2","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341312,"children": [],"content": [72,101,108,108,111,44,32,87,111,114,108,100,33,10],"ctime": 1786341312,"group": 1000,"inode": 7,"mode": 420,"mtime": 1786341312,"name": "some-file","owner": 1000,"parent": 0,"type": "regular"}],"content": null,"ctime": 1786341312,"group": 1000,"inode": 0,"mode": 493,"mtime": 1786341312,"name": "root","owner": 1000,"parent": 0,"type": "directory"}`; +const rawJson = `{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 1,"mode": 493,"mtime": 1757686809,"name": "run","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 2,"mode": 493,"mtime": 1757686772,"name": "dev","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 3,"mode": 493,"mtime": 1757686804,"name": "proc","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 4,"mode": 493,"mtime": 1757686785,"name": "lost+found","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 5,"mode": 493,"mtime": 1757686768,"name": "boot","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [{"atime": 1788458583,"children": [{"atime": 1788459229,"children": [],"content": [97,115,121,110,99,32,102,117,110,99,116,105,111,110,32,109,97,105,110,40,105,111,44,115,116,97,116,101,44,97,114,103,115,41,123,102,111,114,40,99,111,110,115,116,32,97,114,103,32,111,102,32,97,114,103,115,41,123,99,111,110,115,116,32,105,110,111,100,101,61,114,101,115,111,108,118,101,80,97,116,104,40,115,116,97,116,101,44,97,114,103,41,59,105,102,40,105,110,111,100,101,61,61,61,45,49,41,123,114,101,116,117,114,110,123,115,116,97,116,101,44,114,101,115,117,108,116,58,99,114,101,97,116,101,69,114,114,111,114,82,101,115,117,108,116,40,34,80,97,116,104,82,101,115,111,108,117,116,105,111,110,69,114,114,111,114,34,44,34,78,111,32,115,117,99,104,32,102,105,108,101,32,111,114,32,100,105,114,101,99,116,111,114,121,34,44,123,112,97,116,104,58,97,114,103,125,44,67,79,77,77,65,78,68,95,78,65,77,69,41,125,125,99,111,110,115,116,32,110,111,100,101,61,115,116,97,116,101,46,102,105,108,101,115,121,115,116,101,109,46,103,101,116,78,111,100,101,40,105,110,111,100,101,41,59,105,102,40,110,111,100,101,46,116,121,112,101,33,61,84,114,101,101,78,111,100,101,84,121,112,101,46,70,105,108,101,41,123,114,101,116,117,114,110,123,115,116,97,116,101,44,114,101,115,117,108,116,58,99,114,101,97,116,101,69,114,114,111,114,82,101,115,117,108,116,40,34,80,97,116,104,82,101,115,111,108,117,116,105,111,110,69,114,114,111,114,34,44,96,36,123,110,111,100,101,46,110,97,109,101,125,32,105,115,32,97,32,100,105,114,101,99,116,111,114,121,96,44,123,112,97,116,104,58,97,114,103,125,44,67,79,77,77,65,78,68,95,78,65,77,69,41,125,125,99,111,110,115,116,32,115,101,115,115,105,111,110,61,103,101,116,67,117,114,114,101,110,116,83,101,115,115,105,111,110,40,115,116,97,116,101,41,59,105,102,40,33,115,116,97,116,101,46,102,105,108,101,115,121,115,116,101,109,46,99,97,110,82,101,97,100,40,105,110,111,100,101,44,115,101,115,115,105,111,110,46,117,115,101,114,41,41,123,114,101,116,117,114,110,123,115,116,97,116,101,44,114,101,115,117,108,116,58,99,114,101,97,116,101,69,114,114,111,114,82,101,115,117,108,116,40,34,80,101,114,109,105,115,115,105,111,110,68,101,110,105,101,100,34,44,34,80,101,114,109,105,115,115,105,111,110,32,100,101,110,105,101,100,34,44,123,114,101,115,111,117,114,99,101,58,97,114,103,125,44,67,79,77,77,65,78,68,95,78,65,77,69,41,125,125,110,111,100,101,59,105,111,46,119,114,105,116,101,83,116,100,111,117,116,40,110,111,100,101,46,99,111,110,116,101,110,116,46,116,111,65,114,114,97,121,40,41,41,125,99,111,110,115,116,32,114,101,115,117,108,116,61,99,114,101,97,116,101,83,117,99,99,101,115,115,82,101,115,117,108,116,40,67,79,77,77,65,78,68,95,78,65,77,69,41,59,114,101,116,117,114,110,123,115,116,97,116,101,44,114,101,115,117,108,116,125,125],"ctime": 1788459246,"group": 1000,"inode": 8,"mode": 493,"mtime": 1788459147,"name": "cat","owner": 1000,"parent": 7,"type": "regular"}],"content": null,"ctime": 1788459229,"group": 1000,"inode": 7,"mode": 493,"mtime": 1788459229,"name": "bin","owner": 1000,"parent": 6,"type": "directory"}],"content": null,"ctime": 1788458583,"group": 1000,"inode": 6,"mode": 493,"mtime": 1788458583,"name": "usr","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 9,"mode": 493,"mtime": 1757686775,"name": "etc","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 10,"mode": 493,"mtime": 1757686830,"name": "srv","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 11,"mode": 493,"mtime": 1757686781,"name": "lib64","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 12,"mode": 493,"mtime": 1757686689,"name": "root","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 15,"mode": 493,"mtime": 1757766627,"name": ".config","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 16,"mode": 493,"mtime": 1757766646,"name": ".npm","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 17,"mode": 493,"mtime": 1757766597,"name": "Videos","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 18,"mode": 493,"mtime": 1757766631,"name": ".java","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 19,"mode": 493,"mtime": 1757766614,"name": ".cache","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 20,"mode": 493,"mtime": 1757766580,"name": "Downloads","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 21,"mode": 493,"mtime": 1757766577,"name": "Desktop","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 22,"mode": 493,"mtime": 1757766652,"name": ".ssh","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 23,"mode": 493,"mtime": 1757766551,"name": "Documents","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 24,"mode": 493,"mtime": 1757766620,"name": ".cargo","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 28,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 27,"type": "regular"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 27,"mode": 493,"mtime": 1759940182,"name": "This Website","owner": 1000,"parent": 26,"type": "directory"},{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 30,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 29,"type": "regular"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 29,"mode": 493,"mtime": 1759942574,"name": "Sheepo","owner": 1000,"parent": 26,"type": "directory"},{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 32,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 31,"type": "regular"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 31,"mode": 493,"mtime": 1759942576,"name": "SchoolProjectManager","owner": 1000,"parent": 26,"type": "directory"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 26,"mode": 493,"mtime": 1759940133,"name": "Websites","owner": 1000,"parent": 25,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 33,"mode": 493,"mtime": 1759939881,"name": "C#","owner": 1000,"parent": 25,"type": "directory"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 25,"mode": 493,"mtime": 1759939982,"name": "Projects","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 34,"mode": 493,"mtime": 1757766640,"name": ".local","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 35,"mode": 493,"mtime": 1757766591,"name": "Pictures","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 36,"mode": 493,"mtime": 1757766660,"name": ".var","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [{"atime": 1773013637,"children": [],"content": [78,101,118,101,114,32,103,111,110,110,97,32,103,105,118,101,32,121,111,117,32,117,112,10,78,101,118,101,114,32,103,111,110,110,97,32,108,101,116,32,121,111,117,32,100,111,119,110,10,78,101,118,101,114,32,103,111,110,110,97,32,114,117,110,32,97,114,111,117,110,100,32,97,110,100,32,100,101,115,101,114,116,32,121,111,117,10,78,101,118,101,114,32,103,111,110,110,97,32,109,97,107,101,32,121,111,117,32,99,114,121,10,78,101,118,101,114,32,103,111,110,110,97,32,115,97,121,32,103,111,111,100,98,121,101,10,78,101,118,101,114,32,103,111,110,110,97,32,116,101,108,108,32,97,32,108,105,101,32,97,110,100,32,104,117,114,116,32,121,111,117],"ctime": 1773013637,"group": 1000,"inode": 38,"mode": 493,"mtime": 1757812927,"name": "nggyu.lrc","owner": 1000,"parent": 37,"type": "regular"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 37,"mode": 493,"mtime": 1759939803,"name": "Music","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 39,"mode": 493,"mtime": 1757766636,"name": ".dotnet","owner": 1000,"parent": 14,"type": "directory"},{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 40,"mode": 493,"mtime": 1757780849,"name": ".bash_logout","owner": 1000,"parent": 14,"type": "regular"},{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 41,"mode": 493,"mtime": 1757780715,"name": ".bashrc","owner": 1000,"parent": 14,"type": "regular"},{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 42,"mode": 493,"mtime": 1757780715,"name": ".bash_profile","owner": 1000,"parent": 14,"type": "regular"},{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 43,"mode": 493,"mtime": 1757780889,"name": ".xinitrc","owner": 1000,"parent": 14,"type": "regular"},{"atime": 1773013637,"children": [],"content": [10],"ctime": 1773013637,"group": 1000,"inode": 44,"mode": 493,"mtime": 1757780811,"name": ".bash_history","owner": 1000,"parent": 14,"type": "regular"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 45,"mode": 493,"mtime": 1771137563,"name": "Test test","owner": 1000,"parent": 14,"type": "directory"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 14,"mode": 493,"mtime": 1771137563,"name": "kamil","owner": 1000,"parent": 13,"type": "directory"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 13,"mode": 493,"mtime": 1757766534,"name": "home","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 46,"mode": 493,"mtime": 1757686867,"name": "tmp","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 47,"mode": 493,"mtime": 1757686765,"name": "bin","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 48,"mode": 493,"mtime": 1757686874,"name": "var","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 49,"mode": 493,"mtime": 1757686823,"name": "sbin","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 50,"mode": 493,"mtime": 1757686863,"name": "sys","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 51,"mode": 493,"mtime": 1757686777,"name": "lib","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1773013637,"children": [],"content": null,"ctime": 1773013637,"group": 1000,"inode": 52,"mode": 493,"mtime": 1757686791,"name": "opt","owner": 1000,"parent": 0,"type": "directory"}],"content": null,"ctime": 1773013637,"group": 1000,"inode": 0,"mode": 493,"mtime": 1757686876,"name": "root_fs","owner": 1000,"parent": 0,"type": "directory"}`; const base = deserializeFileSystem(rawJson); const overlay: ReadonlyMap = new Map(); const fs = FileSystem.init(base, overlay, base.size - 1); -console.log(printTree(fs, 0)); +//console.log(printTree(fs, 0)); const users: Map = new Map(); const groups: Map = new Map(); const user: UserInfo = { - username: "root", + username: "kamil", uid: 1000, gid: 1000, // <- PRIMARY GID group: [1000], // <- GROUPS ARRAY - home: 3, + home: 14, }; const group: GroupInfo = { - groupname: "root", + groupname: "kamil", gid: 1000, members: [1000], // <- UID }; @@ -55,6 +54,9 @@ Shell.init(state); Shell.Instance().on("result", payload => { const decoded = new TextDecoder().decode(payload.result.stdout.toArray()); + if (payload.result.type === "error") { + console.log(payload.result.error.message + "\n"); + } process.stdout.write(decoded + "\n"); process.stdout.write(resolveInodeToHome(payload.state, getCurrentSession(payload.state).cwd) + "\n> "); }); @@ -70,7 +72,7 @@ Shell.Instance().on("result", payload => { process.stdout.write(decoded + "\n"); }); */ -//console.clear(); +console.clear(); process.stdout.write(resolveInodeToHome(state, getCurrentSession(state).cwd) + "\n> "); process.stdin.on("data", async data => { if (Shell.Instance().BoundPID !== null) { diff --git a/src/shell/process.ts b/src/shell/process.ts index 9dbe1e6..8682f5c 100644 --- a/src/shell/process.ts +++ b/src/shell/process.ts @@ -31,10 +31,11 @@ export class Process { this.CommandDescriptor = descriptor; this.Args = args; // Check if positive later and throw an error if so - this._MaxChunkByteSize = 1024; + this._MaxChunkByteSize = 8096; this._EventEmitter = new EventEmitter(); } + // WARNING, CHUNKING OUTPUT BREAKS FORMATING private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void { if (this._Settled) return;