doti3/bar/src/acpi.php
2019-01-28 11:13:11 +01:00

83 lines
2 KiB
PHP
Executable file

#!/usr/bin/php
<?php
require __DIR__.'/base/block.php';
$acpi = shell_exec('acpi');
if (!preg_match('`Discharging`', $acpi)) {
preg_match('`([0-9]+%)`', $acpi, $values);
if (!empty($values)) {
$charging = (int) $values[1];
if ($charging === 100) {
return;
}
if ($charging < 20) {
$foreground = '#333';
$background = color('critical');
} elseif ($charging < 50) {
$foreground = color('info');
$background = color('warning');
} else {
$foreground = '#333';
$background = color('normal');
}
$charging .= '% '.emoji(0xf0aa);
$text = span(' '.$charging.' ', $foreground, $background);
$block = block(
'acpi',
[
'full_text' => $text,
'color' => $color,
'separator' => false,
'separator_block_width' => 0,
]
);
} else {
$block = block(
'acpi',
[
'full_text' => '??? '.emoji(0xf0aa),
'color' => color('info'),
'separator' => false,
'separator_block_width' => 0,
]
);
}
} else {
preg_match('`([0-9]+%), ([0-9]+:[0-9]+)`', $acpi, $values);
if ($values[0] < 20) {
$foreground = '#333';
$background = color('critical');
} elseif ($values[0] < 50) {
$foreground = color('info');
$background = color('warning');
} else {
$foreground = '#333';
$background = color('normal');
}
$charging = $values[1];
$times = str_replace(':', 'h', $values[2]);
$text = span(' '.implode(' ', [$charging, $times, emoji(0xf0ab)]).' ', $foreground, $background);
$block = block(
'acpi',
[
'full_text' => $text,
'color' => $color,
'separator' => false,
'separator_block_width' => 0,
]
);
}
echo $block;