11"""Provides common utilities to support Rich in cmd2-based applications."""
22
33import re
4- import threading
54from collections .abc import Mapping
65from enum import Enum
76from typing import (
@@ -178,31 +177,12 @@ def __init__(
178177 theme = APP_THEME ,
179178 ** kwargs ,
180179 )
181- self ._thread_local = threading .local ()
182180
183181 def on_broken_pipe (self ) -> None :
184182 """Override which raises BrokenPipeError instead of SystemExit."""
185183 self .quiet = True
186184 raise BrokenPipeError
187185
188- def render_str (
189- self ,
190- text : str ,
191- highlight : bool | None = None ,
192- markup : bool | None = None ,
193- emoji : bool | None = None ,
194- ** kwargs : Any ,
195- ) -> Text :
196- """Override to ensure formatting overrides passed to print() and log() are respected."""
197- if emoji is None :
198- emoji = getattr (self ._thread_local , "emoji" , None )
199- if markup is None :
200- markup = getattr (self ._thread_local , "markup" , None )
201- if highlight is None :
202- highlight = getattr (self ._thread_local , "highlight" , None )
203-
204- return super ().render_str (text , highlight = highlight , markup = markup , emoji = emoji , ** kwargs )
205-
206186 def print (
207187 self ,
208188 * objects : Any ,
@@ -221,52 +201,32 @@ def print(
221201 soft_wrap : bool | None = None ,
222202 new_line_start : bool = False ,
223203 ) -> None :
224- """Override to support ANSI sequences and address a bug in Rich .
204+ """Override to support ANSI sequences.
225205
226206 This method calls [cmd2.rich_utils.prepare_objects_for_rendering][] on the
227207 objects being printed. This ensures that strings containing ANSI style
228208 sequences are converted to Rich Text objects, so that Rich can correctly
229209 calculate their display width.
230-
231- Additionally, it works around a bug in Rich where complex renderables
232- (like Table and Rule) may not receive formatting settings passed to print().
233- By temporarily injecting these settings into thread-local storage, we ensure
234- that all internal rendering calls within the print() operation respect the
235- requested overrides.
236-
237- There is an issue on Rich to fix the latter:
238- https://github.com/Textualize/rich/issues/4028
239210 """
240211 prepared_objects = prepare_objects_for_rendering (* objects )
241212
242- # Inject overrides into thread-local storage
243- self ._thread_local .emoji = emoji
244- self ._thread_local .markup = markup
245- self ._thread_local .highlight = highlight
246-
247- try :
248- super ().print (
249- * prepared_objects ,
250- sep = sep ,
251- end = end ,
252- style = style ,
253- justify = justify ,
254- overflow = overflow ,
255- no_wrap = no_wrap ,
256- emoji = emoji ,
257- markup = markup ,
258- highlight = highlight ,
259- width = width ,
260- height = height ,
261- crop = crop ,
262- soft_wrap = soft_wrap ,
263- new_line_start = new_line_start ,
264- )
265- finally :
266- # Clear overrides from thread-local storage
267- self ._thread_local .emoji = None
268- self ._thread_local .markup = None
269- self ._thread_local .highlight = None
213+ super ().print (
214+ * prepared_objects ,
215+ sep = sep ,
216+ end = end ,
217+ style = style ,
218+ justify = justify ,
219+ overflow = overflow ,
220+ no_wrap = no_wrap ,
221+ emoji = emoji ,
222+ markup = markup ,
223+ highlight = highlight ,
224+ width = width ,
225+ height = height ,
226+ crop = crop ,
227+ soft_wrap = soft_wrap ,
228+ new_line_start = new_line_start ,
229+ )
270230
271231 def log (
272232 self ,
@@ -281,56 +241,35 @@ def log(
281241 log_locals : bool = False ,
282242 _stack_offset : int = 1 ,
283243 ) -> None :
284- """Override to support ANSI sequences and address a bug in Rich .
244+ """Override to support ANSI sequences.
285245
286246 This method calls [cmd2.rich_utils.prepare_objects_for_rendering][] on the
287247 objects being logged. This ensures that strings containing ANSI style
288248 sequences are converted to Rich Text objects, so that Rich can correctly
289249 calculate their display width.
290-
291- Additionally, it works around a bug in Rich where complex renderables
292- (like Table and Rule) may not receive formatting settings passed to log().
293- By temporarily injecting these settings into thread-local storage, we ensure
294- that all internal rendering calls within the log() operation respect the
295- requested overrides.
296-
297- There is an issue on Rich to fix the latter:
298- https://github.com/Textualize/rich/issues/4028
299250 """
300251 prepared_objects = prepare_objects_for_rendering (* objects )
301252
302- # Inject overrides into thread-local storage
303- self ._thread_local .emoji = emoji
304- self ._thread_local .markup = markup
305- self ._thread_local .highlight = highlight
306-
307- try :
308- # Increment _stack_offset because we added this wrapper frame
309- super ().log (
310- * prepared_objects ,
311- sep = sep ,
312- end = end ,
313- style = style ,
314- justify = justify ,
315- emoji = emoji ,
316- markup = markup ,
317- highlight = highlight ,
318- log_locals = log_locals ,
319- _stack_offset = _stack_offset + 1 ,
320- )
321- finally :
322- # Clear overrides from thread-local storage
323- self ._thread_local .emoji = None
324- self ._thread_local .markup = None
325- self ._thread_local .highlight = None
253+ # Increment _stack_offset because we added this wrapper frame
254+ super ().log (
255+ * prepared_objects ,
256+ sep = sep ,
257+ end = end ,
258+ style = style ,
259+ justify = justify ,
260+ emoji = emoji ,
261+ markup = markup ,
262+ highlight = highlight ,
263+ log_locals = log_locals ,
264+ _stack_offset = _stack_offset + 1 ,
265+ )
326266
327267
328268class Cmd2GeneralConsole (Cmd2BaseConsole ):
329269 """Rich console for general-purpose printing.
330270
331- It enables soft wrap and disables Rich's automatic detection for markup,
332- emoji, and highlighting. These defaults can be overridden in calls to the
333- console's or cmd2's print methods.
271+ It enables soft wrap and disables Rich's automatic detection
272+ for markup, emoji, and highlighting.
334273 """
335274
336275 def __init__ (self , * , file : IO [str ] | None = None ) -> None :
0 commit comments