Functions/df.ps1

# Functions\df.ps1

function df {
    <#
    .SYNOPSIS
        CoolPS Disk Usage Monitor with i18n and progress bars.
    #>

    param()

    # 1. i18n
    $lang = $PSCulture
    $t = if ($lang -eq "zh-CN") { 
        @{ label = "磁盘状态"; drive = "驱动器"; total = "总计"; used = "已用"; free = "可用"; usage = "占用" } 
    }
    else { 
        @{ label = "Disk Usage"; drive = "Drive"; total = "Total"; used = "Used"; free = "Free"; usage = "Usage" } 
    }

    # 2. Get Disk Data (Fixed Drives only)
    $disks = Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" # 3 = Local Disk

    Write-Host ""
    Write-Host " $(ColorCyan)$($t.label):$(ColorReset)"
    Write-Host ""

    # Table Header
    # Drive(6) Total(10) Used(10) Free(10) Usage(Bar)
    $headerFormat = " $(ColorRed){0,-8} $(ColorOrange){1,10} $(ColorYellow){2,10} $(ColorGreen){3,10} $(ColorCyan){4}$(ColorReset)"
    Write-Host ($headerFormat -f $($t.drive), $($t.total), $($t.used), $($t.free), $($t.usage))
    Write-Host (" $(ColorGray)" + ("-" * 65) + "$(ColorReset)")

    foreach ($disk in $disks) {
        $total = $disk.Size
        $free = $disk.FreeSpace
        $used = $total - $free
        $percent = if ($total -gt 0) { [Math]::Round(($used / $total) * 100, 1) } else { 0 }

        # Progress Bar for each disk (15 blocks)
        $barWidth = 15
        $filled = [int][Math]::Floor($percent / (100 / $barWidth))
        $barStr = ("█" * $filled) + (" " * ($barWidth - $filled))
        
        # Color bar based on usage (Green < 70% < Yellow < 90% < Red)
        $barColor = if ($percent -gt 90) { ColorRed } elseif ($percent -gt 70) { ColorYellow } else { ColorGreen }

        # Output Row
        $rowFormat = " $(ColorRed){0,-8} $(ColorOrange){1,10} $(ColorYellow){2,10} $(ColorGreen){3,10} [${barColor}{4}$(ColorReset)] {5}%"
        Write-Host ($rowFormat -f $disk.DeviceID, 
            (Format-CoolSize $total), 
            (Format-CoolSize $used), 
            (Format-CoolSize $free), 
            $barStr, 
            $percent)
    }
    Write-Host ""
}