Skip to content

Instantly share code, notes, and snippets.

@ouuan
Last active April 10, 2021 17:57
Show Gist options
  • Select an option

  • Save ouuan/6203b3db204fad54613a544735b817d2 to your computer and use it in GitHub Desktop.

Select an option

Save ouuan/6203b3db204fad54613a544735b817d2 to your computer and use it in GitHub Desktop.
Track time-usage for Xorg windows
#!/bin/bash
file="$HOME/.track-window-stats"
period=1
while true
do
locked=$(loginctl show-session "$XDG_SESSION_ID" -p LockedHint --value)
if [[ "$locked" == 'yes' ]]; then
result='<locked>'
else
focus="$(xprop -id $(xdotool getwindowfocus) | grep WM_CLASS | cut -f 4 -d'"')"
if [[ -n "$focus" ]]; then
result="$focus"
else
result='<empty>'
fi
fi
time="$(date +%s)"
if [[ "$result" == "$lastresult" ]]; then
sed -i '$d' "$file"
echo "$lasttime $((time - lasttime + period)) $result" >> "$file"
else
echo "$time $period $result" >> "$file"
lasttime="$time"
lastresult="$result"
fi
sleep "$period"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment