Skip to content

Instantly share code, notes, and snippets.

@oxyc
Created February 20, 2012 17:41
Show Gist options
  • Save oxyc/1870241 to your computer and use it in GitHub Desktop.
Save oxyc/1870241 to your computer and use it in GitHub Desktop.
#!/bin/bash
# $HOME/bin/track
# Put the following exports in your .profile
# export TRACK_SSH="ssh genero" # Local host only
# export TRACK_LOG_FILE="$HOME/.timetracker.log" # Should exist on all hosts
if [[ "$TRACK_SSH" ]]; then log_content="$TRACK_SSH cat \$TRACK_LOG_FILE"
else log_content="cat $TRACK_LOG_FILE"; fi
output() {
cat << HELP
Usage: track [action]
Dead simple time tracking / logging script.
Options:
--show | Show the latest log entries, takes a number e.g. 'track --show 10'
--find | Filters through log entries with grep, 'track --find Added\ function
--help | Show this output
* | Everything else passed will be logged. If you begin something in the
format HH:MM it will be used as a timestamp
HELP
}
case "$1" in
--help)
output
exit 0
;;
--show)
[[ "$2" =~ ^[[:digit:]]+ ]] && count=$2
$log_content | tail -${count:-10} | awk 'BEGIN { FS = "\t" }; {\
output="\033[1m" $1 " " $2 " \033[0;33m→\033[0m";\
for (i=3; i<=NF;i++) { output=output " "$i }\
print output\
}'
exit 0
;;
--find)
shift
$log_content | grep "$@"
exit 0
;;
*)
todays_date=$(date '+%Y-%m-%d')
timestamp=$(date '+%H:%M')
[[ "$1" =~ ^[[:digit:]]{2}:[[:digit:]]{2} ]] && stamped=true
if test "$stamped"; then
timestamp=$1
shift
fi
entry="${todays_date}\t${timestamp}\t$*"
if [[ "$TRACK_SSH" ]]; then
remote_log_file="\$TRACK_LOG_FILE"
test "$stamped" && do_sort="sort $remote_log_file -o $remote_log_file"
echo -e "$entry" | $TRACK_SSH "cat >> $remote_log_file; ${do_sort}" > /dev/null 2>&1 &
test $? != 0 && echo -e "\033[0;31mSomething went horribly wrong!\033[0m"
else
echo -e "$entry" >> $TRACK_LOG_FILE
test "$stamped" && sort $TRACK_LOG_FILE -o $TRACK_LOG_FILE
fi
echo -e "$entry"
;;
esac
function! TrackTime (...)
let command = (a:0 > 0) ? a:1 : ''
exec ':! track ' . command
endfunction
function! TrackShow (limit)
exec ':! track --show ' . a:limit
endfunction
function! TrackFind (...)
let regex = (a:0 > 0) ? a:1 : ''
exec ':! track --find ' . regex
endfunction
noremap <leader>tt :call TrackTime (input("Message: "))<CR>
noremap <leader>ts :call TrackShow (input("Count: "))<CR>
noremap <leader>tf :call TrackFind (input("Regex: "))<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment