loading...

mycontribution - View Your Contribution Graph From the Terminal

marounmaroun profile image Maroun Maroun Updated on ・2 min read

I wrote a script for fun, that brings your contribution graph to the terminal:

GitHub

The code (GitHub link):

#!/bin/bash

username=$1
symbol=${2:-}
if [[ -z $username ]]; then
    echo "Usage: mycontribution <username>"
    exit 1
fi

declare -a months=("   J a n" "  F e b" "M a r" "  A p r" "   M a y" "  J u n" "   J u l" "     A u g" "   S e p" "     O c t" "  N o v" "    D e c")
currentMonth=`date +%-m`-1
months=("${months[@]:$currentMonth}" "${months[@]::$currentMonth}")

declare -a graph=($(curl -s https://github.com/"$username" | grep '<rect class="day"' | grep -oP '(?<=fill=").*?(?=" )'))

if [[ -z "$graph" ]]; then
    echo "User $username doesn't exist"
    exit 1
fi

# print months
echo
for month in "${months[@]}"
do
    printf "%s" "$month"
done

printf "   "
printf " %s" ${months[0]}

# print contribution graph
echo
i=0
while [[ "$i" -le 6 ]]; do
    for (( n="$i";n<${#graph[@]};n+=7 )); do
        if [[ "${graph[$n]}" == "#c6e48b" ]]; then
            printf "\\e[38;5;71m$symbol\\e[0m|"
        elif [[ "${graph[$n]}" == "#7bc96f" ]]; then
            printf "\\e[38;5;64m$symbol\\e[0m|"
        elif [[ "${graph[$n]}" == "#239a3b" ]]; then
            printf "\\e[38;5;22m$symbol\\e[0m|"
        elif [[ "${graph[$n]}" == "#196127" ]]; then
            printf "\\e[38;5;238m$symbol\\e[0m|"
        # no contribution
        elif [[ "${graph[$n]}" == "#ebedf0" ]]; then
            printf "\\e[38;5;252m$symbol\\e[0m|"
        fi
    done
    echo
    (( i++ ))
done
echo
printf "Less \\e[38;5;252m$symbol\\e[0m|\\e[38;5;71m$symbol\\e[0m|\\e[38;5;64m$symbol\\e[0m|\\e[38;5;22m$symbol\\e[0m|\\e[38;5;238m$symbol\\e[0m More"
echo

Enjoy :)

Posted on by:

marounmaroun profile

Maroun Maroun

@marounmaroun

Nice to meet you 🍻 My name is Maroun. I'm a dad, musician, programmer, Stackoverflower and astronaut wannabe.

Discussion

pic
Editor guide
 

The only problem is that I need to reorder the months every month.

This works:

declare -a months=("    J a n" "  F e b" "  M a r" "   A p r" "   M a y" "    J u n" "   J u l" "   A u g" "     S e p" "   O c t" "     N o v" "   D e c")

currentMonth=`date +%-m`-1

MONTHS=("${months[@]:$currentMonth}" "${months[@]::$currentMonth}")

for month in "${MONTHS[@]}" ; do
  printf "%s" "$month"
done

Explanation:

  • date +%-m: Current month as number between 1-12
  • currentMonth gets assigned to that number minus 1 to be used as an index
  • "${months[@]:$currentMonth}: array slice from currentMonth until end of array
  • "${months[@]:0:$currentMonth}": array slice from beginning until currentMonth
  • MONTHS: array holding the reordered months (you probably want a better name)

If you run this you get the following output:

  M a r   A p r   M a y    J u n   J u l   A u g     S e p   O c t     N o v   D e c    J a n  F e b
 

Thanks for your time and explanation, helps a lot.

Do you want to submit a PR? If not, I'll push :)

 

Glad it helped, feel free to just push it.

 

How about that: Gihhub profile page contains information about the correct order of months and you can use it. Something like

declare -A animals=( ["Jan"]="    J a n" [" Feb"]="  F e b" .... )

And after that mapping GitHub months format to your script months format.

 

haha thats cool