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
31 changes: 15 additions & 16 deletions src/stratis_cli/_actions/_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""

import sys
from typing import Any, Callable, List, Optional
from typing import Any, Callable, Optional
from uuid import UUID

from dbus import Struct
Expand Down Expand Up @@ -71,10 +71,10 @@ def _get_column_len(column_width: int, entry_len: int, entry_width: int) -> int:

def _print_row(
file: Any,
row: Any,
row_widths: List[int],
column_widths: List[int],
column_alignments: List[str],
row: tuple[str, ...],
row_widths: tuple[int, ...],
column_widths: tuple[int, ...],
column_alignments: tuple[str, ...],
):
"""
Print a single row in a table. The row might be the header row, or
Expand All @@ -99,9 +99,9 @@ def _print_row(


def print_table(
column_headings: List[str],
row_entries: List[Any],
alignment: List[str],
column_headings: tuple[str, ...],
row_entries: list[tuple[str, ...]],
alignment: tuple[str, ...],
file=sys.stdout,
):
"""
Expand All @@ -124,18 +124,17 @@ def print_table(
all(wcswidth(i) != -1 for row in rows for item in row)
(i.e., no items to be printed contain unprintable characters)
"""
column_widths = [0] * len(column_headings)
column_widths = (0,) * len(column_headings)
cell_widths = []

# Column header isn't different than any other row, insert into rows.
row_entries.insert(0, column_headings)

for row_index, row in enumerate(row_entries):
cell_widths.append([])
for column_index, cell in enumerate(row):
cell_width = wcswidth(cell)
cell_widths[row_index].append(cell_width)
column_widths[column_index] = max(column_widths[column_index], cell_width)
for row in row_entries:
row_widths = tuple(wcswidth(cell) for cell in row)
column_widths = tuple(
max(cw, rw) for cw, rw in zip(column_widths, row_widths, strict=True)
)
cell_widths.append(row_widths)

for row, row_widths in zip(row_entries, cell_widths):
_print_row(file, row, row_widths, column_widths, alignment)
Expand Down
4 changes: 2 additions & 2 deletions src/stratis_cli/_actions/_list_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ def filesystem_size_quartet(mofs: Any) -> str:
]

print_table(
["Pool", "Filesystem", f"{TOTAL_USED_FREE} / Limit", "Device", "UUID"],
("Pool", "Filesystem", f"{TOTAL_USED_FREE} / Limit", "Device", "UUID"),
sorted(tables, key=lambda entry: (entry[0], entry[1])),
["<", "<", "<", "<", "<"],
("<", "<", "<", "<", "<"),
)


Expand Down
8 changes: 4 additions & 4 deletions src/stratis_cli/_actions/_list_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,9 @@ def alerts_str(mopool: Any, pool_object_path: str) -> str:
]

print_table(
["Name", TOTAL_USED_FREE, "Properties", "UUID", "Alerts"],
("Name", TOTAL_USED_FREE, "Properties", "UUID", "Alerts"),
sorted(tables, key=lambda entry: entry[0]),
["<", ">", ">", ">", "<"],
("<", ">", ">", ">", "<"),
)


Expand Down Expand Up @@ -885,7 +885,7 @@ def key_description_str(
]

print_table(
["Name", "Version", "UUID", "# Devices", "Key Description", "Clevis"],
("Name", "Version", "UUID", "# Devices", "Key Description", "Clevis"),
sorted(tables, key=lambda entry: entry[0]),
["<", ">", "<", ">", "<", "<"],
("<", ">", "<", ">", "<", "<"),
)
8 changes: 4 additions & 4 deletions src/stratis_cli/_actions/_physical.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ def uuid_str(modev: Any) -> str:
return TABLE_UNKNOWN_STRING

tables = [
[
(
pool_name_str(modev),
paths_str(modev),
size_str(modev),
tier_str(modev),
uuid_str(modev),
]
)
for modev in modevs
]
print_table(
["Pool Name", "Device Node", "Physical Size", "Tier", "UUID"],
("Pool Name", "Device Node", "Physical Size", "Tier", "UUID"),
sorted(tables, key=lambda entry: (entry[0], entry[1])),
["<", "<", ">", ">", "<"],
("<", "<", ">", ">", "<"),
)
4 changes: 2 additions & 2 deletions src/stratis_cli/_actions/_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ def list_keys(_: Namespace):
"""
proxy = get_object(TOP_OBJECT)

key_list = [[key_desc] for key_desc in _fetch_keylist(proxy)]
key_list = [(key_desc,) for key_desc in _fetch_keylist(proxy)]

print_table(
["Key Description"], sorted(key_list, key=lambda entry: entry[0]), ["<"]
("Key Description",), sorted(key_list, key=lambda entry: entry[0]), ("<",)
)
20 changes: 10 additions & 10 deletions tests/unit/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,42 @@ def setUp(self):
self.output = io.StringIO()

self.table = [
["Pool Na\u030ame", "Na\u030ame", "Used", "Created", "Device", "UUID"],
[
("Pool Na\u030ame", "Na\u030ame", "Used", "Created", "Device", "UUID"),
(
"unicode",
"e",
"546 MiB",
"Feb 07 2019 15:33",
"/stratis/unicode/e",
"3bf22806a6df4660aa527d646209595f",
],
[
),
(
"unicode",
"☺",
"546 MiB",
"Feb 07 2019 15:33",
"/stratis/unicode/☺",
"17101e39e72e423c90d8be5cb37c055b",
],
[
),
(
"unicodé",
"é",
"546 MiB",
"Feb 07 2019 15:33",
"/stratis/unicodé/é",
"0c2caf641dde41beb40bed6911f75c74",
],
[
),
(
"unicodé",
"漢字",
"546 MiB",
"Feb 07 2019 15:33",
"/stratis/unicodé/漢字",
"4ecacb15fb64453191d7da731c5f1601",
],
),
]
print_table(
self.table[0], self.table[1:], ["<", "<", "<", "<", "<", "<"], self.output
self.table[0], self.table[1:], ("<", "<", "<", "<", "<", "<"), self.output
)

def test_contains_equally_long_rows(self):
Expand Down
Loading