Prompt Toolkit¶
How I’ve utilized prompt_toolkit and extended it.
kb
¶
-
class
BindingPP
(keys: Tuple[Union[prompt_toolkit.keys.Keys, str], ...], handler: Callable[[KeyPressEvent], None], filter: Union[prompt_toolkit.filters.base.Filter, bool] = True, eager: Union[prompt_toolkit.filters.base.Filter, bool] = False, is_global: Union[prompt_toolkit.filters.base.Filter, bool] = False, save_before: Callable[[KeyPressEvent], bool] = <function Binding.<lambda>>, record_in_macro: Union[prompt_toolkit.filters.base.Filter, bool] = True)[source][source]¶ Bases:
prompt_toolkit.key_binding.key_bindings.Binding
Fix the prompt_toolkit binding.
Allow them to compared, called, hashed or evaluated for truthiness. As none of this is originally available.
Todo
__lt__ so we can sort
-
convert_bindings
(bindings: prompt_toolkit.key_binding.key_bindings.KeyBindingsBase)[source][source]¶
-
class
KeyBindingsManager
(kb: Optional[prompt_toolkit.key_binding.key_bindings.KeyBindings] = None, shell: Optional[IPython.core.interactiveshell.InteractiveShell] = None, **kwargs)[source][source]¶ Bases:
prompt_toolkit.key_binding.key_bindings.KeyBindingsBase
An object to make working with keybindings easier.
Subclasses UserList with a list of Keys and their handlers. By defining dunders, the collection of keybindings are much easier to work with.
-
__init__
(kb: Optional[prompt_toolkit.key_binding.key_bindings.KeyBindings] = None, shell: Optional[IPython.core.interactiveshell.InteractiveShell] = None, **kwargs)[source][source]¶ Initialize the class.
- Parameters
kb (
KeyBindings
, optional) – KeyBindings to initialize with.
-
add
(another_one, *args)[source][source]¶ Add another binding.
Takes same parameters as
Binding
__init__
and not the same bindings asKeyBindings.add
.
-
add_binding
(another_one, *args)[source]¶ Add another binding.
Takes same parameters as
Binding
__init__
and not the same bindings asKeyBindings.add
.
-
insert
(another_one, *args)[source]¶ Add another binding.
Takes same parameters as
Binding
__init__
and not the same bindings asKeyBindings.add
.
-
get_bindings_for_keys
(keys)[source][source]¶ Return a list of key bindings that can handle this key.
(This return also inactive bindings, so the
filter
still has to be called, for checking it.)- Parameters
keys – tuple of keys.
-
get_bindings_starting_with_keys
(keys)[source][source]¶ Return a list of key bindings that handle a sequence starting with
keys
.(It does only return bindings for which the sequences are longer than
keys
. And likeget_bindings_for_keys
, it also includes inactive bindings.)- Parameters
keys – tuple of keys.
-
-
class
Documented
(text: str = '', cursor_position: Optional[int] = None, selection: Optional[prompt_toolkit.selection.SelectionState] = None)[source][source]¶ Bases:
prompt_toolkit.document.Document
I’ll admit this subclass doesn’t exist for much of a reason.
However, it’s a LOT easier to work with classes with their dunders defined.
Implement the basics for a class to be considered a sequence IE len and iter.
ptoolkit
¶
Create classes used to enhance the prompt_toolkit objects bound to the running IPython interpreter.
Provides utilities functions and classes to work with both prompt_toolkit
and IPython
. The APIs of both libraries can be individually quite
overwhelming, and the combination and interaction of the 2 can prove difficult
to stay on top of.
The Helpers
class defined here gives a useful reference as to the
relationship between a number of the intertwined classes in a running
PromptSession
.
Notes
Of use might be.:
get_ipython().pt_app.layout
An object that contains the default_buffer, DEFAULT_BUFFER
, a reference
to a container HSplit
and a few other things possibly worth exploring.
-
get_session
()[source][source]¶ A patch to cover up the fact that get_app() returns a DummyApplication.
-
create_jedi_interpreter
(document: prompt_toolkit.document.Document) → Optional[jedi.api.Interpreter][source][source]¶
-
class
Helpers
[source][source]¶ Bases:
object
A class that attempts enumerating the hierarchy of classes in prompt_toolkit.
-
property
app_kb
[source][source]¶ Application instance keybindings.
NOT the same as ‘session_kb’.
In [103]: pt.pt_app_kb == pt.session_kb Out[103]: False
-
property
app_layout
[source][source]¶ The same as session_layout thank god.
In [102]: pt.app_layout == pt.session_layout Out[102]: True
-
property
app_style
[source][source]¶ Of course it’s not the same as the session_style.
In [108]: pt.pt_app.style Out[108]: <prompt_toolkit.styles.base.DynamicStyle at 0x7f164da54f40> In [109]: pt.session.style Out[109]: <prompt_toolkit.styles.base.DynamicStyle at 0x7f1658a90a30>
-
property
editing_mode
[source][source]¶ Thankfully the same on a PromptSession and Application.
In [5]: _ip.pt_app.editing_mode Out[5]: <EditingMode.VI: ‘VI’>
In [6]: _ip.pt_app.app.editing_mode Out[6]: <EditingMode.VI: ‘VI’>
-
property
current_buffer_app
[source][source]¶ Current buffer a returned by the App. Doesn’t exist on the PromptSession.
-
property
current_container
[source][source]¶ Return the container attr of the layout.
Typically will return the HSplit defining the layout of the app.
-
property
current_container_children
[source][source]¶ Return a list of the current container children.
I genuinely don’t think I expected it to be so big.
HSplit.children is a lie.
Use HSplit._all_children
-
property
current_control
[source][source]¶ Return the layout’s current control. Typically a BufferControl.
-
property
current_buffer_texts
[source][source]¶ Return the ‘text’ attr of the current buffer as a string.
-
property
current_document_text
[source][source]¶ Return the ‘lines’ attr of the current document as a list.
-
property
current_content
[source][source]¶ The ‘content’ attribute of a
Window
returns a UIControl instance.In this specific case, it returns a BufferControl.
-
property
content_is_control
[source][source]¶ Is the
Window
content the control returned by the layout?
-
property
session_validator
[source][source]¶ The validator instance on the PromptSession.
Notes
Not only do we not have this attribute on the App, it’s not bound to anything by default in IPython!
-
property
renderer_output
[source][source]¶ The output attribute from the
prompt_toolkit.renderer.Renderer
.Examples
In [9]: h = Helpers() In [10]: h.session.output Out[10]: <prompt_toolkit.output.windows10.Windows10_Output at 0x240ff55b460> In [11]: h.pt_app.output Out[11]: <prompt_toolkit.output.windows10.Windows10_Output at 0x240ff55b460> In [12]: h.session.input Out[12]: <prompt_toolkit.input.win32.Win32Input at 0x240ff722050> In [13]: h.pt_app.input Out[13]: <prompt_toolkit.input.win32.Win32Input at 0x240ff722050>
-
current_buffer_lines
()[source][source]¶ Effectively a full history of every command I’ve run across sessions.
This is crazy to look at and I don’t know where it’s storing this persistent info.
- Returns
_working_lines
- Return type
lines
-
app_context
()[source][source]¶ What is this?
Behaves similarly to a dict but won’t display values.:
[ins] In [165]: _ip.pt_app.app.context Out[165]: <Context at 0x7877b7d880> [ins] In [166]: _ip.pt_app.app.context.items() Out[166]: <items at 0x7877ac00f0> [ins] In [167]: _ip.pt_app.app.context.keys() Out[167]: <keys at 0x7877b46e00> [ins] In [168]: print_formatted_text(_ip.pt_app.app.context) <Context object at 0x787791c380>
-
property
-
all_processors_for_searching
()[source][source]¶ Return a list of
prompt_toolkit.layout.processor.Processor
's.
-
search_layout
()[source][source]¶ Generate a
Layout
with aSearchToolbar
and keybindings.Notes
Windows require their
content
arguments to have a methodreset
.
-
pt_validator
() → prompt_toolkit.validation.Validator[source][source]¶
-
user_overrides
(f, overrides=None, *args, **kwargs)[source][source]¶ Decorator that allows a user to fill in the remainder of a functools.partial.
See Also¶
- completions
Use of the
prompt_toolkit.completions.Completers
.