@@ -8,8 +8,101 @@ applications. Typically, an application consists of a layout (to describe the
88graphical part) and a set of key bindings.
99
1010The sections below describe the components required for full screen
11- applications (or custom, non full screen applications), and how to assemble them
12- together.
11+ applications (or custom, non full screen applications), and how to assemble
12+ them together.
13+ 
14+ 
15+ Running the application
16+ ----------------------- 
17+ 
18+ To run our final Full screen Application, we first need three I/O objects.
19+ And an :class: `~prompt_toolkit.application.Application ` instance.
20+ These are passed as arguments to :class: `~prompt_toolkit.interface.CommandLineInterface `.
21+ 
22+ For the I/O objects::
23+ 
24+  - An :class:`~prompt_toolkit.eventloop.base.EventLoop` instance. This is 
25+  basically a while-true loop that waits for user input, and when it receives 
26+  something (like a key press), it will send that to the application. 
27+  - An :class:`~prompt_toolkit.input.Input` instance. This is an abstraction on 
28+  the input stream (stdin). 
29+  - An :class:`~prompt_toolkit.output.Output` instance. This is an abstraction on 
30+  the output stream, and is called by the renderer. 
31+ 
32+ However, all three of the I/O objects are optional, and prompt_toolkit uses the
33+ obvious default.
34+ 
35+ We'll come back at what the :class: `~prompt_toolkit.application.Application ` instance is later.
36+ 
37+ 
38+ So, the only thing we actually need in order to run an application is this:
39+ 
40+ .. code :: python 
41+ 
42+  from  prompt_toolkit.interface import  CommandLineInterface 
43+  from  prompt_toolkit.application import  Application 
44+ 
45+  application =  Application() 
46+  cli =  CommandLineInterface(application = application) 
47+  #  cli.run() 
48+  print (' Exiting'  
49+ 
50+ 
51+ ..  I'm not sure of the following as sometime `Enter` will make the Application exit,
52+ ..  but better safe that locking the users that follow the tutorial.
53+ 
54+ 
55+ for a signal to exit the event loop. This is why the `cli.run() ` part is commented.
56+ 
57+ Let's now bind a keyboard shortcut to exit.
58+ 
59+ Key bindings
60+ ------------ 
61+ 
62+ In order to react to user action, you need to create a registry of keyboard
63+ shortcut to pass to the :class: `~prompt_toolkit.application.Application ` when
64+ it is constructed. The easiest way to do so is to create a
65+ :class: `~prompt_toolkit.key_binding.manager.KeyBindingManager `, and then attach
66+ callback to desired shortcut. :class: `~prompt_toolkit.keys.Keys ` contains a few
67+ predefined keyboards shortcut that can be useful.
68+ 
69+ To create a `registry ` simply instantiate
70+ :class: `~prompt_toolkit.key_binding.manager.KeyBindingManager ` and get it's
71+ `registry ` attribute:
72+ 
73+ .. code :: python 
74+ 
75+  from  prompt_toolkit.key_binding.manager import  KeyBindingManager 
76+  registry =  KeyBindingManager().registry 
77+ 
78+ Application ` constructor, and pass the registry as one of the argument.
79+ 
80+ .. code :: python 
81+ 
82+  application =  Application(buffer = buffer, key_bindings_registry = registry) 
83+ 
84+ 
85+ :meth: `prompt_toolkit.key_binding.registry.Registry.add_binding ` methods as a
86+ decorator of a callback:
87+ 
88+ .. code :: python 
89+ 
90+  from  prompt_toolkit.keys import  Keys 
91+ 
92+  @registry.add_binding (Keys.ControlQ, eager = True ) 
93+  def  exit_ (event ): 
94+  """  
95+  Pressing Ctrl-Q will exit the user interface. 
96+ 
97+  Setting a return value means: quit the event loop that drives the user 
98+  interface and return this value from the `CommandLineInterface.run()` call. 
99+  """  
100+  event.cli.set_return_value(None ) 
101+ 
102+ eager=True ` to trigger the callback as soon
103+ as the shortcut `Ctrl-Q ` is pressed. The callback is named `exit_ ` to be
104+ explicit, but the name have not much importance.
105+ 
13106
14107
15108Creating a layout
@@ -202,8 +295,6 @@ The :class:`~prompt_toolkit.layout.containers.Window` class exposes many
202295interesting functionality that influences the behaviour of user controls.
203296
204297
205- Key bindings
206- ------------ 
207298
208299
209300Buffers
@@ -246,35 +337,6 @@ We are talking about full screen applications, so it's important to pass
246337``use_alternate_screen=True ``. This switches the terminal buffer.
247338
248339
249- Running the application
250- ----------------------- 
251- 
252- We need three I/O objects to run an application. These are passed as arguments
253- to :class: `~prompt_toolkit.interface.CommandLineInterface `.
254- 
255- - An :class: `~prompt_toolkit.eventloop.base.EventLoop ` instance.
256-  This is basically a while-true loop that waits for user input, and when it
257-  receives something (like a key press), it will send that to the application.
258- 
259- - An :class: `~prompt_toolkit.input.Input ` instance.
260-  This is an abstraction on the input stream (stdin).
261- 
262- - An :class: `~prompt_toolkit.output.Output ` instance. This is an abstraction of
263-  the output stream, and is called by the renderer.
264- 
265- However, all three of the I/O objects are optional, and `prompt_toolkit ` uses the
266- obvious default.
267- 
268- So, the only thing we actually need in order to run an application is this:
269- 
270- .. code :: python 
271- 
272-  from  prompt_toolkit.interface import  CommandLineInterface 
273- 
274-  cli =  CommandLineInterface(application = application) 
275-  cli.run() 
276- 
277- 
278340.. _filters :
279341
280342Filters (reactivity)
0 commit comments