Refactor battery script.

This commit is contained in:
2015-11-30 23:38:00 +01:00
parent eaf92a34bb
commit acbb6b3275

View File

@@ -1,78 +1,77 @@
#!/bin/bash #!/bin/bash
# shitty. this is used for both creating a part of the status conkyrc AND for
# creating nice output if you click on the status bar
# what i done depends on the first paramter
rundir="$RUNDIR/batwarn" rundir="$RUNDIR/batwarn"
logfile="$LOGDIR/batwarn.log" logfile="$LOGDIR/batwarn.log"
[[ ! -d "$rundir" ]] && mkdir -p "$rundir" _PATH_WARN_1="$rundir/batwarn1"
_PATH_WARN_2="$rundir/batwarn2"
PATH_WARN_1="$rundir/batwarn1" _THRESHOLD1=25
PATH_WARN_2="$rundir/batwarn2" _THRESHOLD2=5
THRESHOLD1=25
THRESHOLD2=5
acpi_output=$(acpi -b) acpi_output=$(acpi -b)
log() {
printf '%s\n' "[$(date +%FT%T)] $*" >> $logfile
}
charging() {
[[ "$status" == "C" ]]
}
discharging() {
[[ "$status" == "D" ]]
}
threshold1() {
(( $percent <= $_THRESHOLD1 ))
}
threshold2() {
(( $percent <= $_THRESHOLD2 ))
}
notify_threshold1() {
notify-send --icon dialog-warning "Battery below ${_THRESHOLD2}%" --expire-time 0
}
notify_threshold2() {
notify-send --icon dialog-warning "Battery below ${_THRESHOLD1}%" --expire-time 30000
}
if [[ -n "$acpi_output" ]] ; then if [[ -n "$acpi_output" ]] ; then
has_battery=1 has_battery=1
percent="$(echo "$acpi_output" | grep -oP '\d+(?=%)')" percent="$(printf '%s' "$acpi_output" | grep -oP '\d+(?=%)')"
if [[ $percent == 100 ]] ; then if [[ $percent == 100 ]] ; then
status="Full" status="F"
else else
status="$(echo "$acpi_output" | grep -oP '(?<=: )\w+(?=,)' )" status="$(printf '%s' "$acpi_output" | grep -oP '(?<=: )\w+(?=,)' | cut -c 1 )"
fi fi
shortstatus="$(echo $status | cut -c 1)"
else else
has_battery=0 has_battery=0
fi fi
log() { if (( has_battery )) ; then
echo [$(date +%FT%T)] "$*" >> $logfile
}
charging() {
[[ "$shortstatus" == "C" ]]
}
discharging() {
[[ "$shortstatus" == "D" ]]
}
threshold1() {
(( $percent <= $THRESHOLD1 ))
}
threshold2() {
(( $percent <= $THRESHOLD2 ))
}
short() {
if (( has_battery )) ; then
if discharging ; then if discharging ; then
if threshold2 ; then if threshold2 ; then
if [[ ! -f "$PATH_WARN_2" ]] ; then if [[ ! -f "$_PATH_WARN_2" ]] ; then
log "battery fell below $THRESHOLD2 percent. issuing warning." log "battery fell below $_THRESHOLD2 percent. issuing warning."
echo > "$PATH_WARN_2" touch "$_PATH_WARN_2"
notify-send --icon dialog-warning "Battery below ${THRESHOLD2}%" --expire-time 0 notify_threshold1
fi fi
elif threshold1 ; then elif threshold1 ; then
if [[ ! -f "$PATH_WARN_1" ]] ; then if [[ ! -f "$_PATH_WARN_1" ]] ; then
log "battery fell below $THRESHOLD1 percent. issuing warning." log "battery fell below $_THRESHOLD1 percent. issuing warning."
echo > "$PATH_WARN_1" touch "$_PATH_WARN_1"
notify-send --icon dialog-warning "Battery below ${THRESHOLD1}%" --expire-time 30000 notify_threshold2
fi fi
fi fi
fi elif charging ; then
if [[ -f "$_PATH_WARN_1" ]] ; then
if charging ; then
if [[ -f "$PATH_WARN_1" ]] ; then
log "charging now. resetting warnings." log "charging now. resetting warnings."
rm "$PATH_WARN_1" rm "$_PATH_WARN_1"
fi fi
[[ -f "$PATH_WARN_2" ]] && rm "$PATH_WARN_2" [[ -f "$_PATH_WARN_2" ]] && rm "$_PATH_WARN_2"
fi fi
if threshold2 ; then if threshold2 ; then
@@ -86,14 +85,12 @@ short() {
urgent=0 urgent=0
fi fi
echo "${shortstatus} ${percent}%" printf '%s\n' "${status} ${percent}%"
echo printf '%s\n'
echo $color printf '%s\n' $color
(( $urgent )) && exit 33 (( $urgent )) && exit 33
exit 0 else
else printf '%s\n' "no battery"
echo "no battery" printf '%s\n'
fi printf '%s\n' "#FFFFFF"
} fi
short