From 62b47b6f081f8b22479dca79c29409cb45319e25 Mon Sep 17 00:00:00 2001 From: Roy Sommer Date: Wed, 31 Aug 2022 22:22:08 +0300 Subject: handle listing tasks when there are no tasks (#52) --- .vscode/settings.json | 3 ++- taskipy/exceptions.py | 10 ++++++++++ taskipy/help.py | 33 --------------------------------- taskipy/list.py | 37 +++++++++++++++++++++++++++++++++++++ taskipy/task_runner.py | 4 ++-- tests/test_taskipy.py | 18 ++++++++++++++++++ 6 files changed, 69 insertions(+), 36 deletions(-) delete mode 100644 taskipy/help.py create mode 100644 taskipy/list.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 8150718..6e80a92 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,6 @@ "editor.tabSize": 4, "python.linting.enabled": true, "python.linting.pylintEnabled": true, - "python.linting.mypyEnabled": true + "python.linting.mypyEnabled": true, + "python.formatting.provider": "none" } \ No newline at end of file diff --git a/taskipy/exceptions.py b/taskipy/exceptions.py index 10eccfa..affdd4e 100644 --- a/taskipy/exceptions.py +++ b/taskipy/exceptions.py @@ -81,6 +81,16 @@ class MissingTaskipyTasksSectionError(TaskipyError): ) +class EmptyTasksSectionError(TaskipyError): + exit_code = 127 + + def __str__(self): + return ( + 'no tasks found. create your first task ' + 'by adding it to your pyproject.toml file under [tool.taskipy.tasks]' + ) + + class CircularVariableError(TaskipyError): exit_code = 127 diff --git a/taskipy/help.py b/taskipy/help.py deleted file mode 100644 index 77e3645..0000000 --- a/taskipy/help.py +++ /dev/null @@ -1,33 +0,0 @@ -import shutil -import textwrap -import colorama # type: ignore -from typing import List - -from taskipy.task import Task - - -class HelpFormatter: - def __init__(self, tasks: List[Task]): - self.__tasks = tasks - - def print(self, line_width=shutil.get_terminal_size().columns): - colorama.init() - - tasks_col = [task.name for task in self.__tasks] - longest_item_in_tasks_col = len(max(tasks_col, key=len)) - - desc_col_wrap_indent = ' ' * (longest_item_in_tasks_col + 1) - desc_col_width = line_width - len(desc_col_wrap_indent) - - for task in self.__tasks: - name_text = task.name - desc_text = task.description or task.command - - tasks_col_text = f'{name_text:<{longest_item_in_tasks_col}}' - desc_col_text = '\n'.join(textwrap.wrap(desc_text, - width=desc_col_width, - subsequent_indent=desc_col_wrap_indent)) - print(f'{self.__highlight(tasks_col_text)} {desc_col_text}') - - def __highlight(self, text: str): - return f'{colorama.Fore.CYAN}{text}{colorama.Style.RESET_ALL}' diff --git a/taskipy/list.py b/taskipy/list.py new file mode 100644 index 0000000..50e95aa --- /dev/null +++ b/taskipy/list.py @@ -0,0 +1,37 @@ +import shutil +import textwrap +import colorama # type: ignore +from typing import List + +from taskipy.task import Task +from taskipy.exceptions import EmptyTasksSectionError + + +class TasksListFormatter: + def __init__(self, tasks: List[Task]): + self.__tasks = tasks + + def print(self, line_width=shutil.get_terminal_size().columns): + colorama.init() + + if not self.__tasks: + raise EmptyTasksSectionError() + + tasks_col = [task.name for task in self.__tasks] + longest_item_in_tasks_col = len(max(tasks_col, key=len)) + + desc_col_wrap_indent = ' ' * (longest_item_in_tasks_col + 1) + desc_col_width = line_width - len(desc_col_wrap_indent) + + for task in self.__tasks: + name_text = task.name + desc_text = task.description or task.command + + tasks_col_text = f'{name_text:<{longest_item_in_tasks_col}}' + desc_col_text = '\n'.join(textwrap.wrap(desc_text, + width=desc_col_width, + subsequent_indent=desc_col_wrap_indent)) + print(f'{self.__highlight(tasks_col_text)} {desc_col_text}') + + def __highlight(self, text: str): + return f'{colorama.Fore.CYAN}{text}{colorama.Style.RESET_ALL}' diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py index bf5a3f2..0a72225 100644 --- a/taskipy/task_runner.py +++ b/taskipy/task_runner.py @@ -9,7 +9,7 @@ from typing import Callable, Dict, List, Tuple, Union, Optional import psutil # type: ignore from taskipy.exceptions import CircularVariableError, TaskNotFoundError, MalformedTaskError -from taskipy.help import HelpFormatter +from taskipy.list import TasksListFormatter from taskipy.pyproject import PyProject from taskipy.task import Task from taskipy.variable import Variable @@ -28,7 +28,7 @@ class TaskRunner: def list(self): """lists tasks to stdout""" - formatter = HelpFormatter(self.__project.tasks.values()) + formatter = TasksListFormatter(self.__project.tasks.values()) formatter.print() def run(self, task_name: str, args: List[str]) -> int: diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 7b67fd3..4b9b347 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -221,6 +221,24 @@ class ListTasksTestCase(TaskipyTestCase): self.assertTerminalTextEqual(expected, stdout.strip()) self.assertEqual(exit_code, 0) + def test_running_task_list_no_tasks(self): + py_project_toml = ''' + [tool.taskipy.tasks] + ''' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('--list', cwd=cwd) + + self.assertTerminalTextEqual('no tasks found. create your first task by adding it to your pyproject.toml file under [tool.taskipy.tasks]', stdout.strip()) + self.assertEqual(exit_code, 127) + + def test_running_task_list_no_tasks_section(self): + py_project_toml = '' + cwd = self.create_test_dir_with_py_project_toml(py_project_toml) + exit_code, stdout, _ = self.run_task('--list', cwd=cwd) + + self.assertTerminalTextEqual('no tasks found. add a [tool.taskipy.tasks] section to your pyproject.toml', stdout.strip()) + self.assertEqual(exit_code, 127) + class TaskDescriptionTestCase(TaskipyTestCase): def test_running_task_with_description(self): -- cgit v1.2.3