#!/usr/bin/env python
"""
ptpython: Interactive Python shell.
Usage:
    ptpython [ --vi ] [ --history=<filename> ] [ --no-colors ]
             [ --autocompletion=<type> ]
    ptpython -h | --help

Options:
    --vi                    : Use Vi keybindings instead of Emacs bindings.
    --history=<filename>    : Path to history file.
    --autocompletion=<type> : Type of autocompletion. This can be 'popup-menu'
                              or 'horizontal-menu'.

Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
"""
import docopt
import os

from prompt_toolkit.contrib.repl import embed
from prompt_toolkit.contrib.python_input import AutoCompletionStyle


def _run_repl():
    a = docopt.docopt(__doc__)

    vi_mode = bool(a['--vi'])
    no_colors = bool(a['--no-colors'])

    # Create globals/locals dict.
    globals_, locals_ = {}, {}

    # Log history
    if a['--history']:
        history_filename = os.path.expanduser(a['--history'])
    else:
        history_filename = os.path.expanduser('~/.ptpython_history')

    # Autocompletion type
    if a['--autocompletion'] in (
                        AutoCompletionStyle.POPUP_MENU,
                        AutoCompletionStyle.HORIZONTAL_MENU,
                        AutoCompletionStyle.EXTENDED_POPUP_MENU,
                        AutoCompletionStyle.NONE
                    ):
        autocompletion_style = a['--autocompletion']
    else:
        autocompletion_style = AutoCompletionStyle.POPUP_MENU

    # Startup path
    startup_path = os.environ.get('PYTHONSTARTUP', None)

    # Run interactive shell.
    embed(globals_, locals_, vi_mode=vi_mode, history_filename=history_filename,
                    no_colors=no_colors, autocompletion_style=autocompletion_style,
                    startup_path=startup_path)

if __name__ == '__main__':
    _run_repl()
