Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --bulk option to add task command #11

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/source/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Usage:
Options:

- ``-d``, ``--details`` Allows to add some details to the task.
- ``-b``, ``--bulk`` Open the editor and allows to add multiple tasks at once by writing one title per line. Note that you won't be able to fill details with the bulk mode.
- ``-h``, ``--help`` Command help.

Examples:
Expand All @@ -36,6 +37,15 @@ Examples:

hyf add "Do something with a lot of details" -d -

.. code-block:: bash
:caption: Add multiple tasks at once

hyf add --bulk

# In the editor write one task per line
Call John Doe
Read some articles about dogs

config
------

Expand Down
51 changes: 42 additions & 9 deletions src/hyperfocus/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,58 @@

import click

from hyperfocus.console.core.parameters import NotRequired
from hyperfocus.console.core.parameters import NotRequiredIf
from hyperfocus.services.session import get_current_session
from hyperfocus.termui import formatter
from hyperfocus.termui import printer
from hyperfocus.termui.components import SuccessNotification


@click.command(help="Add task to current working day")
@click.argument("title", metavar="<title>", type=click.STRING)
@click.argument(
"title",
cls=NotRequiredIf,
not_required_if=["bulk"],
metavar="<title>",
type=click.STRING,
)
@click.option(
"-d",
"--details",
"details",
cls=NotRequired,
not_required=["bulk"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be not_required_if ?

Copy link
Owner Author

@u8slvn u8slvn Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's two different classes, see here: parameters.py

The first one NotRequiredIf is a click.Argument class, it allows marking a command arg as "not required if" the following parameters are present. The second one NotRequired is a click.Option class, it raises an error if one of the given parameters is present with the option.

I guess it's time for me to add some docstrings!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood you well, it means that the title argument is not required when the bulk option is setup. But what if both title and bulk are presents ? Shouldn't it raise an error ?

I mean, the bulk option seems to be incompatible with both details and title since those two last arguments refer to a single task, while bulk introduces several tasks.

If indeed it is incompatible, either one should raise an error when setup along with bulk, what do you think ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already how it works.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok fine.

default="",
type=click.STRING,
help="add task details",
)
@click.option(
"-d", "--details", "details", default="", type=click.STRING, help="add task details"
"-b",
"--bulk",
"bulk",
cls=NotRequired,
not_required=["title", "details"],
is_flag=True,
help="add multiple tasks",
)
def add(title: str, details: str) -> None:
def add(title: str, details: str, bulk: bool) -> None:
session = get_current_session()

details_content = click.edit() if details == "-" else details
tasks: list[tuple[str, str | None]] = []

if bulk is True:
bulk_input = click.edit() or ""
task_titles = [line.strip() for line in bulk_input.splitlines() if line.strip()]
tasks.extend((title, "") for title in task_titles)
else:
details_content = click.edit() if details == "-" else details
tasks.append((title, details_content))

task = session.daily_tracker.create_task(title=title, details=details_content)
printer.echo(
SuccessNotification(
text=f"{formatter.task(task=task, show_prefix=True)} created",
for title, details_content in tasks:
task = session.daily_tracker.create_task(title=title, details=details_content)
printer.echo(
SuccessNotification(
text=f"{formatter.task(task=task, show_prefix=True)} created",
)
)
)
35 changes: 34 additions & 1 deletion tests/test_console/test_commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,42 @@ def test_add_with_inline_details(cli):
@pytest.mark.functional
def test_add_with_multiple_lines_details(mocker, cli):
mocker.patch("click.edit", return_value="foo\nbar")
result = runner.invoke(cli, ["add", "foo", "-d", "-"], input="bar\n")
result = runner.invoke(cli, ["add", "foo", "-d", "-"])

assert result.exit_code == 0
assert result.output == (
f"{icons.SUCCESS}(success) " f"Task: #1 {icons.TASK_STATUS} foo created\n"
)


@pytest.mark.functional
def test_add_with_bulk_option(mocker, cli):
mocker.patch("click.edit", return_value="foo\nbar")
result = runner.invoke(cli, ["add", "-b"], input="bar\n")

assert result.exit_code == 0
assert result.output == (
f"{icons.SUCCESS}(success) Task: #1 {icons.TASK_STATUS} foo created\n"
f"{icons.SUCCESS}(success) Task: #2 {icons.TASK_STATUS} bar created\n"
)


@pytest.mark.functional
def test_add_with_bulk_option_ignore_empty_lines(mocker, cli):
mocker.patch("click.edit", return_value=" \n\n \ntask n1\n \n\t\ntask n2")
result = runner.invoke(cli, ["add", "-b"])

assert result.exit_code == 0
assert result.output == (
f"{icons.SUCCESS}(success) Task: #1 {icons.TASK_STATUS} task n1 created\n"
f"{icons.SUCCESS}(success) Task: #2 {icons.TASK_STATUS} task n2 created\n"
)


@pytest.mark.functional
def test_add_with_bulk_option_does_nothing_with_empty_lines(mocker, cli):
mocker.patch("click.edit", return_value=" \n\n \n \n")
result = runner.invoke(cli, ["add", "-b"])

assert result.exit_code == 0
assert result.output == ""
Loading