adileo / squirreldisk

Beautiful, Cross-Platform and Super Fast Disk Usage Analysis Tool - Built With Rust 🦀
https://squirreldisk.com
GNU Affero General Public License v3.0
767 stars 27 forks source link

Show more accurate values instead of GB #17

Open nkormakov opened 1 year ago

nkormakov commented 1 year ago

Hi, I would like to humanise file size display via snippet like this instead just dividing everything thrice but I'm not sure how to build this for mac, any guidance? If not, you can insert this everywhere size is calculated or I can make yolo PR without any tests.

function formatBytes(bytes: number, decimals = 2) {
    if (bytes < 1) return '0 B'

    const div = window.OS_TYPE === "Windows_NT" ? 1024 : 1000;
    const precision = decimals < 0 ? 0 : decimals
    const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    const order = Math.floor(Math.log(bytes) / Math.log(div))

    return `${parseFloat((bytes / Math.pow(div, order)).toFixed(precision))} ${units[order]}`
}