Curl-able contact card

2020-08-08

I saw a Github Gist with a businesss card that can be displayed all pretty-like in the terminal using curl. The one online was quite simplistic, it just padded out the card contents manually with spaces, which required a lot of trial and error to position everything correctly. It also required knowing the ANSI escape sequences to colour and style the text.

I wrote the script below to automate some of this process and produce a similar looking contact card:

#!/usr/bin/env bash

# Define colours and fonts
boxcol=""
default=""
bold=""
underline=""
reverse=""
black=""
red=""
green=""
yellow=""
blue=""
magenta=""
cyan=""
white=""

# Card items, in line order
inputs=()
inputs+=("${red}${underline}John L. Godlee")
inputs+=("${blue}PhD Student, University of Edinburgh")
inputs+=("")
inputs+=("${bold}Email: ${default}johngodlee@gmail.com")
inputs+=("${bold}Blog: ${default}https://johngodlee.github.io")
inputs+=("${bold}GitHub: ${default}https://github.com/johngodlee")
inputs+=("${bold}ORCiD: ${default}https://orcid.org/0000-0001-5595-255X")
inputs+=("")
inputs+=("${yellow}curl -sL https://johngodlee.github.io/files/card")

# Define left-padding
leftpad='    '

# Define box drawing chars
vbord="│"
hbord="─"
tlcor="╭"
trcor="╮"
brcor="╯"
blcor="╰"

# Get length of longest line 
linel=$(for i in "${inputs[@]}"; do
	echo $i | sed 's/\x1b\[[0-9;]*m//g'| wc -c
done | sort -nr | head -n 1)

# Get width of card, with padding
inwidth=$(($linel + 4*2))

# Get length of left-padding
leftpadl=${#leftpad}

# Print top line
printf "$boxcol"
printf "$tlcor"
for ((i=1; i<=inwidth; i++)); do
	printf "$hbord" 
done
printf "$trcor"
printf "\n"
printf "$boxcol"
printf "$vbord"
for ((i=1; i<=inwidth; i++)); do
	printf " " 
done
printf "$vbord"
printf "\n"

# Print each card item
for ((i = 0; i < ${#inputs[@]}; i++))
do
	# Get length of string
	stringcl=$(echo ${inputs[$i]} | sed 's/\x1b\[[0-9;]*m//g')
	stringl=${#stringcl}

	# Get length of right padding 
	rightpadl=$(($inwidth-$stringl-$leftpadl))

	# Print border
	printf "$boxcol$vbord" 

	# Print left-padding
	printf "$leftpad"
		
	# Print string
	printf "$default${inputs[$i]}$default"

	# Print right-padding
	for ((j=1; j<=rightpadl; j++))
	do
		printf " "
	done

	# Print border
	printf "$boxcol$vbord"

	# New-line
	printf "\n"
done

# Print bottom line
printf "$boxcol"
printf "$vbord"
for ((i=1; i<=inwidth; i++)); do
	printf " " 
done
printf "$vbord"
printf "\n"
printf "$boxcol$blcor"
for ((i=1; i<=inwidth; i++)); do
	printf "$hbord" 
done
printf "$brcor"
printf "\n"

The customisation comes from the inputs array, which contains the contents of the contact card, and uses the variables for font and colour to style the text, e.g.:

inputs+=("${red}${underline}John L. Godlee")

The script requires bash rather than a standard POSIX shell, because it uses bash arithmetic, but this could probably be ported to use bc or something. Also not that some of the escape sequences might not have rendered properly on the web, like ^[.

Contact card screenshot

Now you can curl my contact card from this website:

curl -sL https://johngodlee.github.io/files/card