Battery notification #1450
henrygabs7
started this conversation in
Ideas
Replies: 1 comment
-
|
FYI there are quite a few battery applets (spices) that do this. It may be more beneficial to use those as they also have a few more features. As an aside, consider moving from an endlessly looping shell script to using a systemd service. They are fabulous at these kinds of things for a dozen reasons. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there! I'm new to GitHub and to coding in general. The only programming languages I know are intermediate bash and basic python, and this will be my very first contribution in life. Anyway, while messing around with my system I came up with a simple idea that may be quite useful for laptop owners.
One thing that annoyed me a little on my daily use of Linux Mint was that it doesn't show you a notification when your laptop either stops or starts charging. Generally that's not that big of a deal but it might be bad if you accidentally disconnect your charger without your notice. There is an applet that does that but it requires some extra repository and other features so ended up I found it too unnecessary just for the sake of a notification. So I wrote a .sh (that one below) that solves that issue with just a few lines of code (because that's all I'm capable of so far lmao).
`#!/bin/bash
POWER_SUPPLY="/sys/class/power_supply/AC/online"
BATTERY_PATH="/sys/class/power_supply/BAT0/capacity"
previous_state=""
while true; do
current_state=$(cat "$POWER_SUPPLY" 2>/dev/null || echo "0")
bateria=$(cat "$BATTERY_PATH" 2>/dev/null || echo "0")
if [ "$current_state" = "1" ] && [ "$previous_state" = "0" ]; then
if [ "$bateria" -ge 90 ]; then
icone="battery-level-90-charging-symbolic"
elif [ "$bateria" -ge 70 ]; then
icone="battery-level-70-charging-symbolic"
elif [ "$bateria" -ge 50 ]; then
icone="battery-level-50-charging-symbolic"
elif [ "$bateria" -ge 30 ]; then
icone="battery-level-30-charging-symbolic"
elif [ "$bateria" -ge 10 ]; then
icone="battery-level-10-charging-symbolic"
else
icone="battery-level-0-charging-symbolic"
fi
notify-send -u normal "Charging started" "Battery level ($bateria%)" --icon="$icone"
fi
if [ "$current_state" = "0" ] && [ "$previous_state" = "1" ]; then
if [ "$bateria" -ge 90 ]; then
icone="battery-level-90-symbolic"
elif [ "$bateria" -ge 70 ]; then
icone="battery-level-70-symbolic"
elif [ "$bateria" -ge 50 ]; then
icone="battery-level-50-symbolic"
elif [ "$bateria" -ge 30 ]; then
icone="battery-level-30-symbolic"
elif [ "$bateria" -ge 10 ]; then
icone="battery-level-10-symbolic"
else
icone="battery-level-0-symbolic"
fi
notify-send -u normal "Charging stopped" "Battery level ($bateria%)" --icon="$icone"
fi
previous_state=$current_state
sleep 2
done`
I'm pretty aware tho that this solution may be very machine specific bc the necessary values for it to work may be in different paths and also you need to manually add it to work on start by using the built-in Mint Startup Applications software for that, not to mention I'm not sure if bash is the best solution for a more universal and/or official thing, but this code is more of an illustration of what I have in mind than anything else.
So, for simplicity sake, what this code does is, while running on background, it recognizes the moment when charger is plugged/unplugged and shows you a notification which also shows the current battery level and a coherent icon according to your state of energy (charging/not charging) and battery level (above 90%, 70%, 50%, 30%, 10% or 0%).
And that's it. Just a simple suggestion. Sorry if this text is too big and hope you all have an awesome day! ^^
Beta Was this translation helpful? Give feedback.
All reactions