Skip to content

Commit

Permalink
CLI: Improve the formatting of verdi user list
Browse files Browse the repository at this point in the history
Uses the `tabulate` package to create a nicely formatted table as is
used in many other `verdi` commands already. The results are ordered by
the users emails.
  • Loading branch information
sphuber committed Nov 12, 2023
1 parent a43c4cd commit 806d7e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
22 changes: 14 additions & 8 deletions aiida/cmdline/commands/cmd_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`verdi user` command."""

from functools import partial

import click
Expand Down Expand Up @@ -58,15 +57,22 @@ def user_list():
"""Show a list of all users."""
from aiida.orm import User

default_user = User.collection.get_default()
table = []

for user in sorted(User.collection.all(), key=lambda user: user.email):
row = ['*' if user.is_default else '', user.email, user.first_name, user.last_name, user.institution]
if user.is_default:
table.append(list(map(echo.highlight_string, row)))
else:
table.append(row)

if default_user is None:
echo.echo_warning('no default user has been configured')
echo.echo_tabulate(table, headers=['', 'Email', 'First name', 'Last name', 'Institution'])
echo.echo('')

attributes = ['email', 'first_name', 'last_name']
sort = lambda user: user.email
highlight = lambda x: x.email == default_user.email if default_user else None
echo.echo_formatted_list(User.collection.all(), attributes, sort=sort, highlight=highlight)
if User.collection.get_default() is None:
echo.echo_warning('No default user has been configured')
else:
echo.echo_report('The user highlighted and marked with a * is the default user.')


@verdi_user.command('configure')
Expand Down
12 changes: 12 additions & 0 deletions aiida/cmdline/utils/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ class ExitCode(enum.IntEnum):
}


def highlight_string(string: str, color: str = 'highlight') -> str:
"""Highlight a string with a certain color.
Uses ``click.style`` to highlight the string.
:param string: The string to highlight.
:param color: The color to use.
:returns: The highlighted string.
"""
return click.style(string, fg=COLORS[color])


def echo(message: Any, fg: Optional[str] = None, bold: bool = False, nl: bool = True, err: bool = False) -> None:
"""Log a message to the cmdline logger.
Expand Down

0 comments on commit 806d7e2

Please sign in to comment.