''' Print a ruler to stdout. If you give an argument, a ruler of that number of columns is printed. Otherwise, the width is 1 less than the environment variable COLUMNS or 79 if COLUMNS isn't defined. --------------------------------------------------------------------------- Copyright (C) 2009 Don Peterson This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ''' from __future__ import division import sys, os, getopt, functools nl = "\n" supress_tens = False def StreamOut(stream, *s, **kw): t = map(str, s) sep = kw.setdefault("sep", "") stream.write(sep.join(t) + nl) out = functools.partial(StreamOut, sys.stdout) err = functools.partial(StreamOut, sys.stderr) def GetNumberOfColumns(): columns = 80 if "COLUMNS" in os.environ: columns = int(os.environ["COLUMNS"]) columns -= 1 # Reduce so we don't get a second line because of the newline return columns def PrintTens(n): if supress_tens: return out(''.join(["%10d" % i for i in xrange(1, n + 1)])) def Ruler1(columns): n, remainder, s = divmod(columns, 10) + ("1234567890",) PrintTens(n) out(s*n, s[:remainder]) def Ruler2(columns): n, remainder, s = divmod(columns, 10) + ("----+----|",) PrintTens(n) out(n*s + s[:remainder]) def Ruler3(columns): n, remainder, s = divmod(columns, 10) + ("----.----+",) PrintTens(n) out(n*s + s[:remainder]) def Ruler4(columns): n, remainder, s = divmod(columns, 10) + (" |",) PrintTens(n) out(n*s + s[:remainder]) def Ruler5(columns): n, remainder, s = divmod(columns, 10) + (" + |",) PrintTens(n) out(n*s + s[:remainder]) def Ruler6(columns): n, remainder, s = divmod(columns, 10) + (" . |",) PrintTens(n) out(n*s + s[:remainder]) def Ruler7(columns): n, remainder, s = divmod(columns, 10) + ("====+====V",) PrintTens(n) out(n*s + s[:remainder]) def Usage(status=1): name = sys.argv[0] s = '''Usage: {name} [options] [size] Print a variety of rulers to stdout. If you give size, a ruler of that number of columns will be printed. Otherwise, the size is gotten from the environment variable COLUMNS and will be one less than that value. Otherwise, 79 is used. The default ruler is of the form 1 2 3 4 ----+----|----+----|----+----|----+----|--- Use the options to get rulers of other forms. Options: -h Print this help message. -t Suppress the tens numbers The following options specify the type of ruler -a 123456789012345678901234567890123456789 -b ----.----+----.----+----.----+----.---- -c | | | -d + | + | + | + -e . | . | . | . -f ====+====V====+====V====+====V====+==== ''' out(s.format(**locals())) sys.exit(status) def ParseCommandLine(): ruler = Ruler2 try: optlist, args = getopt.getopt(sys.argv[1:], "abcdefht") except getopt.GetoptError, str: msg, option = str out(msg + nl) sys.exit(1) for opt in optlist: if opt[0] == "-a": ruler = Ruler1 if opt[0] == "-b": ruler = Ruler3 if opt[0] == "-c": ruler = Ruler4 if opt[0] == "-d": ruler = Ruler5 if opt[0] == "-e": ruler = Ruler6 if opt[0] == "-f": ruler = Ruler7 if opt[0] == "-h": Usage(0) if opt[0] == "-t": global supress_tens supress_tens = True if len(args) > 1: Usage(1) elif len(args) == 1: try: columns = int(args[0]) except ValueError: err("'%s' is a bad size specification") exit(1) else: columns = GetNumberOfColumns() return ruler, columns def main(): ruler, columns = ParseCommandLine() ruler(columns) main()