Created
February 1, 2016 09:31
-
-
Save denysdovhan/084abdc265fd05bd3641 to your computer and use it in GitHub Desktop.
ZSH Battery Prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################### | |
# .zsh/functions/battery_percent | |
################################### | |
LAST_FULL_CAP=$(grep -h "last full capacity" /proc/acpi/battery/BAT?/*| sed 's/[^0-9]//g') | |
REMANING_CAP=$(grep -h "remaining capacity" /proc/acpi/battery/BAT?/*| sed 's/[^0-9]//g') | |
BATTERY_CHARGING=$(grep -h "charging state:" /proc/acpi/battery/BAT?/state|sed 's/.*:\s*//') | |
BATTERY_PERCENT=$((($REMANING_CAP*100)/$LAST_FULL_CAP)) | |
if [[ "${BATTERY_PERCENT}" -lt 15 ]]; then | |
PR_BATTERY+="%{$fg[red]%}$BATTERY_PERCENT%%" | |
elif [[ "${BATTERY_PERCENT}" -lt 50 ]]; then | |
PR_BATTERY+="%{$fg[yellow]%}$BATTERY_PERCENT%%" | |
else | |
PR_BATTERY+="%{$fg[green]%}$BATTERY_PERCENT%%" | |
fi | |
if [[ "${BATTERY_CHARGING}" == "charging" ]]; then | |
PR_BATTERY+="%{$fg[yellow]%}⚡" | |
fi | |
PR_BATTERY+="%{$reset_color%}" | |
printf "%s" $PR_BATTERY❯ cat zsh/functions/battery_percent | |
LAST_FULL_CAP=$(grep -h "last full capacity" /proc/acpi/battery/BAT?/*| sed 's/[^0-9]//g') | |
REMANING_CAP=$(grep -h "remaining capacity" /proc/acpi/battery/BAT?/*| sed 's/[^0-9]//g') | |
BATTERY_CHARGING=$(grep -h "charging state:" /proc/acpi/battery/BAT?/state|sed 's/.*:\s*//') | |
BATTERY_PERCENT=$((($REMANING_CAP*100)/$LAST_FULL_CAP)) | |
if [[ "${BATTERY_PERCENT}" -lt 15 ]]; then | |
PR_BATTERY+="%{$fg[red]%}$BATTERY_PERCENT%%" | |
elif [[ "${BATTERY_PERCENT}" -lt 50 ]]; then | |
PR_BATTERY+="%{$fg[yellow]%}$BATTERY_PERCENT%%" | |
else | |
PR_BATTERY+="%{$fg[green]%}$BATTERY_PERCENT%%" | |
fi | |
if [[ "${BATTERY_CHARGING}" == "charging" ]]; then | |
PR_BATTERY+="%{$fg[yellow]%}⚡" | |
fi | |
PR_BATTERY+="%{$reset_color%}" | |
printf "%s" $PR_BATTERY | |
################################### | |
# .zshrc | |
################################### | |
# check for any ACPI batteries to find out if we're running on a laptop | |
# if running on a laptop, show battery charge percent | |
/sbin/modprobe battery 2> /dev/null || true | |
if [ -d /sys/class/power_supply ]; then | |
if grep -q Battery /sys/class/power_supply/*/type 2>/dev/null; then | |
PROMPT+=$' $(battery_percent)' | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment