@@ -34,6 +34,7 @@ def __init__(
3434 self .__style = options .style
3535 self .__first_col_heading = options .first_col_heading
3636 self .__last_col_heading = options .last_col_heading
37+ self .__cell_padding = options .cell_padding
3738
3839 # calculate number of columns
3940 self .__columns = self .__count_columns ()
@@ -71,6 +72,10 @@ def __init__(
7172 if options .alignments and len (options .alignments ) != self .__columns :
7273 raise ValueError ("Length of `alignments` list must equal the number of columns" )
7374
75+ # check if the cell padding is valid
76+ if self .__cell_padding < 0 :
77+ raise ValueError ("Cell padding must be greater than or equal to 0" )
78+
7479 def __count_columns (self ) -> int :
7580 """
7681 Get the number of columns in the table based on the
@@ -108,8 +113,8 @@ def widest_line(value: SupportsStr) -> int:
108113 header_size = widest_line (self .__header [i ]) if self .__header else 0
109114 body_size = max (widest_line (row [i ]) for row in self .__body ) if self .__body else 0
110115 footer_size = widest_line (self .__footer [i ]) if self .__footer else 0
111- # get the max and add 2 for padding each side with a space
112- column_widths .append (max (header_size , body_size , footer_size ) + 2 )
116+ # get the max and add 2 for padding each side with a space depending on cell padding
117+ column_widths .append (max (header_size , body_size , footer_size ) + self . __cell_padding * 2 )
113118 return column_widths
114119
115120 def __pad (self , cell_value : SupportsStr , width : int , alignment : Alignment ) -> str :
@@ -125,17 +130,19 @@ def __pad(self, cell_value: SupportsStr, width: int, alignment: Alignment) -> st
125130 The padded text
126131 """
127132 text = str (cell_value )
133+ padding = " " * self .__cell_padding
134+ padded_text = f"{ padding } { text } { padding } "
128135 if alignment == Alignment .LEFT :
129136 # pad with spaces on the end
130- return f" { text } " + (" " * (width - len (text ) - 2 ))
137+ return padded_text + (" " * (width - len (padded_text ) ))
131138 if alignment == Alignment .CENTER :
132139 # pad with spaces, half on each side
133- before = " " * floor ((width - len (text ) - 2 ) / 2 )
134- after = " " * ceil ((width - len (text ) - 2 ) / 2 )
135- return before + f" { text } " + after
140+ before = " " * floor ((width - len (padded_text ) ) / 2 )
141+ after = " " * ceil ((width - len (padded_text ) ) / 2 )
142+ return before + padded_text + after
136143 if alignment == Alignment .RIGHT :
137144 # pad with spaces at the beginning
138- return (" " * (width - len (text ) - 2 )) + f" { text } "
145+ return (" " * (width - len (padded_text ) )) + padded_text
139146 raise ValueError (f"The value '{ alignment } ' is not valid for alignment." )
140147
141148 def __row_to_ascii (
@@ -318,6 +325,7 @@ def table2ascii(
318325 last_col_heading : bool = False ,
319326 column_widths : Optional [List [Optional [int ]]] = None ,
320327 alignments : Optional [List [Alignment ]] = None ,
328+ cell_padding : int = 1 ,
321329 style : TableStyle = PresetStyle .double_thin_compact ,
322330) -> str :
323331 """
@@ -341,6 +349,9 @@ def table2ascii(
341349 alignments: List of alignments for each column
342350 (ex. ``[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT]``). If not specified or set to
343351 :py:obj:`None`, all columns will be center-aligned. Defaults to :py:obj:`None`.
352+ cell_padding: The minimum number of spaces to add between the cell content and the column
353+ separator. If set to ``0``, the cell content will be flush against the column separator.
354+ Defaults to ``1``.
344355 style: Table style to use for styling (preset styles can be imported).
345356 Defaults to :ref:`PresetStyle.double_thin_compact <PresetStyle.double_thin_compact>`.
346357
@@ -356,6 +367,7 @@ def table2ascii(
356367 last_col_heading = last_col_heading ,
357368 column_widths = column_widths ,
358369 alignments = alignments ,
370+ cell_padding = cell_padding ,
359371 style = style ,
360372 ),
361373 ).to_ascii ()
0 commit comments