@@ -29,6 +29,7 @@ def __init__(
2929 body : Optional [List [List ]],
3030 footer : Optional [List ],
3131 column_widths : Optional [List [int ]],
32+ alignments : Optional [List [Alignment ]],
3233 options : Options ,
3334 ):
3435 """Validate arguments and initialize fields"""
@@ -37,6 +38,8 @@ def __init__(
3738 self .__body = body
3839 self .__footer = footer
3940 self .__options = options
41+
42+ # calculate number of columns
4043 self .__columns = self .__count_columns ()
4144
4245 # check if footer has a different number of columns
@@ -50,7 +53,7 @@ def __init__(
5053 "All rows in body must have the same number of columns as the other rows"
5154 )
5255
53- # calculate column widths
56+ # calculate or use given column widths
5457 self .__column_widths = column_widths or self .__auto_column_widths ()
5558
5659 # check if column widths specified have a different number of columns
@@ -64,6 +67,8 @@ def __init__(
6467 "All values in `column_widths` must be greater than or equal to 2"
6568 )
6669
70+ self .__alignments = alignments or [Alignment .CENTER ] * self .__columns
71+
6772 """
6873 ╔═════╦═══════════════════════╗ ABBBBBCBBBBBDBBBBBDBBBBBDBBBBBE
6974 ║ # ║ G H R S ║ F G H H H F
@@ -127,7 +132,7 @@ def __auto_column_widths(self) -> List[int]:
127132 column_widths .append (max (header_size , * body_size , footer_size ) + 2 )
128133 return column_widths
129134
130- def __pad (self , text : str , width : int , alignment : Alignment = Alignment . CENTER ):
135+ def __pad (self , text : str , width : int , alignment : Alignment ):
131136 """Pad a string of text to a given width with specified alignment"""
132137 if alignment == Alignment .LEFT :
133138 # pad with spaces on the end
@@ -163,7 +168,9 @@ def __row_to_ascii(
163168 filler * self .__column_widths [i ]
164169 if isinstance (filler , str )
165170 # otherwise, use the column content
166- else self .__pad (str (filler [i ]), self .__column_widths [i ])
171+ else self .__pad (
172+ str (filler [i ]), self .__column_widths [i ], self .__alignments [i ]
173+ )
167174 )
168175 # column seperator
169176 sep = column_seperator
@@ -273,6 +280,7 @@ def table2ascii(
273280 body : Optional [List [List ]] = None ,
274281 footer : Optional [List ] = None ,
275282 column_widths : Optional [List [int ]] = None ,
283+ alignments : Optional [List [Alignment ]] = None ,
276284 ** options ,
277285) -> str :
278286 """Convert a 2D Python table to ASCII text
@@ -282,8 +290,12 @@ def table2ascii(
282290 :param body: :class:`Optional[List[List]]` 2-dimensional list of values in the table's body
283291 :param footer: :class:`Optional[List]` List of column values in the table's footer row
284292 :param column_widths: :class:`Optional[List[int]]` List of widths in characters for each column (defaults to auto-sizing)
285- :param footer: :class:`Optional[List]` List of column values in the table's footer row
293+ :param alignments: :class:`Optional[List[Alignment]]` List of alignments (ex. `[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT]`)
294+
295+ ### Additional options
296+ :param first_col_heading: :class:`Optional[bool]` Whether to add a header column separator after the first column
297+ :param last_col_heading: :class:`Optional[bool]` Whether to add a header column separator before the last column
286298 """
287299 return TableToAscii (
288- header , body , footer , column_widths , Options (** options )
300+ header , body , footer , column_widths , alignments , Options (** options )
289301 ).to_ascii ()
0 commit comments