Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions battery.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ battery_interpolation=(
"\#{battery_icon_status}"
"\#{battery_percentage}"
"\#{battery_remain}"
"\#{battery_should_charge}"
)

battery_commands=(
Expand All @@ -32,6 +33,7 @@ battery_commands=(
"#($CURRENT_DIR/scripts/battery_icon_status.sh)"
"#($CURRENT_DIR/scripts/battery_percentage.sh)"
"#($CURRENT_DIR/scripts/battery_remain.sh)"
"#($CURRENT_DIR/scripts/battery_chargetime.sh)"
)

set_tmux_option() {
Expand Down
55 changes: 55 additions & 0 deletions scripts/battery_chargetime.sh
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
}
Comment on lines +7 to +36
Copy link
Contributor

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)


recommend_charging() {
local percentage=$(get_percent)
percentage=$(echo ${percentage} | sed -e 's/%//g')
Comment on lines +39 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local percentage=$(get_percent)
percentage=$(echo ${percentage} | sed -e 's/%//g')
local percentage=$($CURRENT_DIR/battery_percentage.sh | sed -e 's/%//g')

I would recommend making use of the existing functions to get the battery percentage and directly piping it through sed rather than doing so in another step.

if [[ $percentage -lt 20 ]]; then
if [[ "$(battery_status)" == "discharging" ]]; then
echo "CHARGE IMMEDIATELY"
Copy link
Contributor

Choose a reason for hiding this comment

The 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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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