- Fixed sort.ts
- Fixed `cd`
This commit is contained in:
+10
-8
@@ -1,5 +1,6 @@
|
||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||
import type { CommandIO } from "@core/command_io";
|
||||
import type { NodePointer } from "@core/filesystem/node";
|
||||
import { createErrorResult, createSuccessResult } from "@core/result";
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import { ShellState, withCwd } from "@core/shell_state";
|
||||
@@ -26,16 +27,17 @@ async function main(__io: CommandIO, state: ShellState, args: string[]): Promise
|
||||
} satisfies CommandOutput<InvalidArgumentsError>;
|
||||
}
|
||||
|
||||
let inode: number;
|
||||
let ptr: NodePointer;
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
if (args[0] === undefined) {
|
||||
inode = session.user.home;
|
||||
ptr = session.user.home;
|
||||
} else if (args[0] === "-") {
|
||||
inode = session.pwd;
|
||||
ptr = session.pwd;
|
||||
} else {
|
||||
inode = resolvePath(state, args[0]);
|
||||
if (inode === -1) {
|
||||
try {
|
||||
ptr = resolvePath(state, args[0]);
|
||||
} catch (e) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult(
|
||||
@@ -48,21 +50,21 @@ async function main(__io: CommandIO, state: ShellState, args: string[]): Promise
|
||||
}
|
||||
}
|
||||
|
||||
if (state.filesystem.isFile(inode)) {
|
||||
if (state.filesystem.isFile(ptr)) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult("PathResolutionError", "Not a directory", { path: args[0]! }, COMMAND_NAME),
|
||||
} satisfies CommandOutput<PathResolutionError>;
|
||||
}
|
||||
|
||||
if (!state.filesystem.canExecute(inode, session.user)) {
|
||||
if (!state.filesystem.canExecute(ptr, session.user)) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult("PermissionDenied", "Permission denied", { resource: args[0]! }, COMMAND_NAME),
|
||||
} satisfies CommandOutput<PermissionDeniedError>;
|
||||
}
|
||||
|
||||
const returnState = withCwd(state, inode);
|
||||
const returnState = withCwd(state, ptr);
|
||||
|
||||
return {
|
||||
state: returnState,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||
import type { CommandIO } from "@core/command_io";
|
||||
import { type DirectoryNode } from "@core/filesystem/node";
|
||||
import { type DirectoryNode, type NodePointer } from "@core/filesystem/node";
|
||||
import { createErrorResult, createSuccessResult } from "@core/result";
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
@@ -13,7 +13,7 @@ import { convertToUnit } from "@termos/util/format_unit";
|
||||
export const COMMAND_NAME = "ls";
|
||||
|
||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||
const entries: { inode: number; path: string }[] = [];
|
||||
const entries: { ptr: NodePointer; path: string }[] = [];
|
||||
const paths: string[] = [];
|
||||
const e = new TextEncoder();
|
||||
|
||||
@@ -211,11 +211,11 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
if (paths.length === 0) {
|
||||
entries.push({ inode: session.cwd, path: "." });
|
||||
entries.push({ ptr: session.cwd, path: "." });
|
||||
} else {
|
||||
for (const path of paths) {
|
||||
const inode = resolvePath(state, path);
|
||||
if (inode === -1) {
|
||||
const ptr = resolvePath(state, path);
|
||||
if (!state.filesystem.exists(ptr)) {
|
||||
return {
|
||||
state,
|
||||
result: createErrorResult(
|
||||
@@ -226,7 +226,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
||||
),
|
||||
};
|
||||
}
|
||||
entries.push({ inode: inode, path });
|
||||
entries.push({ ptr, path });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,8 +246,8 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
||||
result: createSuccessResult(COMMAND_NAME),
|
||||
};
|
||||
|
||||
function listDirEntries(entry: { inode: number; path: string }): void {
|
||||
const node = state.filesystem.getNode(entry.inode) as DirectoryNode;
|
||||
function listDirEntries(entry: { ptr: NodePointer; path: string }): void {
|
||||
const node = state.filesystem.getNode(entry.ptr) as DirectoryNode;
|
||||
|
||||
if (options.recursive) io.writeStdout(e.encode(`${entry.path}:\n`));
|
||||
io.writeStdout(e.encode(`total ${Object.entries(node.children).length}\n`));
|
||||
|
||||
Reference in New Issue
Block a user