From 9b7f3199963f79f5ab9b2f894574b437457fa133 Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Sat, 1 Feb 2020 10:20:29 -0700 Subject: [PATCH] develop#python: use ptpython as default repl --- profiles/develop/python/default.nix | 13 ++- profiles/develop/python/ptconfig.py | 164 ++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 profiles/develop/python/ptconfig.py diff --git a/profiles/develop/python/default.nix b/profiles/develop/python/default.nix index 2cd21f5..dd7f38e 100644 --- a/profiles/develop/python/default.nix +++ b/profiles/develop/python/default.nix @@ -17,15 +17,24 @@ in { environment.sessionVariables = { PYTHONSTARTUP = let startup = pkgs.writers.writePython3 "ptpython.py" { - libraries = [ python3Packages.ptpython ]; + libraries = with python3Packages; [ ptpython ]; } '' + from __future__ import unicode_literals + + from pygments.token import Token + + from ptpython.layout import CompletionVisualisation + import sys + + ${builtins.readFile ./ptconfig.py} + try: from ptpython.repl import embed except ImportError: print("ptpython is not available: falling back to standard prompt") else: - sys.exit(embed(globals(), locals())) + sys.exit(embed(globals(), locals(), configure=configure)) ''; in "${startup}"; }; diff --git a/profiles/develop/python/ptconfig.py b/profiles/develop/python/ptconfig.py new file mode 100644 index 0000000..64a4bde --- /dev/null +++ b/profiles/develop/python/ptconfig.py @@ -0,0 +1,164 @@ +__all__ = ("configure",) + + +def configure(repl): + """ + Configuration method. This is called during the start-up of ptpython. + :param repl: `PythonRepl` instance. + """ + # Show function signature (bool). + repl.show_signature = True + + # Show docstring (bool). + repl.show_docstring = True + + # Show the "[Meta+Enter] Execute" message when pressing [Enter] only + # inserts a newline instead of executing the code. + repl.show_meta_enter_message = True + + # Show completions. (NONE, POP_UP, MULTI_COLUMN or TOOLBAR) + repl.completion_visualisation = CompletionVisualisation.POP_UP + + # When CompletionVisualisation.POP_UP has been chosen, use this + # scroll_offset in the completion menu. + repl.completion_menu_scroll_offset = 0 + + # Show line numbers (when the input contains multiple lines.) + repl.show_line_numbers = False + + # Show status bar. + repl.show_status_bar = True + + # When the sidebar is visible, also show the help text. + repl.show_sidebar_help = True + + # Swap light/dark colors on or off + repl.swap_light_and_dark = False + + # Highlight matching parethesis. + repl.highlight_matching_parenthesis = True + + # Line wrapping. (Instead of horizontal scrolling.) + repl.wrap_lines = True + + # Mouse support. + repl.enable_mouse_support = True + + # Complete while typing. (Don't require tab before the + # completion menu is shown.) + repl.complete_while_typing = True + + # Fuzzy and dictionary completion. + repl.enable_fuzzy_completion = False + repl.enable_dictionary_completion = False + + # Vi mode. + repl.vi_mode = True + + # Paste mode. (When True, don't insert whitespace after new line.) + repl.paste_mode = False + + # Use the classic prompt. (Display '>>>' instead of 'In [1]'.) + repl.prompt_style = "classic" # 'classic' or 'ipython' + + # Don't insert a blank line after the output. + repl.insert_blank_line_after_output = True + + # History Search. + # When True, going back in history will filter the history on the records + # starting with the current input. (Like readline.) + # Note: When enable, please disable the `complete_while_typing` option. + # otherwise, when there is a completion available, the arrows will + # browse through the available completions instead of the history. + repl.enable_history_search = False + + # Enable auto suggestions. (Pressing right arrow will complete the input, + # based on the history.) + repl.enable_auto_suggest = False + + # Enable open-in-editor. Pressing C-X C-E in emacs mode or 'v' in + # Vi navigation mode will open the input in the current editor. + repl.enable_open_in_editor = True + + # Enable system prompt. Pressing meta-! will display the system prompt. + # Also enables Control-Z suspend. + repl.enable_system_bindings = True + + # Ask for confirmation on exit. + repl.confirm_exit = True + + # Enable input validation. (Don't try to execute when the input contains + # syntax errors.) + repl.enable_input_validation = True + + # Use this colorscheme for the code. + repl.use_code_colorscheme("monokai") + + # Set color depth (keep in mind that not all terminals support true color). + + # repl.color_depth = 'DEPTH_1_BIT' # Monochrome. + # repl.color_depth = 'DEPTH_4_BIT' # ANSI colors only. + # repl.color_depth = "DEPTH_8_BIT" # The default, 256 colors. + repl.color_depth = 'DEPTH_24_BIT' # True color. + + # Syntax. + repl.enable_syntax_highlighting = True + + # Install custom colorscheme named 'my-colorscheme' and use it. + """ + repl.install_ui_colorscheme('my-colorscheme', _custom_ui_colorscheme) + repl.use_ui_colorscheme('my-colorscheme') + """ + + # Add custom key binding for PDB. + """ + @repl.add_key_binding(Keys.ControlB) + def _(event): + ' Pressing Control-B will insert "pdb.set_trace()" ' + event.cli.current_buffer.insert_text('\nimport pdb; pdb.set_trace()\n') + """ + + # Typing ControlE twice should also execute the current command. + # (Alternative for Meta-Enter.) + """ + @repl.add_key_binding(Keys.ControlE, Keys.ControlE) + def _(event): + event.current_buffer.validate_and_handle() + """ + + # Typing 'jj' in Vi Insert mode, should send escape. (Go back to navigation + # mode.) + """ + @repl.add_key_binding('j', 'j', filter=ViInsertMode()) + def _(event): + " Map 'jj' to Escape. " + event.cli.key_processor.feed(KeyPress(Keys.Escape)) + """ + + # Custom key binding for some simple autocorrection while typing. + """ + corrections = { + 'impotr': 'import', + 'pritn': 'print', + } + @repl.add_key_binding(' ') + def _(event): + ' When a space is pressed. Check & correct word before cursor. ' + b = event.cli.current_buffer + w = b.document.get_word_before_cursor() + if w is not None: + if w in corrections: + b.delete_before_cursor(count=len(w)) + b.insert_text(corrections[w]) + b.insert_text(' ') + """ + + +# Custom colorscheme for the UI. See `ptpython/layout.py` and +# `ptpython/style.py` for all possible tokens. +_custom_ui_colorscheme = { + # Blue prompt. + Token.Layout.Prompt: "bg:#eeeeff #000000 bold", + # Make the status toolbar red. + Token.Toolbar.Status: "bg:#ff0000 #000000", +}