1"""
2Configuration example for ``ptpython``.
3
4Copy this file to $XDG_CONFIG_HOME/ptpython/config.py
5On Linux, this is: ~/.config/ptpython/config.py
6"""
7from prompt_toolkit.filters import ViInsertMode
8from prompt_toolkit.key_binding.key_processor import KeyPress
9from prompt_toolkit.keys import Keys
10from prompt_toolkit.styles import Style
11
12from ptpython.layout import CompletionVisualisation
13
14__all__ = ["configure"]
15
16
17def configure(repl):
18    """
19    Configuration method. This is called during the start-up of ptpython.
20
21    :param repl: `PythonRepl` instance.
22    """
23    # Show function signature (bool).
24    repl.show_signature = True
25
26    # Show docstring (bool).
27    repl.show_docstring = False
28
29    # Show the "[Meta+Enter] Execute" message when pressing [Enter] only
30    # inserts a newline instead of executing the code.
31    repl.show_meta_enter_message = True
32
33    # Show completions. (NONE, POP_UP, MULTI_COLUMN or TOOLBAR)
34    repl.completion_visualisation = CompletionVisualisation.POP_UP
35
36    # When CompletionVisualisation.POP_UP has been chosen, use this
37    # scroll_offset in the completion menu.
38    repl.completion_menu_scroll_offset = 0
39
40    # Show line numbers (when the input contains multiple lines.)
41    repl.show_line_numbers = False
42
43    # Show status bar.
44    repl.show_status_bar = True
45
46    # When the sidebar is visible, also show the help text.
47    repl.show_sidebar_help = True
48
49    # Swap light/dark colors on or off
50    repl.swap_light_and_dark = False
51
52    # Highlight matching parethesis.
53    repl.highlight_matching_parenthesis = True
54
55    # Line wrapping. (Instead of horizontal scrolling.)
56    repl.wrap_lines = True
57
58    # Mouse support.
59    repl.enable_mouse_support = True
60
61    # Complete while typing. (Don't require tab before the
62    # completion menu is shown.)
63    repl.complete_while_typing = True
64
65    # Fuzzy and dictionary completion.
66    repl.enable_fuzzy_completion = False
67    repl.enable_dictionary_completion = False
68
69    # Vi mode.
70    repl.vi_mode = False
71
72    # Paste mode. (When True, don't insert whitespace after new line.)
73    repl.paste_mode = False
74
75    # Use the classic prompt. (Display '>>>' instead of 'In [1]'.)
76    repl.prompt_style = "classic"  # 'classic' or 'ipython'
77
78    # Don't insert a blank line after the output.
79    repl.insert_blank_line_after_output = False
80
81    # History Search.
82    # When True, going back in history will filter the history on the records
83    # starting with the current input. (Like readline.)
84    # Note: When enable, please disable the `complete_while_typing` option.
85    #       otherwise, when there is a completion available, the arrows will
86    #       browse through the available completions instead of the history.
87    repl.enable_history_search = False
88
89    # Enable auto suggestions. (Pressing right arrow will complete the input,
90    # based on the history.)
91    repl.enable_auto_suggest = False
92
93    # Enable open-in-editor. Pressing C-x C-e in emacs mode or 'v' in
94    # Vi navigation mode will open the input in the current editor.
95    repl.enable_open_in_editor = True
96
97    # Enable system prompt. Pressing meta-! will display the system prompt.
98    # Also enables Control-Z suspend.
99    repl.enable_system_bindings = True
100
101    # Ask for confirmation on exit.
102    repl.confirm_exit = True
103
104    # Enable input validation. (Don't try to execute when the input contains
105    # syntax errors.)
106    repl.enable_input_validation = True
107
108    # Use this colorscheme for the code.
109    repl.use_code_colorscheme("default")
110    # repl.use_code_colorscheme("pastie")
111
112    # Set color depth (keep in mind that not all terminals support true color).
113
114    # repl.color_depth = "DEPTH_1_BIT"  # Monochrome.
115    # repl.color_depth = "DEPTH_4_BIT"  # ANSI colors only.
116    repl.color_depth = "DEPTH_8_BIT"  # The default, 256 colors.
117    # repl.color_depth = "DEPTH_24_BIT"  # True color.
118
119    # Min/max brightness
120    repl.min_brightness = 0.0  # Increase for dark terminal backgrounds.
121    repl.max_brightness = 1.0  # Decrease for light terminal backgrounds.
122
123    # Syntax.
124    repl.enable_syntax_highlighting = True
125
126    # Get into Vi navigation mode at startup
127    repl.vi_start_in_navigation_mode = False
128
129    # Preserve last used Vi input mode between main loop iterations
130    repl.vi_keep_last_used_mode = False
131
132    # Install custom colorscheme named 'my-colorscheme' and use it.
133    """
134    repl.install_ui_colorscheme("my-colorscheme", Style.from_dict(_custom_ui_colorscheme))
135    repl.use_ui_colorscheme("my-colorscheme")
136    """
137
138    # Add custom key binding for PDB.
139    """
140    @repl.add_key_binding("c-b")
141    def _(event):
142        " Pressing Control-B will insert "pdb.set_trace()" "
143        event.cli.current_buffer.insert_text("\nimport pdb; pdb.set_trace()\n")
144    """
145
146    # Typing ControlE twice should also execute the current command.
147    # (Alternative for Meta-Enter.)
148    """
149    @repl.add_key_binding("c-e", "c-e")
150    def _(event):
151        event.current_buffer.validate_and_handle()
152    """
153
154    # Typing 'jj' in Vi Insert mode, should send escape. (Go back to navigation
155    # mode.)
156    """
157    @repl.add_key_binding("j", "j", filter=ViInsertMode())
158    def _(event):
159        " Map 'jj' to Escape. "
160        event.cli.key_processor.feed(KeyPress(Keys("escape")))
161    """
162
163    # Custom key binding for some simple autocorrection while typing.
164    """
165    corrections = {
166        "impotr": "import",
167        "pritn": "print",
168    }
169
170    @repl.add_key_binding(" ")
171    def _(event):
172        " When a space is pressed. Check & correct word before cursor. "
173        b = event.cli.current_buffer
174        w = b.document.get_word_before_cursor()
175
176        if w is not None:
177            if w in corrections:
178                b.delete_before_cursor(count=len(w))
179                b.insert_text(corrections[w])
180
181        b.insert_text(" ")
182    """
183
184    # Add a custom title to the status bar. This is useful when ptpython is
185    # embedded in other applications.
186    """
187    repl.title = "My custom prompt."
188    """
189
190
191# Custom colorscheme for the UI. See `ptpython/layout.py` and
192# `ptpython/style.py` for all possible tokens.
193_custom_ui_colorscheme = {
194    # Blue prompt.
195    "prompt": "bg:#eeeeff #000000 bold",
196    # Make the status toolbar red.
197    "status-toolbar": "bg:#ff0000 #000000",
198}
199