- fixed a bug in dnTaverseTree where children would get pushed into the result in reverse order
- added an inode display to the `printTree` algorithm - removed a redundant type in *`signal.ts`* - improved `resolveInode` to now take `ShellState` instead of `FileSystem` and added an optional `abs` *`boolean`* argument to determine if the retuned path should be normalized or absolute - implemented a builtin `TreeNode` quicksort function - changed the `TreeNodeType` to a numeric enum - added a standalone `Posix` class that holds a const with character weights compliant with posix
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { getCurrentSession } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
||||
import type { FileSystem } from "../filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
||||
@@ -53,15 +55,15 @@ export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
return currentNode.inode;
|
||||
}
|
||||
|
||||
export function resolveInode(fs: FileSystem, inode: number): string {
|
||||
const node = fs.getNode(inode);
|
||||
export function resolveInode(state: ShellState, inode: number, abs: boolean = false): string {
|
||||
const node = state.filesystem.getNode(inode);
|
||||
const components: string[] = [];
|
||||
|
||||
if (node === undefined) return "";
|
||||
|
||||
let currentInode = inode;
|
||||
while (currentInode !== 0) {
|
||||
const node = fs.getNode(currentInode) as TreeNode;
|
||||
const node = state.filesystem.getNode(currentInode) as TreeNode;
|
||||
if (!node) break;
|
||||
|
||||
components.push(node.name);
|
||||
@@ -72,7 +74,19 @@ export function resolveInode(fs: FileSystem, inode: number): string {
|
||||
return components.join("");
|
||||
}
|
||||
|
||||
components.reverse();
|
||||
components.unshift("");
|
||||
return components.join("/");
|
||||
components.reverse().unshift("");
|
||||
let result = components.join("/");
|
||||
|
||||
if (abs) return result;
|
||||
|
||||
const home = resolveInode(state, getCurrentSession(state).user.home, true);
|
||||
const cwd = resolveInode(state, getCurrentSession(state).cwd, true);
|
||||
|
||||
if (result.startsWith(home)) {
|
||||
result = `~${result.slice(home.length)}`;
|
||||
} else if (result.startsWith(cwd) && cwd !== "/") {
|
||||
result = `.${result.slice(cwd.length)}`;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import type { FileNode, TreeNode } from "@core/filesystem/treenode";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import { Posix } from "@termos/util/posix";
|
||||
|
||||
export enum SortTreeNodeBy {
|
||||
NAME = "name",
|
||||
INODE = "inode",
|
||||
SIZE = "size",
|
||||
EXTENSION = "extension",
|
||||
TYPE = "type",
|
||||
MTIME = "mtime",
|
||||
ATIME = "atime",
|
||||
CTIME = "ctime",
|
||||
}
|
||||
|
||||
export function sortTreeNodes<T extends TreeNode | number>(
|
||||
state: ShellState,
|
||||
arr: T[],
|
||||
r: boolean = false,
|
||||
by: SortTreeNodeBy = SortTreeNodeBy.NAME,
|
||||
): T[] {
|
||||
if (arr.length === 0) return [];
|
||||
|
||||
const nodes: TreeNode[] = [];
|
||||
|
||||
if (typeof arr === "number") {
|
||||
for (const e of arr as number[]) {
|
||||
const node = state.filesystem.getNode(e);
|
||||
if (!node) continue;
|
||||
|
||||
nodes.push(node);
|
||||
}
|
||||
} else {
|
||||
nodes.push(...(arr as TreeNode[]));
|
||||
}
|
||||
|
||||
nodeQS(nodes, r, by, 0, nodes.length - 1);
|
||||
|
||||
if (typeof arr === "number") {
|
||||
return nodes.map(n => n.inode) as T[];
|
||||
} else {
|
||||
return nodes as T[];
|
||||
}
|
||||
}
|
||||
|
||||
function nodeQS(arr: TreeNode[], r: boolean, by: SortTreeNodeBy, start: number, end: number): void {
|
||||
if (end <= start) return;
|
||||
|
||||
const pivot = partitionNodeArray(arr, r, by, start, end);
|
||||
nodeQS(arr, r, by, start, pivot - 1);
|
||||
nodeQS(arr, r, by, pivot + 1, end);
|
||||
}
|
||||
|
||||
function partitionNodeArray(part: TreeNode[], r: boolean, by: SortTreeNodeBy, start: number, end: number): number {
|
||||
const pivot = part[end];
|
||||
let i = start - 1;
|
||||
|
||||
for (let j = start; j <= end; j++) {
|
||||
if (compareTreeNode(part[j]!, pivot!, by, r) < 0) {
|
||||
i++;
|
||||
const temp = part[i]!;
|
||||
part[i] = part[j]!;
|
||||
part[j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
const temp = part[i]!;
|
||||
part[i] = part[end]!;
|
||||
part[end] = temp;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
function compareTreeNode(a: TreeNode, b: TreeNode, by: SortTreeNodeBy, r: boolean): number {
|
||||
switch (by) {
|
||||
case SortTreeNodeBy.NAME: {
|
||||
const A: string = a.name.startsWith(".") ? a.name.slice(1) : a.name;
|
||||
const B: string = b.name.startsWith(".") ? b.name.slice(1) : b.name;
|
||||
const min = Math.min(A.length, B.length);
|
||||
|
||||
for (let i = 0; i < min; i++) {
|
||||
const charA = Posix.GetCharWeight(A.charAt(i));
|
||||
const charB = Posix.GetCharWeight(B.charAt(i));
|
||||
|
||||
if (charA !== charB) {
|
||||
return r ? charB - charA : charA - charB;
|
||||
}
|
||||
}
|
||||
|
||||
return r ? B.length - A.length : A.length - B.length;
|
||||
}
|
||||
case SortTreeNodeBy.MTIME:
|
||||
case SortTreeNodeBy.ATIME:
|
||||
case SortTreeNodeBy.CTIME: {
|
||||
const A: number = a[by];
|
||||
const B: number = b[by];
|
||||
|
||||
return r ? A - B : B - A;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.SIZE: {
|
||||
const A: number = (a as FileNode).content.byteSize();
|
||||
const B: number = (b as FileNode).content.byteSize();
|
||||
|
||||
return r ? A - B : B - A;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.EXTENSION: {
|
||||
const A: string = a.name.split(".").pop() ?? "";
|
||||
const B: string = b.name.split(".").pop() ?? "";
|
||||
const min = Math.min(A.length, B.length);
|
||||
|
||||
for (let i = 0; i < min; i++) {
|
||||
const charA = Posix.GetCharWeight(A.charAt(i));
|
||||
const charB = Posix.GetCharWeight(B.charAt(i));
|
||||
|
||||
if (charA !== charB) {
|
||||
return r ? charB - charA : charA - charB;
|
||||
}
|
||||
}
|
||||
|
||||
return r ? B.length - A.length : A.length - B.length;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.INODE: {
|
||||
return r ? a.inode - b.inode : b.inode - a.inode;
|
||||
}
|
||||
|
||||
case SortTreeNodeBy.TYPE: {
|
||||
return r ? b.type - a.type : a.type - b.type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,9 @@ export function dnTraverseTree(fs: FileSystem, root: number): number[] {
|
||||
result.push(node.inode);
|
||||
|
||||
if (node.type === TreeNodeType.Directory) {
|
||||
for (const value of Object.values(node.children)) {
|
||||
stack.push(value);
|
||||
const children = Object.values(node.children);
|
||||
for (let i = children.length - 1; i >= 0; i--) {
|
||||
stack.push(children[i] as number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user