Skip to content

Commit 129d928

Browse files
committed
Remove cargs argument from char_by_char and word_by_word methods, add support for multiple callbacks, rename argument callback to callbacks.
1 parent d02b850 commit 129d928

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

tests/test.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@ def main(win):
2727
# title_colors_pair_nb=3,
2828
end_indicator="o")
2929

30-
textbox.confirm_dialog_key = (32, )
31-
textbox.panic_key = (10, )
30+
textbox.confirm_dialog_keys = (32, )
31+
textbox.panic_keys = (10, )
3232

3333
special_words = {
3434
"test": (curses.A_BOLD, curses.A_ITALIC),
3535
"this": (curses.A_BLINK, curses.color_pair(1))
3636
}
3737

38-
def func(text: str):
38+
def func1():
3939
win.addstr(0, 0, str(visualdialog.__version__))
4040

41+
def func2():
42+
win.addstr(0, 3, str(visualdialog.__version__))
43+
4144
for reply in text:
4245
textbox.char_by_char(win,
4346
reply,
44-
cargs=(reply, ),
45-
callback=func,
47+
callbacks=(func1, func2),
4648
text_attr=(curses.A_ITALIC, curses.A_BOLD),
4749
words_attr=special_words)
4850

visualdialog/dialog.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import curses
77
import random
88
import textwrap
9-
from typing import Any, Callable, List, Mapping, Sequence, Tuple, Union
9+
from typing import (Any, Callable, Iterable, List, Mapping, Sequence, Tuple,
10+
Union)
1011

1112
from .box import BaseTextBox
1213
from .type import CursesTextAttribute, CursesTextAttributes, CursesWindow
@@ -99,8 +100,7 @@ def _write_word_char_by_char(self,
99100
word: str,
100101
delay: int,
101102
random_delay: Sequence[int],
102-
callback: Callable,
103-
cargs: Sequence):
103+
callbacks: Iterable[Callable]):
104104
"""Write word char by char at given positon."""
105105
for x, char in enumerate(word):
106106
win.addstr(pos_y,
@@ -117,7 +117,8 @@ def _write_word_char_by_char(self,
117117
curses.napms(delay
118118
+ rand_delay)
119119

120-
callback(*cargs)
120+
for callback in callbacks:
121+
callback()
121122

122123
def _write_word(self,
123124
win: CursesWindow,
@@ -126,8 +127,7 @@ def _write_word(self,
126127
word: str,
127128
delay: int,
128129
random_delay: Sequence[int],
129-
callback: Callable,
130-
cargs: Sequence):
130+
callbacks: Iterable[Callable]):
131131
"""Write word at given position."""
132132
win.addstr(pos_y,
133133
pos_x,
@@ -138,7 +138,8 @@ def _write_word(self,
138138
curses.napms(delay
139139
+ rand_delay)
140140

141-
callback(*cargs)
141+
for callback in callbacks:
142+
callback()
142143

143144
def _one_by_one(self,
144145
write_method: Callable,
@@ -154,11 +155,9 @@ def _one_by_one(self,
154155
flash_screen: bool,
155156
delay: int,
156157
random_delay: Sequence[int],
157-
callback: Callable,
158-
cargs: Sequence):
158+
callbacks: Iterable[Callable]):
159159
# Test if only one argument is passed instead of a tuple.
160-
if isinstance(text_attr, int):
161-
text_attr = (text_attr, )
160+
text_attr = to_tuple(text_attr)
162161

163162
colors_pair = curses.color_pair(colors_pair_nb)
164163

@@ -188,8 +187,7 @@ def _one_by_one(self,
188187
word,
189188
delay,
190189
random_delay,
191-
callback,
192-
cargs)
190+
callbacks)
193191

194192
# Waiting for space character.
195193
curses.napms(delay)
@@ -212,8 +210,7 @@ def char_by_char(self,
212210
flash_screen: bool = False,
213211
delay: int = 40,
214212
random_delay: Sequence[int] = (0, 0),
215-
callback: Callable = lambda: None,
216-
cargs: Sequence = ()):
213+
callbacks: Iterable[Callable] = ()):
217214
"""Write the given text character by character.
218215
219216
:param win: ``curses`` window object on which the method will
@@ -254,12 +251,9 @@ def char_by_char(self,
254251
number generated in ``random_delay`` interval. This defaults
255252
to ``(0, 0)``.
256253
257-
:param callback: Callable called after writing a character and
258-
the delay time has elapsed. This defaults to a lambda which
259-
do nothing.
260-
261-
:param cargs: All the arguments that will be passed to callback.
262-
This defaults to an empty tuple.
254+
:param callback: Iterable of callable called one by one after
255+
writing a character and the delay time has elapsed. This
256+
defaults to an empty tuple.
263257
264258
.. NOTE::
265259
Method flow:
@@ -268,8 +262,10 @@ def char_by_char(self,
268262
- Cutting text into line to stay within the dialog box
269263
frame.
270264
- Writing paragraph by paragraph.
271-
- Writing each line of the current paragraph, character
272-
by character.
265+
- Writing each line of the current paragraph.
266+
- Writing each word of line.
267+
- Writing each character of word and execute callbacks
268+
between.
273269
- Waits until a key contained in the class attribute
274270
``confirm_keys`` was pressed before writing the
275271
following paragraph.
@@ -297,8 +293,7 @@ def char_by_char(self,
297293
flash_screen,
298294
delay,
299295
random_delay,
300-
callback,
301-
cargs)
296+
callbacks)
302297

303298
def word_by_word(self,
304299
win: CursesWindow,
@@ -313,8 +308,7 @@ def word_by_word(self,
313308
flash_screen: bool = False,
314309
delay: int = 150,
315310
random_delay: Sequence[int] = (0, 0),
316-
callback: Callable = lambda: None,
317-
cargs: Sequence = ()):
311+
callbacks: Iterable[Callable] = ()):
318312
"""Write the given text word by word.
319313
320314
:param win: ``curses`` window object on which the method will
@@ -355,12 +349,9 @@ def word_by_word(self,
355349
generated in ``random_delay`` interval. This defaults to
356350
``(0, 0)``.
357351
358-
:param callback: Callable called after writing a word and the
359-
delay time has elapsed. This defaults to a lambda which do
360-
nothing.
361-
362-
:param cargs: All the arguments that will be passed to callback.
363-
This defaults to an empty tuple.
352+
:param callback: Iterable of callable called one by one after
353+
writing a word and the delay time has elapsed. This defaults
354+
to an empty tuple.
364355
365356
.. NOTE::
366357
Method flow:
@@ -369,8 +360,8 @@ def word_by_word(self,
369360
- Cutting text into line to stay within the dialog box
370361
frame.
371362
- Writing paragraph by paragraph.
372-
- Writing each line of the current paragraph, word by
373-
word.
363+
- Writing each line of the current paragraph.
364+
- Writing each word of line and execute callbacks between.
374365
- Waits until a key contained in the class attribute
375366
``confirm_keys`` was pressed before writing the
376367
following paragraph.
@@ -398,5 +389,4 @@ def word_by_word(self,
398389
flash_screen,
399390
delay,
400391
random_delay,
401-
callback,
402-
cargs)
392+
callbacks)

0 commit comments

Comments
 (0)