#!/usr/bin/env python

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

import argparse

from purdy.__init__ import __version__

from purdy.actions import AppendAll, AppendTypewriter
from purdy.content import LEXERS, CodeFile
from purdy.settings import settings
from purdy.ui import Screen

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

if __name__ == '__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'))

    parser.add_argument('filename', help=('Name of file containing python to '
        'parse'))
    parser.add_argument('--version', action='version', 
        version='%(prog)s {version}'.format(version=__version__ ))

    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'))

    parser.add_argument('-l', '--lexer', choices=LEXERS.choices,
        default=LEXERS.default_name, help=('Choose Pygments Lexer to use to '
            'parse the content. Choices are "con" for console (default), or '
            '"py3" for Python 3'))

    # 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'))

    # --- 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
    screen = Screen()
    code_box = screen.code_box

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

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

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