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

Fix goto CLI handler #50

Merged
merged 1 commit into from
Jul 29, 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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ clean: clean_src

clean_src:
-rm -r src/__pycache__


replace:
make clean && make build && pip install .
8 changes: 2 additions & 6 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ def main():
is_ok, msg = handlers.stop_task_handler(" ".join(args["<time>"]))

elif args["goto"]:
name = " ".join(args["<newtask>"])
tid, is_ok = guess_task_id_from_string(name)
if not is_ok:
msg = f"could not get task ID from: {name}"

is_ok, msg = handlers.goto_task_handler(tid)
description = " ".join(args["<newtask>"])
is_ok, msg = handlers.goto_task_handler(description)

if args["see"]:
if args["<query>"]:
Expand Down
28 changes: 17 additions & 11 deletions src/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def autocomplete_handler():
return autocomplete()


def start_task_handler(description: str, start_str: str) -> (bool, str):
def start_task_handler(description: str, start_str: str="") -> (bool, str):
"""handles a request to start a task"""
if not description:
return False, "task description is mandatory"
Expand Down Expand Up @@ -70,15 +70,21 @@ def stop_task_handler(stop_time: str) -> (bool, str):
return True, msg


def goto_task_handler(tid: int) -> (bool, str):
def goto_task_handler(description: str) -> (bool, str):
"""handles a request to switch to another task given the ID"""
tasks = get_tasks(lambda x: x.tid == tid)
if not tasks:
return False, f"could not find task with ID {tid}"
task = tasks[0]
if not description:
return False, "task description is mandatory"

if Task.get_running():
now = datetime.now().strftime("%H:%M")
is_ok, msg = stop_task_handler(now)
if not is_ok:
return False, msg

tid, got_id = guess_task_id_from_string(description)
if got_id:
work_on(task_id=tid)
return True, ""
else:
return Task(description).start(), ""

now = datetime.now().strftime("%H:%M")
is_ok, msg = stop_task_handler(now)
if not is_ok:
return False, msg
return start_task_handler(task.name, now)