66import curses
77import curses .textpad
88import functools
9- from typing import Callable , List , Literal , Sequence , Tuple , Union
9+ from typing import (Any , Callable , List , Literal , NoReturn , Sequence , Tuple ,
10+ Union )
1011
1112from .error import PanicError , ValueNotInBound
12- from .type import CursesKey , CursesTextAttribute , CursesTextAttributes , CursesWindow
13+ from .type import (CursesKey , CursesTextAttribute , CursesTextAttributes ,
14+ CursesWindow )
1315from .utils import TextAttr , to_tuple
1416
1517
16- def value_checker ( initializer : Callable ) -> Callable :
17- """A decorator which ensures that correct values are passed to
18- :class:` BaseTextBox` initializer to avoid unexpected behavior.
18+ class BoundHeight :
19+ """A descriptor which ensures that correct value is setted to
20+ `` BaseTextBox.height`` to avoid unexpected behavior.
1921 """
20- def __init__ (self ,
21- pos_x , pos_y ,
22- height , width ,
23- title ,
24- * args , ** kwargs ):
25- minimum_box_width = 4
22+ def __get__ (self , obj : "BaseTextBox" , objtype = None ):
23+ return obj ._height
2624
25+ def __set__ (self , obj : "BaseTextBox" , value : int ) -> NoReturn :
2726 title_box_borders_total_height = 5
28- minimum_box_height = len (title ) + title_box_borders_total_height
27+ minimum_box_height = len (obj .title ) + title_box_borders_total_height
28+
29+ if value < minimum_box_height :
30+ raise ValueNotInBound ("height must be more than title length + 5" )
31+ else :
32+ obj ._height = value
33+
2934
30- if width < minimum_box_width :
31- raise ValueNotInBound (f"width must be less than { minimum_box_width } " )
32- elif height < minimum_box_height :
33- raise ValueNotInBound ("height must be less than len(title) + 5" )
35+ class BoundWidth :
36+ """A descriptor which ensures that correct value is setted to
37+ ``BaseTextBox.width`` to avoid unexpected behavior.
38+ """
39+ def __get__ (self , obj : "BaseTextBox" , objtype = None ):
40+ return obj ._width
3441
35- initializer (self ,
36- pos_x , pos_y ,
37- height , width ,
38- title ,
39- * args , ** kwargs )
42+ def __set__ (self , obj : "BaseTextBox" , value : int ) -> NoReturn :
43+ minimum_box_width = 4
4044
41- return __init__
45+ if value < minimum_box_width :
46+ raise ValueNotInBound (f"width must be more than { minimum_box_width } " )
47+ else :
48+ obj ._width = value
4249
4350
4451class BaseTextBox :
@@ -95,7 +102,8 @@ class BaseTextBox:
95102 :ivar panic_keys: initial value: []:
96103 List of accepted key to raise :exc:`PanicError`.
97104 """
98- @value_checker
105+ height , width = BoundHeight (), BoundWidth ()
106+
99107 def __init__ (
100108 self ,
101109 pos_x : int ,
@@ -109,9 +117,6 @@ def __init__(
109117 downtime_chars : Sequence [str ] = ("," , "." , ":" , ";" , "!" , "?" ),
110118 downtime_chars_delay : int = 600 ):
111119 """Initializes instance of :class:`BaseTextBox`."""
112- self .pos_x , self .pos_y = pos_x , pos_y
113- self .height , self .width = height - 1 , width - 1
114-
115120 self .title_offsetting_y = 2 if title else 0
116121
117122 # Compensation for left and upper borders of text box.
@@ -127,6 +132,9 @@ def __init__(
127132 self .title_colors = curses .color_pair (title_colors_pair_nb )
128133 self .title_text_attr = to_tuple (title_text_attr )
129134
135+ self .pos_x , self .pos_y = pos_x , pos_y
136+ self .height , self .width = height - 1 , width - 1
137+
130138 self .downtime_chars = downtime_chars
131139 self .downtime_chars_delay = downtime_chars_delay
132140
0 commit comments