Skip to content

Commit 6c7504f

Browse files
committed
Rename argument stdscr to win, add CursesWindow, fix bug occured during instantiation of DialogBox, improve documentation.
1 parent 47e6836 commit 6c7504f

File tree

6 files changed

+92
-103
lines changed

6 files changed

+92
-103
lines changed

doc/source/faq.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ You can also pass arguments to this callback via ``cargs`` parameter.
1616

1717
The past callback is executed after downtime delay between character or word display.
1818
You can use this behavior to perform multiple tasks while ``DialogBox`` scrolling.
19+
20+
I am not satisfied with the behavior of DialogBox, how can I change it?
21+
-----------------------------------------------------------------------
22+
23+
You can create your own derived class by inheriting from ``BaseTextBox``.
24+
Additionally, you can override the methods of ``DialogBox``.

doc/source/utils.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ Utils
1616
visualdialog.function(args)
1717

1818

19-
.. autoclass:: visualdialog.utils.TextAttributes
19+
.. automodule:: visualdialog.utils
2020
:members:

doc/source/visualdialog.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ TextBox
1313
.. autoclass:: visualdialog.box.BaseTextBox
1414
:members:
1515
:undoc-members:
16-
:private-members:
1716

1817
DialogBox
1918
---------
2019

2120
.. autoclass:: visualdialog.dialog.DialogBox
22-
:special-members:
2321
:undoc-members:
2422
:members:
25-
:private-members:
26-

visualdialog/box.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
CursesKeyConstants,
3030
CursesTextAttributesConstant,
3131
CursesTextAttributesConstants,
32+
CursesWindow,
3233
Numeric,
3334
TextAttributes)
3435

@@ -38,7 +39,6 @@ class PanicError(Exception):
3839
pressed.
3940
4041
:param key: Key pressed that caused the exception to be thrown.
41-
:type key: CursesKeyConstants
4242
"""
4343
def __init__(self,
4444
key: CursesKeyConstant):
@@ -52,54 +52,45 @@ class BaseTextBox:
5252
"""This class provides attributs and methods to manage a text box.
5353
5454
.. NOTE::
55-
This class provides a general API for text boxes, it does not
56-
need to be instantiated.
55+
This class provides a general API for text boxes, it is not
56+
intended to be instantiated.
5757
5858
:param pos_x: x position of the dialog box in ``curses`` window
5959
object on which methods will have effects.
60-
:type pos_x: int
6160
6261
:param pos_y: y position of the dialog box in ``curses`` window
6362
object on which methods will have effects.
64-
:type pos_y: int
6563
6664
:param length: Length of the dialog box in ``curses`` window object
6765
on which methods will have effects.
68-
:type length: int
6966
7067
:param width: Width of the dialog box in ``curses`` window object on
7168
which methods will have effects.
72-
:type width: int
7369
7470
:param title: String that will be displayed in the upper left corner
7571
of dialog box.
7672
If title is an empty string, the title will not be displayed.
7773
This defaults an empty string.
78-
:type title: str
7974
8075
:param title_colors_pair_nb:
8176
Number of the curses color pair that will be used to color the
8277
title. Zero corresponding to the pair of white color on black
8378
background initialized by ``curses``). This defaults to ``0``.
84-
:type title_colors_pair_nb: int
8579
8680
:param title_text_attr:
8781
Dialog box title text attributes. It should be a single curses
8882
text attribute or a tuple of curses text attribute. This
8983
defaults to ``curses.A_BOLD``.
90-
:type title_text_attr: Union[CursesTextAttributesConstant,CursesTextAttributesConstants]
9184
9285
:param downtime_chars:
9386
List of characters that will trigger a ``downtime_chars_delay``
9487
time second between the writing of each character.
9588
This defaults to ``(",", ".", ":", ";", "!", "?")``.
96-
:type downtime_chars: Union[tuple[str],list[str]]
9789
9890
:param downtime_chars_delay:
9991
Waiting time in seconds after writing a character contained in
10092
``downtime_chars``.
10193
This defaults to ``0.6``.
102-
:type downtime_chars_delay: Numeric
10394
"""
10495

10596
def __init__(
@@ -111,9 +102,9 @@ def __init__(
111102
title: str = "",
112103
title_colors_pair_nb: int = 0,
113104
title_text_attr: Union[CursesTextAttributesConstant,
114-
CursesTextAttributesConstants] = curses.A_BOLD,
105+
CursesTextAttributesConstants] = curses.A_BOLD,
115106
downtime_chars: Union[Tuple[str],
116-
List[str]] = (",", ".", ":", ";", "!", "?"),
107+
List[str]] = (",", ".", ":", ";", "!", "?"),
117108
downtime_chars_delay: Numeric = .6):
118109
self.pos_x, self.pos_y = pos_x, pos_y
119110
self.length, self.width = length, width
@@ -142,16 +133,15 @@ def __init__(
142133
self.downtime_chars_delay = downtime_chars_delay
143134

144135
#: List of accepted key codes to skip dialog. ``curses`` constants are supported. This defaults to an empty tuple.
145-
self.confirm_dialog_key: List[CursesKeyConstants] = []
136+
self.confirm_dialog_key: List[CursesKeyConstant] = []
146137
#: List of accepted key codes to raise PanicError. ``curses`` constants are supported. This defaults to an empty tuple.
147-
self.panic_key: List[CursesKeyConstants] = []
138+
self.panic_key: List[CursesKeyConstant] = []
148139

149140
@property
150141
def position(self) -> Tuple[int]:
151142
"""Returns a tuple contains x;y position of ``TextBox``.
152143
153144
:returns: x;y position of ``TextBox``.
154-
:rtype: tuple[int]
155145
"""
156146
return self.text_pos_x - 2, self.text_pos_y - 3
157147

@@ -160,16 +150,15 @@ def dimensions(self) -> Tuple[int]:
160150
"""Returns a tuple contains dimensions of ``TextBox``.
161151
162152
:returns: Length and width of ``TextBox``.
163-
:rtype: tuple[int]
164153
"""
165154
return self.length, self.width
166155

167-
def framing_box(self, stdscr):
156+
def framing_box(self, win: CursesWindow):
168157
"""Displays dialog box borders and his title.
169158
170159
If attribute ``self.title`` is empty doesn't display the title.
171160
172-
:param stdscr: ``curses`` window object on which the method will
161+
:param win: ``curses`` window object on which the method will
173162
have effect.
174163
"""
175164
title_length = len(self.title) + 4
@@ -179,30 +168,30 @@ def framing_box(self, stdscr):
179168
if self.title:
180169
attr = (self.title_colors, *self.title_text_attr)
181170

182-
curses.textpad.rectangle(stdscr,
171+
curses.textpad.rectangle(win,
183172
self.pos_y,
184173
self.pos_x + 1,
185174
self.pos_y + title_width,
186175
self.pos_x + title_length)
187176

188-
with TextAttributes(stdscr, *attr):
189-
stdscr.addstr(self.pos_y + 1,
190-
self.pos_x + 3,
191-
self.title)
177+
with TextAttributes(win, *attr):
178+
win.addstr(self.pos_y + 1,
179+
self.pos_x + 3,
180+
self.title)
192181

193182
# Displays the borders of the dialog box.
194-
curses.textpad.rectangle(stdscr,
183+
curses.textpad.rectangle(win,
195184
self.pos_y + self.title_offsetting_y,
196185
self.pos_x,
197186
self.pos_y + self.title_offsetting_y + self.width,
198187
self.pos_x + self.length)
199188

200-
def getkey(self, stdscr):
189+
def getkey(self, win: CursesWindow):
201190
"""Blocks execution as long as a key contained in
202191
``self.confirm_dialog_key`` is not detected.
203192
204193
205-
:param stdscr: ``curses`` window object on which the method will
194+
:param win: ``curses`` window object on which the method will
206195
have effect.
207196
:raises PanicError: If a key contained in ``self.panic_key`` is
208197
pressed.
@@ -217,7 +206,7 @@ def getkey(self, stdscr):
217206
for more informations.
218207
"""
219208
while 1:
220-
key = stdscr.getch()
209+
key = win.getch()
221210

222211
if key in self.confirm_dialog_key:
223212
break

0 commit comments

Comments
 (0)