Skip to content

Commit 3dfc73b

Browse files
committed
Refactor examples/confrontation.py, add operator < support for DialogBox.
1 parent 06f042b commit 3dfc73b

File tree

5 files changed

+72
-58
lines changed

5 files changed

+72
-58
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pip install sphinx sphinx_rtd_theme
6969

7070
### Hello world with **Visual-dialog**
7171

72-
```python3
72+
```py3
7373
import curses
7474

7575
from visualdialog import DialogBox
@@ -84,8 +84,8 @@ def main(win):
8484
textbox = DialogBox(x, y,
8585
height, width,
8686
title="Demo")
87-
textbox.char_by_char(win,
88-
"Hello world")
87+
textbox.char_by_char("Hello world",
88+
win)
8989

9090

9191
curses.wrapper(main)

doc/source/visualdialog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ DialogBox
3636

3737
.. automethod:: __exit__
3838

39+
.. automethod:: __lt__
40+
3941
.. automethod:: char_by_char
4042

4143
.. automethod:: word_by_word

examples/confrontation.py

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,40 @@
22
# A concrete example exploiting the possibilities of Visual-dialog.
33

44
import curses
5+
from functools import partial
56

67
from visualdialog import DialogBox
78

89

9-
PASS_KEYS = (" ", "\n")
10-
HEIGHT, WIDTH = 35, 5
10+
pass_keys = (" ", "\n")
11+
height, width = 35, 5
1112

1213

1314
# It is preferable to create its own class derived from DialogBox for
14-
# complex applications.
15-
class CustomDialogBox(DialogBox):
16-
17-
def __init__(self,
18-
win,
19-
pos_x: int,
20-
pos_y: int,
21-
title: str,
22-
title_colors_pair_nb: int,
23-
**kwargs):
24-
DialogBox.__init__(self,
25-
pos_x,
26-
pos_y,
27-
HEIGHT,
28-
WIDTH,
29-
title,
30-
title_colors_pair_nb,
31-
global_win=win,
32-
# Use a default window to display text.
33-
# Setting this parameter allows to avoid passing
34-
# `win` parameter to `char_by_char` and
35-
# `word_by_word` methods. Useful when dealing with
36-
# many `DialogBox` methods calls.
37-
**kwargs)
38-
39-
# Definition of accepted key codes to pass a dialog.
40-
self.confirm_keys = PASS_KEYS
15+
# complex applications (or an instance factory like here).
16+
def box_factory(win,
17+
x: int,
18+
y: int,
19+
title: int,
20+
title_colors_pair_nb: int,
21+
**kwargs) -> DialogBox:
22+
box = DialogBox(x, y,
23+
height, width,
24+
title, title_colors_pair_nb,
25+
global_win=win,
26+
# Use a default window to display text.
27+
# Setting this parameter allows to avoid passing `win`
28+
# parameter to `char_by_char` and `word_by_word` methods.
29+
# Useful when dealing with many `DialogBox` methods calls.
30+
**kwargs)
31+
32+
# Definition of accepted key codes to pass a dialog.
33+
box.confirm_keys = pass_keys
34+
# Definition of a partial objet to reduce verbosity and accelerate
35+
# the time it takes to write the text on the screen.
36+
box.char_by_char = partial(box.char_by_char, delay=30)
37+
38+
return box
4139

4240

4341
def main(win):
@@ -52,24 +50,24 @@ def main(win):
5250
max_y, max_x = win.getmaxyx() # Get height and width of the window.
5351

5452
left_x = 2 # Left alignment.
55-
right_x = max_x - HEIGHT - 4 # Calculation of right alignment.
56-
center_x = max_x//2 - HEIGHT//2 # Calculation of center alignment.
57-
bottom_y = max_y - WIDTH - 4 # Calculation of bottom alignment.
58-
59-
phoenix_wright = CustomDialogBox(win,
60-
left_x, bottom_y,
61-
"Phoenix", # Title of dialog box.
62-
1) # Color pair used to colored title.
63-
64-
april_may = CustomDialogBox(win,
65-
center_x, bottom_y,
66-
"April",
67-
2)
68-
69-
miles_edgeworth = CustomDialogBox(win,
70-
right_x, bottom_y,
71-
"Edgeworth",
72-
3)
53+
right_x = max_x - height - 4 # Calculation of right alignment.
54+
center_x = max_x//2 - height//2 # Calculation of center alignment.
55+
bottom_y = max_y - width - 4 # Calculation of bottom alignment.
56+
57+
phoenix_wright = box_factory(win,
58+
left_x, bottom_y,
59+
"Phoenix", # Title of dialog box.
60+
1) # Color pair used to colored title.
61+
62+
april_may = box_factory(win,
63+
center_x, bottom_y,
64+
"April",
65+
2)
66+
67+
miles_edgeworth = box_factory(win,
68+
right_x, bottom_y,
69+
"Edgeworth",
70+
3)
7371

7472
phoenix_wright.char_by_char("This testimony is a pure invention !",
7573
delay=30)

examples/panic.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,25 @@ def main(win):
1010
curses.curs_set(False)
1111

1212
box = DialogBox(1, 1,
13-
40, 6)
13+
40, 6
14+
global_win=win)
1415

1516
# Definition of keys to pass and exit a dialog.
1617
box.confirm_keys.append("\n")
1718
box.panic_keys = ("q", )
1819

1920
try:
2021
box.char_by_char("When a key contained in EXIT_KEY is pressed "
21-
"at end of dialog PanicError exception is raised.",
22-
win)
22+
"at end of dialog PanicError exception is raised.")
2323
except PanicError: # Catch PanicError.
2424
box.char_by_char("PanicError exception has been caught. "
2525
"One of the keys contained in EXIT_CHAR has "
26-
"been pressed.",
27-
win)
26+
"been pressed.")
2827
else:
2928
box.char_by_char("None of the keys contained in EXIT_CHAR have "
30-
"been pressed.",
31-
win)
29+
"been pressed.")
3230
finally: # Code executed in all cases.
33-
box.char_by_char("End of dialog.", win)
31+
box.char_by_char("End of dialog.")
3432

3533

3634
# Execution of main function.

visualdialog/dialog.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ def __exit__(self, type, value, traceback):
8383
"""Return None."""
8484
return None
8585

86+
def __lt__(self, text: str) -> str:
87+
"""A shortcut to call :meth:`char_by_char` by passing only
88+
`text` argument.
89+
90+
>>> DialogBox(x, y,
91+
... height, width,
92+
... global_win=win) < "Foo bar"
93+
94+
This code roughly equivalent to:
95+
96+
>>> DialogBox(x, y,
97+
... height, width).char_by_char("Foo bar", win)
98+
99+
"""
100+
return self.char_by_char(text)
101+
86102
def char_by_char(self,
87103
text: str,
88104
win: CursesWindow = None,

0 commit comments

Comments
 (0)