@@ -34,6 +34,7 @@ def __init__(
34
34
self .__style = options .style
35
35
self .__first_col_heading = options .first_col_heading
36
36
self .__last_col_heading = options .last_col_heading
37
+ self .__cell_padding = options .cell_padding
37
38
38
39
# calculate number of columns
39
40
self .__columns = self .__count_columns ()
@@ -71,6 +72,10 @@ def __init__(
71
72
if options .alignments and len (options .alignments ) != self .__columns :
72
73
raise ValueError ("Length of `alignments` list must equal the number of columns" )
73
74
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
+
74
79
def __count_columns (self ) -> int :
75
80
"""
76
81
Get the number of columns in the table based on the
@@ -108,8 +113,8 @@ def widest_line(value: SupportsStr) -> int:
108
113
header_size = widest_line (self .__header [i ]) if self .__header else 0
109
114
body_size = max (widest_line (row [i ]) for row in self .__body ) if self .__body else 0
110
115
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 )
113
118
return column_widths
114
119
115
120
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
125
130
The padded text
126
131
"""
127
132
text = str (cell_value )
133
+ padding = " " * self .__cell_padding
134
+ padded_text = f"{ padding } { text } { padding } "
128
135
if alignment == Alignment .LEFT :
129
136
# pad with spaces on the end
130
- return f" { text } " + (" " * (width - len (text ) - 2 ))
137
+ return padded_text + (" " * (width - len (padded_text ) ))
131
138
if alignment == Alignment .CENTER :
132
139
# 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
136
143
if alignment == Alignment .RIGHT :
137
144
# pad with spaces at the beginning
138
- return (" " * (width - len (text ) - 2 )) + f" { text } "
145
+ return (" " * (width - len (padded_text ) )) + padded_text
139
146
raise ValueError (f"The value '{ alignment } ' is not valid for alignment." )
140
147
141
148
def __row_to_ascii (
@@ -318,6 +325,7 @@ def table2ascii(
318
325
last_col_heading : bool = False ,
319
326
column_widths : Optional [List [Optional [int ]]] = None ,
320
327
alignments : Optional [List [Alignment ]] = None ,
328
+ cell_padding : int = 1 ,
321
329
style : TableStyle = PresetStyle .double_thin_compact ,
322
330
) -> str :
323
331
"""
@@ -341,6 +349,9 @@ def table2ascii(
341
349
alignments: List of alignments for each column
342
350
(ex. ``[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT]``). If not specified or set to
343
351
: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``.
344
355
style: Table style to use for styling (preset styles can be imported).
345
356
Defaults to :ref:`PresetStyle.double_thin_compact <PresetStyle.double_thin_compact>`.
346
357
@@ -356,6 +367,7 @@ def table2ascii(
356
367
last_col_heading = last_col_heading ,
357
368
column_widths = column_widths ,
358
369
alignments = alignments ,
370
+ cell_padding = cell_padding ,
359
371
style = style ,
360
372
),
361
373
).to_ascii ()
0 commit comments