@@ -27,24 +27,53 @@ def _get_environ_bool(name: str) -> bool:
27
27
return has_environ
28
28
29
29
30
- def _get_environ_int (name : str , default : int ) -> int :
30
+ def _get_environ_int (name : str , default : int , minimum : int | None = None ) -> int :
31
31
"""Retrieves an integer environment variable.
32
32
33
33
Args:
34
34
name: Name of environment variable.
35
35
default: The value to use if the value is not set, or set to something other
36
36
than a valid integer.
37
+ minimum: Optional minimum value.
37
38
38
39
Returns:
39
40
The integer associated with the environment variable if it's set to a valid int
40
41
or the default value otherwise.
41
42
"""
42
43
try :
43
- return int (os .environ [name ])
44
+ value = int (os .environ [name ])
44
45
except KeyError :
45
46
return default
46
47
except ValueError :
47
48
return default
49
+ if minimum is not None :
50
+ return max (minimum , value )
51
+ return value
52
+
53
+
54
+ def _get_environ_port (name : str , default : int ) -> int :
55
+ """Get a port no. from an environment variable.
56
+
57
+ Note that there is no 'minimum' here, as ports are more like names than a scalar value.
58
+
59
+ Args:
60
+ name: Name of environment variable.
61
+ default: The value to use if the value is not set, or set to something other
62
+ than a valid port.
63
+
64
+ Returns:
65
+ An integer port number.
66
+
67
+ """
68
+ try :
69
+ value = int (os .environ [name ])
70
+ except KeyError :
71
+ return default
72
+ except ValueError :
73
+ return default
74
+ if value < 0 or value > 65535 :
75
+ return default
76
+ return value
48
77
49
78
50
79
def _is_valid_animation_level (value : str ) -> TypeGuard [AnimationLevel ]:
@@ -89,11 +118,11 @@ def _get_textual_animations() -> AnimationLevel:
89
118
DEVTOOLS_HOST : Final [str ] = get_environ ("TEXTUAL_DEVTOOLS_HOST" , "127.0.0.1" )
90
119
"""The host where textual console is running."""
91
120
92
- DEVTOOLS_PORT : Final [int ] = _get_environ_int ("TEXTUAL_DEVTOOLS_PORT" , 8081 )
121
+ DEVTOOLS_PORT : Final [int ] = _get_environ_port ("TEXTUAL_DEVTOOLS_PORT" , 8081 )
93
122
"""Constant with the port that the devtools will connect to."""
94
123
95
- SCREENSHOT_DELAY : Final [int ] = _get_environ_int ("TEXTUAL_SCREENSHOT" , - 1 )
96
- """Seconds delay before taking screenshot."""
124
+ SCREENSHOT_DELAY : Final [int ] = _get_environ_int ("TEXTUAL_SCREENSHOT" , - 1 , minimum = - 1 )
125
+ """Seconds delay before taking screenshot, -1 for no screenshot ."""
97
126
98
127
SCREENSHOT_LOCATION : Final [str | None ] = get_environ ("TEXTUAL_SCREENSHOT_LOCATION" )
99
128
"""The location where screenshots should be written."""
@@ -107,7 +136,7 @@ def _get_textual_animations() -> AnimationLevel:
107
136
SHOW_RETURN : Final [bool ] = _get_environ_bool ("TEXTUAL_SHOW_RETURN" )
108
137
"""Write the return value on exit."""
109
138
110
- MAX_FPS : Final [int ] = _get_environ_int ("TEXTUAL_FPS" , 60 )
139
+ MAX_FPS : Final [int ] = _get_environ_int ("TEXTUAL_FPS" , 60 , minimum = 1 )
111
140
"""Maximum frames per second for updates."""
112
141
113
142
COLOR_SYSTEM : Final [str | None ] = get_environ ("TEXTUAL_COLOR_SYSTEM" , "auto" )
@@ -116,10 +145,10 @@ def _get_textual_animations() -> AnimationLevel:
116
145
TEXTUAL_ANIMATIONS : Final [AnimationLevel ] = _get_textual_animations ()
117
146
"""Determines whether animations run or not."""
118
147
119
- ESCAPE_DELAY : Final [float ] = _get_environ_int ("ESCDELAY" , 100 ) / 1000.0
148
+ ESCAPE_DELAY : Final [float ] = _get_environ_int ("ESCDELAY" , 100 , minimum = 1 ) / 1000.0
120
149
"""The delay (in seconds) before reporting an escape key (not used if the extend key protocol is available)."""
121
150
122
- SLOW_THRESHOLD : int = _get_environ_int ("TEXTUAL_SLOW_THRESHOLD" , 500 )
151
+ SLOW_THRESHOLD : int = _get_environ_int ("TEXTUAL_SLOW_THRESHOLD" , 500 , minimum = 100 )
123
152
"""The time threshold (in milliseconds) after which a warning is logged
124
153
if message processing exceeds this duration.
125
154
"""
0 commit comments