@@ -4243,22 +4243,26 @@ def _print_documented_command_topics(self, header: str, cmds: list[str], verbose
4243
4243
category_grid .add_row (topics_table )
4244
4244
self .poutput (category_grid , "" )
4245
4245
4246
- def columnize (self , str_list : list [str ] | None , display_width : int = 80 ) -> None :
4247
- """Display a list of single-line strings as a compact set of columns.
4246
+ def render_columns (self , str_list : list [str ] | None , display_width : int = 80 ) -> str :
4247
+ """Render a list of single-line strings as a compact set of columns.
4248
4248
4249
- Override of cmd's columnize() to handle strings with ANSI style sequences and wide characters.
4249
+ This method correctly handles strings containing ANSI escape codes and
4250
+ full-width characters (like those used in CJK languages). Each column is
4251
+ only as wide as necessary and columns are separated by two spaces.
4250
4252
4251
- Each column is only as wide as necessary.
4252
- Columns are separated by two spaces (one was not legible enough).
4253
+ :param str_list: list of single-line strings to display
4254
+ :param display_width: max number of display columns to fit into
4255
+ :return: a string containing the columnized output
4253
4256
"""
4254
4257
if not str_list :
4255
- self .poutput ("<empty>" )
4256
- return
4258
+ return ""
4257
4259
4258
4260
size = len (str_list )
4259
4261
if size == 1 :
4260
- self .poutput (str_list [0 ])
4261
- return
4262
+ return str_list [0 ]
4263
+
4264
+ rows : list [str ] = []
4265
+
4262
4266
# Try every row count from 1 upwards
4263
4267
for nrows in range (1 , len (str_list )):
4264
4268
ncols = (size + nrows - 1 ) // nrows
@@ -4294,7 +4298,22 @@ def columnize(self, str_list: list[str] | None, display_width: int = 80) -> None
4294
4298
del texts [- 1 ]
4295
4299
for col in range (len (texts )):
4296
4300
texts [col ] = su .align_left (texts [col ], width = colwidths [col ])
4297
- self .poutput (" " .join (texts ))
4301
+ rows .append (" " .join (texts ))
4302
+
4303
+ return "\n " .join (rows )
4304
+
4305
+ def columnize (self , str_list : list [str ] | None , display_width : int = 80 ) -> None :
4306
+ """Display a list of single-line strings as a compact set of columns.
4307
+
4308
+ Override of cmd's columnize() that uses the render_columns() method.
4309
+ The method correctly handles strings with ANSI style sequences and
4310
+ full-width characters (like those used in CJK languages).
4311
+
4312
+ :param str_list: list of single-line strings to display
4313
+ :param display_width: max number of display columns to fit into
4314
+ """
4315
+ columnized_strs = self .render_columns (str_list , display_width )
4316
+ self .poutput (columnized_strs )
4298
4317
4299
4318
@staticmethod
4300
4319
def _build_shortcuts_parser () -> Cmd2ArgumentParser :
0 commit comments