doti3/bar/src/acpi.php

83 lines
2 KiB
PHP
Raw Normal View History

2017-06-19 16:36:59 +02:00
#!/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)) {
2017-12-15 17:43:15 +01:00
$charging = (int) $values[1];
if ($charging === 100) {
return;
}
2017-06-19 16:36:59 +02:00
if ($charging < 20) {
2019-01-28 11:13:11 +01:00
$foreground = '#333';
$background = color('critical');
2017-06-19 16:36:59 +02:00
} elseif ($charging < 50) {
2019-01-28 11:13:11 +01:00
$foreground = color('info');
$background = color('warning');
2017-06-19 16:36:59 +02:00
} else {
2019-01-28 11:13:11 +01:00
$foreground = '#333';
$background = color('normal');
2017-06-19 16:36:59 +02:00
}
2019-01-28 11:13:11 +01:00
$charging .= '% '.emoji(0xf0aa);
$text = span(' '.$charging.' ', $foreground, $background);
2017-06-19 16:36:59 +02:00
$block = block(
2018-01-03 13:53:49 +01:00
'acpi',
2017-06-19 16:36:59 +02:00
[
2019-01-28 11:13:11 +01:00
'full_text' => $text,
2017-06-19 16:36:59 +02:00
'color' => $color,
2019-01-28 11:13:11 +01:00
'separator' => false,
'separator_block_width' => 0,
2017-06-19 16:36:59 +02:00
]
);
} else {
$block = block(
2018-01-03 13:53:49 +01:00
'acpi',
2017-06-19 16:36:59 +02:00
[
2019-01-28 11:13:11 +01:00
'full_text' => '??? '.emoji(0xf0aa),
2017-06-19 16:36:59 +02:00
'color' => color('info'),
2019-01-28 11:13:11 +01:00
'separator' => false,
'separator_block_width' => 0,
2017-06-19 16:36:59 +02:00
]
);
}
} else {
preg_match('`([0-9]+%), ([0-9]+:[0-9]+)`', $acpi, $values);
if ($values[0] < 20) {
2019-01-28 11:13:11 +01:00
$foreground = '#333';
$background = color('critical');
2017-06-19 16:36:59 +02:00
} elseif ($values[0] < 50) {
2019-01-28 11:13:11 +01:00
$foreground = color('info');
$background = color('warning');
2017-06-19 16:36:59 +02:00
} else {
2019-01-28 11:13:11 +01:00
$foreground = '#333';
$background = color('normal');
2017-06-19 16:36:59 +02:00
}
2019-01-28 11:13:11 +01:00
$charging = $values[1];
2017-06-19 16:36:59 +02:00
$times = str_replace(':', 'h', $values[2]);
2019-01-28 11:13:11 +01:00
$text = span(' '.implode(' ', [$charging, $times, emoji(0xf0ab)]).' ', $foreground, $background);
2017-06-19 16:36:59 +02:00
$block = block(
2018-01-03 13:53:49 +01:00
'acpi',
2017-06-19 16:36:59 +02:00
[
'full_text' => $text,
'color' => $color,
2019-01-28 11:13:11 +01:00
'separator' => false,
'separator_block_width' => 0,
2017-06-19 16:36:59 +02:00
]
);
}
echo $block;