view textshaper/main.py @ 12:5b70bbff04b1

STUB: textshaper/main.py
author Jeff Hammel <k0scist@gmail.com>
date Sat, 25 Jan 2014 18:09:48 -0800
parents 71fb16088d54
children c3df7dccb02b
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
package to shape text blocks
"""

import optparse
import os
import subprocess
import sys
import time

def info(content):
    """gathers info about the content and returns a dict"""
    lines = content.splitlines()
    return {'lines': len(lines),
            'chars': len(content),
            'columns': max([len(line) for line in lines])
        }

def display(content, keys=('lines', 'chars', 'columns'), hr='--'):
    print content
    if keys:
        _info = info(content)
        print (hr)
    print ('; '.join(['{}: {}'.format(key, _info[key])
                      for key in keys])

def add_options(parser):
    """add options to the OptionParser instance"""

    # TODO
    #    parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard',
    #                  help="copy to given program on exit")

def main(args=sys.argv[1:]):

    # parse command line options
    usage = '%prog [options] ...'
    class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
        """description formatter for console script entry point"""
        def format_description(self, description):
            if description:
                return description.strip() + '\n'
            else:
                return ''
    parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
    options, args = parser.parse_args(args)

    # read from stdin
    content = sys.stdin.read()

    # print formatted content
    display(content)

    while True:
        time.sleep(1) # XXX

if __name__ == '__main__':
  main()