linkhandler 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/sh --
  2. set -ue
  3. : "${1:?}"
  4. : "${EDITOR:=vim}"
  5. : "${TERMINAL:=st}"
  6. : "${TERMINAL_BROWSER:=w3m}"
  7. # if run from terminal ($WINDOWID setted in st)
  8. if [ "${WINDOWID:-}" ] || [ "${LINKHANDLER_TERMINAL:-}" ]; then
  9. alias sw='exec swallow'
  10. terminal=1
  11. else
  12. alias sw='exec setsid -f --'
  13. terminal=0
  14. fi
  15. case "${1%%\?*}" in
  16. *.mkv|*.webm|*.mp4|*youtube.com/watch*|*youtube.com/playlist*|*youtu.be*)
  17. sw mpv --loop-playlist=inf --quiet -- "${1}"
  18. ;;
  19. *.png|*.[jJ][pP][gG]|*.[jJ][pP][eE][gG]|*.gif|*.webp)
  20. umask 0077
  21. tmpd="$(mktemp -td "${0##*/}.img.XXXXXXXXX")"
  22. fname="$(
  23. fname="${1%%\?*}"
  24. mktemp -p "${tmpd}" "XXXXXXXXX.${fname##*.}"
  25. )"
  26. fname="${fname##*/}"
  27. trap 'rm -rf -- "${tmpd}"' EXIT QUIT HUP INT TERM
  28. curl --output-dir "${tmpd}" -sLo "${fname}" -- "${1}" && (
  29. cd "${tmpd}" && sw sxiv -a -- "${fname}"
  30. )
  31. ;;
  32. *mp3|*flac|*opus)
  33. sw mpv --loop-playlist=inf --no-audio-display -- "${1}"
  34. ;;
  35. https://telegra.ph/*|http://vk.com/@*)
  36. exec torsocks w3m "${1}"
  37. ;;
  38. *)
  39. if [ "${TO_CLIP:-}" ]; then
  40. printf %s "${1}" | xclip -selection clipboard
  41. elif [ -f "${1}" ]; then
  42. [ "${terminal}" -eq 1 ] && exec "${EDITOR}" -- "${1}"
  43. exec "${TERMINAL}" -e "${EDITOR}" -- "${1}"
  44. else
  45. [ "${terminal}" -eq 1 ] && exec "${TERMINAL_BROWSER}" "${1}"
  46. exec "${TERMINAL}" -e "${TERMINAL_BROWSER}" "${1}"
  47. fi
  48. ;;
  49. esac