[Feature] show folder files count and file size
I just add some features showing the file count in folder and file size, downloaded zip size and git clone size.I don't know whether those data is redundant.
it's easy to implement, when the node type is tree,calculate all the node count of this tree recusively. when the node type if blob, humanFilesize the node.size varible
zip size is the size when you click download as zip button, recusively add up all the node.size under root node
the clone size is size one repo takes in github server(not you run git clone all the files size), you could see you own repo size in https://github.com/settings/repositories. this size comes from https://api.github.com/repos/OWNER/REPO api
some of the format size code
//one file size formatter
export function humanFileSize(bytes:number, si=true, dp=1) {
if (bytes===null||bytes===undefined ) {
return ''
}
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10**dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + ' ' + units[u];
}
// get the repo's zip size
export function calculateTotalSize(data) {
let total = 0;
const traverse = (nodes) => {
for (const node of nodes) {
if (node.type === 'blob' && typeof node.size === 'number') {
total += node.size;
} else if (node.type === 'tree' && node.contents) {
traverse(node.contents);
}
}
};
traverse(data.nodes);
return total;
}
//format the size through `https://api.github.com/repos/OWNER/REPO` api
export function formatKiloFileSize(kilobytes) {
const units = ['KB', 'MB', 'GB', 'TB', 'PB'];
let size = kilobytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
const formattedSize = size.toFixed(2).replace(/\.?0+$/, '');
return `${formattedSize} ${units[unitIndex]}`;
}
Interesting... Why would you care about file size? Personally I would clone/download whole repo if I need to work based on it.
Caring about file size because too large file will make the webpage stuck,if we see that file is large,we can avoid opening it. (I'm' just browsing through files, find how others implement something, and I dont need to clone the whole repo)