- ls fixed - fixed a bug where ~ wouldnt resolve properly - temporarily increased the process chunking treshold, because chunking breaks formatting - added clear - added pwd
117 lines
3.6 KiB
TypeScript
117 lines
3.6 KiB
TypeScript
import { getCurrentSession } from "@core/session_manager";
|
|
import type { ShellState } from "@core/shell_state";
|
|
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
|
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
|
|
|
export function resolvePath(state: ShellState, path: string): number {
|
|
const session = getCurrentSession(state);
|
|
const isAbsolute = path.startsWith("/");
|
|
|
|
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
|
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
|
|
|
if (pathComponents.length === 0 && isAbsolute) return 0;
|
|
if (pathComponents.length === 0) return session.cwd;
|
|
|
|
let currentNode = isAbsolute
|
|
? state.filesystem.getRoot()
|
|
: (state.filesystem.getNode(session.cwd) as DirectoryNode);
|
|
|
|
if (!currentNode) return -1;
|
|
|
|
for (let i = 0; i < pathComponents.length; i++) {
|
|
const name = pathComponents[i]!;
|
|
if (name === ".") continue;
|
|
|
|
if (name === "..") {
|
|
currentNode = state.filesystem.getNode(currentNode.parent) as DirectoryNode;
|
|
continue;
|
|
}
|
|
|
|
if (name === "~") {
|
|
currentNode = state.filesystem.getNode(session.user.home) as DirectoryNode;
|
|
continue;
|
|
}
|
|
|
|
const inode = currentNode.children[name];
|
|
|
|
if (!inode)
|
|
throw {
|
|
type: "NodeNotFound",
|
|
message: `Child node in ${currentNode.name} under name ${name} not found`,
|
|
timestamp: Date.now(),
|
|
inode: currentNode.inode,
|
|
} satisfies NodeNotFoundError;
|
|
if (state.filesystem.isFile(inode) && !(i === pathComponents.length - 1))
|
|
throw {
|
|
type: "NotADirectory",
|
|
message: `Child node ${name} is not a directory`,
|
|
timestamp: Date.now(),
|
|
inode: inode,
|
|
nodeType: TreeNodeType.File,
|
|
} satisfies NotADirectoryError;
|
|
|
|
currentNode = state.filesystem.getNode(inode) as DirectoryNode;
|
|
}
|
|
|
|
return currentNode.inode;
|
|
}
|
|
|
|
export function resolveInode(state: ShellState, inode: number): string {
|
|
const node = state.filesystem.getNode(inode);
|
|
const components: string[] = [];
|
|
|
|
if (node === undefined) return "";
|
|
|
|
let currentInode = inode;
|
|
while (currentInode !== 0) {
|
|
const node = state.filesystem.getNode(currentInode) as TreeNode;
|
|
if (!node) break;
|
|
|
|
components.push(node.name);
|
|
currentInode = node.parent;
|
|
}
|
|
if (components.length === 0) {
|
|
components.push("/");
|
|
return components.join("");
|
|
}
|
|
|
|
components.reverse().unshift("");
|
|
let result = components.join("/");
|
|
|
|
return result;
|
|
}
|
|
|
|
export function resolveInodeToHome(state: ShellState, inode: number): string {
|
|
const home = resolveInode(state, getCurrentSession(state).user.home);
|
|
let result = resolveInode(state, inode);
|
|
|
|
if (result.startsWith(home)) {
|
|
result = `~${result.slice(home.length)}`;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export function resolveInodeToCwd(state: ShellState, inode: number): string {
|
|
const cwd = resolveInode(state, getCurrentSession(state).cwd);
|
|
let result = resolveInode(state, inode);
|
|
|
|
if (result.startsWith(cwd)) {
|
|
result = `.${result.slice(cwd.length)}`;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export function resolveInodeRelative(state: ShellState, inode: number, relative: number): string {
|
|
const rel = resolveInode(state, relative);
|
|
let result = resolveInode(state, inode);
|
|
|
|
if (result.startsWith(rel)) {
|
|
result = `.${result.slice(rel.length)}`;
|
|
}
|
|
|
|
return result;
|
|
}
|