DEV Community

Maroun Maroun
Maroun Maroun

Posted on • Edited on

mycontribution - View Your Contribution Graph From the Terminal

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 :)

Top comments (3)

Collapse
 
olegthelilfix profile image
Oleg Aleksandrov

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.

Collapse
 
marounmaroun profile image
Maroun Maroun

Thanks for your time and explanation, helps a lot.

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

Collapse
 
captainawesomedi profile image
Di Wu

haha thats cool