#!/usr/bin/env sh
#
# git-lno - log local and upstream commits side by side
# useful when history has been manipulated

loc="$(git log --pretty=tformat:'%C(auto)%h%d %s %Cgreen(%cd) %Cblue<%an>%Creset' \
  --date=human --no-merges --color=always "$@")"
# shellcheck disable=SC1083
upstream="$(git log --pretty=tformat:'%C(auto)%h%d %s %Cgreen(%cd) %Cblue<%an>%Creset' \
  --date=human --no-merges --color=always @{push} "$@")"

countLoc=$(echo "${loc}" | wc -l)
countUp=$(echo "${upstream}" | wc -l)

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

# for i in $(seq 1 $(expr $countLoc \& $countLoc \< $countUp \| $countUp)); do
for i in $(seq 1 "$((countLoc & countLoc < countUp | countUp))"); do
  # shellcheck disable=SC2028,SC2312
  printf "%-${halfcols}s $(test "${halfcols}" -lt 100 && printf '\\n')%s\n" \
    "$(echo "${loc}" | head -n "${i}" | tail -n 1)" \
    "$(echo "${upstream}" | head -n "${i}" | tail -n 1)"
  # use columns instead? - need to interleave!
done | ${PAGER:-less}
