Skip to content

Instantly share code, notes, and snippets.

@qolarnix
Created November 27, 2022 18:38
Show Gist options
  • Save qolarnix/2d07b5dcffa5ac325488ca3fcfd3cad0 to your computer and use it in GitHub Desktop.
Save qolarnix/2d07b5dcffa5ac325488ca3fcfd3cad0 to your computer and use it in GitHub Desktop.
FreeBSD Polybar Module - Battery
#!/usr/local/bin/php
<?php
// breakpoints for color to change
$bat_high = 71; // 71
$bat_mid = 36; // 36
$bat_low = 0;
// polybar color variables
$blue = '%{F#3A86FF}';
$green = '%{F#06D6A0}';
$orange = '%{F#FFD166}';
$red = '%{F#EF476F}';
// polybar syntax for end of output
$pb_str_end = '%{u-}';
/**
* Get battery status
*/
$apm_status = shell_exec('apm | grep line');
$apm_status_clean = str_replace(array("\r", "\n"), '', $apm_status);
if($apm_status_clean === 'AC Line status: on-line') {
$ac_on = true;
} else {
$ac_on = false;
}
/**
* Get battery percent
*/
$apm_percent = shell_exec('apm | grep %');
$apm_percent_arr = explode(' ', $apm_percent);
$apm_percent_clean = str_replace(array("\r", "\n", "%"), '', $apm_percent_arr[6]);
$bat_int = intval($apm_percent_clean);
/**
* Show results
*/
if($ac_on === false) {
if($bat_int >= $bat_high){ echo $green.$bat_int.'%'.$pb_str_end; }
elseif($bat_int >= $bat_mid){ echo $orange.$bat_int.'%'.$pb_str_end; }
elseif($bat_int >= $bat_low){ echo $red.$bat_int.'%'.$pb_str_end; }
} else {
echo $blue.$bat_int.'%'.$pb_str_end;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment