Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active June 7, 2023 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudiedirkx/2372ea9bbe3a936a32a7f381618bde72 to your computer and use it in GitHub Desktop.
Save rudiedirkx/2372ea9bbe3a936a32a7f381618bde72 to your computer and use it in GitHub Desktop.
<?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