Mercurial > hg > TextShaper
view textshaper/main.py @ 14:30f1781024a4
add splitline functionality
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 04 Feb 2014 14:44:47 -0800 |
parents | c3df7dccb02b |
children | df52326aa08d |
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()