Skip to content

Commit 6f804e9

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 6f804e9

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

CHANGELOG

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

3+
## Version 2.7.0
4+
5+
(released on 2025-07-28)
6+
7+
- Add `mysql` and `mysql_unicode` output formats which right-align numbers.
8+
39
## Version 2.6.0
410

11+
(released on 2025-07-12)
12+
513
- Register the JSON formats so they are actually usable.
614
- Make JSON formats able to encode Decimals and None/NULLs.
715

816
## Version 2.5.0
917

18+
(released on 2025-07-10)
19+
1020
- Added noheader CSV and TSV output formats.
1121
- Added `jsonl` and `jsonl_escaped` output formats.
1222

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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,78 @@ 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(iter(data), headers, format_name="mysql")
118+
)
119+
120+
121+
def test_tabular_output_mysql_unicode():
122+
"""Test the mysql_unicode output format."""
123+
headers = ["text", "numeric"]
124+
data = [
125+
["abc", Decimal(1)],
126+
["defg", Decimal("11.1")],
127+
["hi", Decimal("1.1")],
128+
["Pablo\rß\n", 0],
129+
]
130+
expected = dedent(
131+
"""\
132+
┌───────┬─────────┐
133+
│ text │ numeric │
134+
├───────┼─────────┤
135+
│ abc │ 1 │
136+
│ defg │ 11.1 │
137+
│ hi │ 1.1 │
138+
│ Pablo │ 0 │
139+
│ ß │ │
140+
└───────┴─────────┘"""
141+
)
142+
143+
print(expected)
144+
print(
145+
"\n".join(
146+
TabularOutputFormatter().format_output(
147+
iter(data), headers, format_name="mysql_unicode"
148+
)
149+
)
150+
)
151+
assert expected == "\n".join(
152+
TabularOutputFormatter().format_output(
153+
iter(data), headers, format_name="mysql_unicode"
154+
)
155+
)
156+
157+
86158
def test_tabular_format_output_wrapper():
87159
"""Test the format_output wrapper."""
88160
data = [["1", None], ["2", "Sam"], ["3", "Joe"]]

0 commit comments

Comments
 (0)