Skip to content

Commit 4a20265

Browse files
Improved documentation.
1 parent 624aea9 commit 4a20265

File tree

14 files changed

+222
-116
lines changed

14 files changed

+222
-116
lines changed

CHANGELOG

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Changes:
3030
mostly a proxy to `Application`.)
3131

3232
A consequence is that all almost code which used to received a
33-
`CommandLineInterface`, will now receive an `Application`. Usually, where we
33+
`CommandLineInterface`, will now use an `Application`. Usually, where we
3434
had an attribute `cli`, we'll now have an attribute `app`.
3535

36-
* `CLIFilter` will now take an `Application`, so it was renamed to `AppFilter`.
37-
* `to_cli_filter` was renamed to `to_app_filter`.
36+
Secondly, the `Application` object is no longer passed around. The `get_app`
37+
function can be used at any time to acquire the active application.
3838

3939
(For backwards-compatibility, we have aliases to the old names, whenever
4040
possible.)
@@ -216,6 +216,10 @@ Changes:
216216

217217
- Changes to the `filters` module:
218218

219+
* The `Application` is no longer passed around, so both `CLIFilter` and
220+
`SimpleFilter` were merged into `Filter`. `to_cli_filter` and
221+
`to_simple_filter` became `to_filter`.
222+
219223
* All filters have been turned into functions. For instance, `IsDone`
220224
became `is_done` and `HasCompletions` became `has_completions`.
221225

@@ -224,8 +228,8 @@ Changes:
224228
`HasCompletions()` has to be replaced by `has_completions` (without
225229
parenthesis).
226230

227-
The few filters that took arguments as input, are became functions, but
228-
still have to be called with the given arguments.
231+
The few filters that took arguments as input, became functions, but still
232+
have to be called with the given arguments.
229233

230234
For new filters, it is recommended to use the `@Condition` decorator,
231235
rather then inheriting from `Filter`.

docs/pages/asking_for_input.rst

Lines changed: 76 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ Asking for input (prompts)
44
==========================
55

66
This page is about building prompts. Pieces of code that we can embed in a
7-
program for asking the user for input. If you want to use `prompt_toolkit` for
8-
building full screen terminal applications, it is probably still a good idea to
9-
read this first, before heading to the :ref:`building full screen applications
10-
<full_screen_applications>` page.
7+
program for asking the user for input. Even if you want to use `prompt_toolkit`
8+
for building full screen terminal applications, it is probably still a good
9+
idea to read this first, before heading to the :ref:`building full screen
10+
applications <full_screen_applications>` page.
11+
12+
In this page, we will cover autocompletion, syntax highlighting, key bindings,
13+
and so on.
1114

1215

1316
Hello world
@@ -46,7 +49,7 @@ Instead of calling the :func:`~prompt_toolkit.shortcuts.prompt` function, it's
4649
also possible to create a :class:`~prompt_toolkit.shortcuts.PromptSession`
4750
instance followed by calling its
4851
:meth:`~prompt_toolkit.shortcuts.PromptSession.prompt` method for every input
49-
call. This creates a kind of an input session.
52+
call. This creates a kind of an input session.
5053

5154
.. code:: python
5255
@@ -107,9 +110,13 @@ you can do the following:
107110
from prompt_toolkit.styles.pygments import style_from_pygments
108111
109112
style = style_from_pygments(get_style_by_name('monokai'))
110-
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer), style=style)
113+
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer), style=style,
114+
include_default_pygments_style=False)
111115
print('You said: %s' % text)
112116
117+
We pass ``include_default_pygments_style=False``, because otherwise, both
118+
styles will be merged, possibly giving slightly different colors in the outcome
119+
for cases where where our custom Pygments style doesn't specify a color.
113120

114121
.. _colors:
115122

@@ -165,14 +172,14 @@ Creating a custom style could be done like this:
165172
.. code:: python
166173
167174
from prompt_toolkit.shortcuts import prompt
168-
from prompt_toolkit.styles import style_from_pygments, merge_style
175+
from prompt_toolkit.styles import style_from_pygments_cls, merge_styles
169176
from prompt_toolkit.lexers import PygmentsLexer
170177
171178
from pygments.styles.tango import TangoStyle
172179
from pygments.lexers import HtmlLexer
173180
174-
our_style = merge_style([
175-
style_from_pygments(TangoStyle),
181+
our_style = merge_styles([
182+
style_from_pygments_cls(TangoStyle),
176183
Style.from_dict({
177184
'pygments.comment': '#888888 bold',
178185
'pygments.keyword': '#ff88ff bold',
@@ -186,9 +193,8 @@ Creating a custom style could be done like this:
186193
Coloring the prompt itself
187194
^^^^^^^^^^^^^^^^^^^^^^^^^^
188195

189-
It is possible to add some colors to the prompt itself. For this, we need a
190-
``get_prompt`` function. This is a function that can return a string, but also
191-
a list of ``(style, text)`` tuples.
196+
It is possible to add some colors to the prompt itself. For this, we need to
197+
build some :ref:`formatted text <formatted_text>`.
192198

193199
.. code:: python
194200
@@ -208,17 +214,19 @@ a list of ``(style, text)`` tuples.
208214
'path': '#884444 underline',
209215
})
210216
211-
def get_prompt():
212-
return [
213-
('class:username', 'john'),
214-
('class:at', '@'),
215-
('class:host', 'localhost'),
216-
('class:colon', ':'),
217-
('class:path', '/user/john'),
218-
('class:pound', '# '),
219-
]
217+
message = [
218+
('class:username', 'john'),
219+
('class:at', '@'),
220+
('class:host', 'localhost'),
221+
('class:colon', ':'),
222+
('class:path', '/user/john'),
223+
('class:pound', '# '),
224+
]
225+
226+
text = prompt(message, style=style)
220227
221-
text = prompt(get_prompt, style=style)
228+
The `message` can be any kind of formatted text, as discussed :ref:`here
229+
<formatted_text>`. It can also be a callable that returns some formatted text.
222230

223231
By default, colors are taking from the 256 color palette. If you want to have
224232
24bit true color, this is possible by adding the ``true_color=True`` option to
@@ -285,7 +293,7 @@ that takes a :class:`~prompt_toolkit.document.Document` and yields the current
285293
:class:`~prompt_toolkit.completion.Completion` instances. Each completion
286294
contains a portion of text, and a position.
287295

288-
The position is used in for fixing text before the cursor. Pressing the tab key
296+
The position is used for fixing text before the cursor. Pressing the tab key
289297
could for instance turn parts of the input from lowercase to uppercase. This
290298
makes sense for a case insensitive completer. Or in case of a fuzzy completion,
291299
it could fix typos. When ``start_position`` is something negative, this amount
@@ -295,8 +303,8 @@ of characters will be deleted and replaced.
295303
Styling individual completions
296304
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
297305

298-
Each completion can provide a custom style, which is used when its rendered in
299-
the completion menu or toolbar. This is possible by passing a style to each
306+
Each completion can provide a custom style, which is used when it is rendered
307+
in the completion menu or toolbar. This is possible by passing a style to each
300308
:class:`~prompt_toolkit.completion.Completion` instance.
301309

302310
.. code:: python
@@ -407,42 +415,38 @@ History
407415
-------
408416

409417
A :class:`~prompt_toolkit.history.History` object keeps track of all the
410-
previously entered strings. When nothing is passed into the
411-
:func:`~prompt_toolkit.shortcuts.prompt` function, it will start with an empty
412-
history each time again. Usually, however, for a REPL, you want to keep the
413-
same history between several calls to
414-
:meth:`~prompt_toolkit.shortcuts.prompt`. This is possible by instantiating a
415-
:class:`~prompt_toolkit.history.History` object and passing that to each
416-
:meth:`~prompt_toolkit.shortcuts.prompt` call as follows:
417-
418-
.. code:: python
418+
previously entered strings, so that the up-arrow can reveal previously entered
419+
items.
419420

420-
from prompt_toolkit.history import InMemoryHistory
421-
from prompt_toolkit import prompt
421+
The recommended way is to use a
422+
:class:`~prompt_toolkit.shortcuts.PromptSession`, which uses an
423+
:class:`~prompt_toolkit.history.InMemoryHistory` for the entire session by
424+
default. The following example has a history out of the box:
422425

423-
history = InMemoryHistory()
424-
425-
while True:
426-
prompt(history=history)
426+
.. code:: python
427427
428+
from prompt_toolkit import PromptSession
428429
429-
To persist a history to disk, use :class:`~prompt_toolkit.history.FileHistory`
430-
instead instead of :class:`~prompt_toolkit.history.InMemoryHistory`.
430+
session = PromptSession()
431431
432-
.. note::
432+
while True:
433+
session.prompt()
433434
434-
Note that the same result as in the example above (with an
435-
:class:`~prompt_toolkit.history.InMemoryHistory`) can be achieved by
436-
creating a :func:`~prompt_toolkit.shortcuts.PromptSession` instance.
435+
To persist a history to disk, use a :class:`~prompt_toolkit.history.FileHistory`
436+
instead instead of the default
437+
:class:`~prompt_toolkit.history.InMemoryHistory`. This history object can be
438+
passed either to a :class:`~prompt_toolkit.shortcuts.PromptSession` or to the
439+
:meth:`~prompt_toolkit.shortcuts.prompt` function. For instance:
437440

438-
.. code:: python
441+
.. code:: python
439442
440-
from prompt_toolkit import PromptSession
443+
from prompt_toolkit import PromptSession
444+
from prompt_toolkit.history import FileHistory
441445
442-
session = PromptSession()
446+
session = PromptSession(history=FileHistory('~/.myhistory'))
443447
444-
while True:
445-
session.prompt()
448+
while True:
449+
session.prompt()
446450
447451
448452
Auto suggestion
@@ -459,20 +463,21 @@ the current input. Pressing the right arrow :kbd:`→` will insert this suggesti
459463

460464
When suggestions are based on the history, don't forget to share one
461465
:class:`~prompt_toolkit.history.History` object between consecutive
462-
:func:`~prompt_toolkit.shortcuts.prompt` calls.
466+
:func:`~prompt_toolkit.shortcuts.prompt` calls. Using a
467+
:class:`~prompt_toolkit.prompt.PromptSession` does this for you.
463468

464469
Example:
465470

466471
.. code:: python
467472
468-
from prompt_toolkit import prompt
473+
from prompt_toolkit import PromptSession
469474
from prompt_toolkit.history import InMemoryHistory
470475
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
471476
472-
history = InMemoryHistory()
477+
session = PromptSession()
473478
474479
while True:
475-
text = prompt('> ', history=history, auto_suggest=AutoSuggestFromHistory())
480+
text = session.prompt('> ', auto_suggest=AutoSuggestFromHistory())
476481
print('You said: %s' % text)
477482
478483
@@ -566,7 +571,7 @@ By default, every prompt already has a set of key bindings which implements the
566571
usual Vi or Emacs behaviour. We can extend this by passing another
567572
:class:`~prompt_toolkit.key_binding.KeyBindings` instance to the
568573
``key_bindings`` argument of the :func:`~prompt_toolkit.shortcuts.prompt`
569-
function.
574+
function or the :class:`~prompt_toolkit.shortcuts.PromptSession` class.
570575

571576
An example of a prompt that prints ``'hello world'`` when :kbd:`Control-T` is pressed.
572577

@@ -674,7 +679,8 @@ Using control-space for completion
674679
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
675680

676681
An popular short cut that people sometimes use it to use control-space for
677-
opening the autocompletion menu.
682+
opening the autocompletion menu instead of the tab key. This can be done with
683+
the following key binding.
678684

679685
.. code:: python
680686
@@ -710,15 +716,16 @@ of accepting and returning the input. The user will now have to press
710716
:kbd:`Enter`.)
711717

712718
It is possible to specify a continuation prompt. This works by passing a
713-
``get_continuation_tokens`` callable to ``prompt``. This function can return a
714-
list of ``(style, text)`` tuples. The width of the returned text should not
715-
exceed the given width. (The width of the prompt margin is defined by the
716-
prompt.)
719+
``prompt_continuation`` callable to ``prompt``. This function is supposed to
720+
return formatted text, or a list of ``(style, text)`` tuples. The width of the
721+
returned text should not exceed the given width. (The width of the prompt
722+
margin is defined by the prompt.)
717723

718724
.. code:: python
719725
720726
def prompt_continuation(width):
721-
return [('', '.' * width)]
727+
return '.' * width
728+
# Or: return [('', '.' * width)]
722729
723730
prompt('> ', multiline=True, prompt_continuation=prompt_continuation)
724731
@@ -756,7 +763,7 @@ Line wrapping
756763
^^^^^^^^^^^^^
757764

758765
Line wrapping is enabled by default. This is what most people are used to and
759-
this is what GNU readline does. When it is disabled, the input string will
766+
this is what GNU Readline does. When it is disabled, the input string will
760767
scroll horizontally.
761768

762769
.. code:: python
@@ -793,14 +800,19 @@ interface to run in another thread. (If we have custom key bindings for
793800
instance, it would be better to run them in the same thread as the other code.)
794801

795802
The answer is to run the prompt_toolkit interface on top of the asyncio event
796-
loop. Prompting the user for input is as simple as calling
803+
loop. First we have to tell prompt_toolkit to use the asyncio event loop. Then
804+
prompting the user for input is as simple as calling
797805
:func:`~prompt_toolkit.shortcuts.prompt` with the `async_=True` argument.
798806

799807
.. code:: python
800808
801809
from prompt_toolkit import prompt
810+
from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop
802811
from prompt_toolkit.patch_stdout import patch_stdout
803812
813+
# Tell prompt_toolkit to use the asyncio event loop.
814+
use_asyncio_event_loop()
815+
804816
async def my_coroutine():
805817
while True:
806818
with patch_stdout():

docs/pages/full_screen_apps.rst

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ a key press), it will send that to the the appropriate handler, like for
8383
instance, a key binding.
8484

8585
When :func:`~prompt_toolkit.application.Application.run()` is called, the event
86-
loop will run until the application is done.
86+
loop will run until the application is done. An application will quit when
87+
:func:`~prompt_toolkit.application.Application.exit()` is called.
8788

8889

8990
The layout
@@ -109,7 +110,7 @@ abstraction.
109110
way to define the "shape" of the layout.
110111

111112
The :class:`~prompt_toolkit.layout.containers.Window` object is a special
112-
kind of container that can contain
113+
kind of container that can contain a
113114
:class:`~prompt_toolkit.layout.controls.UIControl` object. The
114115
:class:`~prompt_toolkit.layout.controls.UIControl` object is responsible for
115116
the generation of the actual content. The
@@ -124,6 +125,11 @@ abstraction.
124125
:class:`~prompt_toolkit.layout.controls.FormattedTextControl` for displaying
125126
(:ref:`formatted <formatted_text>`) text.
126127

128+
Normally, it is never needed to create new
129+
:class:`~prompt_toolkit.layout.controls.UIControl` or
130+
:class:`~prompt_toolkit.layout.containers.Container` classes, but instead you
131+
would create the layout by composing instances of the existing built-ins.
132+
127133
- A higher level abstraction of building a layout is by using "widgets". A
128134
widget is a reusable layout component that can contain multiple containers
129135
and controls. It should have a ``__pt__container__`` function, which is
@@ -228,7 +234,27 @@ condition is satisfied, use a
228234
Focusing windows
229235
^^^^^^^^^^^^^^^^^
230236

231-
TODO
237+
Focussing something can be done by calling the
238+
:meth:`~prompt_toolkit.layout.Layout.focus` method. This method is very
239+
flexible and accepts a :class:`~prompt_toolkit.layout.containers.Window`, a
240+
:class:`~prompt_toolkit.buffer.Buffer`, a
241+
:class:`~prompt_toolkit.layout.controls.UIControl` and more.
242+
243+
In the following example, we use :func:`~prompt_toolkit.application.get_app`
244+
for getting the active application.
245+
246+
.. code:: python
247+
248+
from prompt_toolkit.application import get_app
249+
250+
# This window was created earlier.
251+
w = Window()
252+
253+
# ...
254+
255+
# Now focus it.
256+
get_app().layout.focus(w)
257+
232258
233259
Key bindings
234260
------------
@@ -251,6 +277,8 @@ There are two kinds of key bindings:
251277
Global key bindings
252278
^^^^^^^^^^^^^^^^^^^
253279

280+
Key bindings can be passed to the application as follows:
281+
254282
.. code:: python
255283
256284
from prompt_toolkit import Application

docs/pages/getting_started.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ in the repository.
7676

7777
- Secondly, go through the :ref:`asking for input <asking_for_input>` section.
7878
This is useful for almost any use case, even for full screen applications.
79+
It covers autocompletions, syntax highlighting, key bindings, and so on.
7980

8081
- Then, learn about :ref:`dialogs`, which is easy and fun.
8182

0 commit comments

Comments
 (0)