diff --git a/pdm/cli/commands/run.py b/pdm/cli/commands/run.py index f3ce0ff76c..69eefdcf75 100644 --- a/pdm/cli/commands/run.py +++ b/pdm/cli/commands/run.py @@ -52,6 +52,20 @@ class Task(NamedTuple): def __str__(self) -> str: return f"" + @property + def short_description(self) -> str: + """ + A short one line task description + """ + if self.kind == "composite": + fallback = " ➤ ".join(self.args) + else: + lines = [ + line.strip() for line in str(self.args).splitlines() if line.strip() + ] + fallback = f"{lines[0]}⮨" if len(lines) > 1 else lines[0] + return self.options.get("help", fallback) + class TaskRunner: """The task runner for pdm project""" @@ -261,9 +275,9 @@ def run( def show_list(self) -> None: if not self.project.scripts: return - columns = ["Name", "Type", "Script", "Description"] + columns = ["Name", "Type", "Description"] result = [] - for name in self.project.scripts: + for name in sorted(self.project.scripts): if name == "_": continue task = self._get_task(name) @@ -272,8 +286,7 @@ def show_list(self) -> None: ( f"[green]{name}[/]", task.kind, - str(task.args), - task.options.get("help", ""), + task.short_description, ) ) self.project.core.ui.display_columns(result, columns) diff --git a/tests/cli/test_run.py b/tests/cli/test_run.py index 9bc94c5c07..c85ba34854 100644 --- a/tests/cli/test_run.py +++ b/tests/cli/test_run.py @@ -274,22 +274,34 @@ def test_run_script_override_global_env(project, invoke, capfd): def test_run_show_list_of_scripts(project, invoke): project.tool_settings["scripts"] = { + "test_composite": {"composite": ["test_cmd", "test_script", "test_shell"]}, "test_cmd": "flask db upgrade", + "test_multi": """\ + I am a multilines + command + """, "test_script": {"call": "test_script:main", "help": "call a python function"}, "test_shell": {"shell": "echo $FOO", "help": "shell command"}, } project.write_pyproject() result = invoke(["run", "--list"], obj=project) result_lines = result.output.splitlines()[3:] - assert result_lines[0][1:-1].strip() == "test_cmd │ cmd │ flask db upgrade │" + assert ( + result_lines[0][1:-1].strip() == "test_cmd │ cmd │ flask db upgrade" + ) assert ( result_lines[1][1:-1].strip() - == "test_script │ call │ test_script:main │ call a python function" + == "test_composite │ composite │ test_cmd ➤ test_script ➤ test_shell" ) assert ( result_lines[2][1:-1].strip() - == "test_shell │ shell │ echo $FOO │ shell command" + == "test_multi │ cmd │ I am a multilines⮨" + ) + assert ( + result_lines[3][1:-1].strip() + == "test_script │ call │ call a python function" ) + assert result_lines[4][1:-1].strip() == "test_shell │ shell │ shell command" def test_run_with_another_project_root(project, local_finder, invoke, capfd):