@@ -254,6 +254,7 @@ def save(self, f):
254254
255255def multi_col_print (data , columns = 5 ):
256256 '''Print a list of data in a table with columns'''
257+ # pip install rich
257258 from rich .console import Console
258259 from rich .table import Table
259260
@@ -265,10 +266,38 @@ def multi_col_print(data, columns=5):
265266 for i in range (row_count ):
266267 row = data [i ::row_count ]
267268 table .add_row (* row )
268-
269269 console = Console ()
270270 console .print (table )
271271
272+ def rprint (* args , ** kwargs ):
273+ '''
274+ Print function that uses rich library to print (This is different from
275+ rich.print() because that does not support additional arguments like `style`.)
276+ The console obj is created only once and reused.
277+ '''
278+ # pip install rich
279+ from rich .console import Console
280+ if not hasattr (rprint , '_console' ):
281+ rprint ._console = Console ()
282+ rprint ._console .print (* args , ** kwargs )
283+
284+ def print2 (* args , ** kwargs ):
285+ '''
286+ Custom print function that manages line endings.
287+ If a previous print call ended without a newline (e.g., end=''),
288+ and the current call implies a newline (e.g., default end or end='\n '),
289+ an explicit newline is printed first to terminate the previous line.
290+ The state _newlined is stored as an attribute of the print2 function itself.
291+ '''
292+ from builtins import print as builtin_print
293+ if not hasattr (print2 , '_newlined' ):
294+ print2 ._newlined = True
295+ end = kwargs .pop ('end' , '\n ' )
296+ if end .endswith ('\n ' ) and not print2 ._newlined :
297+ builtin_print ()
298+ print2 ._newlined = True if end .endswith ('\n ' ) else False
299+ builtin_print (* args , ** kwargs , end = end )
300+
272301def array_to_range_text (a , sep = ', ' , dash = '-' ):
273302 s = ''
274303 prev_seg = None
0 commit comments