Skip to content

Instantly share code, notes, and snippets.

@streetturtle
Last active September 24, 2020 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save streetturtle/7e0e1829a4df2e202479b54cdf9a42a3 to your computer and use it in GitHub Desktop.
Save streetturtle/7e0e1829a4df2e202479b54cdf9a42a3 to your computer and use it in GitHub Desktop.
Prints contribuion graph in terminal for a given user for a given number of days
#!/bin/sh
## Usage: print-gh-contrib-graph username num_of_days
##
## username GitHub username.
## num_of_days Number of days in the past to print graph for.
##
function print_graph {
if [ "$#" -ne 2 ]; then
echo "Expected 2 arguments: username and a number of days"
exit
fi
local IFS=$'\n'
intensity=`curl -s https://github-contributions.now.sh/api/v1/"$1" | jq -r --arg NUM_OF_DAYS "$2" '[.contributions[] | select ( .date | strptime("%Y-%m-%d")| mktime < now)][:$NUM_OF_DAYS|tonumber]| .[].intensity'`;
intensity=($intensity);
for (( i=${#intensity[@]} - 1; i>=0; i-- )); do
if ((intensity[$i] == 0)); then
echo -en "\e[48;5;248m \e[0m" ;
elif ((intensity[$i] == 1)); then
echo -en "\e[48;5;40m \e[0m" ;
elif ((intensity[$i] == 2)); then
echo -en "\e[48;5;34m \e[0m" ;
elif ((intensity[$i] == 3)); then
echo -en "\e[48;5;28m \e[0m" ;
elif ((intensity[$i] == 4)); then
echo -en "\e[48;5;22m \e[0m" ;
fi
done ;
echo
}
print_graph $1 $2
@streetturtle
Copy link
Author

streetturtle commented Sep 24, 2020

Selection_078

Prerequisite

  • curl - command line tool and library for transferring data with URLs
  • jq - is a lightweight and flexible command-line JSON processor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment