Skip to content

Commit 21a4738

Browse files
author
Tim-ats-d
committed
Added the possibility to pass a callback to char_by_char and word_by_word methods. Added the possibility to pass arguments to callbacks passed to char_by_char and word_by_word methods (via the parameter cargs which accept an infinite number of arguments). DialogBox class is now context manager. Removal of character encoding indications in all file headers (utf-8 by default in python3). Update version numbers in __init__.py and setup.py. Add an example in doc/examples (context.py).
1 parent 22e74d9 commit 21a4738

File tree

5 files changed

+91
-15
lines changed

5 files changed

+91
-15
lines changed

doc/examples/context.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# context.py
3+
4+
import curses
5+
6+
from visualdialog import DialogBox
7+
8+
9+
def main(stdscr):
10+
monologue = (
11+
"I will be the narrator of this story.",
12+
"I will guide you throughout your adventure."
13+
)
14+
15+
# Makes the cursor invisible.
16+
curses.curs_set(0)
17+
18+
# Definition of several colors pairs.
19+
curses.start_color()
20+
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
21+
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
22+
23+
with DialogBox(
24+
5, 5, # Position 5;5 in terminal.
25+
40, 6, # Length and width (in character).
26+
title="Narrator", title_colors_pair_nb=1) as narrator: # We catch the dialog box with as.
27+
28+
# Definition of accepted key codes to pass a dialog.
29+
# See documentation of the curses constants for more informations.
30+
narrator.confirm_dialog_key = (10, 32) # Key Enter and Space.
31+
32+
# We iterate on each sentence contained in monologue.
33+
for text in monologue:
34+
narrator.char_by_char(stdscr,
35+
text,
36+
2) # Display of the reply variable colored with color pair 2.
37+
38+
narrator.getkey(stdscr) # Waiting for a key press.
39+
stdscr.clear() # Clear the screen.
40+
41+
42+
# Execution of the function.
43+
curses.wrapper(main)

doc/examples/monologue.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# monologue_example.py
2+
# monologue.py
33

44
import curses
55

@@ -25,14 +25,14 @@ def main(stdscr):
2525

2626
textbox = DialogBox(
2727
20, 15, # Position 20;15 in terminal.
28-
40, 6, # Length and width (in character).
28+
40, 6, # Length and width (in character).
2929
title="Dogm", title_colors_pair_nb=3) # Title and color_pair used to colored title.
3030

3131
# Definition of accepted key codes to pass a dialog.
3232
# See documentation of the curses constants for more informations.
3333
textbox.confirm_dialog_key = (10, 32) # Key Enter and Space.
3434

35-
# Display each sentence contains in text.
35+
# We iterate on each sentence contained in replys.
3636
for reply in replys:
3737
textbox.char_by_char(stdscr,
3838
reply,

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32

43
from setuptools import setup, find_packages
54

65

76
setup(
87
name="visualdialog",
9-
version=0.2,
8+
version=0.4,
109
packages=find_packages(),
1110
author="Arnouts Timéo",
1211
author_email="[email protected]",

visualdialog/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32

43
"""
54
A librairie which provides class and functions to make easier dialog box in terminal.
65
"""
76

8-
__version__ = 0.3
7+
__version__ = 0.4
98

109
from .core import DialogBox

visualdialog/core.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import textwrap
3333
import time
3434

35-
from typing import Tuple
35+
from typing import Any, Callable, Tuple
3636

3737

3838
class DialogBox :
@@ -51,8 +51,8 @@ def __init__(self,
5151
self.pos_x, self.pos_y = pos_x, pos_y
5252
self.box_length, self.box_width = box_length, box_width
5353

54-
self.text_pos_x = pos_x + 2
55-
self.text_pos_y = pos_y + 3
54+
self.text_pos_x = pos_x + 2 # Compensation for the left border of the dialog box.
55+
self.text_pos_y = pos_y + 3 # Compensation for the upper border of the dialog box.
5656

5757
self.nb_char_max_line = box_length - 2
5858
self.nb_lines_max = box_width - 2
@@ -69,6 +69,12 @@ def __init__(self,
6969

7070
self.end_dialog_indicator_char = end_dialog_indicator[:1] # We keep only first character.
7171

72+
def __enter__(self):
73+
return self
74+
75+
def __exit__(self, type, value, traceback):
76+
...
77+
7278
def framing_box(self, stdscr):
7379
"""Displays dialog box and his title.
7480
@@ -105,7 +111,7 @@ def getkey(self, stdscr):
105111

106112
def _display_end_dialog_indicator(self, stdscr,
107113
colors_pair_nb: int = None, blink: bool = True):
108-
"""Displays an end of dialog indicator in the lower right corner."""
114+
"""Displays an end of dialog indicator in the lower right corner of textbox."""
109115
if self.end_dialog_indicator_char:
110116
blink = curses.A_BLINK if blink else curses.A_NORMAL
111117

@@ -115,7 +121,9 @@ def _display_end_dialog_indicator(self, stdscr,
115121
def char_by_char(self, stdscr,
116122
text: str, colors_pair_nb: int,
117123
flash_screen: bool = False,
118-
delay: int = .05, random_delay: Tuple[int, int] = (0, 0)):
124+
delay: int = .05, random_delay: Tuple[int, int] = (0, 0),
125+
callback: Callable = None,
126+
cargs=()):
119127
"""Writes the given text character by character at position self.pos_x;self.pos_y.
120128
121129
The colors_pair_nb corresponds to the number of the curses color pair with which the text
@@ -146,13 +154,18 @@ def char_by_char(self, stdscr,
146154
else:
147155
time.sleep(delay + random.uniform(*random_delay))
148156

157+
if callback:
158+
callback(*cargs)
159+
149160
self._display_end_dialog_indicator(stdscr)
150161

151162
def word_by_word(self, stdscr,
152163
text: str, colors_pair_nb: int,
153164
cut_char: str = " ",
154165
flash_screen: bool = False,
155-
delay: int = .05, random_delay: Tuple[int, int] = (0, 0)):
166+
delay: int = .05, random_delay: Tuple[int, int] = (0, 0),
167+
callback: Callable = None,
168+
cargs=()):
156169
"""Writes the given text word by word at position at position self.pos_x;self.pos_y.
157170
158171
The colors_pair_nb corresponds to the number of the curses color pair with which the text
@@ -184,6 +197,9 @@ def word_by_word(self, stdscr,
184197
offsetting_x += len(word) + 1 # Compensates for the space between words.
185198
time.sleep(delay + random.uniform(*random_delay))
186199

200+
if callback:
201+
callback(*cargs)
202+
187203
self._display_end_dialog_indicator(stdscr)
188204

189205

@@ -210,13 +226,32 @@ def main(stdscr):
210226

211227
textbox.confirm_dialog_key = (10, 32)
212228

229+
def func(reply: str):
230+
stdscr.addstr(0, 0, reply)
231+
213232
for reply in text:
214233
textbox.framing_box(stdscr)
215-
textbox.char_by_char(stdscr, reply, 2)
234+
textbox.char_by_char(stdscr, reply, 2, cargs=(reply,), callback=func)
216235

217236
textbox.getkey(stdscr)
218237
stdscr.clear()
219238

239+
def main2(stdscr):
240+
curses.curs_set(0)
241+
242+
curses.start_color()
243+
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
244+
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
245+
curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
246+
247+
with DialogBox(
248+
20, 15,
249+
40, 6,
250+
title="Test", title_colors_pair_nb=3,
251+
end_dialog_indicator="t") as e:
252+
253+
e.framing_box(stdscr)
254+
e.char_by_char(stdscr, "c'est le grand test", 2)
220255

221256
if __name__ == "__main__":
222-
curses.wrapper(main)
257+
curses.wrapper(main2)

0 commit comments

Comments
 (0)