citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

ud (708B)


      1 #!/usr/bin/env bash
      2 
      3 usage () {
      4   printf 'Usage: ud <term> [# results]\n'
      5   printf '\nurban dictionary search\n\n'
      6   exit 1
      7 }
      8 
      9 PLAIN=0
     10 
     11 if [ "$1" = -plain ]; then
     12   PLAIN=1
     13   shift
     14 fi
     15 
     16 if [ -z "$1" ]; then
     17   usage
     18   exit 1
     19 fi
     20 
     21 term="$1"
     22 n=${2:-1}
     23 
     24 if [ $PLAIN -eq 0 ]; then
     25   BOLD='\u001b[1m'
     26   RESET='\u001b[0m'
     27 fi
     28 
     29 if [[ $n -gt 1 ]]; then
     30     format='---------------------------\n'
     31 else
     32     format=""
     33 fi
     34 
     35 format+=$(cat <<EOS
     36 ${BOLD}Definition${RESET}
     37 
     38     \\(.definition | sub("\\n";"\\n    "))
     39 
     40 ${BOLD}Example${RESET}
     41 
     42     \\(.example | sub("\\n";"\\n    "))
     43 EOS
     44 )
     45 
     46 curl -GsL 'https://api.urbandictionary.com/v0/define' --data-urlencode "term=$term" | \
     47   jq -C -r 'limit('"$n"';.list[]) | "'"$format"'"'