Add waybar mouse battery module

main
Ensar Sarajčić 2024-11-05 08:06:20 +01:00
parent a97d0af858
commit dda7fd7e81
3 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,113 @@
#!/usr/bin/env bash
set -e
. ~/.local/opt/script_utils/echo.sh
usage () {
if [ ! -z "$1" ]; then
echo -e "Unknown peripheral: $1 \n"
fi
echo "Basic peripheral info component for waybar."
echo -e "\nUsage: peripheral-waybar PERIPHERAL"
echo "Options:"
echo " -h --help - prints this help message (if one of commands is passed, prints help message for that command)"
echo " -v --verbose - enables verbose logging"
echo " -c --check - checks if component should be displayed"
echo " -n --notify - show a notification with full info"
if [ ! -z "$1" ]; then
exit 1
fi
}
POSITIONAL=()
HELP=0
VERBOSE=0
CHECK=0
NOTIFY=0
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
HELP=1
shift # past argument
;;
-v|--verbose)
VERBOSE=1
shift # past argument
;;
-c|--check)
CHECK=1
shift # past argument
;;
-n|--notify)
NOTIFY=1
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
PERIPHERAL="${POSITIONAL[0]}"
echov "Detected peripheral: $PERIPHERAL"
if [ $HELP -eq 1 ]; then
usage $PERIPHERAL
exit 0
fi
mouse_waybar () {
LAST_UPDATE_FILE=$MY_CONFIG_CACHE_DIR/peripheral-waybar-mouse-last-update
LAST_STATUS_FILE=$MY_CONFIG_CACHE_DIR/peripheral-waybar-mouse-last-status
if [ ! -f "$LAST_UPDATE_FILE" ]; then
UPG_DATE=0
else
UPG_DATE=$(cat $LAST_UPDATE_FILE)
fi
if [ -z "$UPG_DATE" ]; then
UPG_DATE=0
fi
CURR_DATE=$( date +%s )
DIFF=$(( CURR_DATE - UPG_DATE ))
if (( DIFF > 32 )); then
BATTERY_LEVEL=$(rivalcfg --battery-level)
echo $CURR_DATE > $LAST_UPDATE_FILE
echo $BATTERY_LEVEL > $LAST_STATUS_FILE
else
BATTERY_LEVEL=$(cat $LAST_STATUS_FILE)
fi
if [ $CHECK -eq 1 ]; then
if [[ $BATTERY_LEVEL == *"Unable"* ]]; then
exit 1
else
exit 0
fi
fi
if [ $NOTIFY -eq 1 ]; then
notify-send "Mouse battery level" "$BATTERY_LEVEL"
exit 0
fi
TOOLTIP=$BATTERY_LEVEL
PERCENTAGE=$(echo $BATTERY_LEVEL | cut -d ']' -f2 | tr -d ' ' | tr -d '%')
CLASS="default"
if (( PERCENTAGE < 30 )); then
CLASS="warning"
fi
if (( PERCENTAGE < 15 )); then
CLASS="critical"
fi
echo "{\"text\":\"$PERCENTAGE%\", \"tooltip\":\"$TOOLTIP\", \"class\":\"$CLASS\", \"alt\":\"$CLASS\", \"percentage\":\"$PERCENTAGE\"}"
}
case $PERIPHERAL in
mouse)
mouse_waybar
;;
*)
usage $PERIPHERAL; exit 1 ;;
esac

View File

@ -35,6 +35,7 @@
"cpu",
"temperature",
"custom/keyboard-layout",
"custom/mouse-battery",
"bluetooth",
"battery",
"tray",
@ -91,6 +92,16 @@
"signal": 1, // SIGHUP
"tooltip": false
},
"custom/mouse-battery": {
"interval": 5,
"exec": "source ~/.profile && peripheral-waybar mouse",
"exec-if": "which rivalcfg && source ~/.profile && peripheral-waybar --check mouse",
"on-click": "source ~/.profile && peripheral-waybar --notify mouse",
"format": "󰍽 {}", // Icon: mouse
"tooltip": false,
"exec-on-event": false,
"return-type": "json"
},
"disk": {
"interval": 30,
"format": " {percentage_used}%"

View File

@ -69,6 +69,7 @@
#custom-apps-menu,
#custom-power-menu,
#custom-notification,
#custom-mouse-battery,
#idle_inhibitor,
#workspaces,
#backlight,
@ -214,6 +215,14 @@
color: gray;
}
#custom-mouse-battery.warning {
color: orange;
}
#custom-mouse-battery.critical {
color: red;
}
#workspaces {
margin: 0px;
}