# codingtime: measure time for @/swapcoding with git

for the full @/swapcoding experience, i need to be able to measure time spent coding. i found a neater way to do it than using a dumb chess clock. do this:

  source <(curl -s https://iio.ie/gittime)

this installs two functions into the current shell. then the workflow in the game is the following:

works in both bash and zsh.

# implementation

gitstart just commits an empty commit with `start` as the sole commit message. gittime then simply adds up the time between the commits except for the start commits. the full source of the script is this:

# install like this: source <(curl -s https://iio.ie/gittime)

gitstart() {
git commit --allow-empty -m start
}

gittime() {
echo "min task"
echo "--- ----"
start=0
total=0

while read t task; do
if test "$task" = "start"; then
((start = t))
elif test "$start" = 0; then
echo " 0 no starttime for \"$task\", ignoring."
else
printf "%3d %s\n" "$(((t - start) / 60))" "$task"
((total += t - start))
((start = t))
fi
done < <(git log --reverse --date=unix --pretty=format:"%ad %s"; echo)

printf "\ntotal time: %2d h %02d m\n" "$((total / 60 / 60))" "$((total / 60 % 60))"
}

echo 'the `gitstart` and `gittime` shell functions successfully installed.'

# example

the output looks something like this:

  min  task
  ---  ----
    3  initial project setup with source code and makefiles
    7  create the game state structures
   11  render the state into a nice interface
    6  add colors to the interface
    5  advance the state based on user prompt
    8  detect end of game state and quit

  total time:  0 h 40 m

the `min` column represents the elapsed minutes for the given task.

# background

so i had the opportunity to try swapcoding with a friend. we both created a simple terminal tic-tac-toe game. it had a nice, colorful terminal interface, an optimal ai player, player hints, etc. the experience was super fun and we achieved much more than i initially expected. it's something i totally want to try again. we did it without a clock but we really missed it. so i came up with this easy to share hack for the next time.

published on 2022-08-21


new comment

see @/comments for the mechanics and ratelimits of commenting.


to the frontpage