diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2023-04-22 21:20:49 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2023-04-22 21:20:49 -0400 |
| commit | a16179f2bdbb7ad86fd7a27bdb22399e1dcb761e (patch) | |
| tree | d1f02fc36865f89be6541c1dec5fc769c8838baa | |
| parent | Clear init file (diff) | |
| download | taskipy2-a16179f2bdbb7ad86fd7a27bdb22399e1dcb761e.zip | |
Add abstract and app IO
| -rwxr-xr-x | taskipy/cli.py | 14 | ||||
| -rw-r--r-- | taskipy/io.py | 12 | ||||
| -rw-r--r-- | taskipy/list.py | 17 |
3 files changed, 32 insertions, 11 deletions
diff --git a/taskipy/cli.py b/taskipy/cli.py index 204880e..e422498 100755 --- a/taskipy/cli.py +++ b/taskipy/cli.py @@ -2,8 +2,9 @@ import argparse import sys from pathlib import Path -from typing import List, Union +from typing import List, Type, Union +from taskipy import io from taskipy.exceptions import TaskipyError, InvalidUsageError from taskipy.task_runner import TaskRunner @@ -13,11 +14,16 @@ def main(): sys.exit(exit_code) -def run(args: List[str], cwd: Union[str, Path, None] = None) -> int: +def run( + args: List[str], + io: Type[io.AbstractIO] = io.AppIO(), + cwd: Union[str, Path, None] = None +) -> int: """Run the taskipy CLI programmatically. Args: args: The arguments passed to the taskipy CLI. + io: The IO to use for outputting to the console. cwd: The working directory to run the task in. If not provided, defaults to the current working directory. @@ -46,10 +52,10 @@ def run(args: List[str], cwd: Union[str, Path, None] = None) -> int: return runner.run(parsed_args.name, parsed_args.args) except TaskipyError as e: - print(e) + io.write_line(e.message) return e.exit_code except Exception as e: - print(e) + io.write_line(e.message) return 1 diff --git a/taskipy/io.py b/taskipy/io.py new file mode 100644 index 0000000..ec604c7 --- /dev/null +++ b/taskipy/io.py @@ -0,0 +1,12 @@ +import abc + +class AbstractIO(abc.ABC): + @abc.abstractmethod + def write_line(self, line: str) -> None: + pass + + +class AppIO(AbstractIO): + def write_line(self, line: str) -> None: + print(line) + diff --git a/taskipy/list.py b/taskipy/list.py index 50e95aa..00d0edd 100644 --- a/taskipy/list.py +++ b/taskipy/list.py @@ -1,21 +1,24 @@ import shutil import textwrap -import colorama # type: ignore -from typing import List +import colorama # type: ignore +from typing import List, Type, Optional +from taskipy import io from taskipy.task import Task from taskipy.exceptions import EmptyTasksSectionError class TasksListFormatter: def __init__(self, tasks: List[Task]): - self.__tasks = tasks + if not tasks: + raise EmptyTasksSectionError() - def print(self, line_width=shutil.get_terminal_size().columns): + self.__tasks = tasks colorama.init() - if not self.__tasks: - raise EmptyTasksSectionError() + def print(self, io: Type[io.AbstractIO], line_width: Optional[int] = None): + if not line_width: + line_width = shutil.get_terminal_size().columns tasks_col = [task.name for task in self.__tasks] longest_item_in_tasks_col = len(max(tasks_col, key=len)) @@ -31,7 +34,7 @@ class TasksListFormatter: 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}') + io.write_line(f'{self.__highlight(tasks_col_text)} {desc_col_text}') def __highlight(self, text: str): return f'{colorama.Fore.CYAN}{text}{colorama.Style.RESET_ALL}' |