@@ -22,30 +22,28 @@ def strip_ansi(value):
2222
2323
2424class Style (IntEnum ):
25- """
26- Heavily borrowed from https://github.com/pallets/click/blob/6.7/click/termui.py
27-
28- Italic added, multiple ansi codes condensed into one block and generally modernised.
29- """
3025 reset = 0
3126
3227 bold = 1
33- un_bold = 22
28+ not_bold = 22
3429
3530 dim = 2
36- un_dim = 22
31+ not_dim = 22
3732
3833 italic = 3
39- un_italic = 23
34+ not_italic = 23
4035
4136 underline = 4
42- un_underline = 24
37+ not_underline = 24
4338
4439 blink = 5
45- un_blink = 25
40+ not_blink = 25
4641
4742 reverse = 7
48- un_reverse = 27
43+ not_reverse = 27
44+
45+ strike_through = 9
46+ not_strike_through = 29
4947
5048 # foreground colours
5149 black = 30
@@ -56,7 +54,6 @@ class Style(IntEnum):
5654 magenta = 35
5755 cyan = 36
5856 white = 37
59- fg_reset = 38
6057
6158 # background colours
6259 bg_black = 40
@@ -67,55 +64,45 @@ class Style(IntEnum):
6764 bg_magenta = 45
6865 bg_cyan = 46
6966 bg_white = 47
70- bg_reset = 48
7167
7268 # this is a meta value used for the "Style" instance which is the "style" function
7369 function = - 1
7470
75- def __call__ (self , text : Any , * styles , reset : bool = True ):
71+ def __call__ (self , input : Any , * styles , reset : bool = True , apply : bool = True ):
7672 """
77- Styles a text with ANSI styles and returns the new string.
73+ Styles text with ANSI styles and returns the new string.
7874
7975 By default the styling is cleared at the end of the string, this can be prevented with``reset=False``.
8076
8177 Examples::
8278
83- print(style ('Hello World!', style .green))
84- print(style ('ATTENTION!', style .bg_magenta))
85- print(style ('Some things', style .reverse, style .bold))
79+ print(sformat ('Hello World!', sformat .green))
80+ print(sformat ('ATTENTION!', sformat .bg_magenta))
81+ print(sformat ('Some things', sformat .reverse, sformat .bold))
8682
87- Supported color names:
88-
89- * ``black`` (might be a gray)
90- * ``red``
91- * ``green``
92- * ``yellow`` (might be an orange)
93- * ``blue``
94- * ``magenta``
95- * ``cyan``
96- * ``white`` (might be light gray)
97- * ``reset`` (reset the color code only)
98-
99- :param text: the string to style with ansi codes.
83+ :param input: the object to style with ansi codes.
10084 :param *styles: zero or more styles to apply to the text, should be either style instances or strings
10185 matching style names.
102- :param reset: by default a reset-all code is added at the end of the
103- string which means that styles do not carry over. This
104- can be disabled to compose styles.
86+ :param reset: if False the ansi reset code is not appended to the end of the string
87+ :param: apply: if False no ansi codes are applied
10588 """
89+ text = str (input )
90+ if not apply :
91+ return text
10692 codes = []
10793 for s in styles :
108- if not isinstance (s , self .__class__ ):
94+ # raw ints are allowed
95+ if not isinstance (s , self .__class__ ) and not isinstance (s , int ):
10996 try :
11097 s = self .styles [s ]
11198 except KeyError :
11299 raise ValueError ('invalid style "{}"' .format (s ))
113100 codes .append (str (s .value ))
114101
115102 if codes :
116- r = _ansi_template .format (';' .join (codes )) + str ( text )
103+ r = _ansi_template .format (';' .join (codes )) + text
117104 else :
118- r = str ( text )
105+ r = text
119106
120107 if reset :
121108 r += _ansi_template .format (self .reset )
@@ -127,7 +114,7 @@ def styles(self):
127114
128115 def __repr__ (self ):
129116 if self == self .function :
130- return '<pseudo function style (text, *styles)>'
117+ return '<pseudo function sformat (text, *styles)>'
131118 else :
132119 return super ().__repr__ ()
133120
@@ -141,7 +128,20 @@ def __str__(self):
141128sformat = Style (- 1 )
142129
143130
144- def sprint (text , * styles , reset = True , flush = True , file = None , ** print_kwargs ):
145- if isatty (file ):
146- text = sformat (text , * styles , reset = reset )
147- print (text , flush = flush , file = file , ** print_kwargs )
131+ class StylePrint :
132+ """
133+ Annoyingly enums do not allow inheritance, this is a lazy design mistake, this is an ugly work around
134+ for that mistake.
135+ """
136+ def __call__ (self , input , * styles , reset = True , flush = True , file = None , ** print_kwargs ):
137+ text = sformat (input , * styles , reset = reset , apply = isatty (file ))
138+ print (text , flush = flush , file = file , ** print_kwargs )
139+
140+ def __getattr__ (self , item ):
141+ return getattr (sformat , item )
142+
143+ def __repr__ (self ):
144+ return '<pseudo function sprint(text, *styles)>'
145+
146+
147+ sprint = StylePrint ()
0 commit comments