Coverage for sources/ictr/standard/printers.py: 94%
25 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-26 12:43 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-26 12:43 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21''' Printers and printer factories. '''
24from . import __
27_validate_arguments = (
28 __.validate_arguments(
29 globalvars = globals( ),
30 errorclass = __.ArgumentClassInvalidity ) )
33class Printer( __.Printer ):
34 ''' Simple printer that writes to a text stream. '''
36 target: __.typx.TextIO
37 colorize: __.Absential[ bool ] = __.absent
38 force_colorize: bool = False
40 def __call__( self, record: str | __.Record ) -> None:
41 text = record if isinstance( record, str ) else str( record )
42 if not self._determine_colorization( ):
43 text = __.remove_ansi_c1_sequences( text )
44 print( text, file = self.target )
46 def provide_textualization_control(
47 self
48 ) -> __.typx.Optional[ __.TextualizationControl ]:
49 colorize = self._determine_colorization( )
50 columns_max_calculator = (
51 __.produce_columns_max_calculator( self.target ) )
52 charset = getattr( self.target, 'encoding', None )
53 if charset:
54 charset = __.codecs.lookup( charset ).name # normalize
55 return __.TextualizationControl(
56 charset = charset,
57 colorize = colorize,
58 columns_max_calculator = columns_max_calculator )
60 def _determine_colorization( self ) -> bool:
61 colorize = self.colorize
62 on_tty = self.target.isatty( )
63 if __.is_absent( colorize ): 63 ↛ 65line 63 didn't jump to line 65 because the condition on line 63 was always true
64 colorize = False if __.os.environ.get( 'NO_COLOR' ) else on_tty
65 else: colorize = colorize and on_tty
66 return colorize or self.force_colorize