Created
November 28, 2016 06:35
-
-
Save Wizek/4a9084bbe2d9a20fa94d8fc65e75f53e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
set -e | |
term="$1" | |
echo $term | |
loop=1 | |
asleep=.95 | |
awake="`echo "$loop - $asleep" | bc`" | |
echo "asleep: $asleep, awake: $awake" | |
trap cleanup INT | |
function cleanup() { | |
function f () { echo "\tExiting: "$1} | |
if [ -n "$lastKnownPid" ]; then | |
f "continuing \$lastKnownPid ($lastKnownPid)" | |
kill -SIGCONT $lastKnownPid | |
else | |
f "no \$lastKnownPid" | |
fi | |
} | |
function log () { | |
if [[ $1 -lt 2 ]]; then | |
echo $2 | |
fi | |
} | |
while true; do | |
if [ -n "$lastKnownPid" ]; then | |
kill -SIGCONT $lastKnownPid | |
fi | |
sleep $awake | |
{ | |
pid=$(xdotool getwindowfocus getwindowpid) | |
cmdline="$(cat "/proc/$pid/cmdline")" | |
pCwd="$(readlink "/proc/$pid/cwd")" | |
# TODO use $cmdline as matchbase instead | |
# TODO or use both | |
# matchbase=$cmdline | |
matchbase="$pCwd $cmdline" | |
if [ -e "/proc/$lastKnownPid/" ]; then | |
# noop | |
else | |
lastKnownPid='' | |
echo 'App runs no more, resetting $lastKnownPid' | |
fi | |
if (echo "$matchbase" | grep -q $term); then | |
log 2 "match ($pid): in $matchbase" | |
lastKnownPid=$pid | |
# kill -SIGCONT $pid | |
else | |
log 2 "no match ($lastKnownPid): $matchbase" | |
if [ -n "$lastKnownPid" ]; then | |
kill -SIGSTOP $lastKnownPid | |
fi | |
fi | |
} || { | |
echo 'An error happened. Continuing' | |
} | |
sleep $asleep | |
done | |
cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment