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.

51 lines
728 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 - < "$f" | cut -d' ' -f2-)
  22. case "$mime" in
  23. application/xml) l=xml ;;
  24. text/x-shellscript) l=sh ;;
  25. text/plain)
  26. case "$(file - < "$f" | cut -d' ' -f2-)" in
  27. a\ */python\ script)
  28. l=python
  29. ;;
  30. *)
  31. case "$f" in
  32. *.sh) l=sh ;;
  33. *.c|*.h|*.cc|*.hh) l=c ;;
  34. *) l=text ;;
  35. esac
  36. ;;
  37. esac
  38. esac
  39. pygmentize ${l:+-l $l} "$f"
  40. done | $PAGER
  41. if [ -n "$tmp_f" ]; then
  42. rm -f "$tmp_f"
  43. fi