|
5 | 5 |
|
6 | 6 | import curses |
7 | 7 | import curses.textpad |
8 | | -from typing import List, Literal, Sequence, Tuple, Union |
| 8 | +from typing import Callable, List, Literal, Sequence, Tuple, Union |
9 | 9 |
|
10 | 10 | from .error import PanicError, ValueNotInBound |
11 | 11 | from .type import CursesKey, CursesTextAttribute, CursesTextAttributes, CursesWindow |
12 | 12 | from .utils import TextAttr, to_tuple |
13 | 13 |
|
14 | 14 |
|
15 | | -MINIMUM_BOX_WIDTH = 4 |
16 | | - |
17 | | - |
18 | 15 | def value_checker(initializer: Callable) -> Callable: |
19 | | - """A decorator which checks if the arguments passed to |
20 | | - ``BaseTextBox`` initializer are consistent. |
| 16 | + """A decorator which ensures that correct values are passed to |
| 17 | + ``BaseTextBox`` initializer to avoid unexpected behavior. |
21 | 18 | """ |
22 | 19 | def __init__(self, |
23 | 20 | pos_x, pos_y, |
24 | 21 | height, width, |
25 | 22 | title, |
26 | 23 | *args, **kwargs): |
27 | | - minimum_box_height = len(title) + 5 |
| 24 | + minimum_box_width = 4 |
| 25 | + |
| 26 | + title_box_borders_total_height = 5 |
| 27 | + minimum_box_height = len(title) + title_box_borders_total_height |
28 | 28 |
|
29 | | - if width < MINIMUM_BOX_WIDTH: |
30 | | - raise ValueNotInBound("ne peut pas faire moins de 4 de hauteur") |
31 | | - elif minimum_box_height > height: |
32 | | - raise ValueNotInBound("ne peut pas faire plus de len(title) + 5") |
| 29 | + if width < minimum_box_width: |
| 30 | + raise ValueNotInBound(f"width must be less than {minimum_box_width}") |
| 31 | + elif height < minimum_box_height: |
| 32 | + raise ValueNotInBound("height must be less than len(title) + 5") |
33 | 33 |
|
34 | 34 | initializer(self, |
35 | 35 | pos_x, pos_y, |
@@ -102,18 +102,17 @@ def __init__( |
102 | 102 |
|
103 | 103 | self.title_offsetting_y = 2 if title else 0 |
104 | 104 |
|
105 | | - # Compensation for the left border of the dialog box. |
| 105 | + # Compensation for left and upper borders of text box. |
106 | 106 | self.text_pos_x = pos_x + 2 |
107 | | - # Compensation for the upper border of the dialog box. |
108 | 107 | self.text_pos_y = pos_y + self.title_offsetting_y + 1 |
109 | 108 |
|
| 109 | + # Text margins. |
110 | 110 | self.nb_char_max_line = height - 5 |
111 | 111 | self.nb_lines_max = width - 3 |
112 | 112 |
|
113 | 113 | self.title = title |
114 | 114 | if title: |
115 | 115 | self.title_colors = curses.color_pair(title_colors_pair_nb) |
116 | | - # Test if only one argument is passed instead of a sequence. |
117 | 116 | self.title_text_attr = to_tuple(title_text_attr) |
118 | 117 |
|
119 | 118 | self.downtime_chars = downtime_chars |
@@ -154,30 +153,31 @@ def dimensions(self) -> Tuple[int]: |
154 | 153 | def framing_box(self, win: CursesWindow): |
155 | 154 | """Display dialog box borders and his title. |
156 | 155 |
|
157 | | - If attribute ``self.title`` is empty doesn't display the title. |
| 156 | + If attribute ``self.title`` is empty doesn't display the title |
| 157 | + box. |
158 | 158 |
|
159 | 159 | :param win: ``curses`` window object on which the method will |
160 | 160 | have effect. |
161 | 161 | """ |
162 | | - title_length = len(self.title) + 4 |
| 162 | + title_height = len(self.title) + 4 |
163 | 163 | title_width = 2 |
164 | 164 |
|
165 | | - # Displays the title and the title box. |
| 165 | + # Display title and title box. |
166 | 166 | if self.title: |
167 | 167 | attr = (self.title_colors, *self.title_text_attr) |
168 | 168 |
|
169 | 169 | curses.textpad.rectangle(win, |
170 | 170 | self.pos_y, |
171 | 171 | self.pos_x + 1, |
172 | 172 | self.pos_y + title_width, |
173 | | - self.pos_x + title_length) |
| 173 | + self.pos_x + title_height) |
174 | 174 |
|
175 | 175 | with TextAttr(win, *attr): |
176 | 176 | win.addstr(self.pos_y + 1, |
177 | 177 | self.pos_x + 3, |
178 | 178 | self.title) |
179 | 179 |
|
180 | | - # Displays the borders of the dialog box. |
| 180 | + # Display borders of text box. |
181 | 181 | curses.textpad.rectangle(win, |
182 | 182 | self.pos_y + self.title_offsetting_y, |
183 | 183 | self.pos_x, |
|
0 commit comments