Mercurial > hg > TextShaper
view textshaper/main.py @ 19:70dde00a4df0
selectable, albeit single, input
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 23 Feb 2014 00:26:19 -0800 |
parents | df52326aa08d |
children | 586631375670 |
line wrap: on
line source
#!/usr/bin/env python # -*- coding: utf-8 -*- """ package to shape text blocks """ import argparse import os import subprocess import sys import time from which import which 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='--'): """displays the content""" 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 parser instance""" parser.add_option('-f', '--file', dest='input', type=argparse.FileType('r'), default=sys.stdin, help="file to read from [DEFAULT: stdin]") if which('xclip'): # TODO: support e.g. xsel or python native parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard', action='store_true', default=False, help="copy to clipboard") def main(args=sys.argv[1:]): # parse command line options parser = argparse.ArgumentParser(description=__doc__, formatter=PlainDescriptionFormatter()) options = parser.parse_args(args) # read from stdin content = options.read() # print formatted content display(content) # main display loop # TODO: read input + commands while True: time.sleep(1) # XXX if options.copy_to_clipboard: # copy content to X clipboard process = subprocess.Popen(['xclip', '-i'], stdin=subprocess.PIPE) _, _ = process.communicate(content) if __name__ == '__main__': main()