@@ -162,13 +162,13 @@ class DefaultFdMixin(ProgressBarMixinBase):
162162 #: Set the terminal to be ANSI compatible. If a terminal is ANSI
163163 #: compatible we will automatically enable `colors` and disable
164164 #: `line_breaks`.
165- is_ansi_terminal : bool = False
165+ is_ansi_terminal : bool | None = False
166166 #: Whether the file descriptor is a terminal or not. This is used to
167167 #: determine whether to use ANSI escape codes or not.
168- is_terminal : bool
168+ is_terminal : bool | None
169169 #: Whether to print line breaks. This is useful for logging the
170170 #: progressbar. When disabled the current line is overwritten.
171- line_breaks : bool = True
171+ line_breaks : bool | None = True
172172 #: Specify the type and number of colors to support. Defaults to auto
173173 #: detection based on the file descriptor type (i.e. interactive terminal)
174174 #: environment variables such as `COLORTERM` and `TERM`. Color output can
@@ -179,9 +179,7 @@ class DefaultFdMixin(ProgressBarMixinBase):
179179 #: For true (24 bit/16M) color support you can use `COLORTERM=truecolor`.
180180 #: For 256 color support you can use `TERM=xterm-256color`.
181181 #: For 16 colorsupport you can use `TERM=xterm`.
182- enable_colors : progressbar .env .ColorSupport | bool | None = (
183- progressbar .env .COLOR_SUPPORT
184- )
182+ enable_colors : progressbar .env .ColorSupport = progressbar .env .COLOR_SUPPORT
185183
186184 def __init__ (
187185 self ,
@@ -200,7 +198,7 @@ def __init__(
200198 fd = self ._apply_line_offset (fd , line_offset )
201199 self .fd = fd
202200 self .is_ansi_terminal = progressbar .env .is_ansi_terminal (fd )
203- self .is_terminal = self . _determine_is_terminal (fd , is_terminal )
201+ self .is_terminal = progressbar . env . is_terminal (fd , is_terminal )
204202 self .line_breaks = self ._determine_line_breaks (line_breaks )
205203 self .enable_colors = self ._determine_enable_colors (enable_colors )
206204
@@ -219,29 +217,47 @@ def _apply_line_offset(
219217 else :
220218 return fd
221219
222- def _determine_is_terminal (
223- self ,
224- fd : base .TextIO ,
225- is_terminal : bool | None ,
226- ) -> bool :
227- if is_terminal is not None :
228- return progressbar .env .is_terminal (fd , is_terminal )
229- else :
230- return progressbar .env .is_ansi_terminal (fd )
231-
232- def _determine_line_breaks (self , line_breaks : bool | None ) -> bool :
220+ def _determine_line_breaks (self , line_breaks : bool | None ) -> bool | None :
233221 if line_breaks is None :
234222 return progressbar .env .env_flag (
235223 'PROGRESSBAR_LINE_BREAKS' ,
236224 not self .is_terminal ,
237225 )
238226 else :
239- return bool ( line_breaks )
227+ return line_breaks
240228
241229 def _determine_enable_colors (
242230 self ,
243231 enable_colors : progressbar .env .ColorSupport | None ,
244232 ) -> progressbar .env .ColorSupport :
233+ '''
234+ Determines the color support for the progress bar.
235+
236+ This method checks the `enable_colors` parameter and the environment
237+ variables `PROGRESSBAR_ENABLE_COLORS` and `FORCE_COLOR` to determine
238+ the color support.
239+
240+ If `enable_colors` is:
241+ - `None`, it checks the environment variables and the terminal
242+ compatibility to ANSI.
243+ - `True`, it sets the color support to XTERM_256.
244+ - `False`, it sets the color support to NONE.
245+ - For different values that are not instances of
246+ `progressbar.env.ColorSupport`, it raises a ValueError.
247+
248+ Args:
249+ enable_colors (progressbar.env.ColorSupport | None): The color
250+ support setting from the user. It can be None, True, False,
251+ or an instance of `progressbar.env.ColorSupport`.
252+
253+ Returns:
254+ progressbar.env.ColorSupport: The determined color support.
255+
256+ Raises:
257+ ValueError: If `enable_colors` is not None, True, False, or an
258+ instance of `progressbar.env.ColorSupport`.
259+ '''
260+ color_support = progressbar .env .ColorSupport .NONE
245261 if enable_colors is None :
246262 colors = (
247263 progressbar .env .env_flag ('PROGRESSBAR_ENABLE_COLORS' ),
@@ -252,21 +268,23 @@ def _determine_enable_colors(
252268 for color_enabled in colors :
253269 if color_enabled is not None :
254270 if color_enabled :
255- enable = progressbar .env .COLOR_SUPPORT
271+ color_support = progressbar .env .COLOR_SUPPORT
256272 else :
257- enable = progressbar .env .ColorSupport .NONE
273+ color_support = progressbar .env .ColorSupport .NONE
258274 break
259275 else :
260- enable = False
276+ color_support = progressbar . env . ColorSupport . NONE
261277
262278 elif enable_colors is True :
263- enable = progressbar .env .ColorSupport .XTERM_256
279+ color_support = progressbar .env .ColorSupport .XTERM_256
264280 elif enable_colors is False :
265- enable = progressbar .env .ColorSupport .NONE
266- elif not isinstance (enable_colors , progressbar .env .ColorSupport ):
281+ color_support = progressbar .env .ColorSupport .NONE
282+ elif isinstance (enable_colors , progressbar .env .ColorSupport ):
283+ color_support = enable_colors
284+ else :
267285 raise ValueError (f'Invalid color support value: { enable_colors } ' )
268286
269- return enable
287+ return color_support
270288
271289 def print (self , * args : types .Any , ** kwargs : types .Any ) -> None :
272290 print (* args , file = self .fd , ** kwargs )
0 commit comments