86 lines
2.1 KiB
Bash
Executable File
86 lines
2.1 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
get_date() {
|
|
date '+%H:%M %a %d/%m/%y'
|
|
}
|
|
|
|
get_weather() {
|
|
CACHE_FILE="$HOME/.cache/weather.txt"
|
|
CACHE_DURATION=600
|
|
|
|
mkdir -p "$(dirname "$CACHE_FILE")"
|
|
|
|
if [ -f "$CACHE_FILE" ]; then
|
|
last_modified=$(stat -c %Y "$CACHE_FILE")
|
|
current_time=$(date +%s)
|
|
age=$((current_time - last_modified))
|
|
else
|
|
age=$((CACHE_DURATION + 1))
|
|
fi
|
|
|
|
if [ "$age" -gt "$CACHE_DURATION" ]; then
|
|
if [ -x "$HOME/.config/sway/scripts/weather.sh" ]; then
|
|
weather_output="$("$HOME/.config/sway/scripts/weather.sh")"
|
|
echo "$weather_output" > "$CACHE_FILE"
|
|
else
|
|
echo "No weather data" > "$CACHE_FILE"
|
|
fi
|
|
fi
|
|
|
|
cat "$CACHE_FILE"
|
|
}
|
|
|
|
get_battery() {
|
|
BATTERY_PATH=$(find /sys/class/power_supply/ -name "BAT*" | head -n 1)
|
|
|
|
if [ -f "$BATTERY_PATH/capacity" ]; then
|
|
BAT_PERCENT=$(cat "$BATTERY_PATH/capacity")
|
|
BAT_STATUS=$(cat "$BATTERY_PATH/status")
|
|
|
|
printf "%s%%" "$BAT_PERCENT"
|
|
|
|
if [ "$BAT_STATUS" = "Charging" ] || [ "$BAT_STATUS" = "Unknown" ]; then
|
|
printf "^"
|
|
fi
|
|
else
|
|
printf "NoBat"
|
|
fi
|
|
}
|
|
|
|
get_volume() {
|
|
SINK=$(pactl info | awk -F': ' '/Default Sink/ {print $2}')
|
|
VOLUME=$(pactl get-sink-volume "$SINK" | awk '{print $5}' | head -n 1)
|
|
MUTE_STATUS=$(pactl get-sink-mute "$SINK" | awk '{print $2}')
|
|
|
|
printf "%s" "$VOLUME"
|
|
[ "$MUTE_STATUS" = "yes" ] && printf " (m)"
|
|
}
|
|
|
|
now_playing() {
|
|
MAXLEN=35
|
|
ARTIST=$(playerctl -a metadata artist 2>/dev/null)
|
|
TITLE=$(playerctl -a metadata title 2>/dev/null)
|
|
|
|
if [ -z "$ARTIST" ] && [ -z "$TITLE" ]; then
|
|
return 0
|
|
fi
|
|
|
|
INFO="$ARTIST - $TITLE"
|
|
if [ ${#INFO} -le $MAXLEN ]; then
|
|
printf "%s" "$INFO"
|
|
else
|
|
printf "%s…" "$(printf "%s" "$INFO" | cut -c1-$((MAXLEN - 1)))"
|
|
fi
|
|
}
|
|
|
|
while true; do
|
|
PLAYING="$(now_playing)"
|
|
if [ -n "$PLAYING" ]; then
|
|
printf "%s | V:%s | %s | %s | B:%s\n" "$PLAYING" "$(get_volume)" "$(get_date)" "$(get_weather)" "$(get_battery)"
|
|
else
|
|
printf "V:%s | %s | %s | B:%s\n" "$(get_volume)" "$(get_date)" "$(get_weather)" "$(get_battery)"
|
|
fi
|
|
sleep 1
|
|
done
|
|
|