@@ -27,24 +27,53 @@ def _get_environ_bool(name: str) -> bool:
2727 return has_environ
2828
2929
30- def _get_environ_int (name : str , default : int ) -> int :
30+ def _get_environ_int (name : str , default : int , minimum : int | None = None ) -> int :
3131 """Retrieves an integer environment variable.
3232
3333 Args:
3434 name: Name of environment variable.
3535 default: The value to use if the value is not set, or set to something other
3636 than a valid integer.
37+ minimum: Optional minimum value.
3738
3839 Returns:
3940 The integer associated with the environment variable if it's set to a valid int
4041 or the default value otherwise.
4142 """
4243 try :
43- return int (os .environ [name ])
44+ value = int (os .environ [name ])
4445 except KeyError :
4546 return default
4647 except ValueError :
4748 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
4877
4978
5079def _is_valid_animation_level (value : str ) -> TypeGuard [AnimationLevel ]:
@@ -89,11 +118,11 @@ def _get_textual_animations() -> AnimationLevel:
89118DEVTOOLS_HOST : Final [str ] = get_environ ("TEXTUAL_DEVTOOLS_HOST" , "127.0.0.1" )
90119"""The host where textual console is running."""
91120
92- DEVTOOLS_PORT : Final [int ] = _get_environ_int ("TEXTUAL_DEVTOOLS_PORT" , 8081 )
121+ DEVTOOLS_PORT : Final [int ] = _get_environ_port ("TEXTUAL_DEVTOOLS_PORT" , 8081 )
93122"""Constant with the port that the devtools will connect to."""
94123
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 ."""
97126
98127SCREENSHOT_LOCATION : Final [str | None ] = get_environ ("TEXTUAL_SCREENSHOT_LOCATION" )
99128"""The location where screenshots should be written."""
@@ -107,7 +136,7 @@ def _get_textual_animations() -> AnimationLevel:
107136SHOW_RETURN : Final [bool ] = _get_environ_bool ("TEXTUAL_SHOW_RETURN" )
108137"""Write the return value on exit."""
109138
110- MAX_FPS : Final [int ] = _get_environ_int ("TEXTUAL_FPS" , 60 )
139+ MAX_FPS : Final [int ] = _get_environ_int ("TEXTUAL_FPS" , 60 , minimum = 1 )
111140"""Maximum frames per second for updates."""
112141
113142COLOR_SYSTEM : Final [str | None ] = get_environ ("TEXTUAL_COLOR_SYSTEM" , "auto" )
@@ -116,10 +145,10 @@ def _get_textual_animations() -> AnimationLevel:
116145TEXTUAL_ANIMATIONS : Final [AnimationLevel ] = _get_textual_animations ()
117146"""Determines whether animations run or not."""
118147
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
120149"""The delay (in seconds) before reporting an escape key (not used if the extend key protocol is available)."""
121150
122- SLOW_THRESHOLD : int = _get_environ_int ("TEXTUAL_SLOW_THRESHOLD" , 500 )
151+ SLOW_THRESHOLD : int = _get_environ_int ("TEXTUAL_SLOW_THRESHOLD" , 500 , minimum = 100 )
123152"""The time threshold (in milliseconds) after which a warning is logged
124153if message processing exceeds this duration.
125154"""
0 commit comments