view textshaper/main.py @ 18:df52326aa08d

start filling out options
author Jeff Hammel <k0scist@gmail.com>
date Sat, 22 Feb 2014 22:49:18 -0800
parents c3df7dccb02b
children 70dde00a4df0
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 OptionParser instance"""

    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 = sys.stdin.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()