Skip to content

Instantly share code, notes, and snippets.

@alok
Created March 9, 2018 10:15
Show Gist options
  • Save alok/8710687865d6ba1969f57373c26fde5a to your computer and use it in GitHub Desktop.
Save alok/8710687865d6ba1969f57373c26fde5a to your computer and use it in GitHub Desktop.
"""
Configuration example for ``ptpython``.
Copy this file to ~/.ptpython/config.py
"""
from __future__ import unicode_literals
from prompt_toolkit.keys import Keys
from ptpython.layout import CompletionVisualisation
from pygments.token import Token
__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
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 = True
# Show status bar.
repl.show_status_bar = True
# When the sidebar is visible, also show the help text.
repl.show_sidebar_help = True
# 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
# Vi mode.
repl.vi_mode = False
# Paste mode. (When True, don't insert whitespace after new line.)
repl.paste_mode = False
# repl.paste_mode = True
# Use the classic prompt. (Display '>>>' instead of 'In [1]'.)
repl.prompt_style = 'ipython' # 'classic' or 'ipython'
# 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 = True
# 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 = False
# 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 = False
# 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('pastie')
# Enable 24bit True color. (Not all terminals support this. -- maybe check
# $TERM before changing.)
repl.true_color = 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('\nfrom ptpdb import set_trace; set_trace()\n')
# Custom key binding for some simple autocorrection while typing.
misspellings = [
'pirnt',
'ipnrt',
'prini',
'prinn',
'pritn',
'prnin',
'prnit',
'prnti',
'prntn',
'prtii',
'prtin',
'prtit',
'prtni',
'prtnn',
'prtnt',
]
corrections = {misspelling: 'print' for misspelling in misspellings}
@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 and w in corrections:
b.delete_before_cursor(count=len(w))
b.insert_text(corrections[w])
b.insert_text(' ')
@repl.add_key_binding(Keys.ControlW)
def _(event) -> None:
''' Remap c-w to be more like vim '''
b = event.cli.current_buffer
b.delete_before_cursor()
# First condition is to avoid swallowing characters like dots.
# The second condition is to avoid an infinite loop when deleting from the middle of a string.
# If there's only one character, the `char_before_cursor` attribute is set to be the only remaining character.
# But then it won't delete anything since there are no characters *actually* before the cursor.
# But `char_before_cursor` is always alphanumeric, so the while loop never breaks.
while b.document.char_before_cursor.isalnum() and b.document.cursor_position > 0:
b.delete_before_cursor()
# 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',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment