Skip to content

Instantly share code, notes, and snippets.

@Lassi-Koykka
Last active March 24, 2022 14:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lassi-Koykka/9fb934732a871ca3c8bc9396983a3310 to your computer and use it in GitHub Desktop.
Save Lassi-Koykka/9fb934732a871ca3c8bc9396983a3310 to your computer and use it in GitHub Desktop.
A simple Zsh/Bash function for getting and displaying a random xkcd comic in the Kitty terminal
# XKCD
# Add this to your .bashrc or if you are using oh-my-zsh add a functions.zsh file under your oh-my-zsh/custom folder.
# Requirements:
# - Kitty terminal
# - jq
# - ImageMagick
function xkcd () {
comicNum=$1
newestJson=$(curl -s https://xkcd.com/info.0.json)
newestNum=$(jq -r .num <<< $newestJson)
if [[ "$1" != "newest" ]] ; then
re='^[0-9]+$'
# Check if argument is not "new", not a number or otherwise not valid and select random comic
if ! [[ $1 =~ $re ]] || [[ comicNum -lt 1 ]] || [[ "$comicNum" -gt "$newestNum" ]] ; then
# Generate a random comic number if none is specified
comicNum=$((1 + $RANDOM % $newestNum))
fi
# Get xkcd by number
json=$(curl -s https://xkcd.com/$comicNum/info.0.json)
else
# Use newest
comicNum=$newestNum
json=$newestJson
fi
# Parse response using jq
url=$(jq -r .img <<< $json)
title=$(jq -r .title <<< $json)
alt=$(jq -r .alt <<< $json)
transcript=$(jq -r .transcript <<< $json)
date="$(jq -r .day <<< $json).$(jq -r .month <<< $json).$(jq -r .year <<< $json)"
echo $url
echo "\n\"$title\"\n"
echo "#$comicNum -- $date\n"
# Display the image using icat or alternatively print the alt-text and transcript of the comic
kitty +kitten icat --align left $url || echo $transcript
echo "\n\"$alt\"\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment