import logging from textual.screen import ModalScreen from textual.widgets import Input, Label, Button, ListView, ListItem from textual.containers import Vertical, Horizontal, Container from textual import on, work from apis.taskwarrior import client as taskwarrior_client class CreateTaskScreen(ModalScreen): """Screen for creating a new task.""" def __init__(self, subject="", from_addr="", **kwargs): super().__init__(**kwargs) self.subject = subject self.from_addr = from_addr self.selected_project = None def compose(self): yield Container( Vertical( Label("Create Task", id="create_task_title"), Horizontal( Label("Subject:"), Input(placeholder="Task subject", value=self.subject, id="subject_input"), ), Horizontal( Label("Project:"), Input(placeholder="Project name", id="project_input"), ), Horizontal( Label("Tags:"), Input(placeholder="Comma-separated tags", id="tags_input"), ), Horizontal( Label("Due:"), Input(placeholder="Due date (e.g., today, tomorrow, fri)", id="due_input"), ), Horizontal( Label("Priority:"), Input(placeholder="Priority (H, M, L)", id="priority_input"), ), Horizontal( Button("Create", id="create_btn", variant="primary"), Button("Cancel", id="cancel_btn", variant="error"), ), id="create_task_form", ), id="create_task_container", ) def on_mount(self): self.styles.align = ("center", "middle") @on(Button.Pressed, "#create_btn") def on_create_pressed(self): """Create the task when the Create button is pressed.""" # Get input values subject = self.query_one("#subject_input").value project = self.query_one("#project_input").value tags_input = self.query_one("#tags_input").value due = self.query_one("#due_input").value priority = self.query_one("#priority_input").value # Process tags (split by commas and trim whitespace) tags = [tag.strip() for tag in tags_input.split(",")] if tags_input else [] # Add a tag for the sender, if provided if self.from_addr and "@" in self.from_addr: domain = self.from_addr.split("@")[1].split(".")[0] if domain and domain not in ["gmail", "yahoo", "hotmail", "outlook"]: tags.append(domain) # Create the task self.create_task_worker(subject, tags, project, due, priority) @on(Button.Pressed, "#cancel_btn") def on_cancel_pressed(self): """Dismiss the screen when Cancel is pressed.""" self.dismiss() @work(exclusive=True) async def create_task_worker(self, subject, tags=None, project=None, due=None, priority=None): """Worker to create a task using the Taskwarrior API client.""" if not subject: self.app.show_status("Task subject cannot be empty.", "error") return # Validate priority if priority and priority not in ["H", "M", "L"]: self.app.show_status("Priority must be H, M, or L.", "warning") priority = None # Create the task success, result = await taskwarrior_client.create_task( task_description=subject, tags=tags or [], project=project, due=due, priority=priority ) if success: self.app.show_status(f"Task created: {subject}", "success") self.dismiss() else: self.app.show_status(f"Failed to create task: {result}", "error")