- Fixed ls (mostly)

- Fixed sorting
- Many minor changes

(need to fix the json serializer to include size in its results *the C++ one*)
This commit is contained in:
2026-11-10 16:19:10 +01:00
parent 492f1ba372
commit b161e42b3f
7 changed files with 179 additions and 226 deletions
+58 -44
View File
@@ -1,17 +1,22 @@
import type { CommandDescriptor, CommandOutput } from "@core/command";
import type { CommandIO } from "@core/command_io";
import { type DirectoryNode, type NodePointer } from "@core/filesystem/node";
import { NodePointer, NodeType, type DirectoryNode, type FileNode, type Node } from "@core/filesystem/node";
import { createErrorResult, createSuccessResult } from "@core/result";
import { getCurrentSession } from "@core/session_manager";
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 { SortNodeBy, sortNodes } from "@core/util/sort";
import { concatUint8Arrays } from "@termos/util/concat";
import { convertToUnit } from "@termos/util/format_unit";
export const COMMAND_NAME = "ls";
interface DirEntry {
name: string;
ptr: NodePointer;
}
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
const entries: { ptr: NodePointer; path: string }[] = [];
const paths: string[] = [];
@@ -24,7 +29,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
noSort: false,
noOwner: false,
noGroup: false,
sortBy: SortTreeNodeBy.NAME,
sortBy: SortNodeBy.NAME,
humanReadable: false,
f: false,
x: false,
@@ -55,7 +60,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
continue;
}
if (flag === "sort" && isValidSortMethod(val)) {
options.sortBy = val as SortTreeNodeBy;
options.sortBy = val as SortNodeBy;
continue;
}
return {
@@ -154,16 +159,16 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
options.x = true;
break;
case "X":
options.sortBy = SortTreeNodeBy.EXTENSION;
options.sortBy = SortNodeBy.EXTENSION;
break;
case "u":
options.timestamp = "atime";
break;
case "t":
options.sortBy = options.timestamp as SortTreeNodeBy;
options.sortBy = options.timestamp as SortNodeBy;
break;
case "S":
options.sortBy = SortTreeNodeBy.SIZE;
options.sortBy = SortNodeBy.SIZE;
break;
case "r":
options.reverse = true;
@@ -252,64 +257,77 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
if (options.recursive) io.writeStdout(e.encode(`${entry.path}:\n`));
io.writeStdout(e.encode(`total ${Object.entries(node.children).length}\n`));
let children: TreeNode[] = Object.entries(node.children).map(c => state.filesystem.getNode(c[1])!);
if (!options.noSort) children = sortTreeNodes(state, children, options.reverse, options.sortBy);
let children: DirEntry[] = Array.from(node.children.entries()).map(([name, inode]) => ({
name,
ptr: NodePointer.of(entry.ptr.device, inode),
}));
if (!options.noSort) children = sortNodes(state, children, options.reverse, options.sortBy);
if (!options.all && !options.almostAll) children = children.filter(c => !c.name.startsWith("."));
if (!options.all && !options.almostAll) {
children = children.filter(c => !c.name.startsWith("."));
}
if (options.long) {
longMode(io, state, children, node);
longMode(io, state, children);
if (options.recursive) io.writeStdout(e.encode("\n"));
}
if (options.recursive) {
for (const child of children) {
if (child.type === TreeNodeType.Directory) {
for (const c of children) {
const child = state.filesystem.getNode(c.ptr)!;
if (child.type === NodeType.Directory) {
let path;
if (entry.path === "/") {
path = "/" + child.name;
path = "/" + c.name;
} else if (entry.path.endsWith("/")) {
path = entry.path + child.name;
} else path = entry.path + "/" + child.name;
listDirEntries({ inode: child.inode, path });
path = entry.path + c.name;
} else path = entry.path + "/" + c.name;
listDirEntries({ ptr: c.ptr, path });
}
}
}
}
function longMode(io: CommandIO, state: ShellState, children: TreeNode[], current: TreeNode): void {
function longMode(io: CommandIO, state: ShellState, children: DirEntry[]): void {
const e = new TextEncoder();
const buff: Uint8Array[] = [];
const nodes: TreeNode[] = [...children];
const nodes: DirEntry[] = [...children];
if (options.all) {
/* if (options.all) {
const parentNode = state.filesystem.getNode(current.parent)!;
const parent: TreeNode = { ...parentNode, name: ".." };
const parent: NodePointer = { ...parentNode, name: ".." };
nodes.unshift(parent);
const cwd: TreeNode = { ...current, 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 maxInodeWidth = Math.max(...nodes.map(c => c.ptr.inode.toString().length));
const maxSizeWidth = Math.max(
...nodes.map(c => (c.type === TreeNodeType.File ? c.content.byteSize().toString().length : 4)),
...nodes.map(c =>
state.filesystem.getNode(c.ptr)!.type === NodeType.File
? (state.filesystem.getNode(c.ptr) as FileNode).size.toString().length
: 4,
),
);
const maxChildrenWidth = Math.max(
...nodes.map(c =>
c.type === TreeNodeType.Directory ? Object.entries(c.children).length.toString().length : 2,
state.filesystem.getNode(c.ptr)!.type === NodeType.Directory
? Object.entries((state.filesystem.getNode(c.ptr) as DirectoryNode)!.children).length
: 2,
),
);
const namePadding = nodes.every(c => !c.name.includes(" "));
for (const node of nodes) {
for (const n of nodes) {
//Inode
const node = state.filesystem.getNode(n.ptr)!;
if (options.inode) {
const inode = node.inode.toString().padStart(maxInodeWidth, " ");
buff.push(e.encode(inode + " "));
@@ -320,17 +338,17 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
const mode: string = parseMode(node.mode);
switch (node.type) {
case TreeNodeType.Directory:
case NodeType.Directory:
buff.push(e.encode("d" + mode + " "));
break;
case TreeNodeType.File:
case NodeType.File:
buff.push(e.encode("-" + mode + " "));
break;
}
// Children amnt (later will be amnt of links)
const childAmnt =
node.type === TreeNodeType.Directory
node.type === NodeType.Directory
? `${Object.entries(node.children).length.toString().padStart(maxChildrenWidth, " ")} `
: `${"10".padStart(maxChildrenWidth, " ")} `;
@@ -354,12 +372,12 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
// 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;
const bytes = node.type === NodeType.File ? node.size : 4096;
size = convertToUnit(bytes, 1, options.si ? 1000 : 1024);
} else {
size =
node.type === TreeNodeType.File
? `${node.content.byteSize().toString().padStart(maxSizeWidth, " ")} `
node.type === NodeType.File
? `${node.size.toString().padStart(maxSizeWidth, " ")} `
: `${"4096".padStart(maxSizeWidth, " ")} `;
}
@@ -386,15 +404,15 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
if (options.quoteName) {
buff.push(
e.encode(
`"${node.type === TreeNodeType.Directory ? node.name + '"' + (options.appendIndicator ? "/" : "") : node.name + '"'}\n`,
`"${node.type === NodeType.Directory ? n.name + '"' + (options.appendIndicator ? "/" : "") : n.name + '"'}\n`,
),
);
} else {
const unpadded = `${node.name.includes(" ") && !options.literal ? "'" + node.name + "'" : node.name}`;
const unpadded = `${n.name.includes(" ") && !options.literal ? "'" + n.name + "'" : n.name}`;
const name = `${!namePadding ? (unpadded.includes(" ") ? unpadded : " " + unpadded) : unpadded}`;
buff.push(
e.encode(
`${node.type === TreeNodeType.Directory ? name + (options.appendIndicator ? "/" : "") : name}\n`,
`${node.type === NodeType.Directory ? name + (options.appendIndicator ? "/" : "") : name}\n`,
),
);
}
@@ -404,7 +422,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
io.writeStdout(out);
}
function getDateTimestamp(node: TreeNode, ts: string): Date {
function getDateTimestamp(node: Node, 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);
@@ -427,16 +445,12 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
return map(owner) + map(group) + map(other);
}
function isValidSortMethod(value: string): value is SortTreeNodeBy {
return Object.values(SortTreeNodeBy).includes(value as SortTreeNodeBy);
function isValidSortMethod(value: string): value is SortNodeBy {
return Object.values(SortNodeBy).includes(value as SortNodeBy);
}
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 isValidTimestamp(value: string): value is SortNodeBy.ATIME | SortNodeBy.MTIME | SortNodeBy.CTIME {
return Object.values(SortNodeBy).includes(value as SortNodeBy.ATIME | SortNodeBy.MTIME | SortNodeBy.CTIME);
}
}