Advertisement
Rnery

Refactoring battery status

Oct 24th, 2023
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.52 KB | Source Code | 0 0
  1. #!/usr/bin/env bash
  2. ##############################
  3. # Battery Status refactoring #
  4. ##############################
  5.  
  6. export DISPLAY=:0.0
  7.  
  8. get_ac_adapter_status() {
  9.     echo $(acpi -a | cut -d' ' -f3 | cut -d- -f1)
  10. }
  11.  
  12. get_battery_level() {
  13.     echo $(acpi -b | grep -P -o '[0-9]+(?=%)')
  14. }
  15.  
  16. notify_user() {
  17.     local message="$1"
  18.     local icon="$2"
  19.     notify-send -u critical "$message" -i "$icon"
  20. }
  21.  
  22. charge_above_threshold() {
  23.     local threshold="$1"
  24.     while true; do
  25.         AC_ADAPTER=$(get_ac_adapter_status)
  26.         BATTERY_LEVEL=$(get_battery_level)
  27.         if [ "$AC_ADAPTER" == "off" ]; then
  28.             break
  29.         elif [ "$BATTERY_LEVEL" -ge "$threshold" ]; then
  30.             notify_user "Desconecte o adaptador de energia! Nível da bateria: ${BATTERY_LEVEL}% (acima de $threshold%)" "battery-full-charged"
  31.         fi
  32.     done
  33. }
  34.  
  35. charge_below_threshold() {
  36.     local threshold="$1"
  37.     while true; do
  38.         AC_ADAPTER=$(get_ac_adapter_status)
  39.         BATTERY_LEVEL=$(get_battery_level)
  40.         if [ "$AC_ADAPTER" == "on" ]; then
  41.             break
  42.         elif [ "$BATTERY_LEVEL" -lt "$threshold" ]; then
  43.             notify_user "Conecte o adaptador de energia! Nível da bateria: ${BATTERY_LEVEL}% (abaixo de $threshold%)" "battery-caution"
  44.         fi
  45.     done
  46. }
  47.  
  48. while true; do
  49.     AC_ADAPTER=$(get_ac_adapter_status)
  50.     BATTERY_LEVEL=$(get_battery_level)
  51.  
  52.     if [ "$AC_ADAPTER" == "on" ]; then
  53.         charge_above_threshold 75
  54.     else
  55.         charge_below_threshold 25
  56.     fi
  57. done
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement