Skip to content

Instantly share code, notes, and snippets.

@0x6d64
Last active May 31, 2021 21:20
Show Gist options
  • Save 0x6d64/5403739 to your computer and use it in GitHub Desktop.
Save 0x6d64/5403739 to your computer and use it in GitHub Desktop.
cyclenext is a shell script to monitor a specific taskwarrior (http://taskwarrior.org/) filter continuously. See https://github.com/0x6d64/cyclenext for improved python version
#!/bin/sh
# Martin Demling, 2013
# [email protected]
# cyclenext is a script to monitor a specific taskwarrior
# (http://taskwarrior.org/) view continuously
# the script was developed on an OSX 10.7.5 system, but should run on other
# UNIX platforms
# usage: cyclenext <filter>
TASKFILE="/Users/martindemling/taskdir/undo.data"
# trigger redraw if size of terminal window changes
trap 'redraw $*' WINCH
# returns age of file in seconds
filemodifiedseconds()
{
local OLD=`stat -f %m $1`;
local NOW=`date +%s`;
(( DIFF = (NOW - OLD) ));
echo $DIFF
}
# returns age of file in minutes
filemodifiedminutes()
{
local OLD=`stat -f %m $1`;
local NOW=`date +%s`;
(( DIFF = (NOW - OLD) ));
let DIFF=$DIFF/60;
echo $DIFF
}
# detects how many lines the terminal can currently display, limit the output
# accordingly. The number 15 was found by trial, you may have to increase/
# decrease based on how many of your tasks need more than one line (due to
# line wrapping)
linestodisplay()
{
lines=$(tput lines)
#columns=$(tput cols)
(( LINESTODISPLAY = ($lines - 15) ))
echo $LINESTODISPLAY
}
draw()
{
clear
local AGE=$(filemodifiedminutes $TASKFILE)
echo "last change: $AGE minutes ago"
task $* rc.gc=off limit:$(linestodisplay)
}
redraw()
{
sleep 1
draw $*
}
#---------
# main loop
if [ "$#" = "0" ]
then
echo "no task filter given!"
echo "usage: cyclenext <filter>"
else
DELAY=2
for ((;;)) do
draw $*
#sleep $DELAY
sleep 1;
# check file every $DELAY seconds, draw if change is detected,
# if 5 minutes (150*2secs) pass without change, draw regardless:
# this will be useful to reflect changes in the config files (which
# are not monitored otherwise
for (( c=1; c<=150; c++ ))
do
AGE=$(filemodifiedseconds $TASKFILE)
if [ $AGE -gt $DELAY ]
then
sleep $DELAY
else
sleep 1
break
fi
done
done
fi
@0x6d64
Copy link
Author

0x6d64 commented Apr 25, 2013

Read my blogpost for a description, and feel free to comment!

@0x6d64
Copy link
Author

0x6d64 commented May 31, 2021

See improved python version here: https://github.com/0x6d64/cyclenext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment