-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#90 Charging Reminder #91
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||||
#!/usr/bin/env bash | ||||||||
|
||||||||
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||||||||
|
||||||||
source "$CURRENT_DIR/helpers.sh" | ||||||||
|
||||||||
get_percent() { | ||||||||
# percentage displayed in the 2nd field of the 2nd row | ||||||||
if command_exists "pmset"; then | ||||||||
pmset -g batt | grep -o "[0-9]\{1,3\}%" | ||||||||
elif command_exists "acpi"; then | ||||||||
acpi -b | grep -m 1 -Eo "[0-9]+%" | ||||||||
elif command_exists "upower"; then | ||||||||
# use DisplayDevice if available otherwise battery | ||||||||
local battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1) | ||||||||
if [ -z "$battery" ]; then | ||||||||
return | ||||||||
fi | ||||||||
local percentage=$(upower -i $battery | awk '/percentage:/ {print $2}') | ||||||||
if [ "$percentage" ]; then | ||||||||
echo ${percentage%.*%} | ||||||||
return | ||||||||
fi | ||||||||
local energy | ||||||||
local energy_full | ||||||||
energy=$(upower -i $battery | awk -v nrg="$energy" '/energy:/ {print nrg+$2}') | ||||||||
energy_full=$(upower -i $battery | awk -v nrgfull="$energy_full" '/energy-full:/ {print nrgfull+$2}') | ||||||||
if [ -n "$energy" ] && [ -n "$energy_full" ]; then | ||||||||
echo $energy $energy_full | awk '{printf("%d%%", ($1/$2)*100)}' | ||||||||
fi | ||||||||
elif command_exists "termux-battery-status"; then | ||||||||
termux-battery-status | jq -r '.percentage' | awk '{printf("%d%%", $1)}' | ||||||||
elif command_exists "apm"; then | ||||||||
apm -l | ||||||||
fi | ||||||||
} | ||||||||
|
||||||||
recommend_charging() { | ||||||||
local percentage=$(get_percent) | ||||||||
percentage=$(echo ${percentage} | sed -e 's/%//g') | ||||||||
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I would recommend making use of the existing functions to get the battery percentage and directly piping it through |
||||||||
if [[ $percentage -lt 20 ]]; then | ||||||||
if [[ "$(battery_status)" == "discharging" ]]; then | ||||||||
echo "CHARGE IMMEDIATELY" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest changing this to be customizable via the config. (You can find examples of this in the "battery_icon_charge.sh" script, among others.) I would also argue that "CHARGE IMMEDIATELY" is too long of a string for a sane default. Maybe something like "CHRG" instead? |
||||||||
fi | ||||||||
elif [[ $percentage -gt 80 ]]; then | ||||||||
if [[ "$(battery_status)" != "discharging" ]]; then | ||||||||
echo "STOP CHARGING" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest changing this to be customizable via the config. (You can find examples of this in the "battery_icon_charge.sh" script, among others.) I would also argue that "STOP CHARGING" is too long of a string for a sane default. Maybe something like "UNPL" instead? |
||||||||
fi | ||||||||
fi | ||||||||
} | ||||||||
|
||||||||
main() { | ||||||||
recommend_charging | ||||||||
} | ||||||||
main |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than just copy/pasting the
print_battery_percentage
function from "battery_percentage.sh", I would suggest just calling that script when needed:foo=$($CURRENT_DIR/battery_percentage.sh)