Thursday, July 30, 2009

Pretty print a bunch of code files and save paper

I used to do:
$ find path... -type f | xargs pr -Fe2 | lpr
This works quite well. But when most of the files are short and only a few take more than one page I end up with a lot of blank lines. A waste of paper.

What I want is to omit pagination but retain the headers which allow me to tell where one file ends and another begins. pr(1) options -t and -T do not help, so I came up with my own solution:
#!/bin/sh
# printcode: prints out a bunch of text code files
# expanding each tab to two spaces and marking the
# beginning of each file, without using form feeds.
#
# Public domain
# Author: Antonio Bonifati http://ninuzzo.freehostia.com

# By default pretty print all files in the current directory.
[ "$1" ] || set .

for i in $(find "$@" -type f); do
  echo -e "\n==========> $i\n"
  expand -t2 $i
done

No comments: