Skip to content

Instantly share code, notes, and snippets.

@maxlawton
Last active May 11, 2018 19:28
Show Gist options
  • Save maxlawton/b35e0b9dcc1ce1f5f0189cd7c5ad9168 to your computer and use it in GitHub Desktop.
Save maxlawton/b35e0b9dcc1ce1f5f0189cd7c5ad9168 to your computer and use it in GitHub Desktop.
Shell script to get metaphorpsum.com filler text
#!/usr/bin/env bash
# --------------------------------------------------------------------
# metaphorpsum.sh
#
# Generate some metaphorical filler text via the API at
# metaphorpsum.com
#
# --------------------------------------------------------------------
usage() {
(cat <<EOF
metaphorpsum.sh
Generate some metaphorical filler text via the API at
metaphorpsum.com.
Output is tee'd to "pbcopy", i.e. copied to the macOS clipboard.
SYNOPSIS
metaphorpsum.sh [options]
OPTIONS
Options are handled by 'getopts' so only single-dash,
single-character option names are supported.
-p <paragraphs>
Specify the number of paragraphs to display.
-s <sentences>
Specify the number of sentences to display. The number is
consider per-paragraph when using the -p option. Default: 3.
-t Wrap paragraphs in HTML <p> tags.
EXAMPLES
Print four sentences:
./metaphorpsum.sh -s4
Print two paragraphs, three sentences each:
./metaphorpsum.sh -p2
Print three paragraphs, each wrapped in <p> tags and
containing one sentence:
./metaphorpsum.sh -p3 -s1 -t
Print three sentences without any <p> tags as the '-t'
option is only applicable when '-p' is specified:
./metaphorpsum.sh -t
EOF
) 1>&2
exit 0;
}
# --------------------------------------------------------------------
base="http://metaphorpsum.com/"
p=0
s=3
t="false"
while getopts ':p:s:th' opt; do
case $opt in
p) p=$OPTARG ;;
s) s=$OPTARG ;;
t) t="true" ;;
h|?) usage ;;
esac
done
if [ $p -gt 0 ]; then
endpoint="paragraphs/${p}/${s}"
else
endpoint="sentences/${s}"
fi
url="${base}${endpoint}"
[ "$t" = "true" ] && url="${url}?p=${t}"
# --------------------------------------------------------------------
# Curl it and tee to pbcopy if available, otherwise just stdout
if command -v pbcopy >/dev/null 2>&1; then
curl -s "$url" | tee >(pbcopy)
else
curl -s "$url"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment