Skip to content

Commit 5d0a24c

Browse files
committed
Support separators between body rows
1 parent 72c17c9 commit 5d0a24c

File tree

3 files changed

+75
-26
lines changed

3 files changed

+75
-26
lines changed

table2ascii/styles.py

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
11
from .table_style import TableStyle
22

3+
thin = TableStyle.from_string("┌─┬─┐││ ├─┼─┤├─┼─┤└┴─┘")
4+
thin_rounded = TableStyle.from_string("╭─┬─╮││ ├─┼─┤├─┼─┤╰┴─╯")
5+
thin_compact = TableStyle.from_string("┌─┬─┐││ ├─┼─┤ └┴─┘")
6+
thin_compact_rounded = TableStyle.from_string("╭─┬─╮││ ├─┼─┤ ╰┴─╯")
7+
thin_thick = TableStyle.from_string("┌─┬─┐││ ┝━┿━┥├─┼─┤└┴─┘")
8+
thin_thick_rounded = TableStyle.from_string("╭─┬─╮││ ┝━┿━┥├─┼─┤╰┴─╯")
9+
thin_double = TableStyle.from_string("┌─┬─┐││ ╞═╪═╡├─┼─┤└┴─┘")
10+
thin_double_rounded = TableStyle.from_string("╭─┬─╮││ ╞═╪═╡├─┼─┤╰┴─╯")
11+
thick_compact = TableStyle.from_string("┏━┳━┓┃┃ ┣━╋━┫ ┗┻━┛")
12+
double = TableStyle.from_string("╔═╦═╗║║ ╠═╬═╣╠═╬═╣╚╩═╝")
13+
double_compact = TableStyle.from_string("╔═╦═╗║║ ╠═╬═╣ ╚╩═╝")
14+
double_thin = TableStyle.from_string("╔═╦═╗║║ ╟─╫─╢╠═╬═╣╚╩═╝")
15+
minimalist = TableStyle.from_string(" ─── ━━━ ─── ── ")
16+
ascii_compact = TableStyle.from_string("+-+-+|| +-+-+ ++-+")
17+
ascii_double = TableStyle.from_string("+-+-+|| +=+=++-+-+++-+")
18+
ascii_minimalist = TableStyle.from_string(" --- === --- -- ")
19+
markdown = TableStyle.from_string(" ||||-||| ")
320

4-
a = TableStyle.from_string("┌─┬─┐││ ├─┼─┤└┴─┘")
5-
b = TableStyle.from_string("┌─┬─┐││┝━┿━┥├─┼─┤└┴─┘")
6-
c = TableStyle.from_string("┌─┬─┐││╞═╪═╡├─┼─┤└┴─┘")
7-
d = TableStyle.from_string("╭─┬─╮││ ├─┼─┤╰┴─╯")
8-
e = TableStyle.from_string("╭─┬─╮││┝━┿━┥├─┼─┤╰┴─╯")
9-
f = TableStyle.from_string("╭─┬─╮││╞═╪═╡├─┼─┤╰┴─╯")
10-
g = TableStyle.from_string("┏━┳━┓┃┃ ┣━╋━┫┗┻━┛")
11-
h = TableStyle.from_string("┌─┬─┐││┠─╂─┨├─┼─┤└┴─┘")
12-
i = TableStyle.from_string("╔═╦═╗║║ ╠═╬═╣╚╩═╝")
13-
j = TableStyle.from_string("╔═╦═╗║║╟─╫─╢╠═╬═╣╚╩═╝")
14-
k = TableStyle.from_string(" ─── ━━━ ─── ── ")
15-
l = TableStyle.from_string("+-+-+|| +-+-+++-+")
16-
m = TableStyle.from_string("+-+-+||+=+=++-+-+++-+")
17-
n = TableStyle.from_string("+=+=+HH +=+=+++=+")
18-
o = TableStyle.from_string("+=+=+HH+-+-++=+=+++=+")
19-
p = TableStyle.from_string(" --- === --- -- ")
20-
q = TableStyle.from_string(" |||-|-| ")
21+
# prints all themes with previews
22+
if __name__ == "__main__":
23+
from .table_to_ascii import table2ascii
24+
25+
styles = {
26+
"thin": thin,
27+
"thin_rounded": thin_rounded,
28+
"thin_compact": thin_compact,
29+
"thin_compact_rounded": thin_compact_rounded,
30+
"thin_thick": thin_thick,
31+
"thin_thick_rounded": thin_thick_rounded,
32+
"thin_double": thin_double,
33+
"thin_double_rounded": thin_double_rounded,
34+
"thick_compact": thick_compact,
35+
"double": double,
36+
"double_compact": double_compact,
37+
"double_thin": double_thin,
38+
"minimalist": minimalist,
39+
"ascii_compact": ascii_compact,
40+
"ascii_double": ascii_double,
41+
"ascii_minimalist": ascii_minimalist,
42+
"markdown": markdown,
43+
}
44+
for style in list(styles.keys()):
45+
full = table2ascii(
46+
header=["#", "G", "H", "R", "S"],
47+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
48+
footer=["SUM", "130", "140", "135", "130"],
49+
first_col_heading=True,
50+
last_col_heading=False,
51+
style=styles[style],
52+
)
53+
body_only = table2ascii(
54+
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
55+
first_col_heading=True,
56+
last_col_heading=False,
57+
style=styles[style],
58+
)
59+
print(f"### `{style}`\n\n```\n{full}\n{body_only}```\n")

table2ascii/table_style.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ class TableStyle:
4343
top_right_corner: str # E
4444
left_and_right_edge: str # F
4545
heading_col_sep: str # G
46-
middle_edge: str # H
46+
col_sep: str # H
4747
heading_row_left_tee: str # I
4848
heading_row_sep: str # J
49-
heading_col_row_cross: str # K
49+
heading_col_heading_row_cross: str # K
5050
heading_row_cross: str # L
5151
heading_row_right_tee: str # M
5252
row_left_tee: str # N
53-
normalrow_sep: str # O
54-
col_row_cross: str # P
55-
row_cross: str # Q
53+
row_sep: str # O
54+
heading_col_row_cross: str # P
55+
col_row_cross: str # Q
5656
row_right_tee: str # R
5757
bottom_left_corner: str # S
5858
heading_col_bottom_tee: str # T

table2ascii/table_to_ascii.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def __heading_row_to_ascii(self, row: List) -> str:
159159
return self.__row_to_ascii(
160160
left_edge=self.__style.left_and_right_edge,
161161
heading_col_sep=self.__style.heading_col_sep,
162-
column_seperator=self.__style.middle_edge,
162+
column_seperator=self.__style.col_sep,
163163
right_edge=self.__style.left_and_right_edge,
164164
filler=row,
165165
)
@@ -168,18 +168,28 @@ def __heading_sep_to_ascii(self) -> str:
168168
"""Assembles the seperator below the header or above footer of the ascii table"""
169169
return self.__row_to_ascii(
170170
left_edge=self.__style.heading_row_left_tee,
171-
heading_col_sep=self.__style.heading_col_row_cross,
171+
heading_col_sep=self.__style.heading_col_heading_row_cross,
172172
column_seperator=self.__style.heading_row_cross,
173173
right_edge=self.__style.heading_row_right_tee,
174174
filler=self.__style.heading_row_sep,
175175
)
176176

177177
def __body_to_ascii(self) -> str:
178-
return "".join(
178+
separation_row = self.__row_to_ascii(
179+
left_edge=self.__style.row_left_tee,
180+
heading_col_sep=self.__style.heading_col_row_cross,
181+
column_seperator=self.__style.col_row_cross,
182+
right_edge=self.__style.row_right_tee,
183+
filler=self.__style.row_sep,
184+
)
185+
# don't use separation row if it's only space
186+
if separation_row.strip() == "":
187+
separation_row = ""
188+
return separation_row.join(
179189
self.__row_to_ascii(
180190
left_edge=self.__style.left_and_right_edge,
181191
heading_col_sep=self.__style.heading_col_sep,
182-
column_seperator=self.__style.middle_edge,
192+
column_seperator=self.__style.col_sep,
183193
right_edge=self.__style.left_and_right_edge,
184194
filler=row,
185195
)

0 commit comments

Comments
 (0)