-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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