Skip to content

Instantly share code, notes, and snippets.

@robin-a-meade
Forked from candu/lipsum
Last active July 1, 2022 21:18
Show Gist options
  • Save robin-a-meade/e010388548c5362a2408131eebc7f658 to your computer and use it in GitHub Desktop.
Save robin-a-meade/e010388548c5362a2408131eebc7f658 to your computer and use it in GitHub Desktop.
Command-line Lorem Ipsum generator using curl, lipsum.com, and jq
#!/bin/sh
AMOUNT=5
WHAT=paras
START=false
MARKDOWN_PARAGRAPHS= # Insert a blank line between paragraphs, like Markdown
HARD_WRAP=
WIDTH=78
while getopts ":n:wpblsmhW:" opt; do
case $opt in
n)
AMOUNT=$OPTARG
;;
w)
WHAT=words
;;
p)
WHAT=paras
;;
b)
WHAT=bytes
;;
l)
WHAT=lines
;;
s)
START=true
;;
m)
MARKDOWN_PARAGRAPHS=1
;;
h)
HARD_WRAP=1
;;
W)
WIDTH=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
*)
echo "An impossible code execution path occurred" >&2
echo "Report 'getopts' bug to [email protected]" >&2
exit 2
;;
esac
done
shift "$((OPTIND - 1))"
readonly AMOUNT WHAT START MARKDOWN_PARAGRAPHS HARD_WRAP WIDTH
markdown_paragraphs() {
if [[ $MARKDOWN_PARAGRAPHS ]]; then
sed -E 's/$/\n/'
else
cat
fi
}
hard_wrap() {
if [[ $HARD_WRAP ]]; then
fmt -w "$WIDTH"
else
cat
fi
}
curl -sS --fail https://lipsum.com/feed/json -d "amount=$AMOUNT" -d "what=$WHAT" -d "start=$START" \
| jq --raw-output '.feed.lipsum' | markdown_paragraphs | hard_wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment