dots

Personal dotfiles
git clone git://git.gormless.xyz/dots.git
Log | Files | Refs

op (2190B)


      1 #!/bin/sh
      2 #
      3 # open things from uri
      4 
      5 cachedir=$HOME/var/cache/op
      6 
      7 usage() {
      8     echo "usage: $(basename $0) [-h] [-s] [uri]" >&2
      9 }
     10 
     11 peekmime() {
     12     curl -sSfL "$1" | cut -b-16 | file -ib - | cut -d\; -f1
     13 }
     14 
     15 # return a uri to a local file (download if necessary)
     16 localuri() {
     17     local="$1"
     18     if [ ! -f "$1" ]; then
     19         tmp=$(mktemp)
     20         curl -o $tmp -sSfL "$1"
     21         local=$(cachefile "$tmp" "${mime##*/}")
     22         rm $tmp
     23     fi
     24     printf '%s\n' "$local"
     25 }
     26 
     27 # copy file to cache
     28 cachefile() {
     29     mkdir -p $cachedir
     30     hash=$(sha256 "$1"|cut -d' ' -f1)
     31     cp "$1" $cachedir/${hash}.$2
     32     printf '%s/%s.%s\n' "$cachedir" "$hash" "$2"
     33 }
     34 
     35 # prompt for a command to open uri
     36 prompt() {
     37     dmenu_path | dmenu -p "$1" -l 8
     38 }
     39 
     40 clip="$(xsel -o 2>/dev/null)"
     41 
     42 while getopts "hs" OPT; do
     43     case $OPT in
     44     s) slurp=$(mktemp -p $cachedir) ;;
     45     h) usage; exit 0 ;;
     46     *) usage; exit 1 ;;
     47     esac
     48 done
     49 shift $((OPTIND - 1))
     50 
     51 # fallback to clipboard when not uri given
     52 uri="${1:-$clip}"
     53 
     54 # special arg "-" read uri(s) from stdin
     55 if [ "$uri" = "-" ]; then
     56     cat | xargs -P$(nproc) -n1 $0
     57 fi
     58 
     59 # can read file _content_ from stdin, then process it
     60 if [ -n "$slurp" ]; then
     61     cat > $slurp
     62     uri="$slurp"
     63 fi
     64 
     65 [ -z "$uri" ] && exit 1
     66 
     67 if [ -f "$uri" ]; then
     68     mime=$(file -ib "$uri" | cut -d\; -f1)
     69 else
     70     proto="${uri%%:*}"
     71     case $proto in
     72     mailto)     mime='application/x-mail' ;;
     73     magnet)     mime='application/x-bittorrent' ;;
     74     *)          mime="$(peekmime $uri)" ;;
     75     esac
     76 fi
     77 
     78 if [ -n "$slurp" ]; then
     79     uri="$(cachefile $slurp ${mime##*/})"
     80     rm "$slurp"
     81 fi
     82 
     83 case "$mime" in
     84     text/html) exec firefox "$uri" >/dev/null & ;;
     85 	 audio/*)	exec mpv --no-audio-display "$uri" ;;
     86     video/*)   exec mpv "$uri" >/dev/null & ;;
     87     image/gif) exec mpv -loop $(localuri "$uri") >/dev/null & ;;
     88     image/*)   exec sxiv $(localuri "$uri") >/dev/null & ;;
     89     text/*)    exec st -e less $(localuri "$uri") >/dev/null & ;;
     90     application/x-mail) exec st -e neomutt $uri >/dev/null & ;;
     91     application/x-bittorrent) exec st -e transadd $uri >/dev/null & ;;
     92 	 application/pdf)	exec zathura $uri >/dev/null & ;;
     93     *) exec $(prompt) "$uri" >/dev/null & ;;
     94 esac