battery.sh
1 #!/usr/bin/env bash 2 # setting the locale, some users have issues with different locales, this forces the correct one 3 export LC_ALL=en_US.UTF-8 4 5 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 6 source $current_dir/utils.sh 7 8 linux_acpi() { 9 arg=$1 10 BAT=$(ls -d /sys/class/power_supply/*) 11 if [ ! -x "$(which acpi 2> /dev/null)" ];then 12 for DEV in $BAT; do 13 case "$arg" in 14 status) 15 [ -f "$DEV/status" ] && cat "$DEV/status" 16 ;; 17 percent) 18 [ -f "$DEV/capacity" ] && cat "$DEV/capacity" 19 ;; 20 *) 21 ;; 22 esac 23 done 24 else 25 case "$arg" in 26 status) 27 acpi | cut -d: -f2- | cut -d, -f1 | tr -d ' ' 28 ;; 29 percent) 30 acpi | cut -d: -f2- | cut -d, -f2 | tr -d '% ' 31 ;; 32 *) 33 ;; 34 esac 35 fi 36 } 37 38 battery_percent() 39 { 40 # Check OS 41 case $(uname -s) in 42 Linux) 43 percent=$(linux_acpi percent) 44 [ -n "$percent" ] && echo "$percent%" 45 ;; 46 47 Darwin) 48 echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%') 49 ;; 50 51 FreeBSD) 52 echo $(apm | sed '8,11d' | grep life | awk '{print $4}') 53 ;; 54 55 CYGWIN*|MINGW32*|MSYS*|MINGW*) 56 # leaving empty - TODO - windows compatability 57 ;; 58 59 *) 60 ;; 61 esac 62 } 63 64 battery_status() 65 { 66 # Check OS 67 case $(uname -s) in 68 Linux) 69 status=$(linux_acpi status) 70 ;; 71 72 Darwin) 73 status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ") 74 ;; 75 76 FreeBSD) 77 status=$(apm | sed '8,11d' | grep Status | awk '{printf $3}') 78 ;; 79 80 CYGWIN*|MINGW32*|MSYS*|MINGW*) 81 # leaving empty - TODO - windows compatability 82 ;; 83 84 *) 85 ;; 86 esac 87 88 tmp_bat_perc=$(battery_percent) 89 bat_perc="${tmp_bat_perc%\%}" 90 91 case $status in 92 discharging|Discharging) 93 # discharging, no AC 94 declare -A battery_labels=( 95 [0]="" 96 [10]="" 97 [20]="" 98 [30]="" 99 [40]="" 100 [50]="" 101 [60]="" 102 [70]="" 103 [80]="" 104 [90]="" 105 [100]="" 106 ) 107 echo "${battery_labels[$((bat_perc/10*10))]:-}" 108 ;; 109 high|charged|Full) 110 echo "" 111 ;; 112 charging|Charging) 113 # charging from AC 114 declare -A battery_labels=( 115 [0]="" 116 [10]="" 117 [20]="" 118 [30]="" 119 [40]="" 120 [50]="" 121 [60]="" 122 [70]="" 123 [80]="" 124 [90]="" 125 [100]="" 126 ) 127 echo "${battery_labels[$((bat_perc/10*10))]:-}" 128 ;; 129 ACattached) 130 # drawing from AC but not charging 131 echo '' 132 ;; 133 finishingcharge) 134 echo '' 135 ;; 136 *) 137 # something wrong... 138 echo '' 139 ;; 140 esac 141 ### Old if statements didn't work on BSD, they're probably not POSIX compliant, not sure 142 # if [ $status = 'discharging' ] || [ $status = 'Discharging' ]; then 143 # echo '' 144 # # elif [ $status = 'charging' ]; then # This is needed for FreeBSD AC checking support 145 # # echo 'AC' 146 # else 147 # echo 'AC' 148 # fi 149 } 150 151 main() 152 { 153 bat_label=$(get_tmux_option "@dracula-battery-label" "♥") 154 if [ "$bat_label" == false ]; then 155 bat_label="" 156 fi 157 158 no_bat_label=$(get_tmux_option "@dracula-no-battery-label" "AC") 159 if [ "$no_bat_label" == false ]; then 160 no_bat_label="" 161 fi 162 163 show_bat_label=$(get_tmux_option "@dracula-show-battery-status" false) 164 if $show_bat_label; then 165 bat_stat=$(battery_status) 166 else 167 bat_stat="" 168 fi 169 170 bat_perc=$(battery_percent) 171 172 if [ -z "$bat_stat" ]; then # Test if status is empty or not 173 echo "$bat_label $bat_perc" 174 elif [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power 175 echo "$no_bat_label" 176 else 177 echo "$bat_label$bat_stat $bat_perc" 178 fi 179 } 180 181 #run main driver program 182 main