You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
809 B

  1. #!/bin/sh
  2. exec 2>&1
  3. tmp_f=
  4. if [ $# -eq 0 ]; then
  5. tmp_f="${TMPDIR:-/tmp}/colorize.$$"
  6. trap "rm -f '$tmp_f'" INT TERM
  7. cat > "$tmp_f"
  8. set -- "$tmp_f"
  9. fi
  10. if [ -t 1 ]; then
  11. if env | grep -q '^PAGER=$'; then
  12. PAGER=cat
  13. elif [ -z "$PAGER" ]; then
  14. PAGER="less -R"
  15. fi
  16. else
  17. PAGER=cat
  18. fi
  19. for f; do
  20. l=
  21. eval mime=$(file -i -L - < "$f" | cut -d' ' -f2-)
  22. case "$mime" in
  23. application/xml) l=xml ;;
  24. text/x-shellscript) l=sh ;;
  25. text/x-diff) l=diff ;;
  26. text/x-c) l=c ;;
  27. text/plain)
  28. case "$(file - < "$f" | cut -d' ' -f2-)" in
  29. a\ */python\ script)
  30. l=python
  31. ;;
  32. *)
  33. case "$f" in
  34. *.sh) l=sh ;;
  35. *.ini) l=ini ;;
  36. *.c|*.h|*.cc|*.hh) l=c ;;
  37. *.go) l=go ;;
  38. *) l=text ;;
  39. esac
  40. ;;
  41. esac
  42. esac
  43. pygmentize ${l:+-l $l} "$f"
  44. done | $PAGER
  45. if [ -n "$tmp_f" ]; then
  46. rm -f "$tmp_f"
  47. fi