Skip to content

Commit 06f042b

Browse files
committed
Add DialogBoxglobal_win instance attribute. Fix doc and examples accordingly.
1 parent 900b35d commit 06f042b

File tree

9 files changed

+125
-115
lines changed

9 files changed

+125
-115
lines changed

doc/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Getting started
3232
---------------
3333

3434
- **First steps:**
35-
- **Examples:** Many examples are available in the `repository <https://github.com/Tim-ats-d/Visual-dialog/issues>`_.
35+
- **Examples:** Many examples are available in the `repository <https://github.com/Tim-ats-d/Visual-dialog/tree/main/examples>`_.
3636

3737
Getting help
3838
------------

examples/confrontation.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,34 @@
77

88

99
PASS_KEYS = (" ", "\n")
10-
HEIGHT, WIDTH = 37, 7
10+
HEIGHT, WIDTH = 35, 5
1111

1212

1313
# It is preferable to create its own class derived from DialogBox for
1414
# complex applications.
1515
class CustomDialogBox(DialogBox):
1616

1717
def __init__(self,
18+
win,
1819
pos_x: int,
1920
pos_y: int,
2021
title: str,
2122
title_colors_pair_nb: int,
2223
**kwargs):
23-
super().__init__(pos_x=pos_x,
24-
pos_y=pos_y,
25-
height=HEIGHT,
26-
width=WIDTH,
27-
title=title,
28-
title_colors_pair_nb=title_colors_pair_nb,
29-
**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)
3038

3139
# Definition of accepted key codes to pass a dialog.
3240
self.confirm_keys = PASS_KEYS
@@ -48,42 +56,40 @@ def main(win):
4856
center_x = max_x//2 - HEIGHT//2 # Calculation of center alignment.
4957
bottom_y = max_y - WIDTH - 4 # Calculation of bottom alignment.
5058

51-
phoenix_wright = CustomDialogBox(left_x, bottom_y,
59+
phoenix_wright = CustomDialogBox(win,
60+
left_x, bottom_y,
5261
"Phoenix", # Title of dialog box.
5362
1) # Color pair used to colored title.
5463

55-
april_may = CustomDialogBox(center_x, bottom_y,
64+
april_may = CustomDialogBox(win,
65+
center_x, bottom_y,
5666
"April",
5767
2)
5868

59-
miles_edgeworth = CustomDialogBox(right_x, bottom_y,
69+
miles_edgeworth = CustomDialogBox(win,
70+
right_x, bottom_y,
6071
"Edgeworth",
6172
3)
6273

63-
phoenix_wright.char_by_char(win,
64-
"This testimony is a pure invention !",
74+
phoenix_wright.char_by_char("This testimony is a pure invention !",
6575
delay=30)
6676
# Set delay between writting each characters to 30 milliseconds
6777

68-
phoenix_wright.char_by_char(win,
69-
"You're lying April May !",
78+
phoenix_wright.char_by_char("You're lying April May !",
7079
flash_screen=True, # A short luminous glow will be displayed before writing the text.
7180
delay=30,
7281
text_attr=curses.A_BOLD)
7382

74-
april_may.char_by_char(win,
75-
"Arghh !",
83+
april_may.char_by_char("Arghh !",
7684
delay=30,
7785
text_attr=curses.A_ITALIC)
7886

79-
miles_edgeworth.char_by_char(win,
80-
"OBJECTION !",
87+
miles_edgeworth.char_by_char("OBJECTION !",
8188
flash_screen=True,
8289
delay=30,
8390
text_attr=curses.A_BOLD)
8491

85-
miles_edgeworth.char_by_char(win,
86-
"These accusations are irrelevant !",
92+
miles_edgeworth.char_by_char("These accusations are irrelevant !",
8793
delay=30)
8894

8995

examples/context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def main(win):
2121
with DialogBox(1, 1,
2222
30, 6) as db:
2323
db.confirm_keys.append("\n") # To match enter key.
24-
db.char_by_char(win,
25-
reply)
24+
db.char_by_char(reply, win)
2625

2726

2827
# Execution of main function.

examples/monologue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def main(win):
3434
# Iterate on each sentence contained in replys.
3535
for reply in replys:
3636
textbox.char_by_char(
37-
win,
3837
reply,
38+
win,
3939
2, # Display text colored with color pair 2.
4040
delay=40) # Set delay between writting each characters to 40 milliseconds.
4141

examples/panic.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@ def main(win):
1717
box.panic_keys = ("q", )
1818

1919
try:
20-
box.char_by_char(win,
21-
"When a key contained in EXIT_KEY has been "
22-
"pressed at end of dialog PanicError "
23-
"exception is raised.")
20+
box.char_by_char("When a key contained in EXIT_KEY is pressed "
21+
"at end of dialog PanicError exception is raised.",
22+
win)
2423
except PanicError: # Catch PanicError.
25-
box.char_by_char(win,
26-
"PanicError exception has been caught. "
24+
box.char_by_char("PanicError exception has been caught. "
2725
"One of the keys contained in EXIT_CHAR has "
28-
"been pressed.")
26+
"been pressed.",
27+
win)
2928
else:
30-
box.char_by_char(win,
31-
"None of the keys contained in EXIT_CHAR have "
32-
"been pressed.")
29+
box.char_by_char("None of the keys contained in EXIT_CHAR have "
30+
"been pressed.",
31+
win)
3332
finally: # Code executed in all cases.
34-
box.char_by_char(win,
35-
"End of dialog.")
33+
box.char_by_char("End of dialog.", win)
3634

3735

3836
# Execution of main function.

examples/text_attributes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def main(win):
4141
textbox.confirm_keys = PASS_KEYS
4242

4343
for text, attributes in sentences.items():
44-
textbox.char_by_char(win,
45-
text,
44+
textbox.char_by_char(text,
45+
win,
4646
2, # Display text colored with color pair 2.
4747
attributes) # Pass attributes to text.
4848

examples/word.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def main(win):
2727

2828
# Iterate on each sentence contained in instructions.
2929
for instruction in instructions:
30-
textbox.word_by_word(win,
31-
instruction,
30+
textbox.word_by_word(instruction,
31+
win,
3232
delay=200)
3333
# Set delay between writting each words to 200 milliseconds.
3434

visualdialog/box.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import curses
77
import curses.textpad
88
import functools
9-
from typing import Callable, List, Literal, Optional, Sequence, Tuple, Union
9+
from typing import Callable, List, Literal, Sequence, Tuple, Union
1010

1111
from .error import PanicError, ValueNotInBound
1212
from .type import CursesKey, CursesTextAttribute, CursesTextAttributes, CursesWindow
@@ -102,7 +102,7 @@ def __init__(
102102
pos_y: int,
103103
height: int,
104104
width: int,
105-
title: Optional[str] = None,
105+
title: str = "",
106106
title_colors_pair_nb: int = 0,
107107
title_text_attr: Union[CursesTextAttribute,
108108
CursesTextAttributes] = curses.A_BOLD,

0 commit comments

Comments
 (0)