#!/usr/bin/env zsh

total="$(free --si | rg Mem | awk '{print $2}')"

human() {
  awk -v mem="$1" 'BEGIN{print sprintf("%.1f%s", mem/1000/(mem > 1000000 ? 1000 : 1), mem > 1000000 ? "G" : "M")}'
}

free --si -s 3 | rg --line-buffered Mem | while read -r line; do
  used=$(echo "$line" | awk '{print $3}')
  echo '{"human": { "total": "'$(human "$total")'", "used": "'$(human "$used")'"}, "total": "'$total'" , "used": "'$used'"}'
done