#!/usr/bin/env python

### Command line script that runs the purdy library with a single file

import argparse

from purdy.actions import Append, AppendTypewriter
from purdy.cmd import version_arg, lexer_arg, filename_arg, max_height_arg
from purdy.content import Code
from purdy.settings import settings
from purdy.ui import SimpleScreen

# =============================================================================
# Main
# =============================================================================

# define command line arguments
parser = argparse.ArgumentParser(description=('Displays a highlighted '
    'version of python text to the screen as if it is being typed'))

filename_arg(parser)
version_arg(parser)
lexer_arg(parser)
max_height_arg(parser)

parser.add_argument('-c', '--continuous', action='store_true', 
    help=('Instead of prentending to type like a human, just dump the file '
    'to the screen'))

parser.add_argument('-x16', action='store_true', help=('Force 16 colour '
    'terminal mode in case 256 is not working to the screen'))

# set up the typing_delay / words_per_minute options
group = parser.add_mutually_exclusive_group()
group.add_argument('-d', '--delay', type=int, 
    help=('Amount of time between each letter when in typewriter mode. '
        'Specified in milliseconds. Defaults to %sms' % settings['delay'] ))
group.add_argument('-w', '--wpm', type=int, help=('Number of words per '
    'minute that the typing speed should look like'))
parser.add_argument('--variance', type=int,
    help=('To make the typing look more real there is a variance in the '
        'delay between keystrokes. This value, in milliseconds is how '
        'much to go over or under the delay by. Defaults to +/- 30ms'))

if __name__ == '__main__':
    # --- setup our parms based on our args
    args = parser.parse_args()

    # calulcate our typing settings delays
    if args.delay:
        settings['delay'] = args.delay
    elif args.wpm:
        # in typing class, wpm calc is based on 5 letter words, change number of
        # words into letters per second then invert to get delay
        settings['delay'] = 1000 / (5 * args.wpm  / 60)

    if args.variance:
        settings['delay_variance'] = args.variance

    if args.x16:
        settings['colour'] = 16

    # initialize our display screen using a single code listing box
    settings['deactivate_args'] = True
    screen = SimpleScreen(settings=settings, max_height=args.maxheight)
    code_box = screen.code_box

    # read the code being displayed and create the appropriate action
    blob = Code(args.filename, lexer_name=args.lexer)

    if args.continuous:
        action = Append(code_box, blob)
    else:
        action = AppendTypewriter(code_box, blob)

    # --- All set, run it
    screen.run([action])
