Skip to content

Commit

Permalink
view-user --parsable: improve output formatting
Browse files Browse the repository at this point in the history
Problem: Since the formatting of the columns in the output of view-user
--parsable is dependent on the length of the longest column name, the
width of every column is forced to match, which can result in some ugly
screen wrapping even in a relatively small font.

Improve the view-user --parsable formatting option by making the width
of each column dynamic based on the length of each column name instead
of based on the length of the longest column name.
  • Loading branch information
cmoussa1 committed Oct 16, 2024
1 parent f45add7 commit 9d551ec
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/bindings/python/fluxacct/accounting/user_subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,26 @@ def create_json_object(conn, user):


def get_user_rows(conn, user, headers, rows, parsable, json_fmt):
user_str = ""

if parsable is True:
# find length of longest column name
col_width = len(sorted(headers, key=len)[-1])
# fetch column names and determine width of each column
col_widths = [
max(len(str(value)) for value in [col] + [row[i] for row in rows])
for i, col in enumerate(headers)
]

for header in headers:
user_str += header.ljust(col_width)
user_str += "\n"
for row in rows:
for col in list(row):
user_str += str(col).ljust(col_width)
def format_row(row):
return " | ".join(
[f"{str(value).ljust(col_widths[i])}" for i, value in enumerate(row)]
)

return user_str
header = format_row(headers)
separator = "-+-".join(["-" * width for width in col_widths])
data_rows = "\n".join([format_row(row) for row in rows])
table = f"{header}\n{separator}\n{data_rows}"

return table

user_str = ""
if json_fmt is True:
user_str += create_json_object(conn, user)

Expand Down

0 comments on commit 9d551ec

Please sign in to comment.