#!/bin/sh -e
#
# glo - git log
# dependencies: trunc

ttywidth="$(stty </dev/tty size 2>/dev/null | awk '{ print $2 }' || echo "80")"

# 34 = 7 (hash) + 1 (spc) + 22 (email)
width=34

# use dc(1) => faster than bc(1)
foldwidth="$(echo "${ttywidth} ${width} - p" | dc)"

git log --format="%as %h %aE %s" \
  | while read -r date hash email subject; do
    email="$(echo "${email}" | trunc 22)"
    subject="$(echo "${subject}" | trunc "${foldwidth}")"

    printf "\033[38m%s  \033[31m%s \033[0;1m%25s\033[m %s\n" \
      "${date}" "${hash}" "<${email}>" "${subject}"
  done | head -n"${1:-10}"
