Skip to content

Commit 1361764

Browse files
committed
add mysql/mysql_unicode output formats
The difference being that for the mysql formats, numbers are right aligned. Addresses dbcli/mycli#1102 .
1 parent 22881bb commit 1361764

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# Changelog
22

3+
## Version 2.7.0
4+
5+
- Add `mysql` and `mysql_unicode` output formats which right-align numbers.
6+
37
## Version 2.6.0
48

9+
(released on 2025-07-12)
10+
511
- Register the JSON formats so they are actually usable.
612
- Make JSON formats able to encode Decimals and None/NULLs.
713

814
## Version 2.5.0
915

16+
(released on 2025-07-10)
17+
1018
- Added noheader CSV and TSV output formats.
1119
- Added `jsonl` and `jsonl_escaped` output formats.
1220

cli_helpers/tabular_output/tabulate_adapter.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,37 @@
6363
with_header_hide=None,
6464
)
6565

66+
tabulate._table_formats["mysql"] = tabulate.TableFormat(
67+
lineabove=tabulate.Line("+", "-", "+", "+"),
68+
linebelowheader=tabulate.Line("+", "-", "+", "+"),
69+
linebetweenrows=None,
70+
linebelow=tabulate.Line("+", "-", "+", "+"),
71+
headerrow=tabulate.DataRow("|", "|", "|"),
72+
datarow=tabulate.DataRow("|", "|", "|"),
73+
padding=1,
74+
with_header_hide=None,
75+
)
76+
77+
tabulate._table_formats["mysql_unicode"] = tabulate.TableFormat(
78+
lineabove=tabulate.Line("┌", "─", "┬", "┐"),
79+
linebelowheader=tabulate.Line("├", "─", "┼", "┤"),
80+
linebetweenrows=None,
81+
linebelow=tabulate.Line("└", "─", "┴", "┘"),
82+
headerrow=tabulate.DataRow("│", "│", "│"),
83+
datarow=tabulate.DataRow("│", "│", "│"),
84+
padding=1,
85+
with_header_hide=None,
86+
)
87+
6688
# "minimal" is the same as "plain", but without headers
6789
tabulate._table_formats["minimal"] = tabulate._table_formats["plain"]
6890

6991
tabulate.multiline_formats["psql_unicode"] = "psql_unicode"
7092
tabulate.multiline_formats["double"] = "double"
7193
tabulate.multiline_formats["ascii"] = "ascii"
7294
tabulate.multiline_formats["minimal"] = "minimal"
95+
tabulate.multiline_formats["mysql"] = "mysql"
96+
tabulate.multiline_formats["mysql_unicode"] = "mysql_unicode"
7397

7498
supported_markup_formats = (
7599
"mediawiki",
@@ -95,13 +119,17 @@
95119
"rst",
96120
"github",
97121
"double",
122+
"mysql",
123+
"mysql_unicode",
98124
)
99125

100126
supported_formats = supported_markup_formats + supported_table_formats
101127

102128
default_kwargs = {
103129
"ascii": {"numalign": "left"},
104130
"ascii_escaped": {"numalign": "left"},
131+
"mysql": {"numalign": "right"},
132+
"mysql_unicode": {"numalign": "right"},
105133
}
106134
headless_formats = ("minimal",)
107135

tests/tabular_output/test_output_formatter.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,80 @@ def test_tabular_output_escaped():
8383
)
8484

8585

86+
def test_tabular_output_mysql():
87+
"""Test the mysql output format."""
88+
headers = ["text", "numeric"]
89+
data = [
90+
["abc", Decimal(1)],
91+
["defg", Decimal("11.1")],
92+
["hi", Decimal("1.1")],
93+
["Pablo\rß\n", 0],
94+
]
95+
expected = dedent(
96+
"""\
97+
+-------+---------+
98+
| text | numeric |
99+
+-------+---------+
100+
| abc | 1 |
101+
| defg | 11.1 |
102+
| hi | 1.1 |
103+
| Pablo | 0 |
104+
| ß | |
105+
+-------+---------+"""
106+
)
107+
108+
print(expected)
109+
print(
110+
"\n".join(
111+
TabularOutputFormatter().format_output(
112+
iter(data), headers, format_name="mysql"
113+
)
114+
)
115+
)
116+
assert expected == "\n".join(
117+
TabularOutputFormatter().format_output(
118+
iter(data), headers, format_name="mysql"
119+
)
120+
)
121+
122+
123+
def test_tabular_output_mysql_unicode():
124+
"""Test the mysql_unicode output format."""
125+
headers = ["text", "numeric"]
126+
data = [
127+
["abc", Decimal(1)],
128+
["defg", Decimal("11.1")],
129+
["hi", Decimal("1.1")],
130+
["Pablo\rß\n", 0],
131+
]
132+
expected = dedent(
133+
"""\
134+
┌───────┬─────────┐
135+
│ text │ numeric │
136+
├───────┼─────────┤
137+
│ abc │ 1 │
138+
│ defg │ 11.1 │
139+
│ hi │ 1.1 │
140+
│ Pablo │ 0 │
141+
│ ß │ │
142+
└───────┴─────────┘"""
143+
)
144+
145+
print(expected)
146+
print(
147+
"\n".join(
148+
TabularOutputFormatter().format_output(
149+
iter(data), headers, format_name="mysql_unicode"
150+
)
151+
)
152+
)
153+
assert expected == "\n".join(
154+
TabularOutputFormatter().format_output(
155+
iter(data), headers, format_name="mysql_unicode"
156+
)
157+
)
158+
159+
86160
def test_tabular_format_output_wrapper():
87161
"""Test the format_output wrapper."""
88162
data = [["1", None], ["2", "Sam"], ["3", "Joe"]]

0 commit comments

Comments
 (0)