view textshaper/main.py @ 23:7ffe000f6f42

removing stub
author Jeff Hammel <k0scist@gmail.com>
date Sun, 23 Feb 2014 10:14:06 -0800
parents 586631375670
children 929a5e97af3e
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_argument('-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_argument('-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__)
    add_options(parser)
    options = parser.parse_args(args)

    # read input
    content = options.input.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()