Last active
June 7, 2023 22:46
-
-
Save rudiedirkx/2372ea9bbe3a936a32a7f381618bde72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$home = rtrim(getenv('HOME'), '\\/'); | |
$moreDirs = array_slice($_SERVER['argv'], 1); | |
$output = `df -h`; | |
if (!preg_match('#(\d+)% +/\s#', "$output ", $match)) { | |
echo trim($output) . "\n"; | |
exit(1); | |
} | |
$curWhen = date('Y-m-d'); | |
$curMainPct = (int) $match[1]; | |
$curMores = array_map(function($dir) { | |
$output = `du -s $dir`; | |
$bytes = (int) trim($output); | |
return ceil($bytes / 1024); | |
}, $moreDirs); | |
if (file_exists($file = "$home/.main-disk-free")) { | |
$history = json_decode(trim(file_get_contents($file)), true) ?: []; | |
$history = array_map(fn($el) => (array) $el, $history); | |
$prevWhen = max(array_keys($history)); | |
$prevMainPct = $history[$prevWhen][0]; | |
$prevMores = array_slice($history[$prevWhen], 1); | |
} | |
else { | |
$prevWhen = '-'; | |
$prevMainPct = 0; | |
$prevMores = []; | |
} | |
$notify = $curMainPct != $prevMainPct; | |
if ($notify || $curMores != $prevMores) { | |
$history[$curWhen] = [$curMainPct, ...$curMores]; | |
file_put_contents($file, json_encode($history) . "\n"); | |
if ($notify) { | |
echo "$curMainPct%\n"; | |
if ($prevMainPct) { | |
echo "\n"; | |
echo "$prevMainPct% ($prevWhen)\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment