Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Changelog

## Version 2.7.0

(released on 2025-07-28)

- Add `mysql` and `mysql_unicode` output formats which right-align numbers.

## Version 2.6.0

(released on 2025-07-12)

- Register the JSON formats so they are actually usable.
- Make JSON formats able to encode Decimals and None/NULLs.

## Version 2.5.0

(released on 2025-07-10)

- Added noheader CSV and TSV output formats.
- Added `jsonl` and `jsonl_escaped` output formats.

Expand Down
28 changes: 28 additions & 0 deletions cli_helpers/tabular_output/tabulate_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,37 @@
with_header_hide=None,
)

tabulate._table_formats["mysql"] = tabulate.TableFormat(
lineabove=tabulate.Line("+", "-", "+", "+"),
linebelowheader=tabulate.Line("+", "-", "+", "+"),
linebetweenrows=None,
linebelow=tabulate.Line("+", "-", "+", "+"),
headerrow=tabulate.DataRow("|", "|", "|"),
datarow=tabulate.DataRow("|", "|", "|"),
padding=1,
with_header_hide=None,
)

tabulate._table_formats["mysql_unicode"] = tabulate.TableFormat(
lineabove=tabulate.Line("┌", "─", "┬", "┐"),
linebelowheader=tabulate.Line("├", "─", "┼", "┤"),
linebetweenrows=None,
linebelow=tabulate.Line("└", "─", "┴", "┘"),
headerrow=tabulate.DataRow("│", "│", "│"),
datarow=tabulate.DataRow("│", "│", "│"),
padding=1,
with_header_hide=None,
)

# "minimal" is the same as "plain", but without headers
tabulate._table_formats["minimal"] = tabulate._table_formats["plain"]

tabulate.multiline_formats["psql_unicode"] = "psql_unicode"
tabulate.multiline_formats["double"] = "double"
tabulate.multiline_formats["ascii"] = "ascii"
tabulate.multiline_formats["minimal"] = "minimal"
tabulate.multiline_formats["mysql"] = "mysql"
tabulate.multiline_formats["mysql_unicode"] = "mysql_unicode"

supported_markup_formats = (
"mediawiki",
Expand All @@ -95,13 +119,17 @@
"rst",
"github",
"double",
"mysql",
"mysql_unicode",
)

supported_formats = supported_markup_formats + supported_table_formats

default_kwargs = {
"ascii": {"numalign": "left"},
"ascii_escaped": {"numalign": "left"},
"mysql": {"numalign": "right"},
"mysql_unicode": {"numalign": "right"},
}
headless_formats = ("minimal",)

Expand Down
72 changes: 72 additions & 0 deletions tests/tabular_output/test_output_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,78 @@ def test_tabular_output_escaped():
)


def test_tabular_output_mysql():
"""Test the mysql output format."""
headers = ["text", "numeric"]
data = [
["abc", Decimal(1)],
["defg", Decimal("11.1")],
["hi", Decimal("1.1")],
["Pablo\rß\n", 0],
]
expected = dedent(
"""\
+-------+---------+
| text | numeric |
+-------+---------+
| abc | 1 |
| defg | 11.1 |
| hi | 1.1 |
| Pablo | 0 |
| ß | |
+-------+---------+"""
)

print(expected)
print(
"\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="mysql"
)
)
)
assert expected == "\n".join(
TabularOutputFormatter().format_output(iter(data), headers, format_name="mysql")
)


def test_tabular_output_mysql_unicode():
"""Test the mysql_unicode output format."""
headers = ["text", "numeric"]
data = [
["abc", Decimal(1)],
["defg", Decimal("11.1")],
["hi", Decimal("1.1")],
["Pablo\rß\n", 0],
]
expected = dedent(
"""\
┌───────┬─────────┐
│ text │ numeric │
├───────┼─────────┤
│ abc │ 1 │
│ defg │ 11.1 │
│ hi │ 1.1 │
│ Pablo │ 0 │
│ ß │ │
└───────┴─────────┘"""
)

print(expected)
print(
"\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="mysql_unicode"
)
)
)
assert expected == "\n".join(
TabularOutputFormatter().format_output(
iter(data), headers, format_name="mysql_unicode"
)
)


def test_tabular_format_output_wrapper():
"""Test the format_output wrapper."""
data = [["1", None], ["2", "Sam"], ["3", "Joe"]]
Expand Down
Loading