-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminils.ts
128 lines (111 loc) · 3.32 KB
/
minils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const color: {
Blue: string;
Reset: string;
} = { Blue: "\u001b[34m", Reset: "\u001b[0m" };
const parts: {
OneSpace: string;
Slash: string;
NewLine: string;
Dot: string;
Empty: string;
} = { OneSpace: " ", Slash: "/", NewLine: "\n", Dot: ".", Empty: "" };
interface OSOriginListEntry extends Deno.DirEntry {
name: string;
}
interface DotCheckedMax {
entry: OSOriginListEntry;
size: number;
}
interface EditedDotCheckedMax {
entry: OSOriginListEntry;
size: number;
}
interface ViewMatrix {
rowsize: number;
colsize: number;
}
type Print = typeof print;
const print = (filename: string, next: boolean) => {
if (next) {
Deno.stdout.write(new TextEncoder().encode(filename + parts.NewLine));
} else {
Deno.stdout.write(new TextEncoder().encode(filename));
}
};
const dotCheck = (filename: string) => {
if (filename.indexOf(parts.Dot)) {
return true;
}
return false;
};
const pathCheck = (path: string) => {
if (path === undefined) {
return parts.Dot;
}
return path;
};
const tpusCols = async () => {
const cmd = Deno.run({ cmd: ["tput", "cols"], stdout: "piped" });
const columnsUintArray = await cmd.output();
cmd.close();
// Decode geted window size with tput cols and cast from decoded string to number
return Number(new TextDecoder().decode(columnsUintArray));
};
const main = async (path: string, windowsize: number, callback: Print) => {
const OSEntriesWithoutDotfiles: OSOriginListEntry[] = [];
for await (const directoryEntry of Deno.readDir(pathCheck(path))) {
if (dotCheck(directoryEntry.name)) {
OSEntriesWithoutDotfiles.push(directoryEntry);
}
}
// initialize Max
const maxentry = OSEntriesWithoutDotfiles.reduce((a, b) =>
a.name.length > b.name.length ? a : b
);
const Max: DotCheckedMax = {
entry: maxentry,
size: maxentry.name.length,
};
const EditedMax: EditedDotCheckedMax = { entry: Max.entry, size: Max.size };
EditedMax.size += 2;
// calc col and row size
const Matrix: ViewMatrix = { rowsize: 0, colsize: 0 };
Matrix.colsize = Math.floor(windowsize / EditedMax.size);
Matrix.rowsize = Math.ceil(OSEntriesWithoutDotfiles.length / Matrix.colsize);
// extract 2 vector directory entry from 1 vector directory entry
OSEntriesWithoutDotfiles.sort((a, b) => (a.name > b.name ? 1 : -1));
for (let i = 0; i < Matrix.rowsize; i++) {
if (i != 0) {
callback(parts.Empty, true);
}
for (let j = i; j < Matrix.colsize * Matrix.rowsize; j += Matrix.rowsize) {
if (OSEntriesWithoutDotfiles[j] === undefined) {
break;
}
// region layout
const addSize = Max.size - OSEntriesWithoutDotfiles[j].name.length;
let sizewithslash = 1;
if (OSEntriesWithoutDotfiles[j].isDirectory) {
OSEntriesWithoutDotfiles[j].name =
color.Blue +
OSEntriesWithoutDotfiles[j].name +
color.Reset +
parts.Slash;
sizewithslash -= 1;
}
for (let k = 0; k <= addSize + sizewithslash; k++) {
OSEntriesWithoutDotfiles[j].name += parts.OneSpace;
}
// endregion layout
callback(OSEntriesWithoutDotfiles[j].name, false);
}
}
};
try {
// top level await with Deno
const windowsize = await tpusCols();
await main(Deno.args[0], windowsize, print);
print(parts.Empty, true);
} catch (e) {
console.error(e.message);
}