gor.sh (1988B)
1 #!/bin/sh 2 # gor: Plato's Gorgias 3 # License: Public domain 4 5 SELF="$0" 6 7 get_data() { 8 sed '1,/^#EOF$/d' < "$SELF" | tar xz -O "$1" 9 } 10 11 if [ -z "$PAGER" ]; then 12 if command -v less >/dev/null; then 13 PAGER="less" 14 else 15 PAGER="cat" 16 fi 17 fi 18 19 show_help() { 20 exec >&2 21 echo "usage: $(basename "$0") [flags] [reference...]" 22 echo 23 echo " -l list books" 24 echo " -W no line wrap" 25 echo " -h show help" 26 echo 27 echo " Reference types:" 28 echo " <Book>" 29 echo " Individual book" 30 echo " <Book> <Page>" 31 echo " Stephanus page" 32 echo " <Book> <Page><Letter>" 33 echo " Stephanus section (e.g. Gor 447a)" 34 echo " <Book> <Page>-<Page>" 35 echo " Range of Stephanus pages" 36 echo " <Book> <Page><Letter>-<Letter>" 37 echo " Range of sections within a page" 38 echo " <Book> <Page><Letter>-<Page><Letter>" 39 echo " Range of sections across pages" 40 echo 41 echo " /<Search>" 42 echo " All sections that match a pattern" 43 echo " <Book>/<Search>" 44 echo " Sections in a book that match a pattern" 45 echo " <Book> <Page>/<Search>" 46 echo " Sections in a page that match a pattern" 47 exit 2 48 } 49 50 while [ $# -gt 0 ]; do 51 isFlag=0 52 firstChar="${1%"${1#?}"}" 53 if [ "$firstChar" = "-" ]; then 54 isFlag=1 55 fi 56 57 if [ "$1" = "--" ]; then 58 shift 59 break 60 elif [ "$1" = "-l" ]; then 61 get_data gor.tsv | awk -v cmd=list "$(get_data gor.awk)" 62 exit 63 elif [ "$1" = "-W" ]; then 64 export GOR_NOLINEWRAP=1 65 shift 66 elif [ "$1" = "-h" ] || [ "$isFlag" -eq 1 ]; then 67 show_help 68 else 69 break 70 fi 71 done 72 73 cols=$(tput cols 2>/dev/null) 74 if [ $? -eq 0 ]; then 75 export GOR_MAX_WIDTH="$cols" 76 fi 77 78 if [ $# -eq 0 ]; then 79 if [ ! -t 0 ]; then 80 show_help 81 fi 82 83 while true; do 84 printf "gor> " 85 if ! read -r ref; then 86 break 87 fi 88 get_data gor.tsv | awk -v cmd=ref -v ref="$ref" "$(get_data gor.awk)" | ${PAGER} 89 done 90 exit 0 91 fi 92 93 get_data gor.tsv | awk -v cmd=ref -v ref="$*" "$(get_data gor.awk)" | ${PAGER}