Compare commits
83 Commits
4dbb7c5fea
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ca266ce1c | ||
|
|
86297ae350 | ||
|
|
f8a179e096 | ||
|
|
c71c506b84 | ||
|
|
b52a06f2cf | ||
|
|
efe417b41a | ||
|
|
8a121d7fec | ||
|
|
2f002081e5 | ||
|
|
09d4bc18d7 | ||
|
|
de61795476 | ||
|
|
279beeabcc | ||
|
|
16995a4465 | ||
|
|
db58cb7a2f | ||
|
|
b89f72cd28 | ||
|
|
fc5c61ddd6 | ||
|
|
55515c050e | ||
|
|
7c685f3044 | ||
|
|
040a180a17 | ||
|
|
dd6d7e645f | ||
|
|
a0057f4d83 | ||
|
|
977c8e4ee0 | ||
|
|
fa54f45998 | ||
|
|
5f3fe302f1 | ||
|
|
de96353554 | ||
|
|
7564d11931 | ||
|
|
b1cd99abf2 | ||
|
|
78ab945a4d | ||
|
|
1c1b86b96b | ||
|
|
504e0d534d | ||
|
|
2b76458de1 | ||
|
|
d6e10e3dc5 | ||
|
|
ab6e080bb4 | ||
|
|
44cfe3f714 | ||
|
|
19bc1c7832 | ||
|
|
c5202793d4 | ||
|
|
95d3098bf3 | ||
|
|
599507068a | ||
|
|
505fdbcd3d | ||
|
|
1337d84369 | ||
|
|
f1ec6c23e1 | ||
|
|
4836bda9f9 | ||
|
|
9f596b10ae | ||
|
|
98c318af04 | ||
|
|
994e545bd0 | ||
|
|
fb0af600a1 | ||
|
|
39a5efbb81 | ||
|
|
b903832d17 | ||
|
|
8233829621 | ||
|
|
36a1ea7c47 | ||
|
|
4e859613f9 | ||
|
|
b9d818ac09 | ||
|
|
ab55d0836e | ||
|
|
f5ad43323c | ||
|
|
8933dadcd0 | ||
|
|
aaabd83fc7 | ||
|
|
560bc1d3bd | ||
|
|
d4b09e5338 | ||
|
|
9a2f8ee211 | ||
|
|
5deebbbf98 | ||
|
|
807736f808 | ||
|
|
a5f7e78d8d | ||
|
|
f56f1931bf | ||
|
|
848e2a43a6 | ||
|
|
bbc53b4ce7 | ||
|
|
8be4b4785c | ||
|
|
0cd7cf6984 | ||
|
|
d3468f7395 | ||
|
|
b75c069035 | ||
|
|
3629757e70 | ||
|
|
be2f67bb7b | ||
|
|
25385c6482 | ||
|
|
3c45e2a154 | ||
|
|
a82f001918 | ||
|
|
48d2455b9c | ||
|
|
d4226caf0a | ||
|
|
a41d59e529 | ||
|
|
0ed7800575 | ||
|
|
a63aadffcb | ||
|
|
36d48c18d1 | ||
|
|
fe65183fb7 | ||
|
|
37be42884f | ||
|
|
4a21eef6f8 | ||
|
|
8244bd94c9 |
961
CALENDAR_INVITE_PLAN.md
Normal file
961
CALENDAR_INVITE_PLAN.md
Normal file
@@ -0,0 +1,961 @@
|
||||
# Calendar Invite Handling Plan
|
||||
|
||||
**Created:** 2025-12-28
|
||||
**Priority:** High
|
||||
**Focus:** Parse and display calendar invite/cancellation emails with user actions
|
||||
|
||||
---
|
||||
|
||||
## Problem Statement
|
||||
|
||||
Users receive calendar-related emails (invites, updates, cancellations) from Outlook/Exchange. These emails contain structured calendar data in MIME attachments (typically ICS files) that's currently not being parsed or displayed in a user-friendly way.
|
||||
|
||||
### Current Issues
|
||||
|
||||
1. **Raw Email Display** - Calendar emails show as raw MIME content
|
||||
2. **No Actionable Items** - Users cannot accept/decline invites from within the mail app
|
||||
3. **Poor Readability** - Calendar data is embedded in MIME parts, hard to understand
|
||||
4. **No Integration** - Actions don't synchronize with the calendar system
|
||||
|
||||
### Example Email Received
|
||||
|
||||
```
|
||||
Subject: Canceled: Technical Refinement
|
||||
From: Marshall, Cody <john.marshall@corteva.com>
|
||||
|
||||
MIME multipart message with:
|
||||
- text/plain part: "Canceled: Technical Refinement"
|
||||
- text/calendar part: base64 encoded ICS file containing:
|
||||
- method=CANCEL (indicates cancellation)
|
||||
- event details (title, date/time, organizer, attendees)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Research: Calendar/I CS File Parsing
|
||||
|
||||
### Standard Libraries
|
||||
|
||||
#### 1. **icalendar** (Recommended)
|
||||
**Repository:** https://github.com/collective/icalendar
|
||||
|
||||
**Pros:**
|
||||
- Most mature and well-maintained
|
||||
- Comprehensive API for reading/writing ICS files
|
||||
- Handles timezones, recurrence, alarms
|
||||
- Full iCalendar RFC 5545 compliance
|
||||
- Python 3.8+ support
|
||||
|
||||
**Installation:**
|
||||
```bash
|
||||
pip install icalendar
|
||||
```
|
||||
|
||||
**Basic Usage:**
|
||||
```python
|
||||
from icalendar import Calendar
|
||||
from datetime import datetime
|
||||
|
||||
# Parse ICS content
|
||||
calendar = Calendar.from_ical(ics_content)
|
||||
|
||||
for event in calendar.events:
|
||||
print(f"Summary: {event.get('summary')}")
|
||||
print(f"Start: {event.get('dtstart').dt}")
|
||||
print(f"End: {event.get('dtend').dt}")
|
||||
print(f"Location: {event.get('location')}")
|
||||
print(f"Organizer: {event.get('organizer')}")
|
||||
print(f"Method: {event.get('method')}") # REQUEST, CANCEL, etc.
|
||||
```
|
||||
|
||||
#### 2. **ics** (Alternative)
|
||||
**Repository:** https://github.com/collective/ics
|
||||
|
||||
**Pros:**
|
||||
- Simpler API than icalendar
|
||||
- Good for basic ICS parsing
|
||||
- Active maintenance
|
||||
- Lightweight
|
||||
|
||||
**Installation:**
|
||||
```bash
|
||||
pip install ics
|
||||
```
|
||||
|
||||
**Basic Usage:**
|
||||
```python
|
||||
import ics
|
||||
|
||||
calendar = ics.Calendar(ics_content)
|
||||
for event in calendar.events:
|
||||
print(event.summary)
|
||||
print(event.begin)
|
||||
print(event.end)
|
||||
print(event.location)
|
||||
```
|
||||
|
||||
#### 3. **python-recurring-ical-events**
|
||||
**Repository:** https://github.com/brotaur/recurring-ical-events
|
||||
|
||||
**Pros:**
|
||||
- Specialized for handling complex recurrence patterns
|
||||
- Good for recurring meetings
|
||||
|
||||
**Note:** More complex, use only if needed for advanced scenarios.
|
||||
|
||||
---
|
||||
|
||||
## Analysis of Calendar Invite Email Structure
|
||||
|
||||
### MIME Parts Detection
|
||||
|
||||
Calendar emails typically use `multipart/alternative` or `multipart/mixed` with these parts:
|
||||
|
||||
1. **Plain Text Part** - Human-readable message
|
||||
2. **Calendar Part** (`text/calendar` content type) - ICS file data
|
||||
3. **HTML Part** - Formatted message (optional)
|
||||
4. **Attachments** - Separate ICS files
|
||||
|
||||
### ICS File Content Structure
|
||||
|
||||
```
|
||||
BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
PRODID:-//Example Corp//Calendar App//EN
|
||||
BEGIN:VEVENT
|
||||
UID:12345@example.com
|
||||
DTSTAMP:20251228T120000Z
|
||||
DTSTART:20251228T120000Z
|
||||
DTEND:20251228T130000Z
|
||||
SUMMARY:Weekly Team Meeting
|
||||
LOCATION:Conference Room A
|
||||
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
|
||||
DESCRIPTION:Weekly team sync meeting
|
||||
ATTENDEE;CN=Jane Smith:mailto:jane.smith@example.com
|
||||
STATUS:CONFIRMED
|
||||
METHOD:REQUEST
|
||||
END:VEVENT
|
||||
END:VCALENDAR
|
||||
```
|
||||
|
||||
### Key Calendar Methods
|
||||
|
||||
The `METHOD` property indicates the type of calendar operation:
|
||||
|
||||
- **REQUEST** - Meeting invite request
|
||||
- **CANCEL** - Meeting cancellation (your email example)
|
||||
- **DECLINE** - Meeting declined
|
||||
- **ACCEPT** - Meeting accepted
|
||||
- **TENTATIVE** - Tentative acceptance
|
||||
- **COUNTER** - Counter proposal
|
||||
- **DELEGATE** - Meeting delegated
|
||||
|
||||
---
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
### Phase 1: Calendar Email Detection (Week 1)
|
||||
|
||||
#### 1.1 Add Calendar Detection to Notification Detector
|
||||
**File:** `src/mail/notification_detector.py`
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
@dataclass
|
||||
class CalendarInvite:
|
||||
"""Calendar invite/cancellation email."""
|
||||
|
||||
# Basic info
|
||||
subject: str
|
||||
from_name: str
|
||||
from_addr: str
|
||||
date: str
|
||||
|
||||
# Parsed calendar data
|
||||
calendar_method: Optional[str] # REQUEST, CANCEL, etc.
|
||||
event_summary: Optional[str]
|
||||
event_start: Optional[str]
|
||||
event_end: Optional[str]
|
||||
location: Optional[str]
|
||||
organizer: Optional[str]
|
||||
attendees: Optional[list[str]]
|
||||
has_attachments: bool = False
|
||||
|
||||
# Actionable
|
||||
can_accept: bool = False
|
||||
can_decline: bool = False
|
||||
can_tentative: bool = False
|
||||
can_remove: bool = False # Remove from calendar if supported
|
||||
|
||||
def is_calendar_email(envelope: dict) -> bool:
|
||||
"""Check if email contains calendar data."""
|
||||
subject = envelope.get("subject", "").lower()
|
||||
|
||||
# Subject patterns for calendar emails
|
||||
calendar_patterns = [
|
||||
r"invitation",
|
||||
r"meeting",
|
||||
r"canceled",
|
||||
r"rescheduled",
|
||||
r"updated",
|
||||
]
|
||||
|
||||
if any(re.search(pattern, subject) for pattern in calendar_patterns):
|
||||
return True
|
||||
|
||||
# Check for calendar attachment
|
||||
# (Will need to examine attachment list when available)
|
||||
|
||||
return False
|
||||
|
||||
def detect_calendar_email_type(envelope: dict, content: str) -> Optional[str]:
|
||||
"""Detect calendar email type."""
|
||||
# Implementation
|
||||
pass
|
||||
```
|
||||
|
||||
#### 1.2 Add ICS Parser Dependency
|
||||
**File:** `pyproject.toml`
|
||||
|
||||
**Changes:**
|
||||
```toml
|
||||
[project.optional-dependencies]
|
||||
icalendar = ">=5.0,<7.0"
|
||||
# OR
|
||||
ics = ">=0.6,<1.0"
|
||||
|
||||
[project.optional-dependencies-extras]
|
||||
icalendar = ["all"]
|
||||
```
|
||||
|
||||
**Install Command:**
|
||||
```bash
|
||||
uv pip install 'luk[icalendar]'
|
||||
# Or if using uv
|
||||
uv add --optional icalendar
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Phase 2: Calendar Content Display Widget (Week 1-2)
|
||||
|
||||
#### 2.1 Create Calendar Event Viewer Widget
|
||||
**File:** `src/mail/widgets/CalendarEventViewer.py`
|
||||
|
||||
**Design:**
|
||||
```python
|
||||
from textual.containers import Vertical, Horizontal
|
||||
from textual.widgets import Static, Button, Label
|
||||
from textual.screen import Screen
|
||||
from textual.app import ComposeResult
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
@dataclass
|
||||
class CalendarEventViewer(Screen):
|
||||
"""Widget to display calendar invite/event details."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "pop_screen", "Close", show=False),
|
||||
Binding("q", "pop_screen", "Close", show=False),
|
||||
]
|
||||
|
||||
def __init__(self, calendar_data: CalendarInvite, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.calendar_data = calendar_data
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical(id="calendar_viewer_container"):
|
||||
# Header with event type indicator
|
||||
event_type = self._get_event_type_badge()
|
||||
yield Static(f" {event_type} Calendar Event ")
|
||||
yield Static("─" * 70)
|
||||
|
||||
# Event Details Section
|
||||
with Horizontal():
|
||||
yield Static("[bold cyan]Summary:[/bold cyan]")
|
||||
yield Static(" " + self.calendar_data.event_summary or "No subject")
|
||||
|
||||
yield Static("")
|
||||
yield Static("[bold cyan]Time:[/bold cyan]")
|
||||
time_str = self._format_time_range()
|
||||
yield Static(" " + time_str)
|
||||
|
||||
if self.calendar_data.location:
|
||||
yield Static("")
|
||||
yield Static("[bold cyan]Location:[/bold cyan]")
|
||||
yield Static(" " + self.calendar_data.location)
|
||||
|
||||
if self.calendar_data.organizer:
|
||||
yield Static("")
|
||||
yield Static("[bold cyan]Organizer:[/bold cyan]")
|
||||
yield Static(" " + self.calendar_data.organizer)
|
||||
|
||||
if self.calendar_data.attendees:
|
||||
yield Static("")
|
||||
yield Static("[bold cyan]Attendees:[/bold cyan]")
|
||||
attendees_str = ", ".join(self.calendar_data.attendees[:5])
|
||||
if len(self.calendar_data.attendees) > 5:
|
||||
attendees_str += f" + {len(self.calendar_data.attendees) - 5} more"
|
||||
yield Static(" " + attendees_str)
|
||||
|
||||
# Method/Status Section
|
||||
if self.calendar_data.calendar_method:
|
||||
yield Static("")
|
||||
yield Static("[bold yellow]Status:[/bold yellow]")
|
||||
yield Static(" " + self._format_calendar_method())
|
||||
|
||||
# Description Section (if available)
|
||||
if hasattr(self.calendar_data, 'description'):
|
||||
desc = self.calendar_data.description
|
||||
if desc and len(desc) > 200:
|
||||
desc = desc[:200] + "..."
|
||||
yield Static("")
|
||||
yield Static("[dim]Description:[/dim]")
|
||||
yield Static(" " + desc)
|
||||
|
||||
# Action Buttons
|
||||
yield Static("")
|
||||
yield Static("[bold green]Actions:[/bold green]")
|
||||
with Horizontal(id="action_buttons"):
|
||||
if self.calendar_data.can_accept:
|
||||
yield Button("✓ Accept", id="btn_accept", variant="success")
|
||||
if self.calendar_data.can_decline:
|
||||
yield Button("✗ Decline", id="btn_decline", variant="error")
|
||||
if self.calendar_data.can_tentative:
|
||||
yield Button("? Tentative", id="btn_tentative", variant="warning")
|
||||
if self.calendar_data.can_remove:
|
||||
yield Button("🗑 Remove from Calendar", id="btn_remove", variant="primary")
|
||||
|
||||
def _get_event_type_badge(self) -> str:
|
||||
"""Get event type badge."""
|
||||
method = self.calendar_data.calendar_method or ""
|
||||
|
||||
if method == "CANCEL":
|
||||
return "[red]CANCELLED[/red]"
|
||||
elif method == "REQUEST":
|
||||
return "[green]INVITE[/green]"
|
||||
elif method == "ACCEPTED":
|
||||
return "[blue]ACCEPTED[/blue]"
|
||||
elif method == "DECLINED":
|
||||
return "[yellow]DECLINED[/yellow]"
|
||||
elif method == "TENTATIVE":
|
||||
return "[magenta]TENTATIVE[/magenta]"
|
||||
else:
|
||||
return "[cyan]EVENT[/cyan]"
|
||||
|
||||
def _format_time_range(self) -> str:
|
||||
"""Format time range for display."""
|
||||
if self.calendar_data.event_start and self.calendar_data.event_end:
|
||||
start = self._parse_date_time(self.calendar_data.event_start)
|
||||
end = self._parse_date_time(self.calendar_data.event_end)
|
||||
return f"{start} - {end}"
|
||||
elif self.calendar_data.event_start:
|
||||
return self._parse_date_time(self.calendar_data.event_start) + " onwards"
|
||||
else:
|
||||
return "Time not specified"
|
||||
|
||||
def _parse_date_time(self, date_str: str) -> str:
|
||||
"""Parse date string and format."""
|
||||
# Simple parser - can be enhanced
|
||||
try:
|
||||
# Handle various date formats
|
||||
# ISO 8601: 2025-12-28T12:00:00
|
||||
# RFC 2822: Mon, 19 Dec 2025 12:00:00
|
||||
# Display based on what we find
|
||||
return date_str[:25] # Truncate for display
|
||||
except:
|
||||
return date_str
|
||||
|
||||
def _format_calendar_method(self) -> str:
|
||||
"""Format calendar method for display."""
|
||||
method = self.calendar_data.calendar_method
|
||||
method_display = method.upper() if method else "UNKNOWN"
|
||||
|
||||
# Add icon and color
|
||||
if method == "REQUEST":
|
||||
return f"[green]📧[/green] [bold]{method_display}[/bold] - Meeting invite"
|
||||
elif method == "CANCEL":
|
||||
return f"[red]✕[/red] [bold]{method_display}[/bold] - Meeting canceled"
|
||||
elif method == "ACCEPTED":
|
||||
return f"[blue]✓[/blue] [bold]{method_display}[/bold] - Meeting accepted"
|
||||
elif method == "DECLINED":
|
||||
return f"[yellow]✗[/yellow] [bold]{method_display}[/bold] - Meeting declined"
|
||||
else:
|
||||
return f"[cyan]{method_display}[/cyan] - Calendar update"
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle button press."""
|
||||
if event.button.id == "btn_accept":
|
||||
self._handle_accept()
|
||||
elif event.button.id == "btn_decline":
|
||||
self._handle_decline()
|
||||
elif event.button.id == "btn_tentative":
|
||||
self._handle_tentative()
|
||||
elif event.button.id == "btn_remove":
|
||||
self._handle_remove()
|
||||
|
||||
def _handle_accept(self) -> None:
|
||||
"""Handle accept action."""
|
||||
self.dismiss("accept")
|
||||
self.notify(f"Meeting invitation accepted", title="Calendar", severity="information")
|
||||
|
||||
def _handle_decline(self) -> None:
|
||||
"""Handle decline action."""
|
||||
self.dismiss("decline")
|
||||
self.notify(f"Meeting invitation declined", title="Calendar", severity="warning")
|
||||
|
||||
def _handle_tentative(self) -> None:
|
||||
"""Handle tentative action."""
|
||||
self.dismiss("tentative")
|
||||
self.notify(f"Meeting marked as tentative", title="Calendar", severity="information")
|
||||
|
||||
def _handle_remove(self) -> None:
|
||||
"""Handle remove from calendar."""
|
||||
self.dismiss("remove")
|
||||
self.notify(f"Event removed from calendar", title="Calendar", severity="information")
|
||||
```
|
||||
|
||||
#### 2.2 Parse ICS Content from Email
|
||||
**File:** `src/mail/utils/calendar_parser.py`
|
||||
|
||||
**Implementation:**
|
||||
```python
|
||||
"""Calendar ICS file parser utilities."""
|
||||
|
||||
import base64
|
||||
from icalendar import Calendar
|
||||
from typing import Optional, List
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class ParsedCalendarEvent:
|
||||
"""Parsed calendar event from ICS file."""
|
||||
|
||||
# Core event properties
|
||||
summary: Optional[str] = None
|
||||
location: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
start: Optional[str] = None
|
||||
end: Optional[str] = None
|
||||
all_day: bool = False
|
||||
|
||||
# Calendar method
|
||||
method: Optional[str] = None # REQUEST, CANCEL, etc.
|
||||
|
||||
# Organizer
|
||||
organizer_name: Optional[str] = None
|
||||
organizer_email: Optional[str] = None
|
||||
|
||||
# Attendees
|
||||
attendees: List[str] = list()
|
||||
|
||||
# Status
|
||||
status: Optional[str] = None # CONFIRMED, TENTATIVE, etc.
|
||||
|
||||
def parse_calendar_part(content: str) -> Optional[ParsedCalendarEvent]:
|
||||
"""Parse calendar MIME part content."""
|
||||
|
||||
try:
|
||||
# Try to parse as ICS file
|
||||
calendar = Calendar.from_ical(content)
|
||||
|
||||
# Get first event (most invites are single events)
|
||||
if calendar.events:
|
||||
event = calendar.events[0]
|
||||
|
||||
# Extract organizer
|
||||
organizer = event.get("organizer")
|
||||
organizer_name = organizer.cn if organizer else None
|
||||
organizer_email = organizer.email if organizer else None
|
||||
|
||||
# Extract attendees
|
||||
attendees = []
|
||||
if event.get("attendees"):
|
||||
for attendee in event.attendees:
|
||||
email = attendee.email if attendee else None
|
||||
name = attendee.cn if attendee else None
|
||||
if email:
|
||||
attendees.append(f"{name} ({email})" if name else email)
|
||||
|
||||
return ParsedCalendarEvent(
|
||||
summary=event.get("summary"),
|
||||
location=event.get("location"),
|
||||
description=event.get("description"),
|
||||
start=str(event.get("dtstart")) if event.get("dtstart") else None,
|
||||
end=str(event.get("dtend")) if event.get("dtend") else None,
|
||||
all_day=event.get("x-google", "all-day") == "true",
|
||||
method=event.get("method"),
|
||||
organizer_name=organizer_name,
|
||||
organizer_email=organizer_email,
|
||||
attendees=attendees,
|
||||
status=event.get("status", "CONFIRMED")
|
||||
)
|
||||
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error parsing calendar ICS: {e}")
|
||||
return None
|
||||
|
||||
def parse_calendar_attachment(attachment_content: str) -> Optional[ParsedCalendarEvent]:
|
||||
"""Parse calendar file attachment."""
|
||||
# Handle base64 encoded ICS files
|
||||
try:
|
||||
decoded = base64.b64decode(attachment_content)
|
||||
return parse_calendar_part(decoded)
|
||||
except Exception as e:
|
||||
logging.error(f"Error decoding calendar attachment: {e}")
|
||||
return None
|
||||
|
||||
def is_cancelled_event(event: ParsedCalendarEvent) -> bool:
|
||||
"""Check if event is cancelled."""
|
||||
return event.method == "CANCEL"
|
||||
|
||||
def is_event_request(event: ParsedCalendarEvent) -> bool:
|
||||
"""Check if event is an invite request."""
|
||||
return event.method == "REQUEST"
|
||||
|
||||
def extract_email_from_vcard(email_str: str) -> Optional[str]:
|
||||
"""Extract email address from VCard format."""
|
||||
# VCard format: "CN=Name:MAILTO:email@example.com"
|
||||
# Simple regex to extract
|
||||
import re
|
||||
match = re.search(r"MAILTO:([^>\s]+)", email_str)
|
||||
return match.group(1) if match else None
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Phase 3: Integration with Mail App (Week 1-3)
|
||||
|
||||
#### 3.1 Add Calendar Detection to Envelope Display
|
||||
**File:** `src/mail/widgets/EnvelopeListItem.py`
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
from .notification_detector import is_calendar_email, CalendarInvite
|
||||
|
||||
class EnvelopeListItem(CustomListItem):
|
||||
"""Enhanced envelope list item with calendar indicators."""
|
||||
|
||||
def __init__(self, envelope: dict, **kwargs):
|
||||
super().__init__(envelope, **kwargs)
|
||||
self.calendar_type = self._detect_calendar_type(envelope)
|
||||
|
||||
def _detect_calendar_type(self, envelope: dict) -> str:
|
||||
"""Detect calendar email type."""
|
||||
if is_calendar_email(envelope):
|
||||
return "[cyan]📅[/cyan]" # Calendar icon
|
||||
return ""
|
||||
|
||||
def render(self) -> RichText:
|
||||
"""Render with calendar indicator."""
|
||||
from rich.text import Text
|
||||
|
||||
# Get base render from parent
|
||||
base_render = super().render()
|
||||
|
||||
# Add calendar icon if applicable
|
||||
calendar_indicator = Text.assemble(
|
||||
self.calendar_type + " ",
|
||||
style="on" if self.calendar_type else ""
|
||||
)
|
||||
|
||||
return Text.assemble(base_render, calendar_indicator)
|
||||
```
|
||||
|
||||
#### 3.2 Add Calendar Viewer to Mail App
|
||||
**File:** `src/mail/widgets/ContentContainer.py`
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class ContentContainer(ScrollableContainer):
|
||||
"""Enhanced with calendar event display support."""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.calendar_data: Optional[CalendarInvite] = None
|
||||
self.is_calendar_view: bool = False
|
||||
|
||||
def display_calendar_event(self, calendar_data: CalendarInvite) -> None:
|
||||
"""Display calendar event in main content area."""
|
||||
self.calendar_data = calendar_data
|
||||
self.is_calendar_view = True
|
||||
|
||||
# Switch to calendar viewer
|
||||
from .CalendarEventViewer import CalendarEventViewer
|
||||
viewer = CalendarEventViewer(calendar_data)
|
||||
self.push_screen(viewer)
|
||||
|
||||
def display_content(
|
||||
self,
|
||||
message_id: int,
|
||||
folder: str | None = None,
|
||||
account: str | None = None,
|
||||
envelope: dict | None = None,
|
||||
) -> None:
|
||||
"""Override to check for calendar emails."""
|
||||
if not message_id:
|
||||
return
|
||||
|
||||
self.current_message_id = message_id
|
||||
self.current_folder = folder
|
||||
self.current_account = account
|
||||
self.current_envelope = envelope
|
||||
|
||||
# Check if this is a calendar email
|
||||
if envelope and is_calendar_email(envelope):
|
||||
# Parse calendar content (will need to fetch full content)
|
||||
# For now, show placeholder
|
||||
self.content.update("Calendar invite detected - parsing...")
|
||||
self.html_content.update("Calendar invite detected - parsing...")
|
||||
```
|
||||
|
||||
#### 3.3 Add Calendar Actions to Keybindings
|
||||
**File:** `src/mail/app.py`
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
# Existing actions preserved
|
||||
# Add new calendar-specific actions
|
||||
|
||||
async def action_calendar_accept(self) -> None:
|
||||
"""Accept calendar invitation."""
|
||||
# Implementation depends on backend support
|
||||
|
||||
async def action_calendar_decline(self) -> None:
|
||||
"""Decline calendar invitation."""
|
||||
# Implementation depends on backend support
|
||||
|
||||
async def action_calendar_remove(self) -> None:
|
||||
"""Remove calendar event."""
|
||||
# Implementation depends on backend support
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Phase 4: Calendar Sync Integration (Week 2-3)
|
||||
|
||||
#### 4.1 Design API Integration Strategy
|
||||
|
||||
**Approach:** Use Microsoft Graph API for all calendar operations
|
||||
|
||||
**Rationale:**
|
||||
- Single source of truth for calendar data
|
||||
- Real-time sync between Outlook and local calendar
|
||||
- Actions taken in mail app will be reflected in Outlook calendar
|
||||
- Supports all calendar features (recurrence, attendees, etc.)
|
||||
- Cancellations will update the actual event in Outlook
|
||||
|
||||
**Key Decision:** Before implementing calendar actions, we should call Microsoft Graph API to:
|
||||
1. Accept meeting → Update event status to ACCEPTED
|
||||
2. Decline meeting → Update event status to DECLINED
|
||||
3. Tentatively accept → Update event status to TENTATIVE
|
||||
4. Cancel meeting → Send cancellation to organizer, update event status
|
||||
|
||||
**Files to Modify:**
|
||||
- `src/services/microsoft_graph/calendar.py` - Add action methods
|
||||
- `src/mail/actions/calendar_actions.py` - Create action handlers
|
||||
- `src/mail/app.py` - Add calendar action keybindings
|
||||
|
||||
**API Calls Needed:**
|
||||
```python
|
||||
# Accept invitation
|
||||
PATCH /me/events/{id}
|
||||
{
|
||||
"response": {
|
||||
"response": "accepted",
|
||||
"comment": "Accepted via LUK Mail app"
|
||||
}
|
||||
}
|
||||
|
||||
# Decline invitation
|
||||
PATCH /me/events/{id}
|
||||
{
|
||||
"response": {
|
||||
"response": "declined",
|
||||
"comment": "Declined via LUK Mail app"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Phase 5: Testing & Documentation (Week 3)
|
||||
|
||||
#### 5.1 Unit Tests for Calendar Parsing
|
||||
**File:** `tests/test_calendar_parser.py`
|
||||
|
||||
**Test Cases:**
|
||||
```python
|
||||
import pytest
|
||||
from src.mail.utils.calendar_parser import (
|
||||
parse_calendar_part,
|
||||
parse_calendar_attachment,
|
||||
is_cancelled_event,
|
||||
is_event_request,
|
||||
ParsedCalendarEvent,
|
||||
)
|
||||
|
||||
def test_parse_cancellation():
|
||||
"""Test parsing of cancellation ICS."""
|
||||
ics_content = """
|
||||
BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
BEGIN:VEVENT
|
||||
UID:test-cancel@example.com
|
||||
DTSTAMP:20251228T120000Z
|
||||
DTSTART:20251228T120000Z
|
||||
DTEND:20251228T130000Z
|
||||
SUMMARY:Canceled Meeting
|
||||
LOCATION:Conference Room A
|
||||
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
|
||||
METHOD:CANCEL
|
||||
STATUS:CANCELLED
|
||||
END:VEVENT
|
||||
END:VCALENDAR
|
||||
"""
|
||||
|
||||
event = parse_calendar_part(ics_content)
|
||||
assert event is not None
|
||||
assert is_cancelled_event(event)
|
||||
assert event.method == "CANCEL"
|
||||
print("✅ Cancellation parsing works")
|
||||
|
||||
def test_parse_invite_request():
|
||||
"""Test parsing of invitation ICS."""
|
||||
ics_content = """
|
||||
BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
BEGIN:VEVENT
|
||||
UID:test-invite@example.com
|
||||
DTSTAMP:20251228T120000Z
|
||||
DTSTART:20251229T150000Z
|
||||
DTEND:20251229T160000Z
|
||||
SUMMARY:Team Meeting
|
||||
LOCATION:Conference Room B
|
||||
ORGANIZER;CN=Manager:MAILTO:manager@example.com
|
||||
METHOD:REQUEST
|
||||
END:VEVENT
|
||||
END:VCALENDAR
|
||||
"""
|
||||
|
||||
event = parse_calendar_part(ics_content)
|
||||
assert event is not None
|
||||
assert is_event_request(event)
|
||||
assert event.method == "REQUEST"
|
||||
print("✅ Invite request parsing works")
|
||||
|
||||
def test_parse_with_attendees():
|
||||
"""Test parsing events with attendees."""
|
||||
# Implementation...
|
||||
pass
|
||||
```
|
||||
|
||||
#### 5.2 Update Help Screen
|
||||
**File:** `src/mail/screens/HelpScreen.py`
|
||||
|
||||
**Additions:**
|
||||
```python
|
||||
# Add to Quick Actions section:
|
||||
yield Static(" [yellow]Calendar:[/yellow]")
|
||||
yield Static(" • Calendar invites automatically detected")
|
||||
yield Static(" • ICS files parsed to show event details")
|
||||
yield Static(" • Accept/Decline/Remove actions for invites")
|
||||
yield Static(" • Actions sync with Microsoft Outlook via Graph API")
|
||||
yield Static("")
|
||||
```
|
||||
|
||||
#### 5.3 Update Configuration
|
||||
**File:** `src/mail/config.py`
|
||||
|
||||
**Additions:**
|
||||
```python
|
||||
class MailAppConfig(BaseModel):
|
||||
# ... existing fields ...
|
||||
|
||||
# Calendar settings
|
||||
calendar_parser_library: Literal["icalendar", "ics"] = "icalendar"
|
||||
auto_detect_calendar_emails: bool = True
|
||||
show_calendar_indicator_in_list: bool = True
|
||||
enable_calendar_actions: bool = False # When Graph API integration ready
|
||||
```
|
||||
|
||||
**Config File Example:**
|
||||
```toml
|
||||
[calendar]
|
||||
# Which ICS library to use (icalendar recommended)
|
||||
parser_library = "icalendar"
|
||||
|
||||
# Automatically detect and highlight calendar emails
|
||||
auto_detect_calendar = true
|
||||
|
||||
# Show calendar icon in message list
|
||||
show_calendar_indicator = true
|
||||
|
||||
# Calendar action integration (requires Microsoft Graph API)
|
||||
enable_calendar_actions = false
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
### Week 1: Foundation
|
||||
1. ✅ Add calendar detection to notification_detector.py
|
||||
2. ✅ Add icalendar dependency to pyproject.toml
|
||||
3. ✅ Create calendar_parser.py with ICS parsing utilities
|
||||
4. ✅ Create CalendarEventViewer widget
|
||||
5. ✅ Add calendar detection to EnvelopeListItem
|
||||
6. ✅ Add calendar viewer to ContentContainer
|
||||
7. ✅ Add calendar action placeholders in app.py
|
||||
8. ✅ Create calendar action handlers
|
||||
9. ✅ Create proper ICS test fixture (calendar invite)
|
||||
10. ✅ Update help screen documentation
|
||||
11. ✅ Add configuration options
|
||||
|
||||
### Week 2: Mail App Integration
|
||||
1. ✅ Integrate calendar detection in EnvelopeListItem
|
||||
2. ✅ Add calendar viewer to ContentContainer
|
||||
3. ✅ Add calendar action placeholders in app.py
|
||||
4. ✅ Add unit tests for calendar parsing
|
||||
|
||||
### Week 3: Advanced Features
|
||||
1. ✅ Implement Microsoft Graph API calendar actions
|
||||
2. ⏳ Test with real calendar invites
|
||||
3. ⏳ Document calendar features in help
|
||||
|
||||
### Week 4: Calendar Sync Integration
|
||||
1. ⏳ Calendar invite acceptance (Graph API)
|
||||
2. ⏳ Calendar invite declination (Graph API)
|
||||
3. ⏳ Calendar event removal (Graph API)
|
||||
|
||||
---
|
||||
|
||||
## Success Metrics
|
||||
|
||||
### User Experience Goals
|
||||
- **Calendar Detection:** 95%+ accuracy for invite/cancellation emails
|
||||
- **ICS Parsing:** 100% RFC 5545 compliance with icalendar
|
||||
- **Display Quality:** Clear, readable calendar event details
|
||||
- **Actionable:** Accept/Decline/Tentative/Remove buttons (ready for Graph API integration)
|
||||
- **Performance:** Parse ICS files in <100ms
|
||||
|
||||
### Technical Metrics
|
||||
- **Library Coverage:** icalendar (mature, RFC 5545 compliant)
|
||||
- **Code Quality:** Type-safe with dataclasses, full error handling
|
||||
- **Test Coverage:** >80% for calendar parsing code
|
||||
- **Configuration:** Flexible parser library selection, toggleable features
|
||||
|
||||
---
|
||||
|
||||
## Configuration Options
|
||||
|
||||
### Parser Library
|
||||
```toml
|
||||
[calendar]
|
||||
parser_library = "icalendar" # or "ics"
|
||||
auto_detect_calendar = true
|
||||
```
|
||||
|
||||
### Display Options
|
||||
```toml
|
||||
[envelope_display]
|
||||
show_calendar_icon = true
|
||||
```
|
||||
|
||||
### Action Configuration
|
||||
```toml
|
||||
[calendar_actions]
|
||||
# When true, actions call Microsoft Graph API
|
||||
enable_graph_api_actions = false
|
||||
|
||||
# User preferences
|
||||
default_response = "accept" # accept, decline, tentative
|
||||
auto_decline_duplicates = true
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Notes & Considerations
|
||||
|
||||
### Important Design Decisions
|
||||
|
||||
1. **Library Choice:** `icalendar` is recommended over `ics` for:
|
||||
- Better RFC compliance
|
||||
- More features (recurrence, timezones)
|
||||
- Better error handling
|
||||
- Active maintenance
|
||||
|
||||
2. **Display Priority:** Calendar events should be displayed prominently:
|
||||
- Use `push_screen()` to show full event details
|
||||
- Show in dedicated viewer, not inline in message list
|
||||
- Provide clear visual distinction for different event types (invite vs cancellation)
|
||||
|
||||
3. **Action Strategy:**
|
||||
- Implement Graph API integration first before enabling actions
|
||||
- Use Graph API as single source of truth for calendar
|
||||
- Actions in mail app should trigger Graph API calls to update Outlook
|
||||
- This prevents sync conflicts and ensures consistency
|
||||
|
||||
4. **Error Handling:**
|
||||
- Gracefully handle malformed ICS files
|
||||
- Provide user feedback when parsing fails
|
||||
- Fall back to raw email display if parsing fails
|
||||
|
||||
5. **Performance:**
|
||||
- Parse ICS files on-demand (not in message list rendering)
|
||||
- Use caching for parsed calendar data
|
||||
- Consider lazy loading for large mailboxes with many calendar emails
|
||||
|
||||
### Future Enhancements
|
||||
|
||||
- **Recurring Events:** Full support for recurring meetings
|
||||
- **Multiple Events:** Handle ICS files with multiple events
|
||||
- **Timezone Support:** Proper timezone handling for events
|
||||
- **Attachments:** Process calendar file attachments
|
||||
- **Proposed Times:** Handle proposed meeting times
|
||||
- **Updates:** Process event updates (time/location changes)
|
||||
- **Decline with Note:** Add optional note when declining
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
### iCalendar Standard (RFC 5545)
|
||||
- https://datatracker.ietf.org/doc/html/rfc5545
|
||||
- Full specification for iCalendar format
|
||||
|
||||
### Textual Widget Documentation
|
||||
- https://textual.textualize.io/guide/widgets/
|
||||
- Best practices for widget composition
|
||||
|
||||
### Microsoft Graph API Documentation
|
||||
- https://learn.microsoft.com/en-us/graph/api/calendar/
|
||||
- Calendar REST API reference
|
||||
|
||||
### Testing Resources
|
||||
- Sample ICS files for testing various scenarios
|
||||
- Calendar test fixtures for different event types
|
||||
|
||||
---
|
||||
|
||||
## Timeline Summary
|
||||
|
||||
**Week 1:** Foundation & Detection
|
||||
**Week 2:** Mail App Integration & Display
|
||||
**Week 3:** Advanced Features & Actions
|
||||
**Week 4:** Calendar Sync Integration (future)
|
||||
|
||||
**Total Estimated Time:** 4-6 weeks for full implementation
|
||||
|
||||
**Deliverable:** A production-ready calendar invite handling system that:
|
||||
- Detects calendar emails automatically
|
||||
- Parses ICS calendar data
|
||||
- Displays events beautifully in TUI
|
||||
- Provides user actions (accept/decline/tentative/remove)
|
||||
- Integrates with Microsoft Graph API for calendar management
|
||||
808
PERFORMANCE_OPTIMIZATION_PLAN.md
Normal file
808
PERFORMANCE_OPTIMIZATION_PLAN.md
Normal file
@@ -0,0 +1,808 @@
|
||||
# LUK Performance Optimization & Cleanup Plan
|
||||
|
||||
**Created:** 2025-12-28
|
||||
**Priority:** High
|
||||
**Focus:** Mail app performance optimization and code quality improvements
|
||||
|
||||
---
|
||||
|
||||
## Problem Statement
|
||||
|
||||
The LUK mail app is experiencing performance issues:
|
||||
- **Slow rendering** when scrolling through messages
|
||||
- **Laggy navigation** between messages
|
||||
- **High memory usage** during extended use
|
||||
- **Flickering** or unresponsive UI
|
||||
- **Poor startup time**
|
||||
|
||||
These issues make the app difficult to use for daily email management.
|
||||
|
||||
---
|
||||
|
||||
## Research: Textual Best Practices
|
||||
|
||||
### Key Principles for High-Performance Textual Apps
|
||||
|
||||
#### 1. **Use `compose()` Method, Not Manual Mounting**
|
||||
```python
|
||||
# ❌ BAD: Manual mounting in on_mount()
|
||||
def on_mount(self) -> None:
|
||||
self.mount(Header())
|
||||
self.mount(Sidebar())
|
||||
self.mount(Content())
|
||||
self.mount(Footer())
|
||||
|
||||
# ✅ GOOD: Use compose() for declarative UI
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header()
|
||||
with Horizontal():
|
||||
yield Sidebar()
|
||||
yield Content()
|
||||
yield Footer()
|
||||
```
|
||||
|
||||
**Why:** `compose()` is called once and builds the widget tree efficiently. Manual mounting triggers multiple render cycles.
|
||||
|
||||
#### 2. **Lazy Load Content - Defer Until Needed**
|
||||
```python
|
||||
# ❌ BAD: Load everything at startup
|
||||
class MailApp(App):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.all_envelopes = load_all_envelopes() # Expensive!
|
||||
self.message_store = build_full_message_store() # Expensive!
|
||||
|
||||
# ✅ GOOD: Load on-demand with workers
|
||||
class MailApp(App):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._envelopes_cache = []
|
||||
self._loading = False
|
||||
|
||||
@work(exclusive=True)
|
||||
async def load_envelopes_lazy(self):
|
||||
if not self._envelopes_cache:
|
||||
envelopes = await fetch_envelopes() # Load in background
|
||||
self._envelopes_cache = envelopes
|
||||
self._update_list()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.load_envelopes_lazy()
|
||||
```
|
||||
|
||||
**Why:** Defers expensive operations until the app is ready and visible.
|
||||
|
||||
#### 3. **Use Reactive Properties Efficiently**
|
||||
```python
|
||||
# ❌ BAD: Re-compute values in methods
|
||||
def action_next(self):
|
||||
index = self.envelopes.index(self.current_envelope)
|
||||
self.current_message_index = index + 1 # Triggers re-render
|
||||
self.update_envelope_list_view() # Another re-render
|
||||
|
||||
# ✅ GOOD: Use reactive for automatic UI updates
|
||||
current_message_index: reactive[int] = reactive(-1)
|
||||
|
||||
@reactive_var.on_change
|
||||
def action_next(self):
|
||||
# Automatically triggers minimal re-render
|
||||
self.current_message_index += 1
|
||||
```
|
||||
|
||||
**Why:** Textual's reactive system only updates changed widgets, not the entire app.
|
||||
|
||||
#### 4. **Avoid String Concatenation in Loops for Updates**
|
||||
```python
|
||||
# ❌ BAD: Creates new strings every time
|
||||
def update_status(self):
|
||||
text = "Status: "
|
||||
for i, item in enumerate(items):
|
||||
text += f"{i+1}. {item.name}\n" # O(n²) string operations
|
||||
self.status.update(text)
|
||||
|
||||
# ✅ GOOD: Build list once
|
||||
def update_status(self):
|
||||
lines = [f"{i+1}. {item.name}" for i, item in enumerate(items)]
|
||||
text = "\n".join(lines) # O(n) operations
|
||||
self.status.update(text)
|
||||
```
|
||||
|
||||
**Why:** String concatenation is O(n²), while join is O(n).
|
||||
|
||||
#### 5. **Use Efficient List Widgets**
|
||||
```python
|
||||
# ❌ BAD: Creating custom widget for each item
|
||||
from textual.widgets import Static
|
||||
|
||||
def create_mail_list(items):
|
||||
for item in items:
|
||||
yield Static(item.subject) # N widgets = N render cycles
|
||||
|
||||
# ✅ GOOD: Use ListView with data binding
|
||||
from textual.widgets import ListView, ListItem
|
||||
|
||||
class MailApp(App):
|
||||
def compose(self) -> ComposeResult:
|
||||
yield ListView(id="envelopes_list")
|
||||
|
||||
def update_list(self, items: list):
|
||||
list_view = self.query_one("#envelopes_list", ListView)
|
||||
list_view.clear()
|
||||
list_view.extend([ListItem(item.subject) for item in items]) # Efficient
|
||||
```
|
||||
|
||||
**Why:** `ListView` is optimized for lists with virtualization and pooling.
|
||||
|
||||
#### 6. **Debounce Expensive Operations**
|
||||
```python
|
||||
from textual.timer import Timer
|
||||
|
||||
# ❌ BAD: Update on every keypress
|
||||
def action_search(self, query: str):
|
||||
results = self.search_messages(query) # Expensive
|
||||
self.update_results(results)
|
||||
|
||||
# ✅ GOOD: Debounce search
|
||||
class MailApp(App):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._search_debounce = None
|
||||
|
||||
def action_search(self, query: str):
|
||||
if self._search_debounce:
|
||||
self._search_debounce.stop() # Cancel pending search
|
||||
self._search_debounce = Timer(
|
||||
0.3, # Wait 300ms
|
||||
self._do_search,
|
||||
query
|
||||
).start()
|
||||
|
||||
def _do_search(self, query: str) -> None:
|
||||
results = self.search_messages(query)
|
||||
self.update_results(results)
|
||||
```
|
||||
|
||||
**Why:** Avoids expensive recomputations for rapid user input.
|
||||
|
||||
#### 7. **Use `work()` Decorator for Background Tasks**
|
||||
```python
|
||||
from textual import work
|
||||
|
||||
class MailApp(App):
|
||||
@work(exclusive=True)
|
||||
async def load_message_content(self, message_id: int):
|
||||
"""Load message content without blocking UI."""
|
||||
content = await himalaya_client.get_message_content(message_id)
|
||||
self._update_content_display(content)
|
||||
```
|
||||
|
||||
**Why:** Background workers don't block the UI thread.
|
||||
|
||||
---
|
||||
|
||||
## Mail App Performance Issues Analysis
|
||||
|
||||
### Current Implementation Problems
|
||||
|
||||
#### 1. **Message List Rendering** (src/mail/app.py)
|
||||
```python
|
||||
# PROBLEM: Rebuilding entire list on navigation
|
||||
def action_next(self) -> None:
|
||||
if not self.current_message_index >= 0:
|
||||
return
|
||||
|
||||
next_id, next_idx = self.message_store.find_next_valid_id(
|
||||
self.current_message_index
|
||||
)
|
||||
if next_id is not None and next_idx is not None:
|
||||
self.current_message_id = next_id
|
||||
self.current_message_index = next_idx
|
||||
self._update_envelope_list_view() # ❌ Rebuilds entire list
|
||||
```
|
||||
|
||||
**Issue:** `_update_envelope_list_view()` rebuilds the entire message list on every navigation.
|
||||
|
||||
#### 2. **Envelope List Item Creation** (src/mail/widgets/EnvelopeListItem.py)
|
||||
```python
|
||||
# PROBLEM: Creating many widgets
|
||||
class EnvelopeListItem(CustomListItem):
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Static(self._from_display, classes="from")
|
||||
yield Static(self._subject_display, classes="subject")
|
||||
yield Static(self._date_display, classes="date")
|
||||
# ❌ Each item creates 4+ Static widgets
|
||||
```
|
||||
|
||||
**Issue:** For 100 emails, this creates 400+ widgets. Should use a single widget.
|
||||
|
||||
#### 3. **Message Content Loading** (src/mail/widgets/ContentContainer.py)
|
||||
```python
|
||||
# PROBLEM: Blocking UI during content fetch
|
||||
def display_content(self, message_id: int):
|
||||
# ... loading logic
|
||||
format_type = "text" if self.current_mode == "markdown" else "html"
|
||||
self.content_worker = self.fetch_message_content(message_id, format_type)
|
||||
```
|
||||
|
||||
**Issue:** Content fetch may block UI. Should use `@work` decorator.
|
||||
|
||||
#### 4. **Envelope List Updates** (src/mail/app.py lines 920-950)
|
||||
```python
|
||||
# PROBLEM: Complex envelope list rebuilding
|
||||
def _update_envelope_list_view(self) -> None:
|
||||
grouped_envelopes = []
|
||||
for i, envelope in enumerate(self.message_store.envelopes):
|
||||
# ❌ Processing every envelope on every update
|
||||
if envelope.get("type") == "header":
|
||||
grouped_envelopes.append({"type": "header", "label": ...})
|
||||
else:
|
||||
# Complex formatting
|
||||
grouped_envelopes.append({...})
|
||||
|
||||
# ❌ Clearing and rebuilding entire list
|
||||
envelopes_list = self.query_one("#envelopes_list", ListView)
|
||||
envelopes_list.clear()
|
||||
envelopes_list.extend([...])
|
||||
```
|
||||
|
||||
**Issue:** Rebuilding entire list is expensive. Should only update changed items.
|
||||
|
||||
#### 5. **Folder/Account Count Updates** (src/mail/app.py)
|
||||
```python
|
||||
# PROBLEM: Re-computing counts on every change
|
||||
def _update_folder_list_view(self) -> None:
|
||||
for folder in self.folders:
|
||||
count = len([e for e in self.envelopes if e.get("folder") == folder]) # ❌ O(n) scan
|
||||
```
|
||||
|
||||
**Issue:** Counting all envelopes for each folder is expensive. Should cache counts.
|
||||
|
||||
---
|
||||
|
||||
## Optimization Plan
|
||||
|
||||
### Phase 1: Critical Performance Fixes (Week 1)
|
||||
|
||||
#### 1.1 Convert to `compose()` Pattern
|
||||
**File:** `src/mail/app.py`
|
||||
|
||||
**Current:** Manual widget mounting in `on_mount()` and other methods
|
||||
**Goal:** Use `compose()` for declarative UI building
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
# Before (BAD):
|
||||
def on_mount(self) -> None:
|
||||
# ... manual mounting
|
||||
|
||||
# After (GOOD):
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical(id="app_container"):
|
||||
with Horizontal():
|
||||
# Left panel
|
||||
with Vertical(id="left_panel"):
|
||||
yield Static("Accounts", id="accounts_header")
|
||||
yield ListView(id="accounts_list")
|
||||
|
||||
yield Static("Folders", id="folders_header")
|
||||
yield ListView(id="folders_list")
|
||||
|
||||
# Middle panel
|
||||
with Vertical(id="middle_panel"):
|
||||
yield Static("Messages", id="messages_header")
|
||||
yield ListView(id="envelopes_list")
|
||||
|
||||
# Right panel
|
||||
yield ContentContainer(id="content_container")
|
||||
```
|
||||
|
||||
**Expected Impact:** 30-50% faster startup, reduced memory usage
|
||||
|
||||
#### 1.2 Implement Lazy Loading for Envelopes
|
||||
**File:** `src/mail/app.py`
|
||||
|
||||
**Current:** Load all envelopes at startup
|
||||
**Goal:** Load envelopes on-demand using background workers
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class MailApp(App):
|
||||
envelopes_loaded: reactive[bool] = reactive(False)
|
||||
_envelopes_cache: list[dict] = []
|
||||
|
||||
def on_mount(self) -> None:
|
||||
# Start background loading
|
||||
self._load_initial_envelopes()
|
||||
|
||||
@work(exclusive=True, group="envelope_loading")
|
||||
async def _load_initial_envelopes(self):
|
||||
"""Load initial envelopes in background."""
|
||||
envelopes, success = await himalaya_client.list_envelopes()
|
||||
if success:
|
||||
self._envelopes_cache = envelopes
|
||||
self.envelopes_loaded = True
|
||||
self._update_envelope_list_view()
|
||||
|
||||
def _load_more_envelopes(self) -> None:
|
||||
"""Load more envelopes when scrolling."""
|
||||
pass # Implement lazy loading
|
||||
```
|
||||
|
||||
**Expected Impact:** 60-70% faster startup, perceived instant UI
|
||||
|
||||
#### 1.3 Optimize Message List Updates
|
||||
**File:** `src/mail/app.py`
|
||||
|
||||
**Current:** Rebuild entire list on navigation
|
||||
**Goal:** Only update changed items, use reactive properties
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class MailApp(App):
|
||||
current_message_index: reactive[int] = reactive(-1)
|
||||
|
||||
def action_next(self) -> None:
|
||||
"""Move to next message efficiently."""
|
||||
if not self.current_message_index >= 0:
|
||||
return
|
||||
|
||||
next_id, next_idx = self.message_store.find_next_valid_id(
|
||||
self.current_message_index
|
||||
)
|
||||
|
||||
if next_id is not None:
|
||||
# ✅ Only update reactive property
|
||||
self.current_message_index = next_idx
|
||||
# ✅ Let Textual handle the update
|
||||
# DON'T call _update_envelope_list_view()
|
||||
```
|
||||
|
||||
**Expected Impact:** 80-90% faster navigation, no UI flicker
|
||||
|
||||
#### 1.4 Use Background Workers for Content Loading
|
||||
**File:** `src/mail/widgets/ContentContainer.py`
|
||||
|
||||
**Current:** Blocking content fetch
|
||||
**Goal:** Use `@work` decorator for non-blocking loads
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class ContentContainer(ScrollableContainer):
|
||||
@work(exclusive=True)
|
||||
async def fetch_message_content(self, message_id: int, format_type: str) -> None:
|
||||
"""Fetch message content in background without blocking UI."""
|
||||
content, success = await himalaya_client.get_message_content(
|
||||
message_id,
|
||||
folder=self.current_folder,
|
||||
account=self.current_account
|
||||
)
|
||||
|
||||
if success and content:
|
||||
self._update_content(content)
|
||||
else:
|
||||
self.notify("Failed to fetch message content")
|
||||
```
|
||||
|
||||
**Expected Impact:** No UI blocking, smooth content transitions
|
||||
|
||||
---
|
||||
|
||||
### Phase 2: Code Quality & Architecture (Week 2)
|
||||
|
||||
#### 2.1 Refactor Message Store for Efficiency
|
||||
**File:** `src/mail/message_store.py`
|
||||
|
||||
**Current:** Linear searches, no caching
|
||||
**Goal:** Implement indexed lookups, cache counts
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class MessageStore:
|
||||
"""Optimized message store with caching."""
|
||||
|
||||
def __init__(self, envelopes: list[dict]):
|
||||
self.envelopes = envelopes
|
||||
self._index_cache = {} # O(1) lookup cache
|
||||
self._folder_counts = {} # Cached folder counts
|
||||
self._unread_counts = {} # Cached unread counts
|
||||
|
||||
# Build caches
|
||||
self._build_caches()
|
||||
|
||||
def _build_caches(self) -> None:
|
||||
"""Build lookup caches."""
|
||||
for idx, envelope in enumerate(self.envelopes):
|
||||
self._index_cache[envelope["id"]] = idx
|
||||
folder = envelope.get("folder", "INBOX")
|
||||
self._folder_counts[folder] = self._folder_counts.get(folder, 0) + 1
|
||||
if not envelope.get("flags", {}).get("seen", False):
|
||||
self._unread_counts[folder] = self._unread_counts.get(folder, 0) + 1
|
||||
|
||||
def get_index(self, message_id: int) -> int | None:
|
||||
"""Get envelope index in O(1)."""
|
||||
return self._index_cache.get(message_id)
|
||||
|
||||
def get_folder_count(self, folder: str) -> int:
|
||||
"""Get folder count in O(1)."""
|
||||
return self._folder_counts.get(folder, 0)
|
||||
|
||||
def get_unread_count(self, folder: str) -> int:
|
||||
"""Get unread count in O(1)."""
|
||||
return self._unread_counts.get(folder, 0)
|
||||
```
|
||||
|
||||
**Expected Impact:** O(1) lookups instead of O(n), instant count retrieval
|
||||
|
||||
#### 2.2 Consolidate Envelope List Item
|
||||
**File:** `src/mail/widgets/EnvelopeListItem.py`
|
||||
|
||||
**Current:** Multiple widgets per item
|
||||
**Goal:** Use single widget with custom rendering
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class EnvelopeListItem(CustomListItem):
|
||||
"""Optimized envelope list item using single widget."""
|
||||
|
||||
def __init__(self, envelope: dict, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.envelope = envelope
|
||||
|
||||
def render(self) -> RichText:
|
||||
"""Render as single RichText widget."""
|
||||
from rich.text import Text, Text as RichText
|
||||
|
||||
# Build RichText once (more efficient than multiple widgets)
|
||||
text = Text.assemble(
|
||||
self._from_display,
|
||||
" ",
|
||||
self._subject_display,
|
||||
" ",
|
||||
self._date_display,
|
||||
style="on" if self.envelope.get("flags", {}).get("seen") else "bold"
|
||||
)
|
||||
|
||||
return text
|
||||
```
|
||||
|
||||
**Expected Impact:** 70% reduction in widget count, faster rendering
|
||||
|
||||
#### 2.3 Add Memoization for Expensive Operations
|
||||
**File:** `src/mail/utils.py`
|
||||
|
||||
**Current:** Re-computing values
|
||||
**Goal:** Cache computed values
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
from functools import lru_cache
|
||||
|
||||
@lru_cache(maxsize=128)
|
||||
def format_sender_name(envelope: dict) -> str:
|
||||
"""Format sender name with caching."""
|
||||
from_name = envelope.get("from", {}).get("name", "")
|
||||
from_addr = envelope.get("from", {}).get("addr", "")
|
||||
|
||||
if not from_name:
|
||||
return from_addr
|
||||
|
||||
# Truncate if too long
|
||||
if len(from_name) > 25:
|
||||
return from_name[:22] + "..."
|
||||
|
||||
return from_name
|
||||
|
||||
@lru_cache(maxsize=128)
|
||||
def format_date(date_str: str) -> str:
|
||||
"""Format date with caching."""
|
||||
# Parse and format date string
|
||||
# Implementation...
|
||||
return formatted_date
|
||||
```
|
||||
|
||||
**Expected Impact:** Faster repeated operations, reduced CPU usage
|
||||
|
||||
#### 2.4 Add Notification Compression Caching
|
||||
**File:** `src/mail/notification_compressor.py`
|
||||
|
||||
**Current:** Re-compressing on every view
|
||||
**Goal:** Cache compressed results
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class NotificationCompressor:
|
||||
"""Compressor with caching for performance."""
|
||||
|
||||
def __init__(self, mode: str = "summary"):
|
||||
self.mode = mode
|
||||
self._compression_cache = {} # Cache compressed content
|
||||
|
||||
def compress(
|
||||
self,
|
||||
content: str,
|
||||
envelope: dict[str, Any]
|
||||
) -> tuple[str, NotificationType | None]:
|
||||
"""Compress with caching."""
|
||||
cache_key = f"{envelope['id']}:{self.mode}"
|
||||
|
||||
# Check cache
|
||||
if cache_key in self._compression_cache:
|
||||
return self._compression_cache[cache_key]
|
||||
|
||||
# Compress and cache
|
||||
compressed, notif_type = self._compress_impl(content, envelope)
|
||||
self._compression_cache[cache_key] = (compressed, notif_type)
|
||||
|
||||
return compressed, notif_type
|
||||
```
|
||||
|
||||
**Expected Impact:** Instant display for previously viewed notifications
|
||||
|
||||
---
|
||||
|
||||
### Phase 3: Advanced Optimizations (Week 3-4)
|
||||
|
||||
#### 3.1 Implement Virtual Scrolling
|
||||
**File:** `src/mail/app.py`
|
||||
|
||||
**Current:** Render all items in list
|
||||
**Goal:** Use ListView virtualization
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
def compose(self) -> ComposeResult:
|
||||
yield ListView(
|
||||
id="envelopes_list",
|
||||
initial_index=0,
|
||||
)
|
||||
|
||||
# ListView automatically virtualizes for performance
|
||||
```
|
||||
|
||||
**Expected Impact:** Constant time rendering regardless of list size
|
||||
|
||||
#### 3.2 Debounce User Input
|
||||
**File:** `src/mail/screens/SearchPanel.py`
|
||||
|
||||
**Current:** Search on every keystroke
|
||||
**Goal:** Debounce search with 300ms delay
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class SearchPanel(Screen):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._search_debounce = None
|
||||
|
||||
def on_input_changed(self, event) -> None:
|
||||
"""Debounce search input."""
|
||||
if self._search_debounce:
|
||||
self._search_debounce.stop()
|
||||
|
||||
self._search_debounce = Timer(
|
||||
0.3,
|
||||
self._perform_search,
|
||||
event.value
|
||||
).start()
|
||||
```
|
||||
|
||||
**Expected Impact:** 80% reduction in expensive search operations
|
||||
|
||||
#### 3.3 Use `dataclass` for Data Models
|
||||
**File:** `src/mail/notification_detector.py`
|
||||
|
||||
**Current:** Dict-based data access
|
||||
**Goal:** Use dataclasses for type safety and performance
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
@dataclass
|
||||
class Envelope:
|
||||
"""Typed envelope data model."""
|
||||
id: int
|
||||
subject: str
|
||||
from_name: str
|
||||
from_addr: str
|
||||
date: str
|
||||
flags: dict = field(default_factory=dict)
|
||||
folder: str = "INBOX"
|
||||
```
|
||||
|
||||
**Expected Impact:** Type safety, better IDE support, faster attribute access
|
||||
|
||||
---
|
||||
|
||||
### Phase 4: Memory & Resource Management (Week 4)
|
||||
|
||||
#### 4.1 Implement Widget Pooling
|
||||
**File:** `src/mail/app.py`
|
||||
|
||||
**Current:** Creating new widgets constantly
|
||||
**Goal:** Reuse widgets to reduce allocations
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class WidgetPool:
|
||||
"""Pool for reusing widgets."""
|
||||
|
||||
def __init__(self, widget_class, max_size: int = 50):
|
||||
self.widget_class = widget_class
|
||||
self.pool = []
|
||||
self.max_size = max_size
|
||||
|
||||
def get(self):
|
||||
"""Get widget from pool or create new."""
|
||||
if self.pool:
|
||||
return self.pool.pop()
|
||||
return self.widget_class()
|
||||
|
||||
def release(self, widget) -> None:
|
||||
"""Return widget to pool."""
|
||||
if len(self.pool) < self.max_size:
|
||||
self.pool.append(widget)
|
||||
```
|
||||
|
||||
**Expected Impact:** Reduced garbage collection, smoother scrolling
|
||||
|
||||
#### 4.2 Implement Content Pagination
|
||||
**File:** `src/mail/widgets/ContentContainer.py`
|
||||
|
||||
**Current:** Load full content
|
||||
**Goal:** Load content in chunks for large emails
|
||||
|
||||
**Changes:**
|
||||
```python
|
||||
class ContentContainer(ScrollableContainer):
|
||||
PAGE_SIZE = 500 # Characters per page
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self._pages: list[str] = []
|
||||
self._current_page = 0
|
||||
|
||||
def _load_next_page(self) -> None:
|
||||
"""Load next page of content when scrolling."""
|
||||
if self._current_page + 1 < len(self._pages):
|
||||
self._current_page += 1
|
||||
self.content.update(self._pages[self._current_page])
|
||||
```
|
||||
|
||||
**Expected Impact:** Faster initial load, smoother scrolling for large emails
|
||||
|
||||
#### 4.3 Clean Up Unused Imports
|
||||
**Files:** All Python files in `src/mail/`
|
||||
|
||||
**Current:** Unused imports, circular dependencies
|
||||
**Goal:** Remove all unused code
|
||||
|
||||
**Changes:**
|
||||
- Run `ruff check` and fix all unused imports
|
||||
- Remove circular dependencies
|
||||
- Clean up `__all__` exports
|
||||
- Optimize import order
|
||||
|
||||
**Expected Impact:** Faster import time, smaller memory footprint
|
||||
|
||||
---
|
||||
|
||||
## Implementation Order
|
||||
|
||||
### Week 1: Critical Performance Fixes
|
||||
1. Day 1-2: Implement `compose()` pattern
|
||||
2. Day 3-4: Lazy loading for envelopes
|
||||
3. Day 5: Optimize message list navigation
|
||||
4. Day 6-7: Background workers for content loading
|
||||
5. Day 8-10: Testing and benchmarking
|
||||
|
||||
### Week 2: Code Quality
|
||||
1. Day 1-2: Refactor MessageStore with caching
|
||||
2. Day 3-4: Consolidate EnvelopeListItem
|
||||
3. Day 5: Add memoization utilities
|
||||
4. Day 6-7: Notification compression caching
|
||||
5. Day 8-10: Code review and cleanup
|
||||
|
||||
### Week 3: Advanced Optimizations
|
||||
1. Day 1-3: Virtual scrolling implementation
|
||||
2. Day 4-5: Debounce user input
|
||||
3. Day 6-7: Data model refactoring
|
||||
4. Day 8-10: Performance testing
|
||||
|
||||
### Week 4: Memory Management
|
||||
1. Day 1-3: Widget pooling
|
||||
2. Day 4-5: Content pagination
|
||||
3. Day 6-7: Import cleanup
|
||||
4. Day 8-10: Final optimization and polish
|
||||
|
||||
---
|
||||
|
||||
## Success Metrics
|
||||
|
||||
### Performance Targets
|
||||
- **Startup Time:** < 1 second (currently: 3-5 seconds)
|
||||
- **Navigation Latency:** < 50ms between messages (currently: 200-500ms)
|
||||
- **List Rendering:** < 100ms for 100 items (currently: 500-1000ms)
|
||||
- **Memory Usage:** < 100MB for 1000 emails (currently: 300-500MB)
|
||||
- **Frame Rate:** 60 FPS during navigation (currently: 10-20 FPS)
|
||||
|
||||
### Code Quality Targets
|
||||
- **Test Coverage:** > 80% (currently: ~10%)
|
||||
- **Ruff Warnings:** 0 critical, < 5 style warnings
|
||||
- **Import Cleanup:** 100% of files cleaned
|
||||
- **Type Coverage:** 100% typed
|
||||
|
||||
---
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
### Performance Benchmarking
|
||||
```python
|
||||
# benchmark_performance.py
|
||||
import time
|
||||
import tracemalloc
|
||||
from src.mail.app import EmailViewerApp
|
||||
|
||||
def benchmark_startup():
|
||||
"""Benchmark app startup time."""
|
||||
tracemalloc.start()
|
||||
|
||||
start = time.time()
|
||||
app = EmailViewerApp()
|
||||
app.run()
|
||||
|
||||
end = time.time()
|
||||
current, peak = tracemalloc.get_traced_memory()
|
||||
|
||||
print(f"Startup Time: {end - start:.3f}s")
|
||||
print(f"Memory Usage: {peak / 1024 / 1024:.2f} MB")
|
||||
|
||||
def benchmark_navigation():
|
||||
"""Benchmark message navigation."""
|
||||
app = EmailViewerApp()
|
||||
# ... measure navigation timing
|
||||
|
||||
timings = []
|
||||
for i in range(100):
|
||||
start = time.time()
|
||||
app.action_next()
|
||||
end = time.time()
|
||||
timings.append(end - start)
|
||||
|
||||
print(f"Average Navigation Time: {sum(timings) / len(timings) * 1000:.1f}ms")
|
||||
```
|
||||
|
||||
### Integration Tests
|
||||
- Test with 100, 1000, and 10000 messages
|
||||
- Measure memory usage over time
|
||||
- Test with slow network conditions
|
||||
- Test on different terminal sizes
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
### Textual Documentation
|
||||
- **Main Docs:** https://textual.textualize.io/
|
||||
- **Widget Guide:** https://textual.textualize.io/guide/widgets/
|
||||
- **Best Practices:** https://textual.textualize.io/blog/
|
||||
- **Performance Guide:** https://textual.textualize.io/blog/2024/12/12/algorithms-for-high-performance-terminal-apps/
|
||||
|
||||
### Python Performance Guides
|
||||
- **Python Performance Guide:** https://www.fyld.pt/blog/python-performance-guide-writing-code-2025
|
||||
- **Optimization Techniques:** https://analyticsvidhya.com/blog/2024/01/optimize-python-code-for-high-speed-execution
|
||||
|
||||
### Similar Projects
|
||||
- **Rich Console Examples:** https://github.com/Textualize/rich
|
||||
- **Prompt Toolkit:** https://github.com/prompt-toolkit/python-prompt-toolkit
|
||||
- **Urwid:** https://github.com/urwid/urwid
|
||||
|
||||
---
|
||||
|
||||
## Notes
|
||||
|
||||
- This plan focuses on the mail app but principles apply to calendar and tasks apps
|
||||
- All changes should be backward compatible
|
||||
- Run performance benchmarks before and after each phase
|
||||
- Document any Textual-specific optimizations discovered during implementation
|
||||
675
PROJECT_PLAN.md
Normal file
675
PROJECT_PLAN.md
Normal file
@@ -0,0 +1,675 @@
|
||||
# LUK Project Plan
|
||||
|
||||
This document outlines planned improvements across the LUK applications (Mail, Calendar, Sync, Tasks).
|
||||
|
||||
---
|
||||
|
||||
## Bug Fixes
|
||||
|
||||
### Tasks App - Table Not Displaying (FIXED)
|
||||
**Priority:** Critical
|
||||
**File:** `src/tasks/app.py`
|
||||
|
||||
The tasks app was not showing the task table due to a CSS grid layout issue. The grid layout with mixed `dock` and `grid` positioning caused the main content area to have 0 height.
|
||||
|
||||
**Fix:** Changed from grid layout to horizontal layout with docked header/footer.
|
||||
|
||||
---
|
||||
|
||||
## Sync App
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
#### 1. Parallelize Message Downloads
|
||||
**Priority:** High
|
||||
**Files:** `src/services/microsoft_graph/mail.py`, `src/services/microsoft_graph/client.py`
|
||||
|
||||
Currently, message downloads are sequential (`for` loop with `await`). This is a significant bottleneck.
|
||||
|
||||
**Current code pattern:**
|
||||
```python
|
||||
for msg_id in message_ids:
|
||||
content = await client.get_message(msg_id)
|
||||
await save_to_maildir(content)
|
||||
```
|
||||
|
||||
**Proposed changes:**
|
||||
1. Increase semaphore limit in `client.py` from 2 to 5 concurrent HTTP requests
|
||||
2. Batch parallel downloads using `asyncio.gather()` with batches of 5-10 messages
|
||||
3. Batch sync state writes every N messages instead of after each message (currently an I/O bottleneck in archive sync)
|
||||
|
||||
**Example implementation:**
|
||||
```python
|
||||
BATCH_SIZE = 5
|
||||
|
||||
async def fetch_batch(batch_ids):
|
||||
return await asyncio.gather(*[client.get_message(id) for id in batch_ids])
|
||||
|
||||
for i in range(0, len(message_ids), BATCH_SIZE):
|
||||
batch = message_ids[i:i + BATCH_SIZE]
|
||||
results = await fetch_batch(batch)
|
||||
for content in results:
|
||||
await save_to_maildir(content)
|
||||
# Write sync state every batch instead of every message
|
||||
save_sync_state()
|
||||
```
|
||||
|
||||
#### 2. Optimize Maildir Writes
|
||||
**Priority:** Medium
|
||||
**File:** `src/utils/mail_utils/maildir.py`
|
||||
|
||||
The `save_mime_to_maildir_async` function is a potential bottleneck. Consider:
|
||||
- Batching file writes
|
||||
- Using thread pool for I/O operations
|
||||
|
||||
---
|
||||
|
||||
### CLI Improvements
|
||||
|
||||
#### 3. Default to TUI Mode
|
||||
**Priority:** Medium
|
||||
**File:** `src/cli/sync.py`
|
||||
|
||||
Currently `luk sync` requires subcommands. Change to:
|
||||
- `luk sync` → Opens TUI (interactive mode) by default
|
||||
- `luk sync --once` / `luk sync -1` → One-shot sync
|
||||
- `luk sync --daemon` / `luk sync -d` → Daemon mode
|
||||
- `luk sync status` → Show sync status
|
||||
|
||||
---
|
||||
|
||||
### UI Consistency
|
||||
|
||||
#### 4. Navigation and Styling
|
||||
**Priority:** Low
|
||||
**File:** `src/cli/sync_dashboard.py`
|
||||
|
||||
- Add `j`/`k` keybindings for list navigation (vim-style)
|
||||
- Use `border: round` in TCSS for consistency with other apps
|
||||
- Add `.border_title` styling for list containers
|
||||
|
||||
#### 5. Notifications Toggle
|
||||
**Priority:** Low
|
||||
**Files:** `src/cli/sync_dashboard.py`, `src/utils/notifications.py`
|
||||
|
||||
Add a UI switch to enable/disable desktop notifications during sync.
|
||||
|
||||
---
|
||||
|
||||
## Inter-Process Communication (IPC)
|
||||
|
||||
### Overview
|
||||
**Priority:** High
|
||||
**Goal:** Enable real-time updates between apps when data changes:
|
||||
- Mail app refreshes when sync downloads new messages
|
||||
- Tasks app refreshes when syncing or when tasks are added from mail app
|
||||
- Calendar app reloads when sync updates events
|
||||
|
||||
### Platform Options
|
||||
|
||||
#### macOS Options
|
||||
|
||||
1. **Distributed Notifications (NSDistributedNotificationCenter)**
|
||||
- Native macOS IPC mechanism
|
||||
- Simple pub/sub model
|
||||
- Lightweight, no server needed
|
||||
- Python: Use `pyobjc` (`Foundation.NSDistributedNotificationCenter`)
|
||||
- Example: `CFNotificationCenterPostNotification()`
|
||||
|
||||
2. **Unix Domain Sockets**
|
||||
- Works on both macOS and Linux
|
||||
- Requires a listener process (daemon or embedded in sync)
|
||||
- More complex but more flexible
|
||||
- Can send structured data (JSON messages)
|
||||
|
||||
3. **Named Pipes (FIFO)**
|
||||
- Simple, works on both platforms
|
||||
- One-way communication
|
||||
- Less suitable for bidirectional messaging
|
||||
|
||||
4. **File-based Signaling**
|
||||
- Write a "dirty" file that apps watch via `watchdog` or `fsevents`
|
||||
- Simple but has latency
|
||||
- Works cross-platform
|
||||
|
||||
#### Linux Options
|
||||
|
||||
1. **D-Bus**
|
||||
- Standard Linux IPC
|
||||
- Python: Use `dbus-python` or `pydbus`
|
||||
- Powerful but more complex
|
||||
- Not available on macOS by default
|
||||
|
||||
2. **Unix Domain Sockets**
|
||||
- Same as macOS, fully compatible
|
||||
- Recommended for cross-platform compatibility
|
||||
|
||||
3. **systemd Socket Activation**
|
||||
- If running as a systemd service
|
||||
- Clean integration with Linux init
|
||||
|
||||
### Recommended Approach
|
||||
|
||||
Use **Unix Domain Sockets** for cross-platform compatibility:
|
||||
|
||||
```python
|
||||
# Socket path
|
||||
SOCKET_PATH = Path.home() / ".cache" / "luk" / "ipc.sock"
|
||||
|
||||
# Message types
|
||||
class IPCMessage:
|
||||
MAIL_UPDATED = "mail.updated"
|
||||
TASKS_UPDATED = "tasks.updated"
|
||||
CALENDAR_UPDATED = "calendar.updated"
|
||||
|
||||
# Sync daemon sends notifications
|
||||
async def notify_mail_updated(folder: str):
|
||||
await send_ipc_message({"type": IPCMessage.MAIL_UPDATED, "folder": folder})
|
||||
|
||||
# Mail app listens
|
||||
async def listen_for_updates():
|
||||
async for message in ipc_listener():
|
||||
if message["type"] == IPCMessage.MAIL_UPDATED:
|
||||
self.load_messages()
|
||||
```
|
||||
|
||||
**Implementation files:**
|
||||
- `src/utils/ipc.py` - IPC client/server utilities
|
||||
- `src/cli/sync_daemon.py` - Add notification sending
|
||||
- `src/mail/app.py` - Add listener for mail updates
|
||||
- `src/tasks/app.py` - Add listener for task updates
|
||||
- `src/calendar/app.py` - Add listener for calendar updates
|
||||
|
||||
---
|
||||
|
||||
## Calendar App
|
||||
|
||||
### Visual Improvements
|
||||
|
||||
#### 1. Current Time Hour Line Styling
|
||||
**Priority:** High
|
||||
**File:** `src/calendar/widgets/WeekGrid.py`
|
||||
|
||||
The current time indicator hour line should have a subtle contrasting background color to make it more visible.
|
||||
|
||||
#### 2. Cursor Hour Header Highlighting
|
||||
**Priority:** Medium
|
||||
**File:** `src/calendar/widgets/WeekGrid.py`
|
||||
|
||||
The hour header at cursor position should have a brighter background, similar to how the day header is highlighted when selected.
|
||||
|
||||
---
|
||||
|
||||
### Layout Improvements
|
||||
|
||||
#### 3. Responsive Detail Panel
|
||||
**Priority:** Medium
|
||||
**Files:** `src/calendar/app.py`, `src/calendar/widgets/`
|
||||
|
||||
When the terminal is wider than X characters (e.g., 120), show a side-docked detail panel for the selected event instead of a modal/overlay.
|
||||
|
||||
#### 4. Sidebar Mini-Calendar
|
||||
**Priority:** Medium
|
||||
**Files:** `src/calendar/app.py`, `src/calendar/widgets/MonthCalendar.py`
|
||||
|
||||
When the sidebar is toggled on, display a mini-calendar in the top-left corner showing:
|
||||
- Current day highlighted
|
||||
- The week(s) currently visible in the main WeekGrid pane
|
||||
- Click/navigate to jump to a specific date
|
||||
|
||||
**Implementation:**
|
||||
- Reuse existing `MonthCalendar` widget in compact mode
|
||||
- Add reactive property to sync selected week with main pane
|
||||
- Add to sidebar composition when toggled
|
||||
|
||||
---
|
||||
|
||||
### Microsoft Graph Integration
|
||||
|
||||
#### 5. Calendar Invites Sidebar
|
||||
**Priority:** Medium
|
||||
**Files:** `src/calendar/app.py`, `src/services/microsoft_graph/calendar.py`
|
||||
|
||||
Display a list of pending calendar invites from Microsoft Graph API in a sidebar panel:
|
||||
- List pending invites (meeting requests)
|
||||
- Show invite details (organizer, time, subject)
|
||||
- Accept/Decline/Tentative actions
|
||||
- No sync needed - fetch on-demand from API
|
||||
|
||||
**API Endpoints:**
|
||||
- `GET /me/calendar/events?$filter=responseStatus/response eq 'notResponded'`
|
||||
- `POST /me/events/{id}/accept`
|
||||
- `POST /me/events/{id}/decline`
|
||||
- `POST /me/events/{id}/tentativelyAccept`
|
||||
|
||||
**UI:**
|
||||
- Toggle with keybinding (e.g., `i` for invites)
|
||||
- Sidebar shows list of pending invites
|
||||
- Detail view shows full invite info
|
||||
- Action buttons/keys for response
|
||||
|
||||
---
|
||||
|
||||
### Search Feature
|
||||
|
||||
#### 6. Calendar Search
|
||||
**Priority:** Medium
|
||||
**Files:** `src/calendar/app.py`, `src/services/khal/client.py` (if khal supports search)
|
||||
|
||||
Add search functionality if the underlying backend (khal) supports it:
|
||||
- `/` keybinding to open search input
|
||||
- Search by event title, description, location
|
||||
- Display search results in a modal or replace main view
|
||||
- Navigate to selected event
|
||||
|
||||
**Check:** Does `khal search` command exist?
|
||||
|
||||
---
|
||||
|
||||
### Help System
|
||||
|
||||
#### 7. Help Toast (Keep Current Implementation)
|
||||
**Priority:** Low
|
||||
**File:** `src/calendar/app.py`
|
||||
|
||||
The `?` key shows a help toast using `self.notify()`. This pattern should be implemented in other apps (Mail, Tasks, Sync) for consistency.
|
||||
|
||||
---
|
||||
|
||||
## Mail App
|
||||
|
||||
### Layout Fixes
|
||||
|
||||
#### 1. Remove Envelope Icon/Checkbox Gap
|
||||
**Priority:** High
|
||||
**File:** `src/mail/widgets/EnvelopeListItem.py`
|
||||
|
||||
There's a 1-character space between the envelope icon and checkbox that should be removed for tighter layout.
|
||||
|
||||
---
|
||||
|
||||
### Theme Improvements
|
||||
|
||||
#### 2. Replace Hardcoded RGB Colors
|
||||
**Priority:** High
|
||||
**File:** `src/mail/email_viewer.tcss`
|
||||
|
||||
Multiple hardcoded RGB values should use Textual theme variables for better theming support:
|
||||
|
||||
| Line | Current | Replacement |
|
||||
|------|---------|-------------|
|
||||
| 6 | `border: round rgb(117, 106, 129)` | `border: round $border` |
|
||||
| 46 | `background: rgb(55, 53, 57)` | `background: $surface` |
|
||||
| 52, 57 | RGB label colors | Theme variables |
|
||||
| 69 | `background: rgb(64, 62, 65)` | `background: $panel` |
|
||||
| 169, 225, 228, 272, 274 | Various RGB colors | Theme variables |
|
||||
|
||||
---
|
||||
|
||||
### Keybindings
|
||||
|
||||
#### 3. Add Refresh Keybinding
|
||||
**Priority:** Medium
|
||||
**File:** `src/mail/app.py`
|
||||
|
||||
Add `r` keybinding to refresh/reload the message list.
|
||||
|
||||
#### 4. Add Mark Read/Unread Action
|
||||
**Priority:** Medium
|
||||
**Files:** `src/mail/app.py`, `src/mail/actions/` (new file)
|
||||
|
||||
Add action to toggle read/unread status on selected message(s).
|
||||
|
||||
---
|
||||
|
||||
### Search Feature
|
||||
|
||||
#### 5. Mail Search
|
||||
**Priority:** Medium
|
||||
**Files:** `src/mail/app.py`, backend integration
|
||||
|
||||
Add search functionality if the underlying mail backend supports it:
|
||||
- `/` keybinding to open search input
|
||||
- Search by subject, sender, body
|
||||
- Display results in message list
|
||||
- Check: Does himalaya or configured backend support search?
|
||||
|
||||
---
|
||||
|
||||
### UI Enhancements
|
||||
|
||||
#### 6. Folder Message Counts
|
||||
**Priority:** Medium
|
||||
**Files:** `src/mail/app.py`, `src/mail/widgets/`
|
||||
|
||||
Display total message count next to each folder name (e.g., "Inbox (42)").
|
||||
|
||||
#### 7. Sort Setting in Config/UI
|
||||
**Priority:** Low
|
||||
**Files:** `src/mail/config.py`, `src/mail/app.py`
|
||||
|
||||
Add configurable sort order (date, sender, subject) with UI toggle.
|
||||
|
||||
---
|
||||
|
||||
### Message Display
|
||||
|
||||
#### 8. URL Compression in Markdown View
|
||||
**Priority:** Medium
|
||||
**Files:** `src/mail/widgets/ContentContainer.py`, `src/mail/screens/LinkPanel.py`
|
||||
|
||||
Compress long URLs in the markdown view to ~50 characters with a nerdfont icon. The `_shorten_url` algorithm in `LinkPanel.py` can be reused.
|
||||
|
||||
**Considerations:**
|
||||
- Cache processed markdown to avoid re-processing on scroll
|
||||
- Store URL mapping for click handling
|
||||
|
||||
#### 9. Remove Emoji from Border Title
|
||||
**Priority:** Low
|
||||
**File:** `src/mail/widgets/ContentContainer.py` or `EnvelopeHeader.py`
|
||||
|
||||
Remove the envelope emoji prefix before message ID in border titles.
|
||||
|
||||
#### 10. Enhance Subject Styling
|
||||
**Priority:** Medium
|
||||
**File:** `src/mail/widgets/EnvelopeHeader.py`
|
||||
|
||||
- Move subject line to the top of the header
|
||||
- Make it bolder/brighter for better visual hierarchy
|
||||
|
||||
---
|
||||
|
||||
## Tasks App
|
||||
|
||||
### Search Feature
|
||||
|
||||
#### 1. Task Search
|
||||
**Priority:** Medium
|
||||
**Files:** `src/tasks/app.py`, `src/services/dstask/client.py`
|
||||
|
||||
Add search functionality:
|
||||
- `/` keybinding to open search input
|
||||
- Search by task summary, notes, project, tags
|
||||
- Display matching tasks
|
||||
- Check: dstask likely supports filtering which can be used for search
|
||||
|
||||
**Implementation:**
|
||||
- Add search input widget (TextInput)
|
||||
- Filter tasks locally or via dstask command
|
||||
- Update table to show only matching tasks
|
||||
- Clear search with Escape
|
||||
|
||||
---
|
||||
|
||||
### Help System
|
||||
|
||||
#### 2. Implement Help Toast
|
||||
**Priority:** Low
|
||||
**File:** `src/tasks/app.py`
|
||||
|
||||
Add `?` keybinding to show help toast (matching Calendar app pattern).
|
||||
**Note:** This is already implemented in the current code.
|
||||
|
||||
---
|
||||
|
||||
## Cross-App Improvements
|
||||
|
||||
### 1. Consistent Help System
|
||||
Implement `?` key help toast in all apps using `self.notify()`:
|
||||
- Mail: `src/mail/app.py`
|
||||
- Tasks: `src/tasks/app.py` (already has it)
|
||||
- Sync: `src/cli/sync_dashboard.py`
|
||||
|
||||
### 2. Consistent Navigation
|
||||
Add vim-style `j`/`k` navigation to all list views across apps.
|
||||
|
||||
### 3. Consistent Border Styling
|
||||
Use `border: round` and `.border_title` styling consistently in all TCSS files.
|
||||
|
||||
### 4. Consistent Search Interface
|
||||
Implement `/` keybinding for search across all apps with similar UX:
|
||||
- `/` opens search input
|
||||
- Enter executes search
|
||||
- Escape clears/closes search
|
||||
- Results displayed in main view or filtered list
|
||||
|
||||
---
|
||||
|
||||
## Implementation Priority
|
||||
|
||||
### Phase 1: Critical/High Priority
|
||||
1. ~~Tasks App: Fix table display~~ (DONE)
|
||||
2. ~~Sync: Parallelize message downloads~~ (DONE - connection pooling + batch size increase)
|
||||
3. ~~Mail: Replace hardcoded RGB colors~~ (DONE - already using theme variables)
|
||||
4. ~~Mail: Remove envelope icon/checkbox gap~~ (DONE)
|
||||
5. ~~Calendar: Current time hour line styling~~ (DONE - added surface background)
|
||||
6. ~~IPC: Implement cross-app refresh notifications~~ (DONE)
|
||||
|
||||
### Phase 2: Medium Priority
|
||||
1. ~~Sync: Default to TUI mode~~ (DONE - already implemented)
|
||||
2. ~~Calendar: Cursor hour header highlighting~~ (DONE)
|
||||
3. Calendar: Responsive detail panel
|
||||
4. Calendar: Sidebar mini-calendar
|
||||
5. Calendar: Calendar invites sidebar
|
||||
6. ~~Mail: Add refresh keybinding~~ (DONE - `r` key)
|
||||
7. ~~Mail: Add mark read/unread action~~ (DONE - `u` key)
|
||||
8. ~~Mail: Folder message counts~~ (DONE)
|
||||
8. ~~Mail: URL compression in markdown view~~ (DONE)
|
||||
9. ~~Mail: Enhance subject styling~~ (DONE)
|
||||
10. Mail: Search feature
|
||||
11. ~~Tasks: Search feature~~ (DONE - `/` key with live filtering)
|
||||
12. ~~Calendar: Search feature~~ (DONE - `/` key using khal search)
|
||||
|
||||
### Phase 3: Low Priority
|
||||
1. Sync: UI consistency (j/k navigation, borders)
|
||||
2. Sync: Notifications toggle
|
||||
3. Calendar: Help toast (already implemented, replicate to other apps)
|
||||
4. Mail: Remove emoji from border title
|
||||
5. Mail: Sort setting in config/UI
|
||||
6. Cross-app: Consistent help, navigation, and styling
|
||||
|
||||
---
|
||||
|
||||
## Technical Notes
|
||||
|
||||
### IPC Implementation Details
|
||||
|
||||
For macOS-first with Linux compatibility:
|
||||
|
||||
```python
|
||||
# src/utils/ipc.py
|
||||
import asyncio
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
SOCKET_PATH = Path.home() / ".cache" / "luk" / "ipc.sock"
|
||||
|
||||
class IPCServer:
|
||||
"""Server for sending notifications to listening apps."""
|
||||
|
||||
async def broadcast(self, message: dict):
|
||||
"""Send message to all connected clients."""
|
||||
pass
|
||||
|
||||
class IPCClient:
|
||||
"""Client for receiving notifications in apps."""
|
||||
|
||||
async def listen(self, callback):
|
||||
"""Listen for messages and call callback."""
|
||||
pass
|
||||
```
|
||||
|
||||
### Backend Search Capabilities
|
||||
|
||||
| Backend | Search Support |
|
||||
|---------|---------------|
|
||||
| dstask | Filter by project/tag, summary search via shell |
|
||||
| himalaya | Check `himalaya search` command |
|
||||
| khal | Check `khal search` command |
|
||||
| Microsoft Graph | Full text search via `$search` parameter |
|
||||
|
||||
---
|
||||
|
||||
## Notes
|
||||
|
||||
- All UI improvements should be tested with different terminal sizes
|
||||
- Theme changes should be tested with multiple Textual themes
|
||||
- Performance improvements should include before/after benchmarks
|
||||
- New keybindings should be documented in each app's help toast
|
||||
- IPC should gracefully handle missing socket (apps work standalone)
|
||||
- Search should be responsive and not block UI
|
||||
|
||||
---
|
||||
|
||||
## Mail Rendering Improvements
|
||||
|
||||
### Smart Notification Email Compression (COMPLETED)
|
||||
**Priority:** High
|
||||
**Files:** `src/mail/notification_detector.py`, `src/mail/notification_compressor.py`, `src/mail/config.py`
|
||||
|
||||
**Problem:** Transactional notification emails from tools like Renovate, Jira, Confluence, and Datadog are hard to parse and display poorly in plain text terminal environments.
|
||||
|
||||
**Solution:** Implemented intelligent notification detection and compression system:
|
||||
|
||||
1. **Notification Type Detection**
|
||||
- Automatically detects emails from:
|
||||
- GitLab (pipelines, MRs, mentions)
|
||||
- GitHub (PRs, issues, reviews)
|
||||
- Jira (issues, status changes)
|
||||
- Confluence (page updates, comments)
|
||||
- Datadog (alerts, incidents)
|
||||
- Renovate (dependency updates)
|
||||
- General notifications (digests, automated emails)
|
||||
- Uses sender domain and subject pattern matching
|
||||
- Distinguishes between similar services (e.g., Jira vs Confluence)
|
||||
|
||||
2. **Structured Summary Extraction**
|
||||
- Type-specific extractors for each platform
|
||||
- Extracts: IDs, titles, status changes, action items
|
||||
- Preserves important links for quick access
|
||||
|
||||
3. **Terminal-Friendly Formatting**
|
||||
- Two compression modes:
|
||||
- `summary`: Brief one-page view
|
||||
- `detailed`: Structured table format
|
||||
- Markdown rendering with icons and clear hierarchy
|
||||
- Shows notification type, key details, actions
|
||||
- Footer indicates compressed view (toggle to full with `m`)
|
||||
|
||||
4. **Configuration Options**
|
||||
```toml
|
||||
[content_display]
|
||||
compress_notifications = true
|
||||
notification_compression_mode = "summary" # "summary", "detailed", or "off"
|
||||
```
|
||||
|
||||
5. **Features**
|
||||
- Zero-dependency (no LLM required, fast)
|
||||
- Rule-based for reliability
|
||||
- Extensible to add new notification types
|
||||
- Preserves original content for full view toggle
|
||||
- Type-specific icons using NerdFont glyphs
|
||||
|
||||
**Implementation Details:**
|
||||
- `NotificationType` dataclass for type definitions
|
||||
- `is_notification_email()` for detection
|
||||
- `classify_notification()` for type classification
|
||||
- `extract_notification_summary()` for structured data
|
||||
- `NotificationCompressor` for formatting
|
||||
- `DetailedCompressor` for extended summaries
|
||||
- Integrated with `ContentContainer` widget
|
||||
- 13 unit tests covering all notification types
|
||||
|
||||
**Future Enhancements:**
|
||||
- Add LLM-based summarization option (opt-in)
|
||||
- Learn notification patterns from user feedback
|
||||
- Custom notification type definitions
|
||||
- Action-based email filtering (e.g., "archive all Renovate emails")
|
||||
- Bulk actions on notification emails (archive all, mark all read)
|
||||
- Notification grouping in envelope list
|
||||
- Statistics on notification emails (counts by type, frequency)
|
||||
|
||||
---
|
||||
|
||||
---
|
||||
|
||||
## Note-taking integration and more cross-app integrations
|
||||
|
||||
I like the `tdo` (https://github.com/2KAbhishek/tdo) program for managing markdown notes with fzf and my terminal text editor. It makes it easy to have a "today" note and a list of todos. Perhaps we can gather those todos from the text files in the $NOTES_DIR and put them into the task list during regular sync - and when users mark a task complete the sync can find the text file it was in and mark it complete on that line of markdown text. We need a little ore features for the related annotations then, because when I press `n` in the notes app we would want to actually open the textfile that task came from, not just make another annotation. So we would need a special cross-linking format for knowing which tasks came from a $NOTES_DIR sync. And then we can work on the same IPC scenario for tasks that were created in the email app. Then those tasks should be connected so that when the user views the notes on those tasks they see the whole email. That would be simpe enough if we just copied the email text into an annotation. But maybe we need a way to actually change the selected message ID in the mail app if it's open. So if the user activates the "open" feature on a task the related email will be displayed in the other terminal window where the user has mail open.
|
||||
|
||||
---
|
||||
|
||||
## Polish for release and Auth
|
||||
|
||||
- Authentication isn't working if the user is in a different directory than expected. Store the auth token in a ~/.local directory so it's consistently availabe in every dir.
|
||||
- Setup scripts, look at popular modern python CLI or TUI projects and create an install or first run script that works for most scenarios, using `uv` or `pip` to install globally for users, or maybe get a homebrew installation to work? Get users to set up himalaya and khal and dstask but ask them for their choice once we have other backends available.
|
||||
- Documentation files in the repo, and documentation site to publish.
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Library Updates & Python Version Review
|
||||
|
||||
### Priority: Medium (Scheduled Review)
|
||||
|
||||
Periodically review the latest releases of heavily-used libraries to identify:
|
||||
- Bug fixes that address issues we've encountered
|
||||
- New features that could improve the codebase
|
||||
- Deprecation warnings that need to be addressed
|
||||
- Security updates
|
||||
|
||||
### Key Libraries to Review
|
||||
|
||||
| Library | Current Use | Review Focus |
|
||||
|---------|-------------|--------------|
|
||||
| **Textual** | All TUI apps | New widgets, performance improvements, theming changes, CSS features |
|
||||
| **aiohttp** | Microsoft Graph API client | Async improvements, connection pooling |
|
||||
| **msal** | Microsoft authentication | Token caching, auth flow improvements |
|
||||
| **rich** | Console output (via Textual) | New formatting options |
|
||||
| **orjson** | Fast JSON parsing | Performance improvements |
|
||||
| **pyobjc** (macOS) | Notifications | API changes, compatibility |
|
||||
|
||||
### Textual Changelog Review Checklist
|
||||
|
||||
When reviewing Textual releases, check for:
|
||||
1. **New widgets** - Could replace custom implementations
|
||||
2. **CSS features** - New selectors, pseudo-classes, properties
|
||||
3. **Theming updates** - New theme variables, design token changes
|
||||
4. **Performance** - Rendering optimizations, memory improvements
|
||||
5. **Breaking changes** - Deprecated APIs, signature changes
|
||||
6. **Worker improvements** - Background task handling
|
||||
|
||||
### Python Version Upgrade
|
||||
|
||||
#### Current Status
|
||||
- Check `.python-version` and `pyproject.toml` for current Python version
|
||||
- Evaluate upgrade to Python 3.13 or 3.14 when stable
|
||||
|
||||
#### Python 3.13 Features to Consider
|
||||
- Improved error messages
|
||||
- Type system enhancements (`typing` module improvements)
|
||||
- Performance optimizations (PEP 709 - inline comprehensions)
|
||||
|
||||
#### Python 3.14 Considerations
|
||||
- **Status:** Currently in alpha/beta (as of Dec 2024)
|
||||
- **Expected stable release:** October 2025
|
||||
- **Recommendation:** Wait for stable release before adopting
|
||||
- **Pre-release testing:** Can test compatibility in CI/CD before adoption
|
||||
|
||||
#### Upgrade Checklist
|
||||
1. [ ] Review Python release notes for breaking changes
|
||||
2. [ ] Check library compatibility (especially `pyobjc`, `textual`, `msal`)
|
||||
3. [ ] Update `.python-version` (mise/pyenv)
|
||||
4. [ ] Update `pyproject.toml` `requires-python` field
|
||||
5. [ ] Run full test suite
|
||||
6. [ ] Test on both macOS and Linux (if applicable)
|
||||
7. [ ] Update CI/CD Python version
|
||||
|
||||
### Action Items
|
||||
|
||||
1. **Quarterly Review** - Schedule quarterly reviews of library changelogs
|
||||
2. **Dependabot/Renovate** - Consider adding automated dependency update PRs
|
||||
3. **Changelog Reading** - Before updating, read changelogs for breaking changes
|
||||
4. **Test Coverage** - Ensure adequate test coverage before major updates
|
||||
|
||||
32
README.md
32
README.md
@@ -12,6 +12,7 @@ A CLI tool for syncing Microsoft Outlook email, calendar, and tasks to local fil
|
||||
- **TUI Dashboard**: Interactive terminal dashboard for monitoring sync progress
|
||||
- **Daemon Mode**: Background daemon with proper Unix logging
|
||||
- **Cross-Platform**: Works on macOS, Linux, and Windows
|
||||
- **Smart Notification Compression**: Automatically detects and compresses transactional notification emails (GitLab, GitHub, Jira, Confluence, Datadog, Renovate) into terminal-friendly summaries
|
||||
|
||||
## Quick Start
|
||||
|
||||
@@ -50,6 +51,37 @@ mkdir -p ~/.local/share/luk
|
||||
|
||||
Create a configuration file at `~/.config/luk/config.env`:
|
||||
|
||||
### Notification Email Compression
|
||||
|
||||
LUK includes intelligent notification email compression to reduce visual noise from automated emails (GitLab, GitHub, Jira, Confluence, Datadog, Renovate). Configure it in `~/.config/luk/mail.toml`:
|
||||
|
||||
```toml
|
||||
[content_display]
|
||||
# Enable/disable notification compression
|
||||
compress_notifications = true
|
||||
|
||||
# Compression mode:
|
||||
# - "summary": Brief one-page view (default)
|
||||
# - "detailed": More details in structured format
|
||||
# - "off": Disable compression, show full emails
|
||||
notification_compression_mode = "summary"
|
||||
```
|
||||
|
||||
**Features:**
|
||||
- **Automatic detection**: Identifies notification emails by sender domain and subject patterns
|
||||
- **Type-specific summaries**: Extracts relevant info per platform (pipeline IDs, PR numbers, etc.)
|
||||
- **Terminal-friendly formatting**: Clean markdown with icons and clear hierarchy
|
||||
- **Toggle support**: Press `m` in mail view to switch between compressed and full email
|
||||
|
||||
**Demo:**
|
||||
```bash
|
||||
python demo_notification_compression.py
|
||||
```
|
||||
|
||||
See `mail.toml.example` for full configuration options.
|
||||
|
||||
### General Configuration
|
||||
|
||||
```bash
|
||||
# Microsoft Graph settings
|
||||
MICROSOFT_CLIENT_ID=your_client_id
|
||||
|
||||
202
demo_notification_compression.py
Normal file
202
demo_notification_compression.py
Normal file
@@ -0,0 +1,202 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Demo script for notification email compression.
|
||||
|
||||
This script demonstrates how notification emails are detected and compressed
|
||||
into terminal-friendly summaries.
|
||||
"""
|
||||
|
||||
from src.mail.notification_detector import (
|
||||
is_notification_email,
|
||||
classify_notification,
|
||||
extract_notification_summary,
|
||||
NOTIFICATION_TYPES,
|
||||
)
|
||||
from src.mail.notification_compressor import NotificationCompressor, DetailedCompressor
|
||||
|
||||
|
||||
def demo_detection():
|
||||
"""Demonstrate notification detection for various email types."""
|
||||
|
||||
test_emails = [
|
||||
{
|
||||
"from": {"addr": "notifications@gitlab.com", "name": "GitLab"},
|
||||
"subject": "Pipeline #12345 failed by john.doe",
|
||||
},
|
||||
{
|
||||
"from": {"addr": "noreply@github.com", "name": "GitHub"},
|
||||
"subject": "[GitHub] PR #42: Add new feature",
|
||||
},
|
||||
{
|
||||
"from": {"addr": "jira@atlassian.net", "name": "Jira"},
|
||||
"subject": "[Jira] ABC-123: Fix login bug",
|
||||
},
|
||||
{
|
||||
"from": {"addr": "confluence@atlassian.net", "name": "Confluence"},
|
||||
"subject": "[Confluence] New comment on page",
|
||||
},
|
||||
{
|
||||
"from": {"addr": "alerts@datadoghq.com", "name": "Datadog"},
|
||||
"subject": "[Datadog] Alert: High CPU usage",
|
||||
},
|
||||
{
|
||||
"from": {"addr": "renovate@renovatebot.com", "name": "Renovate"},
|
||||
"subject": "[Renovate] Update dependency to v2.0.0",
|
||||
},
|
||||
{
|
||||
"from": {"addr": "john.doe@example.com", "name": "John Doe"},
|
||||
"subject": "Let's meet for lunch",
|
||||
},
|
||||
]
|
||||
|
||||
print("=" * 70)
|
||||
print("NOTIFICATION DETECTION DEMO")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
for i, envelope in enumerate(test_emails, 1):
|
||||
from_addr = envelope.get("from", {}).get("addr", "")
|
||||
subject = envelope.get("subject", "")
|
||||
|
||||
print(f"Email {i}: {subject}")
|
||||
print(f" From: {from_addr}")
|
||||
|
||||
# Check if notification
|
||||
is_notif = is_notification_email(envelope)
|
||||
print(f" Is Notification: {is_notif}")
|
||||
|
||||
if is_notif:
|
||||
notif_type = classify_notification(envelope)
|
||||
if notif_type:
|
||||
print(f" Type: {notif_type.name}")
|
||||
print(f" Icon: {notif_type.icon}")
|
||||
|
||||
print()
|
||||
|
||||
|
||||
def demo_compression():
|
||||
"""Demonstrate notification compression."""
|
||||
|
||||
# GitLab pipeline email content (simplified)
|
||||
gitlab_content = """
|
||||
Pipeline #12345 failed by john.doe
|
||||
|
||||
The pipeline failed on stage: build
|
||||
Commit: abc123def
|
||||
|
||||
View pipeline: https://gitlab.com/project/pipelines/12345
|
||||
"""
|
||||
|
||||
# GitHub PR email content (simplified)
|
||||
github_content = """
|
||||
PR #42: Add new feature
|
||||
|
||||
@john.doe requested your review
|
||||
|
||||
View PR: https://github.com/repo/pull/42
|
||||
"""
|
||||
|
||||
gitlab_envelope = {
|
||||
"from": {"addr": "notifications@gitlab.com", "name": "GitLab"},
|
||||
"subject": "Pipeline #12345 failed",
|
||||
"date": "2025-12-28T15:00:00Z",
|
||||
}
|
||||
|
||||
github_envelope = {
|
||||
"from": {"addr": "noreply@github.com", "name": "GitHub"},
|
||||
"subject": "[GitHub] PR #42: Add new feature",
|
||||
"date": "2025-12-28T15:00:00Z",
|
||||
}
|
||||
|
||||
print("=" * 70)
|
||||
print("NOTIFICATION COMPRESSION DEMO")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
# GitLab compression - summary mode
|
||||
print("1. GitLab Pipeline (Summary Mode)")
|
||||
print("-" * 70)
|
||||
compressor = NotificationCompressor(mode="summary")
|
||||
compressed, notif_type = compressor.compress(gitlab_content, gitlab_envelope)
|
||||
print(compressed)
|
||||
print()
|
||||
|
||||
# GitLab compression - detailed mode
|
||||
print("2. GitLab Pipeline (Detailed Mode)")
|
||||
print("-" * 70)
|
||||
detailed_compressor = DetailedCompressor(mode="detailed")
|
||||
compressed, notif_type = detailed_compressor.compress(
|
||||
gitlab_content, gitlab_envelope
|
||||
)
|
||||
print(compressed)
|
||||
print()
|
||||
|
||||
# GitHub PR - summary mode
|
||||
print("3. GitHub PR (Summary Mode)")
|
||||
print("-" * 70)
|
||||
compressor = NotificationCompressor(mode="summary")
|
||||
compressed, notif_type = compressor.compress(github_content, github_envelope)
|
||||
print(compressed)
|
||||
print()
|
||||
|
||||
|
||||
def demo_summary_extraction():
|
||||
"""Demonstrate structured summary extraction."""
|
||||
|
||||
test_content = """
|
||||
ABC-123: Fix login bug
|
||||
|
||||
Status changed from In Progress to Done
|
||||
|
||||
View issue: https://jira.atlassian.net/browse/ABC-123
|
||||
"""
|
||||
|
||||
print("=" * 70)
|
||||
print("SUMMARY EXTRACTION DEMO")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
notif_type = NOTIFICATION_TYPES[2] # jira
|
||||
summary = extract_notification_summary(test_content, notif_type)
|
||||
|
||||
print("Extracted Summary:")
|
||||
print(f" Title: {summary.get('title')}")
|
||||
print(f" Metadata: {summary.get('metadata')}")
|
||||
print(f" Action Items: {summary.get('action_items')}")
|
||||
print()
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all demos."""
|
||||
|
||||
print()
|
||||
print("╔" + "═" * 68 + "╗")
|
||||
print("║" + " " * 68 + "║")
|
||||
print("║" + " LUK Notification Email Compression - Feature Demo".center(68) + "║")
|
||||
print("║" + " " * 68 + "║")
|
||||
print("╚" + "═" * 68 + "╝")
|
||||
print()
|
||||
|
||||
# Run demos
|
||||
demo_detection()
|
||||
print()
|
||||
demo_compression()
|
||||
print()
|
||||
demo_summary_extraction()
|
||||
|
||||
print()
|
||||
print("=" * 70)
|
||||
print("DEMO COMPLETE")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print("The notification compression feature is now integrated into the mail app.")
|
||||
print("Configure it in ~/.config/luk/mail.toml:")
|
||||
print()
|
||||
print(" [content_display]")
|
||||
print(" compress_notifications = true")
|
||||
print(" notification_compression_mode = 'summary' # or 'detailed' or 'off'")
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -37,7 +37,7 @@ from textual.widgets.option_list import Option
|
||||
from src.utils.file_icons import get_file_icon
|
||||
|
||||
# Import our DocumentViewerScreen
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), "src", "maildir_gtd"))
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), "src", "mail"))
|
||||
from screens.DocumentViewer import DocumentViewerScreen
|
||||
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ Requires-Dist: msal>=1.32.3
|
||||
Requires-Dist: openai>=1.78.1
|
||||
Requires-Dist: orjson>=3.10.18
|
||||
Requires-Dist: pillow>=11.2.1
|
||||
Requires-Dist: pydantic>=2.0.0
|
||||
Requires-Dist: pydantic-settings>=2.0.0
|
||||
Requires-Dist: python-dateutil>=2.9.0.post0
|
||||
Requires-Dist: python-docx>=1.1.2
|
||||
Requires-Dist: requests>=2.31.0
|
||||
@@ -39,6 +41,7 @@ Requires-Dist: rich>=14.0.0
|
||||
Requires-Dist: textual>=3.2.0
|
||||
Requires-Dist: textual-image>=0.8.2
|
||||
Requires-Dist: ticktick-py>=2.0.0
|
||||
Requires-Dist: toml>=0.10.0
|
||||
|
||||
# luk
|
||||
|
||||
|
||||
@@ -6,6 +6,12 @@ luk.egg-info/dependency_links.txt
|
||||
luk.egg-info/entry_points.txt
|
||||
luk.egg-info/requires.txt
|
||||
luk.egg-info/top_level.txt
|
||||
src/calendar/__init__.py
|
||||
src/calendar/app.py
|
||||
src/calendar/backend.py
|
||||
src/calendar/screens/__init__.py
|
||||
src/calendar/widgets/WeekGrid.py
|
||||
src/calendar/widgets/__init__.py
|
||||
src/cli/__init__.py
|
||||
src/cli/__main__.py
|
||||
src/cli/calendar.py
|
||||
@@ -16,30 +22,38 @@ src/cli/godspeed.py
|
||||
src/cli/sync.py
|
||||
src/cli/sync_daemon.py
|
||||
src/cli/sync_dashboard.py
|
||||
src/cli/tasks.py
|
||||
src/cli/ticktick.py
|
||||
src/maildir_gtd/__init__.py
|
||||
src/maildir_gtd/app.py
|
||||
src/maildir_gtd/email_viewer.tcss
|
||||
src/maildir_gtd/message_store.py
|
||||
src/maildir_gtd/utils.py
|
||||
src/maildir_gtd/actions/__init__.py
|
||||
src/maildir_gtd/actions/archive.py
|
||||
src/maildir_gtd/actions/delete.py
|
||||
src/maildir_gtd/actions/newest.py
|
||||
src/maildir_gtd/actions/next.py
|
||||
src/maildir_gtd/actions/oldest.py
|
||||
src/maildir_gtd/actions/open.py
|
||||
src/maildir_gtd/actions/previous.py
|
||||
src/maildir_gtd/actions/show_message.py
|
||||
src/maildir_gtd/actions/task.py
|
||||
src/maildir_gtd/screens/CreateTask.py
|
||||
src/maildir_gtd/screens/DocumentViewer.py
|
||||
src/maildir_gtd/screens/OpenMessage.py
|
||||
src/maildir_gtd/screens/__init__.py
|
||||
src/maildir_gtd/widgets/ContentContainer.py
|
||||
src/maildir_gtd/widgets/EnvelopeHeader.py
|
||||
src/maildir_gtd/widgets/__init__.py
|
||||
src/mail/__init__.py
|
||||
src/mail/app.py
|
||||
src/mail/config.py
|
||||
src/mail/email_viewer.tcss
|
||||
src/mail/message_store.py
|
||||
src/mail/utils.py
|
||||
src/mail/actions/__init__.py
|
||||
src/mail/actions/archive.py
|
||||
src/mail/actions/delete.py
|
||||
src/mail/actions/newest.py
|
||||
src/mail/actions/next.py
|
||||
src/mail/actions/oldest.py
|
||||
src/mail/actions/open.py
|
||||
src/mail/actions/previous.py
|
||||
src/mail/actions/show_message.py
|
||||
src/mail/actions/task.py
|
||||
src/mail/screens/ConfirmDialog.py
|
||||
src/mail/screens/CreateTask.py
|
||||
src/mail/screens/DocumentViewer.py
|
||||
src/mail/screens/LinkPanel.py
|
||||
src/mail/screens/OpenMessage.py
|
||||
src/mail/screens/__init__.py
|
||||
src/mail/widgets/ContentContainer.py
|
||||
src/mail/widgets/EnvelopeHeader.py
|
||||
src/mail/widgets/EnvelopeListItem.py
|
||||
src/mail/widgets/__init__.py
|
||||
src/services/__init__.py
|
||||
src/services/task_client.py
|
||||
src/services/dstask/__init__.py
|
||||
src/services/dstask/client.py
|
||||
src/services/gitlab_monitor/__init__.py
|
||||
src/services/gitlab_monitor/config.py
|
||||
src/services/gitlab_monitor/daemon.py
|
||||
@@ -52,6 +66,8 @@ src/services/godspeed/config.py
|
||||
src/services/godspeed/sync.py
|
||||
src/services/himalaya/__init__.py
|
||||
src/services/himalaya/client.py
|
||||
src/services/khal/__init__.py
|
||||
src/services/khal/client.py
|
||||
src/services/microsoft_graph/__init__.py
|
||||
src/services/microsoft_graph/auth.py
|
||||
src/services/microsoft_graph/calendar.py
|
||||
@@ -63,6 +79,16 @@ src/services/ticktick/__init__.py
|
||||
src/services/ticktick/auth.py
|
||||
src/services/ticktick/client.py
|
||||
src/services/ticktick/direct_client.py
|
||||
src/tasks/__init__.py
|
||||
src/tasks/app.py
|
||||
src/tasks/backend.py
|
||||
src/tasks/config.py
|
||||
src/tasks/screens/AddTaskScreen.py
|
||||
src/tasks/screens/FilterScreens.py
|
||||
src/tasks/screens/NotesEditor.py
|
||||
src/tasks/screens/__init__.py
|
||||
src/tasks/widgets/AddTaskForm.py
|
||||
src/tasks/widgets/__init__.py
|
||||
src/utils/calendar_utils.py
|
||||
src/utils/file_icons.py
|
||||
src/utils/notifications.py
|
||||
|
||||
@@ -8,6 +8,8 @@ msal>=1.32.3
|
||||
openai>=1.78.1
|
||||
orjson>=3.10.18
|
||||
pillow>=11.2.1
|
||||
pydantic>=2.0.0
|
||||
pydantic-settings>=2.0.0
|
||||
python-dateutil>=2.9.0.post0
|
||||
python-docx>=1.1.2
|
||||
requests>=2.31.0
|
||||
@@ -15,3 +17,4 @@ rich>=14.0.0
|
||||
textual>=3.2.0
|
||||
textual-image>=0.8.2
|
||||
ticktick-py>=2.0.0
|
||||
toml>=0.10.0
|
||||
|
||||
82
mail.toml.example
Normal file
82
mail.toml.example
Normal file
@@ -0,0 +1,82 @@
|
||||
# LUK Mail Configuration Example
|
||||
# Copy this file to ~/.config/luk/mail.toml and customize
|
||||
|
||||
# [task]
|
||||
# # Task management backend (taskwarrior or dstask)
|
||||
# backend = "taskwarrior"
|
||||
# taskwarrior_path = "task"
|
||||
# dstask_path = "~/.local/bin/dstask"
|
||||
|
||||
[envelope_display]
|
||||
# Sender name maximum length before truncation
|
||||
max_sender_length = 25
|
||||
|
||||
# Date/time formatting
|
||||
date_format = "%m/%d"
|
||||
time_format = "%H:%M"
|
||||
show_date = true
|
||||
show_time = true
|
||||
|
||||
# Group envelopes by date
|
||||
# "relative" = Today, Yesterday, This Week, etc.
|
||||
# "absolute" = December 2025, November 2025, etc.
|
||||
group_by = "relative"
|
||||
|
||||
# Layout: 2-line or 3-line (3-line shows preview)
|
||||
lines = 2
|
||||
show_checkbox = true
|
||||
show_preview = false
|
||||
|
||||
# NerdFont icons
|
||||
icon_unread = "\uf0e0" # nf-fa-envelope (filled)
|
||||
icon_read = "\uf2b6" # nf-fa-envelope_open (open)
|
||||
icon_flagged = "\uf024" # nf-fa-flag
|
||||
icon_attachment = "\uf0c6" # nf-fa-paperclip
|
||||
|
||||
[content_display]
|
||||
# Default view mode: "markdown" or "html"
|
||||
default_view_mode = "markdown"
|
||||
|
||||
# URL compression settings
|
||||
compress_urls = true
|
||||
max_url_length = 50
|
||||
|
||||
# Notification email compression
|
||||
# "summary" - Brief one-page summary
|
||||
# "detailed" - More details in structured format
|
||||
# "off" - Disable notification compression
|
||||
compress_notifications = true
|
||||
notification_compression_mode = "summary"
|
||||
|
||||
[link_panel]
|
||||
# Close link panel after opening a link
|
||||
close_on_open = false
|
||||
|
||||
[mail]
|
||||
# Default folder to archive messages to
|
||||
archive_folder = "Archive"
|
||||
|
||||
[keybindings]
|
||||
# Custom keybindings (leave blank to use defaults)
|
||||
# next_message = "j"
|
||||
# prev_message = "k"
|
||||
# delete = "#"
|
||||
# archive = "e"
|
||||
# open_by_id = "o"
|
||||
# quit = "q"
|
||||
# toggle_header = "h"
|
||||
# create_task = "t"
|
||||
# reload = "%"
|
||||
# toggle_sort = "s"
|
||||
# toggle_selection = "space"
|
||||
# clear_selection = "escape"
|
||||
# scroll_page_down = "pagedown"
|
||||
# scroll_page_up = "b"
|
||||
# toggle_main_content = "w"
|
||||
# open_links = "l"
|
||||
# toggle_view_mode = "m"
|
||||
|
||||
[theme]
|
||||
# Textual theme name
|
||||
# Available themes: monokai, dracula, gruvbox, nord, etc.
|
||||
theme_name = "monokai"
|
||||
@@ -27,6 +27,7 @@ dependencies = [
|
||||
"certifi>=2025.4.26",
|
||||
"click>=8.1.0",
|
||||
"html2text>=2025.4.15",
|
||||
"icalendar>=6.0.0",
|
||||
"mammoth>=1.9.0",
|
||||
"markitdown[all]>=0.1.1",
|
||||
"msal>=1.32.3",
|
||||
|
||||
6
src/calendar/__init__.py
Normal file
6
src/calendar/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
"""Calendar TUI package."""
|
||||
|
||||
from .backend import CalendarBackend, Event
|
||||
from .app import CalendarApp, run_app
|
||||
|
||||
__all__ = ["CalendarBackend", "Event", "CalendarApp", "run_app"]
|
||||
698
src/calendar/app.py
Normal file
698
src/calendar/app.py
Normal file
@@ -0,0 +1,698 @@
|
||||
"""Calendar TUI application.
|
||||
|
||||
A Textual-based TUI for viewing calendar events via khal.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
import os
|
||||
from datetime import date, datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Container, Horizontal, Vertical
|
||||
from textual.logging import TextualHandler
|
||||
from textual.widgets import Footer, Header, Static, Input
|
||||
from textual.reactive import reactive
|
||||
|
||||
from src.calendar.backend import CalendarBackend, Event
|
||||
from src.calendar.widgets.WeekGrid import WeekGrid
|
||||
from src.calendar.widgets.MonthCalendar import MonthCalendar
|
||||
from src.calendar.widgets.InvitesPanel import InvitesPanel, CalendarInvite
|
||||
from src.calendar.widgets.AddEventForm import EventFormData
|
||||
from src.utils.shared_config import get_theme_name
|
||||
from src.utils.ipc import IPCListener, IPCMessage
|
||||
|
||||
# Add the parent directory to the system path to resolve relative imports
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
logging.basicConfig(
|
||||
level="NOTSET",
|
||||
handlers=[TextualHandler()],
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CalendarStatusBar(Static):
|
||||
"""Status bar showing current week and selected event."""
|
||||
|
||||
week_label: str = ""
|
||||
event_info: str = ""
|
||||
|
||||
def render(self) -> str:
|
||||
if self.event_info:
|
||||
return f"{self.week_label} | {self.event_info}"
|
||||
return self.week_label
|
||||
|
||||
|
||||
class CalendarApp(App):
|
||||
"""A TUI for viewing calendar events via khal."""
|
||||
|
||||
CSS = """
|
||||
Screen {
|
||||
layout: vertical;
|
||||
}
|
||||
|
||||
#main-content {
|
||||
layout: horizontal;
|
||||
height: 1fr;
|
||||
}
|
||||
|
||||
#sidebar {
|
||||
width: 26;
|
||||
border-right: solid $surface-darken-1;
|
||||
background: $surface;
|
||||
padding: 1 0;
|
||||
}
|
||||
|
||||
#sidebar.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#sidebar-calendar {
|
||||
height: auto;
|
||||
}
|
||||
|
||||
#sidebar-invites {
|
||||
height: auto;
|
||||
margin-top: 1;
|
||||
border-top: solid $surface-darken-1;
|
||||
padding-top: 1;
|
||||
}
|
||||
|
||||
#week-grid {
|
||||
height: 1fr;
|
||||
}
|
||||
|
||||
#week-grid > WeekGridHeader {
|
||||
height: 1;
|
||||
dock: top;
|
||||
background: $surface;
|
||||
}
|
||||
|
||||
#week-grid > WeekGridBody {
|
||||
height: 1fr;
|
||||
}
|
||||
|
||||
#status-bar {
|
||||
dock: bottom;
|
||||
height: 1;
|
||||
background: $surface;
|
||||
color: $text-muted;
|
||||
padding: 0 1;
|
||||
}
|
||||
|
||||
#event-detail {
|
||||
dock: bottom;
|
||||
height: auto;
|
||||
max-height: 12;
|
||||
border-top: solid $primary;
|
||||
padding: 1;
|
||||
background: $surface;
|
||||
}
|
||||
|
||||
#event-detail.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#search-container {
|
||||
dock: top;
|
||||
height: 4;
|
||||
width: 100%;
|
||||
background: $surface;
|
||||
border-bottom: solid $primary;
|
||||
padding: 0 1;
|
||||
align: left middle;
|
||||
}
|
||||
|
||||
#search-container.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#search-container .search-label {
|
||||
width: auto;
|
||||
padding: 0 1;
|
||||
color: $primary;
|
||||
}
|
||||
|
||||
#search-input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
#search-results {
|
||||
dock: bottom;
|
||||
height: 40%;
|
||||
border-top: solid $primary;
|
||||
background: $surface;
|
||||
padding: 1;
|
||||
}
|
||||
|
||||
#search-results.hidden {
|
||||
display: none;
|
||||
}
|
||||
"""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("q", "quit", "Quit", show=True),
|
||||
Binding("j", "cursor_down", "Down", show=False),
|
||||
Binding("k", "cursor_up", "Up", show=False),
|
||||
Binding("h", "cursor_left", "Left", show=False),
|
||||
Binding("l", "cursor_right", "Right", show=False),
|
||||
Binding("H", "prev_week", "Prev Week", show=True),
|
||||
Binding("L", "next_week", "Next Week", show=True),
|
||||
Binding("g", "goto_today", "Today", show=True),
|
||||
Binding("w", "toggle_weekends", "Weekends", show=True),
|
||||
Binding("s", "toggle_sidebar", "Sidebar", show=True),
|
||||
Binding("i", "focus_invites", "Invites", show=True),
|
||||
Binding("r", "refresh", "Refresh", show=True),
|
||||
Binding("enter", "view_event", "View", show=True),
|
||||
Binding("a", "add_event", "Add", show=True),
|
||||
Binding("slash", "search", "Search", show=True),
|
||||
Binding("escape", "clear_search", "Clear Search", show=False),
|
||||
Binding("?", "help", "Help", show=True),
|
||||
]
|
||||
|
||||
# Reactive attributes
|
||||
include_weekends: reactive[bool] = reactive(True)
|
||||
show_sidebar: reactive[bool] = reactive(True)
|
||||
|
||||
# Instance attributes
|
||||
backend: Optional[CalendarBackend]
|
||||
_invites: list[CalendarInvite]
|
||||
_search_results: list[Event]
|
||||
|
||||
def __init__(self, backend: Optional[CalendarBackend] = None):
|
||||
super().__init__()
|
||||
self._invites = []
|
||||
self._search_results = []
|
||||
|
||||
if backend:
|
||||
self.backend = backend
|
||||
else:
|
||||
# Create backend from config (default: khal)
|
||||
from src.services.khal import KhalClient
|
||||
|
||||
self.backend = KhalClient()
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Create the app layout."""
|
||||
yield Header()
|
||||
yield Horizontal(
|
||||
Static("\uf002 Search:", classes="search-label"), # nf-fa-search
|
||||
Input(placeholder="Search events...", id="search-input", disabled=True),
|
||||
id="search-container",
|
||||
classes="hidden",
|
||||
)
|
||||
with Horizontal(id="main-content"):
|
||||
with Vertical(id="sidebar"):
|
||||
yield MonthCalendar(id="sidebar-calendar")
|
||||
yield InvitesPanel(id="sidebar-invites")
|
||||
yield WeekGrid(id="week-grid")
|
||||
yield Static(id="search-results", classes="hidden")
|
||||
yield Static(id="event-detail", classes="hidden")
|
||||
yield CalendarStatusBar(id="status-bar")
|
||||
yield Footer()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Initialize the app on mount."""
|
||||
self.theme = get_theme_name()
|
||||
|
||||
# Start IPC listener for refresh notifications from sync daemon
|
||||
self._ipc_listener = IPCListener("calendar", self._on_ipc_message)
|
||||
self._ipc_listener.start()
|
||||
|
||||
# Load events for current week
|
||||
self.load_events()
|
||||
|
||||
# Sync sidebar calendar with current week
|
||||
self._sync_sidebar_calendar()
|
||||
|
||||
# Load invites in background
|
||||
self.run_worker(self._load_invites_async(), exclusive=True)
|
||||
|
||||
# Update status bar and title
|
||||
self._update_status()
|
||||
self._update_title()
|
||||
|
||||
# Focus the week grid (not the hidden search input)
|
||||
self.query_one("#week-grid", WeekGrid).focus()
|
||||
|
||||
def _on_ipc_message(self, message: IPCMessage) -> None:
|
||||
"""Handle IPC messages from sync daemon."""
|
||||
if message.event == "refresh":
|
||||
# Schedule a reload on the main thread
|
||||
self.call_from_thread(self.load_events)
|
||||
|
||||
async def _load_invites_async(self) -> None:
|
||||
"""Load pending calendar invites from Microsoft Graph."""
|
||||
try:
|
||||
from src.services.microsoft_graph.auth import get_access_token
|
||||
from src.services.microsoft_graph.calendar import fetch_pending_invites
|
||||
from dateutil import parser as date_parser
|
||||
|
||||
# Get auth token
|
||||
scopes = ["https://graph.microsoft.com/Calendars.Read"]
|
||||
_, headers = get_access_token(scopes)
|
||||
|
||||
# Fetch invites
|
||||
raw_invites = await fetch_pending_invites(headers, days_forward=30)
|
||||
|
||||
# Convert to CalendarInvite objects
|
||||
invites = []
|
||||
for inv in raw_invites:
|
||||
try:
|
||||
start_str = inv.get("start", {}).get("dateTime", "")
|
||||
end_str = inv.get("end", {}).get("dateTime", "")
|
||||
start_dt = (
|
||||
date_parser.parse(start_str) if start_str else datetime.now()
|
||||
)
|
||||
end_dt = date_parser.parse(end_str) if end_str else start_dt
|
||||
|
||||
organizer_data = inv.get("organizer", {}).get("emailAddress", {})
|
||||
organizer_name = organizer_data.get(
|
||||
"name", organizer_data.get("address", "Unknown")
|
||||
)
|
||||
|
||||
invite = CalendarInvite(
|
||||
id=inv.get("id", ""),
|
||||
subject=inv.get("subject", "No Subject"),
|
||||
organizer=organizer_name,
|
||||
start=start_dt,
|
||||
end=end_dt,
|
||||
location=inv.get("location", {}).get("displayName"),
|
||||
is_all_day=inv.get("isAllDay", False),
|
||||
response_status=inv.get("responseStatus", {}).get(
|
||||
"response", "notResponded"
|
||||
),
|
||||
)
|
||||
invites.append(invite)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to parse invite: {e}")
|
||||
|
||||
# Update the panel
|
||||
self._invites = invites
|
||||
self.call_from_thread(self._update_invites_panel)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to load invites: {e}")
|
||||
# Silently fail - invites are optional
|
||||
|
||||
def _update_invites_panel(self) -> None:
|
||||
"""Update the invites panel with loaded invites."""
|
||||
try:
|
||||
panel = self.query_one("#sidebar-invites", InvitesPanel)
|
||||
panel.set_invites(self._invites)
|
||||
if self._invites:
|
||||
self.notify(f"Loaded {len(self._invites)} pending invite(s)")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _sync_sidebar_calendar(self) -> None:
|
||||
"""Sync the sidebar calendar with the main week grid."""
|
||||
try:
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
calendar = self.query_one("#sidebar-calendar", MonthCalendar)
|
||||
calendar.update_week(grid.week_start)
|
||||
calendar.update_selected(grid.get_cursor_date())
|
||||
except Exception:
|
||||
pass # Sidebar might not exist yet
|
||||
|
||||
def load_events(self) -> None:
|
||||
"""Load events from backend for the current week."""
|
||||
if not self.backend:
|
||||
return
|
||||
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
week_start = grid.week_start
|
||||
|
||||
# Get events using backend's helper method
|
||||
events_by_date = self.backend.get_week_events(
|
||||
week_start, include_weekends=self.include_weekends
|
||||
)
|
||||
|
||||
# Set events on grid
|
||||
grid.set_events(events_by_date)
|
||||
|
||||
# Update status bar with week label
|
||||
self._update_status()
|
||||
|
||||
def _update_status(self) -> None:
|
||||
"""Update the status bar."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
status = self.query_one("#status-bar", CalendarStatusBar)
|
||||
|
||||
# Week label
|
||||
week_start = grid.week_start
|
||||
week_end = week_start + timedelta(days=6)
|
||||
status.week_label = (
|
||||
f"Week of {week_start.strftime('%b %d')} - {week_end.strftime('%b %d, %Y')}"
|
||||
)
|
||||
|
||||
# Event info
|
||||
event = grid.get_event_at_cursor()
|
||||
if event:
|
||||
time_str = event.start.strftime("%H:%M") + "-" + event.end.strftime("%H:%M")
|
||||
status.event_info = f"{time_str} {event.title}"
|
||||
else:
|
||||
status.event_info = ""
|
||||
|
||||
status.refresh()
|
||||
|
||||
# Also update title when status changes
|
||||
self._update_title()
|
||||
|
||||
def _update_title(self) -> None:
|
||||
"""Update the app title with full date range and week number."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
week_start = grid.week_start
|
||||
week_end = week_start + timedelta(days=6)
|
||||
week_num = week_start.isocalendar()[1]
|
||||
|
||||
# Format: "2025 December 14 - 20 (Week 48)"
|
||||
if week_start.month == week_end.month:
|
||||
# Same month
|
||||
self.title = (
|
||||
f"{week_start.year} {week_start.strftime('%B')} "
|
||||
f"{week_start.day} - {week_end.day} (Week {week_num})"
|
||||
)
|
||||
else:
|
||||
# Different months
|
||||
self.title = (
|
||||
f"{week_start.strftime('%B %d')} - "
|
||||
f"{week_end.strftime('%B %d, %Y')} (Week {week_num})"
|
||||
)
|
||||
|
||||
def _update_event_detail(self, event: Optional[Event]) -> None:
|
||||
"""Update the event detail pane."""
|
||||
detail = self.query_one("#event-detail", Static)
|
||||
|
||||
if event:
|
||||
detail.remove_class("hidden")
|
||||
|
||||
# Format event details
|
||||
date_str = event.start.strftime("%A, %B %d")
|
||||
time_str = (
|
||||
event.start.strftime("%H:%M") + " - " + event.end.strftime("%H:%M")
|
||||
)
|
||||
duration = event.duration_minutes
|
||||
hours, mins = divmod(duration, 60)
|
||||
dur_str = f"{hours}h {mins}m" if hours else f"{mins}m"
|
||||
|
||||
lines = [
|
||||
f"[bold]{event.title}[/bold]",
|
||||
f"{date_str}",
|
||||
f"{time_str} ({dur_str})",
|
||||
]
|
||||
if event.location:
|
||||
lines.append(f"[dim]Location:[/dim] {event.location}")
|
||||
if event.organizer:
|
||||
lines.append(f"[dim]Organizer:[/dim] {event.organizer}")
|
||||
if event.categories:
|
||||
lines.append(f"[dim]Categories:[/dim] {event.categories}")
|
||||
if event.url:
|
||||
lines.append(f"[dim]URL:[/dim] {event.url}")
|
||||
if event.status:
|
||||
lines.append(f"[dim]Status:[/dim] {event.status}")
|
||||
if event.recurring:
|
||||
lines.append("[dim]Recurring:[/dim] Yes")
|
||||
if event.description:
|
||||
# Truncate long descriptions
|
||||
desc = (
|
||||
event.description[:200] + "..."
|
||||
if len(event.description) > 200
|
||||
else event.description
|
||||
)
|
||||
lines.append(f"[dim]Description:[/dim] {desc}")
|
||||
|
||||
detail.update("\n".join(lines))
|
||||
else:
|
||||
detail.add_class("hidden")
|
||||
|
||||
# Handle WeekGrid messages
|
||||
def on_week_grid_week_changed(self, message: WeekGrid.WeekChanged) -> None:
|
||||
"""Handle week change - reload events."""
|
||||
self.load_events()
|
||||
self._sync_sidebar_calendar()
|
||||
|
||||
def on_week_grid_event_selected(self, message: WeekGrid.EventSelected) -> None:
|
||||
"""Handle event selection."""
|
||||
self._update_event_detail(message.event)
|
||||
|
||||
# Handle MonthCalendar messages
|
||||
def on_month_calendar_date_selected(
|
||||
self, message: MonthCalendar.DateSelected
|
||||
) -> None:
|
||||
"""Handle date selection from sidebar calendar."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.goto_date(message.date)
|
||||
self.load_events()
|
||||
self._sync_sidebar_calendar()
|
||||
|
||||
# Navigation actions (forwarded to grid)
|
||||
def action_cursor_down(self) -> None:
|
||||
"""Move cursor down."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.action_cursor_down()
|
||||
self._update_status()
|
||||
|
||||
def action_cursor_up(self) -> None:
|
||||
"""Move cursor up."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.action_cursor_up()
|
||||
self._update_status()
|
||||
|
||||
def action_cursor_left(self) -> None:
|
||||
"""Move cursor left."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.action_cursor_left()
|
||||
self._update_status()
|
||||
|
||||
def action_cursor_right(self) -> None:
|
||||
"""Move cursor right."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.action_cursor_right()
|
||||
self._update_status()
|
||||
|
||||
def action_prev_week(self) -> None:
|
||||
"""Navigate to previous week."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.action_prev_week()
|
||||
|
||||
def action_next_week(self) -> None:
|
||||
"""Navigate to next week."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.action_next_week()
|
||||
|
||||
def action_goto_today(self) -> None:
|
||||
"""Navigate to today."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.action_goto_today()
|
||||
self.load_events()
|
||||
|
||||
def action_toggle_weekends(self) -> None:
|
||||
"""Toggle weekend display."""
|
||||
self.include_weekends = not self.include_weekends
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.include_weekends = self.include_weekends
|
||||
self.load_events()
|
||||
|
||||
mode = "7 days" if self.include_weekends else "5 days (weekdays)"
|
||||
self.notify(f"Showing {mode}")
|
||||
|
||||
def action_toggle_sidebar(self) -> None:
|
||||
"""Toggle sidebar visibility."""
|
||||
self.show_sidebar = not self.show_sidebar
|
||||
sidebar = self.query_one("#sidebar", Vertical)
|
||||
if self.show_sidebar:
|
||||
sidebar.remove_class("hidden")
|
||||
else:
|
||||
sidebar.add_class("hidden")
|
||||
|
||||
def action_refresh(self) -> None:
|
||||
"""Refresh events from backend."""
|
||||
self.load_events()
|
||||
self.notify("Refreshed")
|
||||
|
||||
def action_view_event(self) -> None:
|
||||
"""View the selected event details."""
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
event = grid.get_event_at_cursor()
|
||||
if event:
|
||||
self._update_event_detail(event)
|
||||
else:
|
||||
self.notify("No event at cursor")
|
||||
|
||||
def action_add_event(self) -> None:
|
||||
"""Open the add event modal."""
|
||||
from src.calendar.screens.AddEventScreen import AddEventScreen
|
||||
|
||||
# Get calendars from backend
|
||||
calendars: list[str] = []
|
||||
if self.backend:
|
||||
try:
|
||||
calendars = self.backend.get_calendars()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Get current cursor date/time for initial values
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
cursor_date = grid.get_cursor_date()
|
||||
cursor_time = grid.get_cursor_time()
|
||||
|
||||
def handle_result(data: EventFormData | None) -> None:
|
||||
if data is None:
|
||||
return
|
||||
|
||||
if not self.backend:
|
||||
self.notify("No calendar backend available", severity="error")
|
||||
return
|
||||
|
||||
try:
|
||||
self.backend.create_event(
|
||||
title=data.title,
|
||||
start=data.start_datetime,
|
||||
end=data.end_datetime,
|
||||
calendar=data.calendar,
|
||||
location=data.location,
|
||||
description=data.description,
|
||||
all_day=data.all_day,
|
||||
)
|
||||
self.notify(f"Created event: {data.title}")
|
||||
self.load_events() # Refresh to show new event
|
||||
except Exception as e:
|
||||
self.notify(f"Failed to create event: {e}", severity="error")
|
||||
|
||||
self.push_screen(
|
||||
AddEventScreen(
|
||||
calendars=calendars,
|
||||
initial_date=cursor_date,
|
||||
initial_time=cursor_time,
|
||||
),
|
||||
handle_result,
|
||||
)
|
||||
|
||||
def action_help(self) -> None:
|
||||
"""Show help."""
|
||||
help_text = """
|
||||
Keybindings:
|
||||
j/k - Move cursor up/down (time)
|
||||
h/l - Move cursor left/right (day)
|
||||
H/L - Previous/Next week
|
||||
g - Go to today
|
||||
w - Toggle weekends (5/7 days)
|
||||
s - Toggle sidebar
|
||||
i - Focus invites panel
|
||||
/ - Search events
|
||||
Esc - Clear search
|
||||
Enter - View event details
|
||||
a - Add new event
|
||||
r - Refresh
|
||||
q - Quit
|
||||
"""
|
||||
self.notify(help_text.strip(), timeout=10)
|
||||
|
||||
# Search actions
|
||||
def action_search(self) -> None:
|
||||
"""Show search input and focus it."""
|
||||
search_container = self.query_one("#search-container")
|
||||
search_container.remove_class("hidden")
|
||||
search_input = self.query_one("#search-input", Input)
|
||||
search_input.disabled = False
|
||||
search_input.focus()
|
||||
|
||||
def action_clear_search(self) -> None:
|
||||
"""Clear search and hide search UI."""
|
||||
search_container = self.query_one("#search-container")
|
||||
search_results = self.query_one("#search-results", Static)
|
||||
search_input = self.query_one("#search-input", Input)
|
||||
|
||||
# Only act if search is visible
|
||||
if not search_container.has_class("hidden") or not search_results.has_class(
|
||||
"hidden"
|
||||
):
|
||||
search_input.value = ""
|
||||
search_input.disabled = True
|
||||
search_container.add_class("hidden")
|
||||
search_results.add_class("hidden")
|
||||
self._search_results = []
|
||||
# Focus back to grid
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.focus()
|
||||
|
||||
def on_input_submitted(self, event: Input.Submitted) -> None:
|
||||
"""Handle Enter in search input - perform search."""
|
||||
if event.input.id != "search-input":
|
||||
return
|
||||
|
||||
query = event.value.strip()
|
||||
if not query:
|
||||
return
|
||||
|
||||
self._perform_search(query)
|
||||
|
||||
def _perform_search(self, query: str) -> None:
|
||||
"""Perform event search and display results."""
|
||||
if not self.backend:
|
||||
return
|
||||
|
||||
# Check if backend has search_events method
|
||||
if not hasattr(self.backend, "search_events"):
|
||||
self.notify(
|
||||
"Search not supported by this calendar backend", severity="warning"
|
||||
)
|
||||
return
|
||||
|
||||
results = self.backend.search_events(query)
|
||||
self._search_results = results
|
||||
|
||||
# Update results display
|
||||
search_results = self.query_one("#search-results", Static)
|
||||
|
||||
if results:
|
||||
lines = [f"[b]Search results for '{query}': {len(results)} found[/b]", ""]
|
||||
for event in results[:20]: # Limit display to 20 results
|
||||
date_str = event.start.strftime("%Y-%m-%d %H:%M")
|
||||
lines.append(f" {date_str} [b]{event.title}[/b]")
|
||||
if event.location:
|
||||
lines.append(f" [dim]{event.location}[/dim]")
|
||||
if len(results) > 20:
|
||||
lines.append(f" ... and {len(results) - 20} more")
|
||||
search_results.update("\n".join(lines))
|
||||
search_results.remove_class("hidden")
|
||||
self.notify(f"Found {len(results)} event(s)")
|
||||
else:
|
||||
search_results.update(f"[b]No events found matching '{query}'[/b]")
|
||||
search_results.remove_class("hidden")
|
||||
self.notify("No events found")
|
||||
|
||||
# Focus back to grid
|
||||
grid = self.query_one("#week-grid", WeekGrid)
|
||||
grid.focus()
|
||||
|
||||
async def action_quit(self) -> None:
|
||||
"""Quit the app and clean up IPC listener."""
|
||||
if hasattr(self, "_ipc_listener"):
|
||||
self._ipc_listener.stop()
|
||||
self.exit()
|
||||
|
||||
def action_focus_invites(self) -> None:
|
||||
"""Focus on the invites panel and show invite count."""
|
||||
if not self.show_sidebar:
|
||||
self.action_toggle_sidebar()
|
||||
|
||||
if self._invites:
|
||||
self.notify(f"You have {len(self._invites)} pending invite(s)")
|
||||
else:
|
||||
self.notify("No pending invites")
|
||||
|
||||
|
||||
def run_app(backend: Optional[CalendarBackend] = None) -> None:
|
||||
"""Run the Calendar TUI application."""
|
||||
app = CalendarApp(backend=backend)
|
||||
app.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_app()
|
||||
232
src/calendar/backend.py
Normal file
232
src/calendar/backend.py
Normal file
@@ -0,0 +1,232 @@
|
||||
"""Calendar backend abstraction for Calendar TUI.
|
||||
|
||||
This module defines the abstract interface that all calendar backends must implement,
|
||||
allowing the TUI to work with different calendar systems (khal, calcurse, etc.)
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, date, time, timedelta
|
||||
from typing import Optional, List, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class Event:
|
||||
"""Unified calendar event representation across backends."""
|
||||
|
||||
uid: str
|
||||
title: str
|
||||
start: datetime
|
||||
end: datetime
|
||||
location: str = ""
|
||||
description: str = ""
|
||||
calendar: str = ""
|
||||
all_day: bool = False
|
||||
recurring: bool = False
|
||||
organizer: str = ""
|
||||
url: str = ""
|
||||
categories: str = ""
|
||||
status: str = "" # CONFIRMED, TENTATIVE, CANCELLED
|
||||
|
||||
@property
|
||||
def duration_minutes(self) -> int:
|
||||
"""Get duration in minutes."""
|
||||
delta = self.end - self.start
|
||||
return int(delta.total_seconds() / 60)
|
||||
|
||||
@property
|
||||
def start_time(self) -> time:
|
||||
"""Get start time."""
|
||||
return self.start.time()
|
||||
|
||||
@property
|
||||
def end_time(self) -> time:
|
||||
"""Get end time."""
|
||||
return self.end.time()
|
||||
|
||||
@property
|
||||
def date(self) -> date:
|
||||
"""Get the date of the event."""
|
||||
return self.start.date()
|
||||
|
||||
def overlaps(self, other: "Event") -> bool:
|
||||
"""Check if this event overlaps with another."""
|
||||
return self.start < other.end and self.end > other.start
|
||||
|
||||
def get_row_span(self, minutes_per_row: int = 30) -> Tuple[int, int]:
|
||||
"""Get the row range for this event in a grid.
|
||||
|
||||
Args:
|
||||
minutes_per_row: Minutes each row represents (default 30)
|
||||
|
||||
Returns:
|
||||
Tuple of (start_row, end_row) where rows are 0-indexed from midnight
|
||||
"""
|
||||
start_minutes = self.start.hour * 60 + self.start.minute
|
||||
end_minutes = self.end.hour * 60 + self.end.minute
|
||||
|
||||
# Handle events ending at midnight (next day)
|
||||
if end_minutes == 0 and self.end.date() > self.start.date():
|
||||
end_minutes = 24 * 60
|
||||
|
||||
start_row = start_minutes // minutes_per_row
|
||||
end_row = (end_minutes + minutes_per_row - 1) // minutes_per_row # Round up
|
||||
|
||||
return start_row, end_row
|
||||
|
||||
|
||||
class CalendarBackend(ABC):
|
||||
"""Abstract base class for calendar backends."""
|
||||
|
||||
@abstractmethod
|
||||
def get_events(
|
||||
self,
|
||||
start_date: date,
|
||||
end_date: date,
|
||||
calendar: Optional[str] = None,
|
||||
) -> List[Event]:
|
||||
"""Get events in a date range.
|
||||
|
||||
Args:
|
||||
start_date: Start of range (inclusive)
|
||||
end_date: End of range (inclusive)
|
||||
calendar: Optional calendar name to filter by
|
||||
|
||||
Returns:
|
||||
List of events in the range, sorted by start time
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_event(self, uid: str) -> Optional[Event]:
|
||||
"""Get a single event by UID.
|
||||
|
||||
Args:
|
||||
uid: Event unique identifier
|
||||
|
||||
Returns:
|
||||
Event if found, None otherwise
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_calendars(self) -> List[str]:
|
||||
"""Get list of available calendar names.
|
||||
|
||||
Returns:
|
||||
List of calendar names
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def create_event(
|
||||
self,
|
||||
title: str,
|
||||
start: datetime,
|
||||
end: datetime,
|
||||
calendar: Optional[str] = None,
|
||||
location: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
all_day: bool = False,
|
||||
) -> Event:
|
||||
"""Create a new event.
|
||||
|
||||
Args:
|
||||
title: Event title
|
||||
start: Start datetime
|
||||
end: End datetime
|
||||
calendar: Calendar to add event to
|
||||
location: Event location
|
||||
description: Event description
|
||||
all_day: Whether this is an all-day event
|
||||
|
||||
Returns:
|
||||
The created event
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def delete_event(self, uid: str) -> bool:
|
||||
"""Delete an event.
|
||||
|
||||
Args:
|
||||
uid: Event unique identifier
|
||||
|
||||
Returns:
|
||||
True if deleted successfully
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def update_event(
|
||||
self,
|
||||
uid: str,
|
||||
title: Optional[str] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
location: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
) -> Optional[Event]:
|
||||
"""Update an existing event.
|
||||
|
||||
Args:
|
||||
uid: Event unique identifier
|
||||
title: New title (if provided)
|
||||
start: New start time (if provided)
|
||||
end: New end time (if provided)
|
||||
location: New location (if provided)
|
||||
description: New description (if provided)
|
||||
|
||||
Returns:
|
||||
Updated event if successful, None otherwise
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_week_events(
|
||||
self,
|
||||
week_start: date,
|
||||
include_weekends: bool = True,
|
||||
) -> dict[date, List[Event]]:
|
||||
"""Get events for a week, grouped by date.
|
||||
|
||||
Args:
|
||||
week_start: First day of the week
|
||||
include_weekends: Whether to include Saturday/Sunday
|
||||
|
||||
Returns:
|
||||
Dict mapping dates to lists of events
|
||||
"""
|
||||
days = 7 if include_weekends else 5
|
||||
end_date = week_start + timedelta(days=days - 1)
|
||||
events = self.get_events(week_start, end_date)
|
||||
|
||||
# Group by date
|
||||
by_date: dict[date, List[Event]] = {}
|
||||
for i in range(days):
|
||||
d = week_start + timedelta(days=i)
|
||||
by_date[d] = []
|
||||
|
||||
for event in events:
|
||||
event_date = event.date
|
||||
if event_date in by_date:
|
||||
by_date[event_date].append(event)
|
||||
|
||||
# Sort each day's events by start time
|
||||
for d in by_date:
|
||||
by_date[d].sort(key=lambda e: e.start)
|
||||
|
||||
return by_date
|
||||
|
||||
def search_events(self, query: str) -> List[Event]:
|
||||
"""Search for events matching a query string.
|
||||
|
||||
Default implementation returns empty list. Override in subclasses
|
||||
that support search.
|
||||
|
||||
Args:
|
||||
query: Search string to match against event titles and descriptions
|
||||
|
||||
Returns:
|
||||
List of matching events
|
||||
"""
|
||||
return []
|
||||
110
src/calendar/config.py
Normal file
110
src/calendar/config.py
Normal file
@@ -0,0 +1,110 @@
|
||||
"""Calendar TUI configuration."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
try:
|
||||
import toml
|
||||
except ImportError:
|
||||
toml = None # type: ignore
|
||||
|
||||
|
||||
# Default configuration values
|
||||
DEFAULT_CONFIG = {
|
||||
"display": {
|
||||
"work_day_start_hour": 7, # 7 AM
|
||||
"work_day_end_hour": 19, # 7 PM
|
||||
"include_weekends": True,
|
||||
"minutes_per_row": 30,
|
||||
"day_column_width": 20,
|
||||
"week_start_day": 0, # 0=Sunday, 1=Monday, ..., 6=Saturday
|
||||
},
|
||||
"backend": {
|
||||
"type": "khal", # khal, calcurse, etc.
|
||||
"calendar_path": "~/Calendar/corteva",
|
||||
},
|
||||
"theme": {
|
||||
"event_color": "blue",
|
||||
"overlap_color": "dark_orange",
|
||||
"cursor_style": "reverse",
|
||||
"work_hours_time_color": "blue",
|
||||
"off_hours_time_color": "bright_black",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def get_config_path() -> Path:
|
||||
"""Get the calendar config file path."""
|
||||
# Check XDG_CONFIG_HOME first, then fall back to ~/.config
|
||||
config_home = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
|
||||
return Path(config_home) / "luk" / "calendar.toml"
|
||||
|
||||
|
||||
def load_config() -> dict:
|
||||
"""Load calendar configuration from TOML file.
|
||||
|
||||
Returns merged config with defaults for any missing values.
|
||||
"""
|
||||
config = DEFAULT_CONFIG.copy()
|
||||
|
||||
config_path = get_config_path()
|
||||
if config_path.exists() and toml is not None:
|
||||
try:
|
||||
user_config = toml.load(config_path)
|
||||
# Deep merge user config into defaults
|
||||
for section, values in user_config.items():
|
||||
if section in config and isinstance(config[section], dict):
|
||||
config[section].update(values)
|
||||
else:
|
||||
config[section] = values
|
||||
except Exception:
|
||||
pass # Use defaults on error
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def get_display_config() -> dict:
|
||||
"""Get display-related configuration."""
|
||||
return load_config().get("display", DEFAULT_CONFIG["display"])
|
||||
|
||||
|
||||
def get_backend_config() -> dict:
|
||||
"""Get backend-related configuration."""
|
||||
return load_config().get("backend", DEFAULT_CONFIG["backend"])
|
||||
|
||||
|
||||
def get_theme_config() -> dict:
|
||||
"""Get theme-related configuration."""
|
||||
return load_config().get("theme", DEFAULT_CONFIG["theme"])
|
||||
|
||||
|
||||
# Convenience accessors
|
||||
def work_day_start_hour() -> int:
|
||||
"""Get the work day start hour (for initial scroll position)."""
|
||||
return get_display_config().get("work_day_start_hour", 7)
|
||||
|
||||
|
||||
def work_day_end_hour() -> int:
|
||||
"""Get the work day end hour."""
|
||||
return get_display_config().get("work_day_end_hour", 19)
|
||||
|
||||
|
||||
def include_weekends_default() -> bool:
|
||||
"""Get default for including weekends."""
|
||||
return get_display_config().get("include_weekends", True)
|
||||
|
||||
|
||||
def minutes_per_row() -> int:
|
||||
"""Get minutes per row (default 30)."""
|
||||
return get_display_config().get("minutes_per_row", 30)
|
||||
|
||||
|
||||
def day_column_width() -> int:
|
||||
"""Get day column width."""
|
||||
return get_display_config().get("day_column_width", 20)
|
||||
|
||||
|
||||
def week_start_day() -> int:
|
||||
"""Get the week start day (0=Sunday, 1=Monday, ..., 6=Saturday)."""
|
||||
return get_display_config().get("week_start_day", 0)
|
||||
155
src/calendar/screens/AddEventScreen.py
Normal file
155
src/calendar/screens/AddEventScreen.py
Normal file
@@ -0,0 +1,155 @@
|
||||
"""Add Event modal screen for Calendar TUI."""
|
||||
|
||||
from datetime import date, time
|
||||
from typing import Optional
|
||||
|
||||
from textual import on
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Button, Input, Label
|
||||
|
||||
from src.calendar.widgets.AddEventForm import AddEventForm, EventFormData
|
||||
|
||||
|
||||
class AddEventScreen(ModalScreen[Optional[EventFormData]]):
|
||||
"""Modal screen for adding a new calendar event."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("ctrl+s", "submit", "Save"),
|
||||
]
|
||||
|
||||
DEFAULT_CSS = """
|
||||
AddEventScreen {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
AddEventScreen #add-event-container {
|
||||
width: 80%;
|
||||
height: auto;
|
||||
max-height: 85%;
|
||||
background: $surface;
|
||||
border: thick $primary;
|
||||
padding: 1;
|
||||
}
|
||||
|
||||
AddEventScreen #add-event-title {
|
||||
text-style: bold;
|
||||
width: 100%;
|
||||
height: 1;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
AddEventScreen #add-event-content {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
AddEventScreen #add-event-form {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddEventScreen #add-event-sidebar {
|
||||
width: 16;
|
||||
height: auto;
|
||||
padding: 1;
|
||||
align: center top;
|
||||
}
|
||||
|
||||
AddEventScreen #add-event-sidebar Button {
|
||||
width: 100%;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
AddEventScreen #help-text {
|
||||
width: 100%;
|
||||
height: 1;
|
||||
color: $text-muted;
|
||||
text-align: center;
|
||||
margin-top: 1;
|
||||
}
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
calendars: list[str] | None = None,
|
||||
initial_date: date | None = None,
|
||||
initial_time: time | None = None,
|
||||
initial_data: EventFormData | None = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the add event screen.
|
||||
|
||||
Args:
|
||||
calendars: List of available calendar names for the dropdown
|
||||
initial_date: Pre-populate with this date
|
||||
initial_time: Pre-populate with this time
|
||||
initial_data: Pre-populate form with this data (overrides date/time)
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._calendars = calendars or []
|
||||
self._initial_date = initial_date
|
||||
self._initial_time = initial_time
|
||||
self._initial_data = initial_data
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical(id="add-event-container"):
|
||||
yield Label("Add New Event", id="add-event-title")
|
||||
|
||||
with Horizontal(id="add-event-content"):
|
||||
yield AddEventForm(
|
||||
calendars=self._calendars,
|
||||
initial_date=self._initial_date,
|
||||
initial_time=self._initial_time,
|
||||
initial_data=self._initial_data,
|
||||
id="add-event-form",
|
||||
)
|
||||
|
||||
with Vertical(id="add-event-sidebar"):
|
||||
yield Button("Create", id="create", variant="primary")
|
||||
yield Button("Cancel", id="cancel", variant="default")
|
||||
|
||||
yield Label("Ctrl+S to save, Escape to cancel", id="help-text")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the title input."""
|
||||
try:
|
||||
form = self.query_one("#add-event-form", AddEventForm)
|
||||
title_input = form.query_one("#title-input")
|
||||
title_input.focus()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@on(Button.Pressed, "#create")
|
||||
def handle_create(self) -> None:
|
||||
"""Handle create button press."""
|
||||
self.action_submit()
|
||||
|
||||
@on(Button.Pressed, "#cancel")
|
||||
def handle_cancel(self) -> None:
|
||||
"""Handle cancel button press."""
|
||||
self.action_cancel()
|
||||
|
||||
@on(Input.Submitted, "#title-input")
|
||||
def handle_title_submit(self) -> None:
|
||||
"""Handle Enter key in title input."""
|
||||
self.action_submit()
|
||||
|
||||
def action_submit(self) -> None:
|
||||
"""Validate and submit the form."""
|
||||
form = self.query_one("#add-event-form", AddEventForm)
|
||||
is_valid, error = form.validate()
|
||||
|
||||
if not is_valid:
|
||||
self.notify(error, severity="error")
|
||||
return
|
||||
|
||||
data = form.get_form_data()
|
||||
self.dismiss(data)
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
"""Cancel and dismiss."""
|
||||
self.dismiss(None)
|
||||
5
src/calendar/screens/__init__.py
Normal file
5
src/calendar/screens/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Calendar TUI screens."""
|
||||
|
||||
from .AddEventScreen import AddEventScreen
|
||||
|
||||
__all__ = ["AddEventScreen"]
|
||||
380
src/calendar/widgets/AddEventForm.py
Normal file
380
src/calendar/widgets/AddEventForm.py
Normal file
@@ -0,0 +1,380 @@
|
||||
"""Reusable Add Event form widget for Calendar TUI.
|
||||
|
||||
This widget can be used standalone in modals or embedded in other screens.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, date, time, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Horizontal, Vertical, ScrollableContainer
|
||||
from textual.message import Message
|
||||
from textual.widget import Widget
|
||||
from textual.widgets import Input, Label, Select, TextArea, Checkbox, MaskedInput
|
||||
|
||||
|
||||
@dataclass
|
||||
class EventFormData:
|
||||
"""Data from the add event form."""
|
||||
|
||||
title: str
|
||||
start_date: date
|
||||
start_time: time
|
||||
end_date: date
|
||||
end_time: time
|
||||
location: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
calendar: Optional[str] = None
|
||||
all_day: bool = False
|
||||
|
||||
@property
|
||||
def start_datetime(self) -> datetime:
|
||||
"""Get start as datetime."""
|
||||
return datetime.combine(self.start_date, self.start_time)
|
||||
|
||||
@property
|
||||
def end_datetime(self) -> datetime:
|
||||
"""Get end as datetime."""
|
||||
return datetime.combine(self.end_date, self.end_time)
|
||||
|
||||
|
||||
class AddEventForm(Widget):
|
||||
"""A reusable form widget for creating/editing calendar events.
|
||||
|
||||
This widget emits EventFormData when submitted and can be embedded
|
||||
in various contexts (modal screens, sidebars, etc.)
|
||||
"""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
AddEventForm {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
padding: 1;
|
||||
}
|
||||
|
||||
AddEventForm ScrollableContainer {
|
||||
height: auto;
|
||||
max-height: 100%;
|
||||
}
|
||||
|
||||
AddEventForm .form-row {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
AddEventForm .form-label {
|
||||
width: 12;
|
||||
height: 2;
|
||||
padding-top: 1;
|
||||
padding-right: 1;
|
||||
}
|
||||
|
||||
AddEventForm .form-input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddEventForm #title-input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddEventForm .date-input {
|
||||
width: 18;
|
||||
}
|
||||
|
||||
AddEventForm .time-input {
|
||||
width: 10;
|
||||
}
|
||||
|
||||
AddEventForm #calendar-select {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddEventForm #location-input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddEventForm #description-textarea {
|
||||
width: 1fr;
|
||||
height: 6;
|
||||
}
|
||||
|
||||
AddEventForm .required {
|
||||
color: $error;
|
||||
}
|
||||
|
||||
AddEventForm .datetime-row {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
AddEventForm .datetime-group {
|
||||
width: auto;
|
||||
height: auto;
|
||||
margin-right: 2;
|
||||
}
|
||||
|
||||
AddEventForm .datetime-label {
|
||||
width: auto;
|
||||
padding-right: 1;
|
||||
height: 2;
|
||||
padding-top: 1;
|
||||
color: $text-muted;
|
||||
}
|
||||
"""
|
||||
|
||||
class Submitted(Message):
|
||||
"""Message emitted when the form is submitted."""
|
||||
|
||||
def __init__(self, data: EventFormData) -> None:
|
||||
super().__init__()
|
||||
self.data = data
|
||||
|
||||
class Cancelled(Message):
|
||||
"""Message emitted when the form is cancelled."""
|
||||
|
||||
pass
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
calendars: list[str] | None = None,
|
||||
initial_date: date | None = None,
|
||||
initial_time: time | None = None,
|
||||
initial_data: EventFormData | None = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the add event form.
|
||||
|
||||
Args:
|
||||
calendars: List of available calendar names for the dropdown
|
||||
initial_date: Pre-populate with this date
|
||||
initial_time: Pre-populate with this time
|
||||
initial_data: Pre-populate form with this data (overrides date/time)
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._calendars = calendars or []
|
||||
self._initial_date = initial_date or date.today()
|
||||
self._initial_time = initial_time or time(9, 0)
|
||||
self._initial_data = initial_data
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Compose the form layout."""
|
||||
if self._initial_data:
|
||||
initial = self._initial_data
|
||||
start_date = initial.start_date
|
||||
start_time = initial.start_time
|
||||
end_date = initial.end_date
|
||||
end_time = initial.end_time
|
||||
title = initial.title
|
||||
location = initial.location or ""
|
||||
description = initial.description or ""
|
||||
calendar = initial.calendar or ""
|
||||
all_day = initial.all_day
|
||||
else:
|
||||
start_date = self._initial_date
|
||||
start_time = self._initial_time
|
||||
# Default to 1 hour duration
|
||||
end_date = start_date
|
||||
end_time = time(start_time.hour + 1, start_time.minute)
|
||||
if start_time.hour >= 23:
|
||||
end_time = time(23, 59)
|
||||
title = ""
|
||||
location = ""
|
||||
description = ""
|
||||
calendar = ""
|
||||
all_day = False
|
||||
|
||||
with ScrollableContainer():
|
||||
# Title (required)
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Title", classes="form-label")
|
||||
yield Label("*", classes="required")
|
||||
yield Input(
|
||||
value=title,
|
||||
placeholder="Event title...",
|
||||
id="title-input",
|
||||
classes="form-input",
|
||||
)
|
||||
|
||||
# Start Date/Time
|
||||
with Vertical(classes="form-row"):
|
||||
yield Label("Start", classes="form-label")
|
||||
with Horizontal(classes="datetime-row"):
|
||||
with Horizontal(classes="datetime-group"):
|
||||
yield Label("Date:", classes="datetime-label")
|
||||
yield MaskedInput(
|
||||
template="9999-99-99",
|
||||
value=start_date.strftime("%Y-%m-%d"),
|
||||
id="start-date-input",
|
||||
classes="date-input",
|
||||
)
|
||||
with Horizontal(classes="datetime-group"):
|
||||
yield Label("Time:", classes="datetime-label")
|
||||
yield MaskedInput(
|
||||
template="99:99",
|
||||
value=start_time.strftime("%H:%M"),
|
||||
id="start-time-input",
|
||||
classes="time-input",
|
||||
)
|
||||
|
||||
# End Date/Time
|
||||
with Vertical(classes="form-row"):
|
||||
yield Label("End", classes="form-label")
|
||||
with Horizontal(classes="datetime-row"):
|
||||
with Horizontal(classes="datetime-group"):
|
||||
yield Label("Date:", classes="datetime-label")
|
||||
yield MaskedInput(
|
||||
template="9999-99-99",
|
||||
value=end_date.strftime("%Y-%m-%d"),
|
||||
id="end-date-input",
|
||||
classes="date-input",
|
||||
)
|
||||
with Horizontal(classes="datetime-group"):
|
||||
yield Label("Time:", classes="datetime-label")
|
||||
yield MaskedInput(
|
||||
template="99:99",
|
||||
value=end_time.strftime("%H:%M"),
|
||||
id="end-time-input",
|
||||
classes="time-input",
|
||||
)
|
||||
|
||||
# All day checkbox
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("", classes="form-label")
|
||||
yield Checkbox("All day event", value=all_day, id="all-day-checkbox")
|
||||
|
||||
# Calendar selection (optional dropdown)
|
||||
if self._calendars:
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Calendar", classes="form-label")
|
||||
options = [("(default)", "")] + [(c, c) for c in self._calendars]
|
||||
yield Select(
|
||||
options=options,
|
||||
value=calendar,
|
||||
id="calendar-select",
|
||||
allow_blank=True,
|
||||
)
|
||||
|
||||
# Location (optional)
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Location", classes="form-label")
|
||||
yield Input(
|
||||
value=location,
|
||||
placeholder="Event location...",
|
||||
id="location-input",
|
||||
classes="form-input",
|
||||
)
|
||||
|
||||
# Description (optional textarea)
|
||||
with Vertical(classes="form-row"):
|
||||
yield Label("Description", classes="form-label")
|
||||
yield TextArea(
|
||||
description,
|
||||
id="description-textarea",
|
||||
)
|
||||
|
||||
def get_form_data(self) -> EventFormData:
|
||||
"""Extract current form data.
|
||||
|
||||
Returns:
|
||||
EventFormData with current form values
|
||||
"""
|
||||
title = self.query_one("#title-input", Input).value.strip()
|
||||
|
||||
# Parse start date/time from MaskedInput
|
||||
start_date_input = self.query_one("#start-date-input", MaskedInput)
|
||||
start_time_input = self.query_one("#start-time-input", MaskedInput)
|
||||
start_date_str = start_date_input.value.strip()
|
||||
start_time_str = start_time_input.value.strip()
|
||||
try:
|
||||
start_date = datetime.strptime(start_date_str, "%Y-%m-%d").date()
|
||||
except ValueError:
|
||||
start_date = date.today()
|
||||
try:
|
||||
start_time = datetime.strptime(start_time_str, "%H:%M").time()
|
||||
except ValueError:
|
||||
start_time = time(9, 0)
|
||||
|
||||
# Parse end date/time from MaskedInput
|
||||
end_date_input = self.query_one("#end-date-input", MaskedInput)
|
||||
end_time_input = self.query_one("#end-time-input", MaskedInput)
|
||||
end_date_str = end_date_input.value.strip()
|
||||
end_time_str = end_time_input.value.strip()
|
||||
try:
|
||||
end_date = datetime.strptime(end_date_str, "%Y-%m-%d").date()
|
||||
except ValueError:
|
||||
end_date = start_date
|
||||
try:
|
||||
end_time = datetime.strptime(end_time_str, "%H:%M").time()
|
||||
except ValueError:
|
||||
end_time = time(start_time.hour + 1, start_time.minute)
|
||||
|
||||
# All day
|
||||
all_day = self.query_one("#all-day-checkbox", Checkbox).value
|
||||
|
||||
# Calendar
|
||||
calendar: str | None = None
|
||||
try:
|
||||
calendar_select = self.query_one("#calendar-select", Select)
|
||||
cal_value = calendar_select.value
|
||||
if isinstance(cal_value, str) and cal_value:
|
||||
calendar = cal_value
|
||||
except Exception:
|
||||
pass # No calendar select
|
||||
|
||||
# Location
|
||||
location = self.query_one("#location-input", Input).value.strip() or None
|
||||
|
||||
# Description
|
||||
try:
|
||||
desc_area = self.query_one("#description-textarea", TextArea)
|
||||
description = desc_area.text.strip() or None
|
||||
except Exception:
|
||||
description = None
|
||||
|
||||
return EventFormData(
|
||||
title=title,
|
||||
start_date=start_date,
|
||||
start_time=start_time,
|
||||
end_date=end_date,
|
||||
end_time=end_time,
|
||||
location=location,
|
||||
description=description,
|
||||
calendar=calendar,
|
||||
all_day=all_day,
|
||||
)
|
||||
|
||||
def validate(self) -> tuple[bool, str]:
|
||||
"""Validate the form data.
|
||||
|
||||
Returns:
|
||||
Tuple of (is_valid, error_message)
|
||||
"""
|
||||
data = self.get_form_data()
|
||||
|
||||
if not data.title:
|
||||
return False, "Title is required"
|
||||
|
||||
# Validate that end is after start
|
||||
if data.end_datetime <= data.start_datetime:
|
||||
return False, "End time must be after start time"
|
||||
|
||||
return True, ""
|
||||
|
||||
def submit(self) -> bool:
|
||||
"""Validate and submit the form.
|
||||
|
||||
Returns:
|
||||
True if form was valid and submitted, False otherwise
|
||||
"""
|
||||
is_valid, error = self.validate()
|
||||
if not is_valid:
|
||||
return False
|
||||
|
||||
self.post_message(self.Submitted(self.get_form_data()))
|
||||
return True
|
||||
|
||||
def cancel(self) -> None:
|
||||
"""Cancel the form."""
|
||||
self.post_message(self.Cancelled())
|
||||
208
src/calendar/widgets/InvitesPanel.py
Normal file
208
src/calendar/widgets/InvitesPanel.py
Normal file
@@ -0,0 +1,208 @@
|
||||
"""Calendar invites panel widget for Calendar TUI sidebar.
|
||||
|
||||
Displays pending calendar invites that need a response.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
from rich.segment import Segment
|
||||
from rich.style import Style
|
||||
from textual.message import Message
|
||||
from textual.reactive import reactive
|
||||
from textual.strip import Strip
|
||||
from textual.widget import Widget
|
||||
|
||||
|
||||
@dataclass
|
||||
class CalendarInvite:
|
||||
"""A calendar invite pending response."""
|
||||
|
||||
id: str
|
||||
subject: str
|
||||
organizer: str
|
||||
start: datetime
|
||||
end: datetime
|
||||
location: Optional[str] = None
|
||||
is_all_day: bool = False
|
||||
response_status: str = "notResponded" # notResponded, tentativelyAccepted
|
||||
|
||||
|
||||
class InvitesPanel(Widget):
|
||||
"""Panel showing pending calendar invites."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
InvitesPanel {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
min-height: 3;
|
||||
padding: 0 1;
|
||||
border: round $primary;
|
||||
border-title-color: $primary;
|
||||
}
|
||||
"""
|
||||
|
||||
# Reactive attributes
|
||||
invites: reactive[List[CalendarInvite]] = reactive(list)
|
||||
selected_index: reactive[int] = reactive(0)
|
||||
|
||||
class InviteSelected(Message):
|
||||
"""An invite was selected."""
|
||||
|
||||
def __init__(self, invite: CalendarInvite) -> None:
|
||||
super().__init__()
|
||||
self.invite = invite
|
||||
|
||||
class InviteRespond(Message):
|
||||
"""User wants to respond to an invite."""
|
||||
|
||||
def __init__(self, invite: CalendarInvite, response: str) -> None:
|
||||
super().__init__()
|
||||
self.invite = invite
|
||||
self.response = response # accept, tentativelyAccept, decline
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
invites: Optional[List[CalendarInvite]] = None,
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
classes: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
if invites:
|
||||
self.invites = invites
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Set border title on mount."""
|
||||
self._update_border_title()
|
||||
|
||||
def _update_border_title(self) -> None:
|
||||
"""Update border title with invite count."""
|
||||
count = len(self.invites)
|
||||
self.border_title = f"Invites ({count})" if count else "Invites"
|
||||
|
||||
def _get_theme_color(self, color_name: str) -> str:
|
||||
"""Get a color from the current theme."""
|
||||
try:
|
||||
theme = self.app.current_theme
|
||||
color = getattr(theme, color_name, None)
|
||||
if color:
|
||||
return str(color)
|
||||
except Exception:
|
||||
pass
|
||||
# Fallback colors
|
||||
fallbacks = {
|
||||
"secondary": "#81A1C1",
|
||||
"primary": "#88C0D0",
|
||||
"accent": "#B48EAD",
|
||||
"foreground": "#D8DEE9",
|
||||
"surface": "#3B4252",
|
||||
"warning": "#EBCB8B",
|
||||
}
|
||||
return fallbacks.get(color_name, "white")
|
||||
|
||||
def get_content_height(self, container, viewport, width: int) -> int:
|
||||
"""Calculate height: invite rows only (no internal header)."""
|
||||
if not self.invites:
|
||||
return 1 # "No pending invites"
|
||||
return len(self.invites) * 2 # 2 lines per invite
|
||||
|
||||
def render_line(self, y: int) -> Strip:
|
||||
"""Render a line of the panel."""
|
||||
if not self.invites:
|
||||
if y == 0:
|
||||
return self._render_empty_message()
|
||||
return Strip.blank(self.size.width)
|
||||
|
||||
# Each invite takes 2 lines
|
||||
invite_idx = y // 2
|
||||
line_in_invite = y % 2
|
||||
|
||||
if 0 <= invite_idx < len(self.invites):
|
||||
return self._render_invite_line(
|
||||
self.invites[invite_idx],
|
||||
line_in_invite,
|
||||
invite_idx == self.selected_index,
|
||||
)
|
||||
|
||||
return Strip.blank(self.size.width)
|
||||
|
||||
def _render_empty_message(self) -> Strip:
|
||||
"""Render empty state message."""
|
||||
msg = "No pending invites"
|
||||
msg = msg[: self.size.width].ljust(self.size.width)
|
||||
style = Style(color="bright_black")
|
||||
return Strip([Segment(msg, style)])
|
||||
|
||||
def _render_invite_line(
|
||||
self, invite: CalendarInvite, line: int, is_selected: bool
|
||||
) -> Strip:
|
||||
"""Render a line of an invite."""
|
||||
if line == 0:
|
||||
# First line: subject (truncated)
|
||||
text = invite.subject[: self.size.width - 2]
|
||||
if is_selected:
|
||||
text = "> " + text[: self.size.width - 2]
|
||||
text = text[: self.size.width].ljust(self.size.width)
|
||||
|
||||
if is_selected:
|
||||
style = Style(bold=True, reverse=True)
|
||||
else:
|
||||
style = Style()
|
||||
|
||||
return Strip([Segment(text, style)])
|
||||
else:
|
||||
# Second line: date/time and organizer
|
||||
date_str = invite.start.strftime("%m/%d %H:%M")
|
||||
organizer = invite.organizer[:15] if invite.organizer else ""
|
||||
info = f" {date_str} - {organizer}"
|
||||
info = info[: self.size.width].ljust(self.size.width)
|
||||
|
||||
warning_color = self._get_theme_color("warning")
|
||||
if invite.response_status == "tentativelyAccepted":
|
||||
style = Style(color=warning_color, italic=True)
|
||||
else:
|
||||
style = Style(color="bright_black")
|
||||
|
||||
return Strip([Segment(info, style)])
|
||||
|
||||
def set_invites(self, invites: List[CalendarInvite]) -> None:
|
||||
"""Update the list of invites."""
|
||||
self.invites = invites
|
||||
if self.selected_index >= len(invites):
|
||||
self.selected_index = max(0, len(invites) - 1)
|
||||
self._update_border_title()
|
||||
self.refresh()
|
||||
|
||||
def select_next(self) -> None:
|
||||
"""Select the next invite."""
|
||||
if self.invites and self.selected_index < len(self.invites) - 1:
|
||||
self.selected_index += 1
|
||||
self.refresh()
|
||||
|
||||
def select_previous(self) -> None:
|
||||
"""Select the previous invite."""
|
||||
if self.invites and self.selected_index > 0:
|
||||
self.selected_index -= 1
|
||||
self.refresh()
|
||||
|
||||
def get_selected_invite(self) -> Optional[CalendarInvite]:
|
||||
"""Get the currently selected invite."""
|
||||
if self.invites and 0 <= self.selected_index < len(self.invites):
|
||||
return self.invites[self.selected_index]
|
||||
return None
|
||||
|
||||
def on_click(self, event) -> None:
|
||||
"""Handle mouse clicks."""
|
||||
y = event.y
|
||||
|
||||
if not self.invites:
|
||||
return
|
||||
|
||||
# Calculate which invite was clicked (2 lines per invite)
|
||||
invite_idx = y // 2
|
||||
if 0 <= invite_idx < len(self.invites):
|
||||
self.selected_index = invite_idx
|
||||
self.post_message(self.InviteSelected(self.invites[invite_idx]))
|
||||
self.refresh()
|
||||
312
src/calendar/widgets/MonthCalendar.py
Normal file
312
src/calendar/widgets/MonthCalendar.py
Normal file
@@ -0,0 +1,312 @@
|
||||
"""Mini month calendar widget for Calendar TUI sidebar.
|
||||
|
||||
Displays a compact month view with day numbers, highlighting:
|
||||
- Today
|
||||
- Current week
|
||||
- Selected day
|
||||
"""
|
||||
|
||||
from datetime import date, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from rich.segment import Segment
|
||||
from rich.style import Style
|
||||
from textual.message import Message
|
||||
from textual.reactive import reactive
|
||||
from textual.strip import Strip
|
||||
from textual.widget import Widget
|
||||
|
||||
from src.calendar import config
|
||||
|
||||
|
||||
def get_month_calendar(
|
||||
year: int, month: int, week_start_day: int = 0
|
||||
) -> list[list[Optional[date]]]:
|
||||
"""Generate a calendar grid for a month.
|
||||
|
||||
Returns a list of weeks, where each week is a list of 7 dates (or None for empty cells).
|
||||
|
||||
Args:
|
||||
year: The year
|
||||
month: The month (1-12)
|
||||
week_start_day: Config format (0=Sunday, 1=Monday, ..., 6=Saturday)
|
||||
"""
|
||||
# Get first day of month and number of days
|
||||
first_day = date(year, month, 1)
|
||||
if month == 12:
|
||||
last_day = date(year + 1, 1, 1) - timedelta(days=1)
|
||||
else:
|
||||
last_day = date(year, month + 1, 1) - timedelta(days=1)
|
||||
|
||||
# Convert config week start to python weekday
|
||||
# Config: 0=Sunday, 1=Monday, ..., 6=Saturday
|
||||
# Python: 0=Monday, 1=Tuesday, ..., 6=Sunday
|
||||
python_week_start = (week_start_day - 1) % 7
|
||||
|
||||
# Calculate which day of the week the first day falls on (relative to week start)
|
||||
first_python_weekday = first_day.weekday() # 0=Monday, 6=Sunday
|
||||
days_offset = (first_python_weekday - python_week_start) % 7
|
||||
|
||||
weeks: list[list[Optional[date]]] = []
|
||||
current_week: list[Optional[date]] = [None] * days_offset
|
||||
|
||||
current = first_day
|
||||
while current <= last_day:
|
||||
current_week.append(current)
|
||||
if len(current_week) == 7:
|
||||
weeks.append(current_week)
|
||||
current_week = []
|
||||
current += timedelta(days=1)
|
||||
|
||||
# Fill remaining days in last week
|
||||
if current_week:
|
||||
while len(current_week) < 7:
|
||||
current_week.append(None)
|
||||
weeks.append(current_week)
|
||||
|
||||
return weeks
|
||||
|
||||
|
||||
def get_day_names(week_start_day: int = 0) -> str:
|
||||
"""Get day name headers based on week start day.
|
||||
|
||||
Args:
|
||||
week_start_day: Config format (0=Sunday, 1=Monday, ..., 6=Saturday)
|
||||
|
||||
Returns:
|
||||
String like "Su Mo Tu We Th Fr Sa" or "Mo Tu We Th Fr Sa Su"
|
||||
"""
|
||||
# Full list starting from Sunday (config day 0)
|
||||
all_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
|
||||
# Rotate to start from week_start_day
|
||||
rotated = all_days[week_start_day:] + all_days[:week_start_day]
|
||||
return " ".join(rotated)
|
||||
|
||||
|
||||
class MonthCalendar(Widget):
|
||||
"""A compact month calendar widget for sidebars."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
MonthCalendar {
|
||||
width: 24;
|
||||
height: auto;
|
||||
padding: 0;
|
||||
}
|
||||
"""
|
||||
|
||||
# Reactive attributes
|
||||
display_month: reactive[date] = reactive(lambda: date.today().replace(day=1))
|
||||
selected_date: reactive[date] = reactive(date.today)
|
||||
week_start: reactive[date] = reactive(lambda: date.today())
|
||||
|
||||
class DateSelected(Message):
|
||||
"""A date was clicked/selected."""
|
||||
|
||||
def __init__(self, selected: date) -> None:
|
||||
super().__init__()
|
||||
self.date = selected
|
||||
|
||||
class MonthChanged(Message):
|
||||
"""Month navigation occurred."""
|
||||
|
||||
def __init__(self, month: date) -> None:
|
||||
super().__init__()
|
||||
self.month = month
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
selected_date: Optional[date] = None,
|
||||
week_start: Optional[date] = None,
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
classes: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
if selected_date:
|
||||
self.selected_date = selected_date
|
||||
self.display_month = selected_date.replace(day=1)
|
||||
if week_start:
|
||||
self.week_start = week_start
|
||||
|
||||
def _get_theme_color(self, color_name: str) -> str:
|
||||
"""Get a color from the current theme."""
|
||||
try:
|
||||
theme = self.app.current_theme
|
||||
color = getattr(theme, color_name, None)
|
||||
if color:
|
||||
return str(color)
|
||||
except Exception:
|
||||
pass
|
||||
# Fallback colors
|
||||
fallbacks = {
|
||||
"secondary": "#81A1C1",
|
||||
"primary": "#88C0D0",
|
||||
"accent": "#B48EAD",
|
||||
"foreground": "#D8DEE9",
|
||||
"surface": "#3B4252",
|
||||
}
|
||||
return fallbacks.get(color_name, "white")
|
||||
|
||||
@property
|
||||
def _weeks(self) -> list[list[Optional[date]]]:
|
||||
"""Get the weeks for the current display month."""
|
||||
return get_month_calendar(
|
||||
self.display_month.year,
|
||||
self.display_month.month,
|
||||
config.week_start_day(),
|
||||
)
|
||||
|
||||
def get_content_height(self, container, viewport, width: int) -> int:
|
||||
"""Calculate height: header + day names + weeks."""
|
||||
return 2 + len(self._weeks) # Month header + day names + week rows
|
||||
|
||||
def render_line(self, y: int) -> Strip:
|
||||
"""Render a line of the calendar."""
|
||||
if y == 0:
|
||||
return self._render_month_header()
|
||||
elif y == 1:
|
||||
return self._render_day_names()
|
||||
else:
|
||||
week_idx = y - 2
|
||||
weeks = self._weeks
|
||||
if 0 <= week_idx < len(weeks):
|
||||
return self._render_week(weeks[week_idx])
|
||||
return Strip.blank(self.size.width)
|
||||
|
||||
def _render_month_header(self) -> Strip:
|
||||
"""Render the month/year header with navigation arrows."""
|
||||
month_name = self.display_month.strftime("%B %Y")
|
||||
header = f"< {month_name:^16} >"
|
||||
header = header[: self.size.width].ljust(self.size.width)
|
||||
|
||||
primary_color = self._get_theme_color("primary")
|
||||
style = Style(bold=True, color=primary_color)
|
||||
return Strip([Segment(header, style)])
|
||||
|
||||
def _render_day_names(self) -> Strip:
|
||||
"""Render the day name headers based on week start setting."""
|
||||
day_names = get_day_names(config.week_start_day())
|
||||
# Pad to widget width
|
||||
line = day_names[: self.size.width].ljust(self.size.width)
|
||||
style = Style(color="bright_black")
|
||||
return Strip([Segment(line, style)])
|
||||
|
||||
def _render_week(self, week: list[Optional[date]]) -> Strip:
|
||||
"""Render a week row."""
|
||||
segments = []
|
||||
today = date.today()
|
||||
|
||||
# Calculate the week containing week_start
|
||||
week_end = self.week_start + timedelta(days=6)
|
||||
|
||||
secondary_color = self._get_theme_color("secondary")
|
||||
primary_color = self._get_theme_color("primary")
|
||||
|
||||
for i, day in enumerate(week):
|
||||
if day is None:
|
||||
segments.append(Segment(" "))
|
||||
else:
|
||||
day_str = f"{day.day:2d} "
|
||||
|
||||
# Determine styling
|
||||
if day == self.selected_date:
|
||||
# Selected date - reverse video
|
||||
style = Style(bold=True, reverse=True)
|
||||
elif day == today:
|
||||
# Today - highlighted with secondary color
|
||||
style = Style(bold=True, color=secondary_color)
|
||||
elif self.week_start <= day <= week_end:
|
||||
# In current week view - subtle highlight
|
||||
style = Style(color=primary_color)
|
||||
elif day.weekday() >= 5:
|
||||
# Weekend
|
||||
style = Style(color="bright_black")
|
||||
else:
|
||||
# Normal day
|
||||
style = Style()
|
||||
|
||||
segments.append(Segment(day_str, style))
|
||||
|
||||
# Pad remaining width
|
||||
current_width = sum(len(s.text) for s in segments)
|
||||
if current_width < self.size.width:
|
||||
segments.append(Segment(" " * (self.size.width - current_width)))
|
||||
|
||||
return Strip(segments)
|
||||
|
||||
def update_week(self, week_start: date) -> None:
|
||||
"""Update the current week highlight.
|
||||
|
||||
Also updates display_month if the week is in a different month.
|
||||
"""
|
||||
self.week_start = week_start
|
||||
# Optionally auto-update display month to show the week
|
||||
week_month = week_start.replace(day=1)
|
||||
if week_month != self.display_month:
|
||||
self.display_month = week_month
|
||||
self.refresh()
|
||||
|
||||
def update_selected(self, selected: date) -> None:
|
||||
"""Update the selected date."""
|
||||
self.selected_date = selected
|
||||
self.refresh()
|
||||
|
||||
def next_month(self) -> None:
|
||||
"""Navigate to next month."""
|
||||
year = self.display_month.year
|
||||
month = self.display_month.month + 1
|
||||
if month > 12:
|
||||
month = 1
|
||||
year += 1
|
||||
self.display_month = date(year, month, 1)
|
||||
self.post_message(self.MonthChanged(self.display_month))
|
||||
self.refresh()
|
||||
|
||||
def prev_month(self) -> None:
|
||||
"""Navigate to previous month."""
|
||||
year = self.display_month.year
|
||||
month = self.display_month.month - 1
|
||||
if month < 1:
|
||||
month = 12
|
||||
year -= 1
|
||||
self.display_month = date(year, month, 1)
|
||||
self.post_message(self.MonthChanged(self.display_month))
|
||||
self.refresh()
|
||||
|
||||
def on_click(self, event) -> None:
|
||||
"""Handle mouse clicks on the calendar."""
|
||||
# Row 0 is the month header (< Month Year >)
|
||||
# Row 1 is day names (Mo Tu We ...)
|
||||
# Row 2+ are the week rows
|
||||
y = event.y
|
||||
x = event.x
|
||||
|
||||
if y == 0:
|
||||
# Month header - check for navigation arrows
|
||||
if x < 2:
|
||||
self.prev_month()
|
||||
elif x >= self.size.width - 2:
|
||||
self.next_month()
|
||||
return
|
||||
|
||||
if y == 1:
|
||||
# Day names row - ignore
|
||||
return
|
||||
|
||||
# Week row - calculate which date was clicked
|
||||
week_idx = y - 2
|
||||
weeks = self._weeks
|
||||
if week_idx < 0 or week_idx >= len(weeks):
|
||||
return
|
||||
|
||||
week = weeks[week_idx]
|
||||
# Each day takes 3 characters ("DD "), so find which day column
|
||||
day_col = x // 3
|
||||
if day_col < 0 or day_col >= 7:
|
||||
return
|
||||
|
||||
clicked_date = week[day_col]
|
||||
if clicked_date is not None:
|
||||
self.selected_date = clicked_date
|
||||
self.post_message(self.DateSelected(clicked_date))
|
||||
self.refresh()
|
||||
786
src/calendar/widgets/WeekGrid.py
Normal file
786
src/calendar/widgets/WeekGrid.py
Normal file
@@ -0,0 +1,786 @@
|
||||
"""Week view grid widget for Calendar TUI.
|
||||
|
||||
Displays a week of calendar events in a grid layout where:
|
||||
- Columns represent days (5 or 7)
|
||||
- Rows represent time slots (30 minutes per row)
|
||||
- Events span multiple rows proportionally to their duration
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import date, datetime, timedelta
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from rich.style import Style
|
||||
from rich.segment import Segment
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Vertical
|
||||
from textual.geometry import Size
|
||||
from textual.message import Message
|
||||
from textual.reactive import reactive
|
||||
from textual.scroll_view import ScrollView
|
||||
from textual.strip import Strip
|
||||
from textual.widget import Widget
|
||||
|
||||
from src.calendar.backend import Event
|
||||
from src.calendar import config
|
||||
|
||||
|
||||
# Column widths
|
||||
TIME_COLUMN_WIDTH = 6 # "HH:MM "
|
||||
MIN_DAY_COLUMN_WIDTH = 10 # Minimum width for each day column
|
||||
DEFAULT_DAY_COLUMN_WIDTH = 20 # Default/preferred width for each day column
|
||||
|
||||
|
||||
def get_rows_per_hour() -> int:
|
||||
"""Get rows per hour from config."""
|
||||
return 60 // config.minutes_per_row()
|
||||
|
||||
|
||||
def get_total_rows() -> int:
|
||||
"""Get total rows for 24 hours."""
|
||||
return 24 * get_rows_per_hour()
|
||||
|
||||
|
||||
def get_week_start_for_date(target_date: date) -> date:
|
||||
"""Get the week start date for a given date based on config.
|
||||
|
||||
Config uses: 0=Sunday, 1=Monday, ..., 6=Saturday
|
||||
Python weekday() uses: 0=Monday, ..., 6=Sunday
|
||||
"""
|
||||
week_start_cfg = config.week_start_day() # 0=Sunday, 1=Monday, etc.
|
||||
python_weekday = target_date.weekday() # 0=Monday, 6=Sunday
|
||||
|
||||
# Convert config week start to python weekday
|
||||
# Sunday(0) -> 6, Monday(1) -> 0, Tuesday(2) -> 1, etc.
|
||||
python_week_start = (week_start_cfg - 1) % 7
|
||||
|
||||
# Calculate days since week start
|
||||
days_since_week_start = (python_weekday - python_week_start) % 7
|
||||
|
||||
return target_date - timedelta(days=days_since_week_start)
|
||||
|
||||
|
||||
def get_day_column_for_date(target_date: date, week_start: date) -> int:
|
||||
"""Get the column index for a date within its week.
|
||||
|
||||
Returns the number of days since week_start.
|
||||
"""
|
||||
return (target_date - week_start).days
|
||||
|
||||
|
||||
@dataclass
|
||||
class DayColumn:
|
||||
"""Events and layout for a single day column."""
|
||||
|
||||
day: date
|
||||
events: List[Event] = field(default_factory=list)
|
||||
# 2D grid: row -> list of events at that row
|
||||
grid: List[List[Event]] = field(default_factory=list)
|
||||
|
||||
def __post_init__(self):
|
||||
# Initialize grid with rows for 24 hours
|
||||
self.grid = [[] for _ in range(get_total_rows())]
|
||||
|
||||
def layout_events(self) -> None:
|
||||
"""Layout events handling overlaps."""
|
||||
total_rows = get_total_rows()
|
||||
minutes_per_row = config.minutes_per_row()
|
||||
|
||||
# Clear the grid
|
||||
self.grid = [[] for _ in range(total_rows)]
|
||||
|
||||
# Sort events by start time, then by duration (longer first)
|
||||
sorted_events = sorted(
|
||||
self.events, key=lambda e: (e.start, -(e.end - e.start).total_seconds())
|
||||
)
|
||||
|
||||
for event in sorted_events:
|
||||
if event.all_day:
|
||||
continue # Handle all-day events separately
|
||||
|
||||
start_row, end_row = event.get_row_span(minutes_per_row)
|
||||
|
||||
# Clamp to valid range
|
||||
start_row = max(0, min(start_row, total_rows - 1))
|
||||
end_row = max(start_row + 1, min(end_row, total_rows))
|
||||
|
||||
# Add event to each row it spans
|
||||
for row in range(start_row, end_row):
|
||||
if event not in self.grid[row]:
|
||||
self.grid[row].append(event)
|
||||
|
||||
|
||||
class WeekGridHeader(Widget):
|
||||
"""Fixed header widget showing day names."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
WeekGridHeader {
|
||||
height: 1;
|
||||
background: $surface;
|
||||
}
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
days: List[date],
|
||||
cursor_col: int = 0,
|
||||
include_weekends: bool = True,
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id)
|
||||
self._days = days
|
||||
self._cursor_col = cursor_col
|
||||
self._include_weekends = include_weekends
|
||||
|
||||
def update_days(self, days: List[date], cursor_col: int) -> None:
|
||||
"""Update the displayed days."""
|
||||
self._days = days
|
||||
self._cursor_col = cursor_col
|
||||
self.refresh()
|
||||
|
||||
def set_include_weekends(self, include_weekends: bool) -> None:
|
||||
"""Update the include_weekends setting."""
|
||||
self._include_weekends = include_weekends
|
||||
self.refresh()
|
||||
|
||||
@property
|
||||
def num_days(self) -> int:
|
||||
return 7 if self._include_weekends else 5
|
||||
|
||||
def _get_day_column_width(self) -> int:
|
||||
"""Calculate day column width based on available space."""
|
||||
available_width = self.size.width - TIME_COLUMN_WIDTH
|
||||
if available_width <= 0 or self.num_days == 0:
|
||||
return DEFAULT_DAY_COLUMN_WIDTH
|
||||
width_per_day = available_width // self.num_days
|
||||
return max(MIN_DAY_COLUMN_WIDTH, width_per_day)
|
||||
|
||||
def _get_theme_color(self, color_name: str) -> str:
|
||||
"""Get a color from the current theme."""
|
||||
try:
|
||||
theme = self.app.current_theme
|
||||
color = getattr(theme, color_name, None)
|
||||
if color:
|
||||
return str(color)
|
||||
except Exception:
|
||||
pass
|
||||
# Fallback colors
|
||||
fallbacks = {
|
||||
"secondary": "#81A1C1",
|
||||
"primary": "#88C0D0",
|
||||
"foreground": "#D8DEE9",
|
||||
"surface": "#3B4252",
|
||||
}
|
||||
return fallbacks.get(color_name, "white")
|
||||
|
||||
def render_line(self, y: int) -> Strip:
|
||||
"""Render the header row."""
|
||||
day_col_width = self._get_day_column_width()
|
||||
|
||||
if y != 0:
|
||||
return Strip.blank(TIME_COLUMN_WIDTH + (day_col_width * self.num_days))
|
||||
|
||||
segments = []
|
||||
|
||||
# Time column spacer
|
||||
segments.append(Segment(" " * TIME_COLUMN_WIDTH))
|
||||
|
||||
# Get theme colors
|
||||
secondary_color = self._get_theme_color("secondary")
|
||||
|
||||
# Day headers
|
||||
today = date.today()
|
||||
for i, day in enumerate(self._days):
|
||||
day_name = day.strftime("%a %m/%d")
|
||||
|
||||
# Style based on selection and today
|
||||
if i == self._cursor_col:
|
||||
style = Style(bold=True, reverse=True)
|
||||
elif day == today:
|
||||
# Highlight today with theme secondary color
|
||||
style = Style(bold=True, color=secondary_color)
|
||||
elif day.weekday() >= 5: # Weekend
|
||||
style = Style(color="bright_black")
|
||||
else:
|
||||
style = Style()
|
||||
|
||||
# Center the day name in the column
|
||||
header = day_name.center(day_col_width)
|
||||
segments.append(Segment(header, style))
|
||||
|
||||
return Strip(segments)
|
||||
|
||||
|
||||
class WeekGridBody(ScrollView):
|
||||
"""Scrollable body of the week grid showing time slots and events."""
|
||||
|
||||
# Reactive attributes
|
||||
cursor_row: reactive[int] = reactive(0)
|
||||
cursor_col: reactive[int] = reactive(0)
|
||||
|
||||
# Messages
|
||||
class CursorMoved(Message):
|
||||
"""Cursor position changed."""
|
||||
|
||||
def __init__(self, row: int, col: int) -> None:
|
||||
super().__init__()
|
||||
self.row = row
|
||||
self.col = col
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
include_weekends: bool = True,
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
classes: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
self._days: List[DayColumn] = []
|
||||
self._include_weekends = include_weekends
|
||||
self._work_day_start = config.work_day_start_hour()
|
||||
self._work_day_end = config.work_day_end_hour()
|
||||
|
||||
def _get_theme_color(self, color_name: str) -> str:
|
||||
"""Get a color from the current theme."""
|
||||
try:
|
||||
theme = self.app.current_theme
|
||||
color = getattr(theme, color_name, None)
|
||||
if color:
|
||||
return str(color)
|
||||
except Exception:
|
||||
pass
|
||||
# Fallback colors
|
||||
fallbacks = {
|
||||
"secondary": "#81A1C1",
|
||||
"primary": "#88C0D0",
|
||||
"accent": "#B48EAD",
|
||||
"foreground": "#D8DEE9",
|
||||
"surface": "#3B4252",
|
||||
"warning": "#EBCB8B",
|
||||
"error": "#BF616A",
|
||||
}
|
||||
return fallbacks.get(color_name, "white")
|
||||
|
||||
@property
|
||||
def num_days(self) -> int:
|
||||
return 7 if self._include_weekends else 5
|
||||
|
||||
def _get_day_column_width(self) -> int:
|
||||
"""Calculate day column width based on available space."""
|
||||
available_width = self.size.width - TIME_COLUMN_WIDTH
|
||||
if available_width <= 0 or self.num_days == 0:
|
||||
return DEFAULT_DAY_COLUMN_WIDTH
|
||||
width_per_day = available_width // self.num_days
|
||||
return max(MIN_DAY_COLUMN_WIDTH, width_per_day)
|
||||
|
||||
@property
|
||||
def content_width(self) -> int:
|
||||
return TIME_COLUMN_WIDTH + (self._get_day_column_width() * self.num_days)
|
||||
|
||||
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
|
||||
return get_total_rows()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Set up virtual size for scrolling."""
|
||||
self._update_virtual_size()
|
||||
|
||||
def _update_virtual_size(self) -> None:
|
||||
"""Update the virtual size based on content dimensions."""
|
||||
self.virtual_size = Size(self.content_width, get_total_rows())
|
||||
|
||||
def set_days(self, days: List[DayColumn]) -> None:
|
||||
"""Set the day columns to display."""
|
||||
self._days = days
|
||||
self._update_virtual_size()
|
||||
self.refresh()
|
||||
|
||||
def set_include_weekends(self, include_weekends: bool) -> None:
|
||||
"""Update the include_weekends setting."""
|
||||
self._include_weekends = include_weekends
|
||||
self._update_virtual_size()
|
||||
self.refresh()
|
||||
|
||||
def watch_cursor_row(self, old: int, new: int) -> None:
|
||||
"""Handle cursor row changes."""
|
||||
total_rows = get_total_rows()
|
||||
# Clamp cursor row
|
||||
if new < 0:
|
||||
self.cursor_row = 0
|
||||
elif new >= total_rows:
|
||||
self.cursor_row = total_rows - 1
|
||||
else:
|
||||
# Scroll to keep cursor visible with a 2-row margin from viewport edges
|
||||
self._scroll_to_keep_cursor_visible(new)
|
||||
self.post_message(self.CursorMoved(new, self.cursor_col))
|
||||
self.refresh()
|
||||
|
||||
def _scroll_to_keep_cursor_visible(self, cursor_row: int) -> None:
|
||||
"""Scroll viewport only when cursor gets within 2 rows of the edge."""
|
||||
margin = 2 # Number of rows to keep between cursor and viewport edge
|
||||
|
||||
scroll_y = int(self.scroll_offset.y)
|
||||
viewport_height = self.size.height
|
||||
|
||||
# Calculate visible range
|
||||
visible_top = scroll_y
|
||||
visible_bottom = scroll_y + viewport_height - 1
|
||||
|
||||
# Check if cursor is too close to the top edge
|
||||
if cursor_row < visible_top + margin:
|
||||
# Scroll up to keep margin above cursor
|
||||
new_scroll_y = max(0, cursor_row - margin)
|
||||
self.scroll_to(y=new_scroll_y, animate=False)
|
||||
# Check if cursor is too close to the bottom edge
|
||||
elif cursor_row > visible_bottom - margin:
|
||||
# Scroll down to keep margin below cursor
|
||||
new_scroll_y = cursor_row - viewport_height + margin + 1
|
||||
self.scroll_to(y=new_scroll_y, animate=False)
|
||||
|
||||
def watch_cursor_col(self, old: int, new: int) -> None:
|
||||
"""Handle cursor column changes."""
|
||||
self.post_message(self.CursorMoved(self.cursor_row, new))
|
||||
self.refresh()
|
||||
|
||||
def render_line(self, y: int) -> Strip:
|
||||
"""Render a single line of the grid."""
|
||||
scroll_y = int(self.scroll_offset.y)
|
||||
row_index = y + scroll_y
|
||||
|
||||
total_rows = get_total_rows()
|
||||
if row_index < 0 or row_index >= total_rows:
|
||||
return Strip.blank(self.content_width)
|
||||
|
||||
return self._render_time_row(row_index)
|
||||
|
||||
def _render_time_row(self, row_index: int) -> Strip:
|
||||
"""Render a time row with events."""
|
||||
rows_per_hour = get_rows_per_hour()
|
||||
minutes_per_row = config.minutes_per_row()
|
||||
segments = []
|
||||
|
||||
# Check if this is the current time row
|
||||
now = datetime.now()
|
||||
current_row = (now.hour * rows_per_hour) + (now.minute // minutes_per_row)
|
||||
is_current_time_row = row_index == current_row
|
||||
|
||||
# Check if cursor is on this row
|
||||
is_cursor_row = row_index == self.cursor_row
|
||||
|
||||
# Time label (only show on the hour)
|
||||
if row_index % rows_per_hour == 0:
|
||||
hour = row_index // rows_per_hour
|
||||
time_str = f"{hour:02d}:00 "
|
||||
else:
|
||||
time_str = " " # Blank for half-hour
|
||||
|
||||
# Style time label - highlight current time or cursor, dim outside work hours
|
||||
if is_cursor_row:
|
||||
# Highlight the hour label when cursor is on this row
|
||||
primary_color = self._get_theme_color("primary")
|
||||
time_style = Style(color=primary_color, bold=True, reverse=True)
|
||||
elif is_current_time_row:
|
||||
error_color = self._get_theme_color("error")
|
||||
# Add subtle background to current time row for better visibility
|
||||
surface_color = self._get_theme_color("surface")
|
||||
time_style = Style(color=error_color, bold=True, bgcolor=surface_color)
|
||||
elif (
|
||||
row_index < self._work_day_start * rows_per_hour
|
||||
or row_index >= self._work_day_end * rows_per_hour
|
||||
):
|
||||
time_style = Style(color="bright_black")
|
||||
else:
|
||||
primary_color = self._get_theme_color("primary")
|
||||
time_style = Style(color=primary_color)
|
||||
|
||||
segments.append(Segment(time_str, time_style))
|
||||
|
||||
# Event cells for each day
|
||||
for col_idx, day_col in enumerate(self._days):
|
||||
cell_text, cell_style = self._render_event_cell(
|
||||
day_col, row_index, col_idx, is_current_time_row
|
||||
)
|
||||
segments.append(Segment(cell_text, cell_style))
|
||||
|
||||
return Strip(segments)
|
||||
|
||||
def _render_event_cell(
|
||||
self,
|
||||
day_col: DayColumn,
|
||||
row_index: int,
|
||||
col_idx: int,
|
||||
is_current_time_row: bool = False,
|
||||
) -> Tuple[str, Style]:
|
||||
"""Render a single cell for a day/time slot."""
|
||||
events_at_row = day_col.grid[row_index] if row_index < len(day_col.grid) else []
|
||||
rows_per_hour = get_rows_per_hour()
|
||||
minutes_per_row = config.minutes_per_row()
|
||||
day_col_width = self._get_day_column_width()
|
||||
|
||||
is_cursor = col_idx == self.cursor_col and row_index == self.cursor_row
|
||||
|
||||
# Get colors for current time line
|
||||
error_color = self._get_theme_color("error") if is_current_time_row else None
|
||||
|
||||
if not events_at_row:
|
||||
# Empty cell
|
||||
if is_cursor:
|
||||
return ">" + " " * (day_col_width - 1), Style(reverse=True)
|
||||
elif is_current_time_row:
|
||||
# Current time indicator line
|
||||
return "─" * day_col_width, Style(color=error_color, bold=True)
|
||||
else:
|
||||
# Grid line style
|
||||
if row_index % rows_per_hour == 0:
|
||||
return "-" * day_col_width, Style(color="bright_black")
|
||||
else:
|
||||
return " " * day_col_width, Style()
|
||||
|
||||
# Get the event to display (first one if multiple)
|
||||
event = events_at_row[0]
|
||||
|
||||
# Determine if this is the start row for this event
|
||||
start_row, _ = event.get_row_span(minutes_per_row)
|
||||
is_start = row_index == max(0, start_row)
|
||||
|
||||
# Build cell text
|
||||
if is_start:
|
||||
# Show event title with time
|
||||
time_str = event.start.strftime("%H:%M")
|
||||
title = event.title[: day_col_width - 7] # Leave room for time
|
||||
cell_text = f"{time_str} {title}"
|
||||
else:
|
||||
# Continuation of event
|
||||
cell_text = "│ " + event.title[: day_col_width - 3]
|
||||
|
||||
# Pad/truncate to column width
|
||||
cell_text = cell_text[:day_col_width].ljust(day_col_width)
|
||||
|
||||
# Style based on event and cursor
|
||||
if is_cursor:
|
||||
style = Style(bold=True, reverse=True)
|
||||
elif len(events_at_row) > 1:
|
||||
# Overlapping events - use warning color
|
||||
warning_color = self._get_theme_color("warning")
|
||||
style = Style(bgcolor=warning_color, color="black")
|
||||
else:
|
||||
# Normal event - use primary color
|
||||
primary_color = self._get_theme_color("primary")
|
||||
style = Style(bgcolor=primary_color, color="black")
|
||||
|
||||
return cell_text, style
|
||||
|
||||
def get_event_at_cursor(self) -> Optional[Event]:
|
||||
"""Get the event at the current cursor position."""
|
||||
if self.cursor_col < 0 or self.cursor_col >= len(self._days):
|
||||
return None
|
||||
|
||||
day_col = self._days[self.cursor_col]
|
||||
if self.cursor_row < 0 or self.cursor_row >= len(day_col.grid):
|
||||
return None
|
||||
|
||||
events = day_col.grid[self.cursor_row]
|
||||
return events[0] if events else None
|
||||
|
||||
|
||||
class WeekGrid(Vertical):
|
||||
"""Week view calendar grid widget with fixed header."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
WeekGrid {
|
||||
height: 1fr;
|
||||
}
|
||||
|
||||
WeekGridHeader {
|
||||
height: 1;
|
||||
}
|
||||
|
||||
WeekGridBody {
|
||||
height: 1fr;
|
||||
scrollbar-gutter: stable;
|
||||
}
|
||||
"""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("j", "cursor_down", "Down", show=False),
|
||||
Binding("k", "cursor_up", "Up", show=False),
|
||||
Binding("h", "cursor_left", "Left", show=False),
|
||||
Binding("l", "cursor_right", "Right", show=False),
|
||||
Binding("H", "prev_week", "Prev Week", show=True),
|
||||
Binding("L", "next_week", "Next Week", show=True),
|
||||
Binding("g", "goto_today", "Today", show=True),
|
||||
Binding("enter", "select_event", "View", show=True),
|
||||
]
|
||||
|
||||
# Reactive attributes
|
||||
week_start: reactive[date] = reactive(date.today)
|
||||
include_weekends: reactive[bool] = reactive(True)
|
||||
|
||||
# Messages
|
||||
class EventSelected(Message):
|
||||
"""Event was selected."""
|
||||
|
||||
def __init__(self, event: Event) -> None:
|
||||
super().__init__()
|
||||
self.event = event
|
||||
|
||||
class WeekChanged(Message):
|
||||
"""Week was changed via navigation."""
|
||||
|
||||
def __init__(self, week_start: date) -> None:
|
||||
super().__init__()
|
||||
self.week_start = week_start
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
week_start: Optional[date] = None,
|
||||
include_weekends: bool = True,
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
classes: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
|
||||
# Initialize state BEFORE setting reactive attributes
|
||||
self._days: List[DayColumn] = []
|
||||
self._events_by_date: dict[date, List[Event]] = {}
|
||||
self._header: Optional[WeekGridHeader] = None
|
||||
self._body: Optional[WeekGridBody] = None
|
||||
|
||||
# Set week start based on config.week_start_day() if not provided
|
||||
if week_start is None:
|
||||
today = date.today()
|
||||
week_start = get_week_start_for_date(today)
|
||||
|
||||
self.include_weekends = include_weekends
|
||||
self.week_start = week_start
|
||||
|
||||
@property
|
||||
def num_days(self) -> int:
|
||||
return 7 if self.include_weekends else 5
|
||||
|
||||
@property
|
||||
def cursor_row(self) -> int:
|
||||
"""Get current cursor row."""
|
||||
if self._body:
|
||||
return self._body.cursor_row
|
||||
return 0
|
||||
|
||||
@cursor_row.setter
|
||||
def cursor_row(self, value: int) -> None:
|
||||
"""Set cursor row."""
|
||||
if self._body:
|
||||
self._body.cursor_row = value
|
||||
|
||||
@property
|
||||
def cursor_col(self) -> int:
|
||||
"""Get current cursor column."""
|
||||
if self._body:
|
||||
return self._body.cursor_col
|
||||
return 0
|
||||
|
||||
@cursor_col.setter
|
||||
def cursor_col(self, value: int) -> None:
|
||||
"""Set cursor column."""
|
||||
if self._body:
|
||||
self._body.cursor_col = value
|
||||
if self._header:
|
||||
self._header.update_days([d.day for d in self._days], value)
|
||||
|
||||
def compose(self):
|
||||
"""Compose the widget."""
|
||||
days = [d.day for d in self._days] if self._days else []
|
||||
self._header = WeekGridHeader(
|
||||
days=days,
|
||||
cursor_col=0,
|
||||
include_weekends=self.include_weekends,
|
||||
)
|
||||
self._body = WeekGridBody(
|
||||
include_weekends=self.include_weekends,
|
||||
)
|
||||
yield self._header
|
||||
yield self._body
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Initialize on mount - set cursor to current day/time."""
|
||||
self._init_week()
|
||||
self.goto_now()
|
||||
|
||||
def _init_week(self) -> None:
|
||||
"""Initialize the week's day columns."""
|
||||
self._days = []
|
||||
# Always iterate through 7 days from week_start
|
||||
for i in range(7):
|
||||
day = self.week_start + timedelta(days=i)
|
||||
# Skip weekend days (Saturday=5, Sunday=6) when not including weekends
|
||||
if not self.include_weekends and day.weekday() >= 5:
|
||||
continue
|
||||
col = DayColumn(day=day)
|
||||
if day in self._events_by_date:
|
||||
col.events = self._events_by_date[day]
|
||||
col.layout_events()
|
||||
self._days.append(col)
|
||||
|
||||
# Update child widgets
|
||||
if self._header:
|
||||
self._header.update_days(
|
||||
[d.day for d in self._days], self._body.cursor_col if self._body else 0
|
||||
)
|
||||
if self._body:
|
||||
self._body.set_days(self._days)
|
||||
|
||||
def set_events(self, events_by_date: dict[date, List[Event]]) -> None:
|
||||
"""Set the events to display."""
|
||||
self._events_by_date = events_by_date
|
||||
self._init_week()
|
||||
self.refresh()
|
||||
|
||||
def goto_now(self) -> None:
|
||||
"""Set cursor to current day and time, scroll to work day start."""
|
||||
today = date.today()
|
||||
now = datetime.now()
|
||||
rows_per_hour = get_rows_per_hour()
|
||||
minutes_per_row = config.minutes_per_row()
|
||||
|
||||
# Set week to contain today using configurable week start day
|
||||
week_start_date = get_week_start_for_date(today)
|
||||
if self.week_start != week_start_date:
|
||||
self.week_start = week_start_date
|
||||
|
||||
# Set cursor column to today (relative to week start)
|
||||
col = get_day_column_for_date(today, self.week_start)
|
||||
if not self.include_weekends and col >= 5:
|
||||
col = 4 # Last weekday if weekend
|
||||
self.cursor_col = col
|
||||
|
||||
# Set cursor row to current time
|
||||
current_row = (now.hour * rows_per_hour) + (now.minute // minutes_per_row)
|
||||
self.cursor_row = current_row
|
||||
|
||||
# Always scroll to work day start initially (e.g., 7am)
|
||||
if self._body:
|
||||
work_start_row = config.work_day_start_hour() * rows_per_hour
|
||||
self._body.scroll_to(y=work_start_row, animate=False)
|
||||
|
||||
def watch_week_start(self, old: date, new: date) -> None:
|
||||
"""Handle week_start changes."""
|
||||
self._init_week()
|
||||
self.post_message(self.WeekChanged(new))
|
||||
self.refresh()
|
||||
|
||||
def watch_include_weekends(self, old: bool, new: bool) -> None:
|
||||
"""Handle include_weekends changes."""
|
||||
if self._header:
|
||||
self._header.set_include_weekends(new)
|
||||
if self._body:
|
||||
self._body.set_include_weekends(new)
|
||||
self._init_week()
|
||||
self.refresh()
|
||||
|
||||
def on_week_grid_body_cursor_moved(self, message: WeekGridBody.CursorMoved) -> None:
|
||||
"""Handle cursor moves in body - update header."""
|
||||
if self._header:
|
||||
self._header.update_days([d.day for d in self._days], message.col)
|
||||
|
||||
def get_event_at_cursor(self) -> Optional[Event]:
|
||||
"""Get the event at the current cursor position."""
|
||||
if self._body:
|
||||
return self._body.get_event_at_cursor()
|
||||
return None
|
||||
|
||||
def get_cursor_date(self) -> date:
|
||||
"""Get the date at the current cursor column."""
|
||||
if self._days and 0 <= self.cursor_col < len(self._days):
|
||||
return self._days[self.cursor_col].day
|
||||
return date.today()
|
||||
|
||||
def get_cursor_time(self):
|
||||
"""Get the time at the current cursor row.
|
||||
|
||||
Returns:
|
||||
A time object for the cursor row position.
|
||||
"""
|
||||
from datetime import time as time_type
|
||||
|
||||
minutes_per_row = config.minutes_per_row()
|
||||
total_minutes = self.cursor_row * minutes_per_row
|
||||
hour = total_minutes // 60
|
||||
minute = total_minutes % 60
|
||||
# Clamp to valid range
|
||||
hour = max(0, min(23, hour))
|
||||
minute = max(0, min(59, minute))
|
||||
return time_type(hour, minute)
|
||||
|
||||
# Actions
|
||||
def action_cursor_down(self) -> None:
|
||||
"""Move cursor down."""
|
||||
if self._body:
|
||||
self._body.cursor_row += 1
|
||||
|
||||
def action_cursor_up(self) -> None:
|
||||
"""Move cursor up."""
|
||||
if self._body:
|
||||
self._body.cursor_row -= 1
|
||||
|
||||
def action_cursor_left(self) -> None:
|
||||
"""Move cursor left (wraps to previous week)."""
|
||||
if self._body:
|
||||
if self._body.cursor_col <= 0:
|
||||
# Wrap to previous week
|
||||
self._body.cursor_col = self.num_days - 1
|
||||
self.action_prev_week()
|
||||
else:
|
||||
self._body.cursor_col -= 1
|
||||
if self._header:
|
||||
self._header.update_days(
|
||||
[d.day for d in self._days], self._body.cursor_col
|
||||
)
|
||||
|
||||
def action_cursor_right(self) -> None:
|
||||
"""Move cursor right (wraps to next week)."""
|
||||
if self._body:
|
||||
if self._body.cursor_col >= self.num_days - 1:
|
||||
# Wrap to next week
|
||||
self._body.cursor_col = 0
|
||||
self.action_next_week()
|
||||
else:
|
||||
self._body.cursor_col += 1
|
||||
if self._header:
|
||||
self._header.update_days(
|
||||
[d.day for d in self._days], self._body.cursor_col
|
||||
)
|
||||
|
||||
def action_prev_week(self) -> None:
|
||||
"""Navigate to previous week."""
|
||||
self.week_start = self.week_start - timedelta(weeks=1)
|
||||
|
||||
def action_next_week(self) -> None:
|
||||
"""Navigate to next week."""
|
||||
self.week_start = self.week_start + timedelta(weeks=1)
|
||||
|
||||
def action_goto_today(self) -> None:
|
||||
"""Navigate to current week and today's column/time."""
|
||||
self.goto_now()
|
||||
|
||||
def action_select_event(self) -> None:
|
||||
"""Select the event at cursor."""
|
||||
event = self.get_event_at_cursor()
|
||||
if event:
|
||||
self.post_message(self.EventSelected(event))
|
||||
|
||||
def goto_date(self, target_date: date) -> None:
|
||||
"""Navigate to a specific date.
|
||||
|
||||
Sets the week to contain the target date and places cursor on that day.
|
||||
"""
|
||||
# Get the week start for the target date
|
||||
week_start_date = get_week_start_for_date(target_date)
|
||||
|
||||
if self.week_start != week_start_date:
|
||||
self.week_start = week_start_date
|
||||
|
||||
# Set cursor column to the target date
|
||||
col = get_day_column_for_date(target_date, self.week_start)
|
||||
if not self.include_weekends and col >= 5:
|
||||
col = 4 # Last weekday if weekend
|
||||
self.cursor_col = col
|
||||
15
src/calendar/widgets/__init__.py
Normal file
15
src/calendar/widgets/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
"""Calendar TUI widgets."""
|
||||
|
||||
from .WeekGrid import WeekGrid
|
||||
from .AddEventForm import AddEventForm, EventFormData
|
||||
from .MonthCalendar import MonthCalendar
|
||||
from .InvitesPanel import InvitesPanel, CalendarInvite
|
||||
|
||||
__all__ = [
|
||||
"WeekGrid",
|
||||
"AddEventForm",
|
||||
"EventFormData",
|
||||
"MonthCalendar",
|
||||
"InvitesPanel",
|
||||
"CalendarInvite",
|
||||
]
|
||||
@@ -1,33 +1,55 @@
|
||||
# CLI module for the application
|
||||
# Uses lazy imports to speed up startup time
|
||||
|
||||
import click
|
||||
|
||||
from .sync import sync
|
||||
from .drive import drive
|
||||
from .email import email
|
||||
from .calendar import calendar
|
||||
from .ticktick import ticktick
|
||||
from .godspeed import godspeed
|
||||
from .gitlab_monitor import gitlab_monitor
|
||||
import importlib
|
||||
|
||||
|
||||
@click.group()
|
||||
class LazyGroup(click.Group):
|
||||
"""A click Group that lazily loads subcommands."""
|
||||
|
||||
def __init__(self, *args, lazy_subcommands=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self._lazy_subcommands = lazy_subcommands or {}
|
||||
|
||||
def list_commands(self, ctx):
|
||||
base = super().list_commands(ctx)
|
||||
lazy = list(self._lazy_subcommands.keys())
|
||||
return sorted(base + lazy)
|
||||
|
||||
def get_command(self, ctx, cmd_name):
|
||||
if cmd_name in self._lazy_subcommands:
|
||||
return self._load_command(cmd_name)
|
||||
return super().get_command(ctx, cmd_name)
|
||||
|
||||
def _load_command(self, cmd_name):
|
||||
module_path, attr_name = self._lazy_subcommands[cmd_name]
|
||||
# Handle relative imports
|
||||
if module_path.startswith("."):
|
||||
module = importlib.import_module(module_path, package="src.cli")
|
||||
else:
|
||||
module = importlib.import_module(module_path)
|
||||
return getattr(module, attr_name)
|
||||
|
||||
|
||||
# Create CLI with lazy loading - commands only imported when invoked
|
||||
@click.group(
|
||||
cls=LazyGroup,
|
||||
lazy_subcommands={
|
||||
"sync": (".sync", "sync"),
|
||||
"drive": (".drive", "drive"),
|
||||
"email": (".email", "email"),
|
||||
"mail": (".email", "email"), # alias
|
||||
"calendar": (".calendar", "calendar"),
|
||||
"ticktick": (".ticktick", "ticktick"),
|
||||
"tt": (".ticktick", "ticktick"), # alias
|
||||
"godspeed": (".godspeed", "godspeed"),
|
||||
"gs": (".godspeed", "godspeed"), # alias
|
||||
"gitlab_monitor": (".gitlab_monitor", "gitlab_monitor"),
|
||||
"glm": (".gitlab_monitor", "gitlab_monitor"), # alias
|
||||
"tasks": (".tasks", "tasks"),
|
||||
},
|
||||
)
|
||||
def cli():
|
||||
"""Root command for the CLI."""
|
||||
"""LUK - Local Unix Kit for productivity."""
|
||||
pass
|
||||
|
||||
|
||||
cli.add_command(sync)
|
||||
cli.add_command(drive)
|
||||
cli.add_command(email)
|
||||
cli.add_command(calendar)
|
||||
cli.add_command(ticktick)
|
||||
cli.add_command(godspeed)
|
||||
cli.add_command(gitlab_monitor)
|
||||
|
||||
# Add 'tt' as a short alias for ticktick
|
||||
cli.add_command(ticktick, name="tt")
|
||||
# Add 'gs' as a short alias for godspeed
|
||||
cli.add_command(godspeed, name="gs")
|
||||
# Add 'glm' as a short alias for gitlab_monitor
|
||||
cli.add_command(gitlab_monitor, name="glm")
|
||||
|
||||
@@ -1,8 +1,32 @@
|
||||
"""Calendar CLI commands."""
|
||||
|
||||
import click
|
||||
import subprocess
|
||||
|
||||
|
||||
@click.command()
|
||||
def calendar():
|
||||
"""Open the calendar (khal interactive)."""
|
||||
click.echo("Opening calendar...")
|
||||
subprocess.run(["khal", "interactive"])
|
||||
@click.option("--interactive", "-i", is_flag=True, help="Use khal interactive mode")
|
||||
@click.option("--weekdays", "-w", is_flag=True, help="Show only weekdays (Mon-Fri)")
|
||||
def calendar(interactive: bool, weekdays: bool):
|
||||
"""Open the calendar TUI.
|
||||
|
||||
Displays a week view of calendar events from khal.
|
||||
|
||||
Navigation:
|
||||
j/k - Move up/down (time)
|
||||
h/l - Move left/right (day)
|
||||
H/L - Previous/Next week
|
||||
g - Go to today
|
||||
w - Toggle weekends
|
||||
Enter - View event details
|
||||
q - Quit
|
||||
"""
|
||||
if interactive:
|
||||
# Fallback to khal interactive mode
|
||||
import subprocess
|
||||
|
||||
click.echo("Opening khal interactive...")
|
||||
subprocess.run(["khal", "interactive"])
|
||||
else:
|
||||
from src.calendar import run_app
|
||||
|
||||
run_app()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import click
|
||||
from src.maildir_gtd.app import launch_email_viewer
|
||||
from src.mail.app import launch_email_viewer
|
||||
|
||||
|
||||
@click.command()
|
||||
|
||||
327
src/cli/sync.py
327
src/cli/sync.py
@@ -20,6 +20,7 @@ from src.services.microsoft_graph.calendar import (
|
||||
)
|
||||
from src.services.microsoft_graph.mail import (
|
||||
fetch_mail_async,
|
||||
fetch_archive_mail_async,
|
||||
archive_mail_async,
|
||||
delete_mail_async,
|
||||
synchronize_maildir_async,
|
||||
@@ -216,7 +217,9 @@ def create_maildir_structure(base_path):
|
||||
ensure_directory_exists(os.path.join(base_path, "cur"))
|
||||
ensure_directory_exists(os.path.join(base_path, "new"))
|
||||
ensure_directory_exists(os.path.join(base_path, "tmp"))
|
||||
ensure_directory_exists(os.path.join(base_path, ".Archives"))
|
||||
ensure_directory_exists(os.path.join(base_path, ".Archive", "cur"))
|
||||
ensure_directory_exists(os.path.join(base_path, ".Archive", "new"))
|
||||
ensure_directory_exists(os.path.join(base_path, ".Archive", "tmp"))
|
||||
ensure_directory_exists(os.path.join(base_path, ".Trash", "cur"))
|
||||
# Create outbox structure for sending emails
|
||||
ensure_directory_exists(os.path.join(base_path, "outbox", "new"))
|
||||
@@ -436,6 +439,7 @@ async def _sync_outlook_data(
|
||||
|
||||
with progress:
|
||||
task_fetch = progress.add_task("[green]Syncing Inbox...", total=0)
|
||||
task_fetch_archive = progress.add_task("[green]Syncing Archive...", total=0)
|
||||
task_calendar = progress.add_task("[cyan]Fetching calendar...", total=0)
|
||||
task_local_calendar = progress.add_task(
|
||||
"[magenta]Syncing local calendar...", total=0
|
||||
@@ -471,7 +475,13 @@ async def _sync_outlook_data(
|
||||
archive_mail_async(maildir_path, headers, progress, task_archive, dry_run),
|
||||
delete_mail_async(maildir_path, headers, progress, task_delete, dry_run),
|
||||
process_outbox_async(
|
||||
base_maildir_path, org, headers, progress, task_outbox, dry_run
|
||||
base_maildir_path,
|
||||
org,
|
||||
headers,
|
||||
progress,
|
||||
task_outbox,
|
||||
dry_run,
|
||||
access_token=access_token,
|
||||
),
|
||||
)
|
||||
progress.console.print("[bold green]Step 1: Local changes synced.[/bold green]")
|
||||
@@ -515,6 +525,15 @@ async def _sync_outlook_data(
|
||||
dry_run,
|
||||
download_attachments,
|
||||
),
|
||||
fetch_archive_mail_async(
|
||||
maildir_path,
|
||||
attachments_dir,
|
||||
headers,
|
||||
progress,
|
||||
task_fetch_archive,
|
||||
dry_run,
|
||||
download_attachments,
|
||||
),
|
||||
fetch_calendar_async(
|
||||
headers,
|
||||
progress,
|
||||
@@ -575,10 +594,188 @@ async def _sync_outlook_data(
|
||||
click.echo("Sync complete.")
|
||||
|
||||
|
||||
@click.group()
|
||||
def sync():
|
||||
"""Email and calendar synchronization."""
|
||||
pass
|
||||
@click.group(invoke_without_command=True)
|
||||
@click.option(
|
||||
"--once",
|
||||
is_flag=True,
|
||||
help="Run a single sync and exit (non-interactive).",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--daemon",
|
||||
is_flag=True,
|
||||
help="Run in background daemon mode.",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--org",
|
||||
help="Specify the organization name for the subfolder to store emails and calendar events",
|
||||
default="corteva",
|
||||
)
|
||||
@click.option(
|
||||
"--vdir",
|
||||
help="Output calendar events in vdir format to the specified directory",
|
||||
default="~/Calendar",
|
||||
)
|
||||
@click.option(
|
||||
"--notify/--no-notify",
|
||||
help="Send macOS notifications for new email messages",
|
||||
default=True,
|
||||
)
|
||||
@click.option(
|
||||
"--dry-run",
|
||||
is_flag=True,
|
||||
help="Run in dry-run mode without making changes.",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--demo",
|
||||
is_flag=True,
|
||||
help="Run with simulated sync (demo mode)",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--days-back",
|
||||
type=int,
|
||||
help="Number of days to look back for calendar events",
|
||||
default=1,
|
||||
)
|
||||
@click.option(
|
||||
"--days-forward",
|
||||
type=int,
|
||||
help="Number of days to look forward for calendar events",
|
||||
default=30,
|
||||
)
|
||||
@click.option(
|
||||
"--download-attachments",
|
||||
is_flag=True,
|
||||
help="Download email attachments",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--two-way-calendar",
|
||||
is_flag=True,
|
||||
help="Enable two-way calendar sync (sync local changes to server)",
|
||||
default=False,
|
||||
)
|
||||
@click.pass_context
|
||||
def sync(
|
||||
ctx,
|
||||
once,
|
||||
daemon,
|
||||
org,
|
||||
vdir,
|
||||
notify,
|
||||
dry_run,
|
||||
demo,
|
||||
days_back,
|
||||
days_forward,
|
||||
download_attachments,
|
||||
two_way_calendar,
|
||||
):
|
||||
"""Email and calendar synchronization.
|
||||
|
||||
By default, opens the interactive TUI dashboard.
|
||||
Use --once for a single sync, or --daemon for background mode.
|
||||
"""
|
||||
# If a subcommand is invoked, let it handle everything
|
||||
if ctx.invoked_subcommand is not None:
|
||||
return
|
||||
|
||||
# Handle the default behavior (no subcommand)
|
||||
if daemon:
|
||||
# Run in daemon mode
|
||||
from .sync_daemon import create_daemon_config, SyncDaemon
|
||||
|
||||
config = create_daemon_config(
|
||||
dry_run=dry_run,
|
||||
vdir=vdir,
|
||||
icsfile=None,
|
||||
org=org,
|
||||
days_back=days_back,
|
||||
days_forward=days_forward,
|
||||
continue_iteration=False,
|
||||
download_attachments=download_attachments,
|
||||
two_way_calendar=two_way_calendar,
|
||||
notify=notify,
|
||||
)
|
||||
|
||||
daemon_instance = SyncDaemon(config)
|
||||
daemon_instance.start()
|
||||
elif once:
|
||||
# Run a single sync (non-interactive)
|
||||
asyncio.run(
|
||||
_sync_outlook_data(
|
||||
dry_run,
|
||||
vdir,
|
||||
None, # icsfile
|
||||
org,
|
||||
days_back,
|
||||
days_forward,
|
||||
False, # continue_iteration
|
||||
download_attachments,
|
||||
two_way_calendar,
|
||||
notify,
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Default: Launch interactive TUI dashboard
|
||||
from .sync_dashboard import run_dashboard_sync
|
||||
from src.services.microsoft_graph.auth import has_valid_cached_token
|
||||
|
||||
# Check if we need to authenticate before starting the TUI
|
||||
# This prevents the TUI from appearing to freeze during device flow auth
|
||||
if not demo:
|
||||
scopes = [
|
||||
"https://graph.microsoft.com/Calendars.Read",
|
||||
"https://graph.microsoft.com/Mail.ReadWrite",
|
||||
]
|
||||
if not has_valid_cached_token(scopes):
|
||||
click.echo("Authentication required. Please complete the login flow...")
|
||||
try:
|
||||
# This will trigger the device flow auth in the console
|
||||
get_access_token(scopes)
|
||||
click.echo("Authentication successful! Starting dashboard...")
|
||||
except Exception as e:
|
||||
click.echo(f"Authentication failed: {e}")
|
||||
return
|
||||
|
||||
# Pre-authenticate SMTP token only if SMTP sending is enabled in config
|
||||
from src.mail.config import get_config
|
||||
|
||||
config = get_config()
|
||||
if config.mail.enable_smtp_send:
|
||||
from src.services.microsoft_graph.auth import get_smtp_access_token
|
||||
|
||||
smtp_token = get_smtp_access_token(silent_only=True)
|
||||
if not smtp_token:
|
||||
click.echo(
|
||||
"SMTP authentication required for sending calendar replies..."
|
||||
)
|
||||
try:
|
||||
smtp_token = get_smtp_access_token(silent_only=False)
|
||||
if smtp_token:
|
||||
click.echo("SMTP authentication successful!")
|
||||
except Exception as e:
|
||||
click.echo(
|
||||
f"SMTP authentication failed (calendar replies may not work): {e}"
|
||||
)
|
||||
|
||||
sync_config = {
|
||||
"org": org,
|
||||
"vdir": vdir,
|
||||
"notify": notify,
|
||||
"dry_run": dry_run,
|
||||
"days_back": days_back,
|
||||
"days_forward": days_forward,
|
||||
"download_attachments": download_attachments,
|
||||
"two_way_calendar": two_way_calendar,
|
||||
"continue_iteration": False,
|
||||
"icsfile": None,
|
||||
}
|
||||
asyncio.run(
|
||||
run_dashboard_sync(notify=notify, sync_config=sync_config, demo_mode=demo)
|
||||
)
|
||||
|
||||
|
||||
def daemonize():
|
||||
@@ -669,18 +866,6 @@ def daemonize():
|
||||
help="Enable two-way calendar sync (sync local changes to server)",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--daemon",
|
||||
is_flag=True,
|
||||
help="Run in daemon mode.",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--dashboard",
|
||||
is_flag=True,
|
||||
help="Run with TUI dashboard.",
|
||||
default=False,
|
||||
)
|
||||
@click.option(
|
||||
"--notify",
|
||||
is_flag=True,
|
||||
@@ -697,59 +882,23 @@ def run(
|
||||
continue_iteration,
|
||||
download_attachments,
|
||||
two_way_calendar,
|
||||
daemon,
|
||||
dashboard,
|
||||
notify,
|
||||
):
|
||||
if dashboard:
|
||||
from .sync_dashboard import run_dashboard_sync
|
||||
|
||||
sync_config = {
|
||||
"dry_run": dry_run,
|
||||
"vdir": vdir,
|
||||
"icsfile": icsfile,
|
||||
"org": org,
|
||||
"days_back": days_back,
|
||||
"days_forward": days_forward,
|
||||
"continue_iteration": continue_iteration,
|
||||
"download_attachments": download_attachments,
|
||||
"two_way_calendar": two_way_calendar,
|
||||
"notify": notify,
|
||||
}
|
||||
asyncio.run(run_dashboard_sync(notify=notify, sync_config=sync_config))
|
||||
elif daemon:
|
||||
from .sync_daemon import create_daemon_config, SyncDaemon
|
||||
|
||||
config = create_daemon_config(
|
||||
dry_run=dry_run,
|
||||
vdir=vdir,
|
||||
icsfile=icsfile,
|
||||
org=org,
|
||||
days_back=days_back,
|
||||
days_forward=days_forward,
|
||||
continue_iteration=continue_iteration,
|
||||
download_attachments=download_attachments,
|
||||
two_way_calendar=two_way_calendar,
|
||||
notify=notify,
|
||||
)
|
||||
|
||||
daemon_instance = SyncDaemon(config)
|
||||
daemon_instance.start()
|
||||
else:
|
||||
asyncio.run(
|
||||
_sync_outlook_data(
|
||||
dry_run,
|
||||
vdir,
|
||||
icsfile,
|
||||
org,
|
||||
days_back,
|
||||
days_forward,
|
||||
continue_iteration,
|
||||
download_attachments,
|
||||
two_way_calendar,
|
||||
notify,
|
||||
)
|
||||
"""Run a single sync operation (legacy command, prefer 'luk sync --once')."""
|
||||
asyncio.run(
|
||||
_sync_outlook_data(
|
||||
dry_run,
|
||||
vdir,
|
||||
icsfile,
|
||||
org,
|
||||
days_back,
|
||||
days_forward,
|
||||
continue_iteration,
|
||||
download_attachments,
|
||||
two_way_calendar,
|
||||
notify,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@sync.command()
|
||||
@@ -832,6 +981,48 @@ def status():
|
||||
def interactive(org, vdir, notify, dry_run, demo):
|
||||
"""Launch interactive TUI dashboard for sync operations."""
|
||||
from .sync_dashboard import run_dashboard_sync
|
||||
from src.services.microsoft_graph.auth import (
|
||||
has_valid_cached_token,
|
||||
get_access_token,
|
||||
)
|
||||
|
||||
# Check if we need to authenticate before starting the TUI
|
||||
# This prevents the TUI from appearing to freeze during device flow auth
|
||||
if not demo:
|
||||
scopes = [
|
||||
"https://graph.microsoft.com/Calendars.Read",
|
||||
"https://graph.microsoft.com/Mail.ReadWrite",
|
||||
]
|
||||
if not has_valid_cached_token(scopes):
|
||||
click.echo("Authentication required. Please complete the login flow...")
|
||||
try:
|
||||
# This will trigger the device flow auth in the console
|
||||
get_access_token(scopes)
|
||||
click.echo("Authentication successful! Starting dashboard...")
|
||||
except Exception as e:
|
||||
click.echo(f"Authentication failed: {e}")
|
||||
return
|
||||
|
||||
# Pre-authenticate SMTP token only if SMTP sending is enabled in config
|
||||
from src.mail.config import get_config
|
||||
|
||||
config = get_config()
|
||||
if config.mail.enable_smtp_send:
|
||||
from src.services.microsoft_graph.auth import get_smtp_access_token
|
||||
|
||||
smtp_token = get_smtp_access_token(silent_only=True)
|
||||
if not smtp_token:
|
||||
click.echo(
|
||||
"SMTP authentication required for sending calendar replies..."
|
||||
)
|
||||
try:
|
||||
smtp_token = get_smtp_access_token(silent_only=False)
|
||||
if smtp_token:
|
||||
click.echo("SMTP authentication successful!")
|
||||
except Exception as e:
|
||||
click.echo(
|
||||
f"SMTP authentication failed (calendar replies may not work): {e}"
|
||||
)
|
||||
|
||||
sync_config = {
|
||||
"org": org,
|
||||
|
||||
@@ -14,6 +14,7 @@ from typing import Optional, Dict, Any
|
||||
|
||||
from src.cli.sync import _sync_outlook_data, should_run_godspeed_sync, should_run_sweep
|
||||
from src.cli.sync import run_godspeed_sync, run_task_sweep, load_sync_state
|
||||
from src.utils.ipc import notify_all, notify_refresh
|
||||
|
||||
|
||||
class SyncDaemon:
|
||||
@@ -247,6 +248,13 @@ class SyncDaemon:
|
||||
notify=self.config.get("notify", False),
|
||||
)
|
||||
self.logger.info("Sync completed successfully")
|
||||
|
||||
# Notify all running TUI apps to refresh their data
|
||||
results = await notify_all({"source": "sync_daemon"})
|
||||
notified = [app for app, success in results.items() if success]
|
||||
if notified:
|
||||
self.logger.info(f"Notified apps to refresh: {', '.join(notified)}")
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"Sync failed: {e}")
|
||||
|
||||
|
||||
@@ -17,15 +17,45 @@ from textual.binding import Binding
|
||||
from rich.text import Text
|
||||
from datetime import datetime, timedelta
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from typing import Dict, Any, Optional, List, Callable
|
||||
from pathlib import Path
|
||||
|
||||
from src.utils.shared_config import get_theme_name
|
||||
|
||||
# Default sync interval in seconds (5 minutes)
|
||||
DEFAULT_SYNC_INTERVAL = 300
|
||||
|
||||
# Sync tasks config file path
|
||||
SYNC_TASKS_CONFIG_PATH = os.path.expanduser("~/.config/luk/sync_tasks.json")
|
||||
|
||||
|
||||
def load_sync_tasks_config() -> Dict[str, bool]:
|
||||
"""Load sync tasks enabled/disabled state from config file.
|
||||
|
||||
Returns:
|
||||
Dict mapping task_id to enabled state (True = enabled, False = disabled)
|
||||
"""
|
||||
if os.path.exists(SYNC_TASKS_CONFIG_PATH):
|
||||
try:
|
||||
with open(SYNC_TASKS_CONFIG_PATH, "r") as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
# Default: all tasks enabled
|
||||
return {}
|
||||
|
||||
|
||||
def save_sync_tasks_config(config: Dict[str, bool]) -> None:
|
||||
"""Save sync tasks enabled/disabled state to config file."""
|
||||
os.makedirs(os.path.dirname(SYNC_TASKS_CONFIG_PATH), exist_ok=True)
|
||||
with open(SYNC_TASKS_CONFIG_PATH, "w") as f:
|
||||
json.dump(config, f, indent=2)
|
||||
|
||||
|
||||
# Futuristic spinner frames
|
||||
# SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
|
||||
# Alternative spinners you could use:
|
||||
@@ -73,7 +103,9 @@ class TaskStatus:
|
||||
class TaskListItem(ListItem):
|
||||
"""A list item representing a sync task."""
|
||||
|
||||
def __init__(self, task_id: str, task_name: str, *args, **kwargs):
|
||||
def __init__(
|
||||
self, task_id: str, task_name: str, enabled: bool = True, *args, **kwargs
|
||||
):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.task_id = task_id
|
||||
self.task_name = task_name
|
||||
@@ -81,6 +113,7 @@ class TaskListItem(ListItem):
|
||||
self.progress = 0
|
||||
self.total = 100
|
||||
self.spinner_frame = 0
|
||||
self.enabled = enabled
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Compose the task item layout."""
|
||||
@@ -88,6 +121,8 @@ class TaskListItem(ListItem):
|
||||
|
||||
def _get_status_icon(self) -> str:
|
||||
"""Get icon based on status."""
|
||||
if not self.enabled:
|
||||
return "⊘" # Disabled icon
|
||||
if self.status == TaskStatus.RUNNING:
|
||||
return SPINNER_FRAMES[self.spinner_frame % len(SPINNER_FRAMES)]
|
||||
icons = {
|
||||
@@ -103,6 +138,8 @@ class TaskListItem(ListItem):
|
||||
|
||||
def _get_status_color(self) -> str:
|
||||
"""Get color based on status."""
|
||||
if not self.enabled:
|
||||
return "dim italic"
|
||||
colors = {
|
||||
TaskStatus.PENDING: "dim",
|
||||
TaskStatus.RUNNING: "cyan",
|
||||
@@ -116,6 +153,13 @@ class TaskListItem(ListItem):
|
||||
icon = self._get_status_icon()
|
||||
color = self._get_status_color()
|
||||
|
||||
# Disabled tasks show differently
|
||||
if not self.enabled:
|
||||
text = Text()
|
||||
text.append(f"{icon} ", style="dim")
|
||||
text.append(f"{self.task_name} [Disabled]", style=color)
|
||||
return text
|
||||
|
||||
# Use green checkmark for completed, but white text for readability
|
||||
if self.status == TaskStatus.RUNNING:
|
||||
progress_pct = (
|
||||
@@ -156,6 +200,7 @@ class SyncDashboard(App):
|
||||
Binding("s", "sync_now", "Sync Now"),
|
||||
Binding("d", "daemonize", "Daemonize"),
|
||||
Binding("r", "refresh", "Refresh"),
|
||||
Binding("t", "toggle_task", "Toggle"),
|
||||
Binding("+", "increase_interval", "+Interval"),
|
||||
Binding("-", "decrease_interval", "-Interval"),
|
||||
Binding("up", "cursor_up", "Up", show=False),
|
||||
@@ -171,16 +216,10 @@ class SyncDashboard(App):
|
||||
.sidebar {
|
||||
width: 30;
|
||||
height: 100%;
|
||||
border: solid $primary;
|
||||
border: round $primary;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.sidebar-title {
|
||||
text-style: bold;
|
||||
padding: 1;
|
||||
background: $primary-darken-2;
|
||||
}
|
||||
|
||||
.countdown-container {
|
||||
height: 5;
|
||||
padding: 0 1;
|
||||
@@ -224,15 +263,10 @@ class SyncDashboard(App):
|
||||
|
||||
.log-container {
|
||||
height: 1fr;
|
||||
border: solid $primary;
|
||||
border: round $primary;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.log-title {
|
||||
padding: 0 1;
|
||||
background: $primary-darken-2;
|
||||
}
|
||||
|
||||
ListView {
|
||||
height: 1fr;
|
||||
}
|
||||
@@ -277,28 +311,75 @@ class SyncDashboard(App):
|
||||
self._initial_sync_interval = sync_interval
|
||||
self._notify = notify
|
||||
self._demo_mode = demo_mode
|
||||
# Load task enabled/disabled config
|
||||
self._tasks_config = load_sync_tasks_config()
|
||||
# Merge provided config with defaults
|
||||
self._sync_config = {**DEFAULT_SYNC_CONFIG, **(sync_config or {})}
|
||||
self._sync_config["notify"] = notify
|
||||
|
||||
def _is_task_enabled(self, task_id: str) -> bool:
|
||||
"""Check if a task is enabled (default: True)."""
|
||||
return self._tasks_config.get(task_id, True)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Compose the dashboard layout."""
|
||||
yield Header()
|
||||
|
||||
with Horizontal(classes="dashboard"):
|
||||
# Sidebar with task list
|
||||
with Vertical(classes="sidebar"):
|
||||
yield Static("Tasks", classes="sidebar-title")
|
||||
with Vertical(classes="sidebar", id="tasks-sidebar"):
|
||||
yield ListView(
|
||||
# Stage 1: Sync local changes to server
|
||||
TaskListItem("archive", "Archive Mail", id="task-archive"),
|
||||
TaskListItem("outbox", "Outbox Send", id="task-outbox"),
|
||||
TaskListItem(
|
||||
"archive",
|
||||
"Archive Mail",
|
||||
enabled=self._is_task_enabled("archive"),
|
||||
id="task-archive",
|
||||
),
|
||||
TaskListItem(
|
||||
"outbox",
|
||||
"Outbox Send",
|
||||
enabled=self._is_task_enabled("outbox"),
|
||||
id="task-outbox",
|
||||
),
|
||||
# Stage 2: Fetch from server
|
||||
TaskListItem("inbox", "Inbox Sync", id="task-inbox"),
|
||||
TaskListItem("calendar", "Calendar Sync", id="task-calendar"),
|
||||
TaskListItem(
|
||||
"inbox",
|
||||
"Inbox Sync",
|
||||
enabled=self._is_task_enabled("inbox"),
|
||||
id="task-inbox",
|
||||
),
|
||||
TaskListItem(
|
||||
"archive-fetch",
|
||||
"Archive Sync",
|
||||
enabled=self._is_task_enabled("archive-fetch"),
|
||||
id="task-archive-fetch",
|
||||
),
|
||||
TaskListItem(
|
||||
"calendar",
|
||||
"Calendar Sync",
|
||||
enabled=self._is_task_enabled("calendar"),
|
||||
id="task-calendar",
|
||||
),
|
||||
# Stage 3: Task management
|
||||
TaskListItem("godspeed", "Godspeed Sync", id="task-godspeed"),
|
||||
TaskListItem("sweep", "Task Sweep", id="task-sweep"),
|
||||
TaskListItem(
|
||||
"godspeed",
|
||||
"Godspeed Sync",
|
||||
enabled=self._is_task_enabled("godspeed"),
|
||||
id="task-godspeed",
|
||||
),
|
||||
TaskListItem(
|
||||
"dstask",
|
||||
"dstask Sync",
|
||||
enabled=self._is_task_enabled("dstask"),
|
||||
id="task-dstask",
|
||||
),
|
||||
TaskListItem(
|
||||
"sweep",
|
||||
"Task Sweep",
|
||||
enabled=self._is_task_enabled("sweep"),
|
||||
id="task-sweep",
|
||||
),
|
||||
id="task-list",
|
||||
)
|
||||
# Countdown timer at bottom of sidebar
|
||||
@@ -323,14 +404,23 @@ class SyncDashboard(App):
|
||||
yield Static("0%", id="progress-percent")
|
||||
|
||||
# Log for selected task
|
||||
with Vertical(classes="log-container"):
|
||||
yield Static("Activity Log", classes="log-title")
|
||||
with Vertical(classes="log-container", id="log-container"):
|
||||
yield Log(id="task-log")
|
||||
|
||||
yield Footer()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Initialize the dashboard."""
|
||||
# Set theme from shared config
|
||||
self.theme = get_theme_name()
|
||||
|
||||
# Set border titles
|
||||
try:
|
||||
self.query_one("#tasks-sidebar").border_title = "Tasks"
|
||||
self.query_one("#log-container").border_title = "Activity Log"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Store references to task items
|
||||
task_list = self.query_one("#task-list", ListView)
|
||||
for item in task_list.children:
|
||||
@@ -435,6 +525,12 @@ class SyncDashboard(App):
|
||||
if task_id == self.selected_task:
|
||||
self._update_main_panel()
|
||||
|
||||
def is_task_enabled(self, task_id: str) -> bool:
|
||||
"""Check if a task is enabled."""
|
||||
if task_id in self._task_items:
|
||||
return self._task_items[task_id].enabled
|
||||
return self._is_task_enabled(task_id)
|
||||
|
||||
def update_task(self, task_id: str, progress: int, message: str = "") -> None:
|
||||
"""Update task progress."""
|
||||
if task_id in self._task_items:
|
||||
@@ -494,6 +590,25 @@ class SyncDashboard(App):
|
||||
task_list = self.query_one("#task-list", ListView)
|
||||
task_list.action_cursor_down()
|
||||
|
||||
def action_toggle_task(self) -> None:
|
||||
"""Toggle the selected task enabled/disabled state."""
|
||||
if self.selected_task not in self._task_items:
|
||||
return
|
||||
|
||||
item = self._task_items[self.selected_task]
|
||||
# Toggle enabled state
|
||||
item.enabled = not item.enabled
|
||||
item.update_display()
|
||||
|
||||
# Update config and save
|
||||
self._tasks_config[self.selected_task] = item.enabled
|
||||
save_sync_tasks_config(self._tasks_config)
|
||||
|
||||
# Log the change
|
||||
state = "enabled" if item.enabled else "disabled"
|
||||
self._log_to_task(self.selected_task, f"Task {state}")
|
||||
self._update_main_panel()
|
||||
|
||||
def action_sync_now(self) -> None:
|
||||
"""Trigger an immediate sync."""
|
||||
if self._sync_callback:
|
||||
@@ -769,6 +884,10 @@ class SyncProgressTracker:
|
||||
"""Mark a task as skipped."""
|
||||
self.dashboard.skip_task(task_id, reason)
|
||||
|
||||
def is_task_enabled(self, task_id: str) -> bool:
|
||||
"""Check if a task is enabled."""
|
||||
return self.dashboard.is_task_enabled(task_id)
|
||||
|
||||
|
||||
# Global dashboard instance
|
||||
_dashboard_instance: Optional[SyncDashboard] = None
|
||||
@@ -822,64 +941,108 @@ async def run_dashboard_sync(
|
||||
# Stage 1: Sync local changes to server
|
||||
|
||||
# Archive mail
|
||||
tracker.start_task("archive", 100)
|
||||
tracker.update_task("archive", 50, "Scanning for archived messages...")
|
||||
await asyncio.sleep(0.3)
|
||||
tracker.update_task("archive", 100, "Moving 3 messages to archive...")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.complete_task("archive", "3 messages archived")
|
||||
if tracker.is_task_enabled("archive"):
|
||||
tracker.start_task("archive", 100)
|
||||
tracker.update_task("archive", 50, "Scanning for archived messages...")
|
||||
await asyncio.sleep(0.3)
|
||||
tracker.update_task("archive", 100, "Moving 3 messages to archive...")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.complete_task("archive", "3 messages archived")
|
||||
else:
|
||||
tracker.skip_task("archive", "Disabled")
|
||||
|
||||
# Outbox
|
||||
tracker.start_task("outbox", 100)
|
||||
tracker.update_task("outbox", 50, "Checking outbox...")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.complete_task("outbox", "No pending emails")
|
||||
if tracker.is_task_enabled("outbox"):
|
||||
tracker.start_task("outbox", 100)
|
||||
tracker.update_task("outbox", 50, "Checking outbox...")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.complete_task("outbox", "No pending emails")
|
||||
else:
|
||||
tracker.skip_task("outbox", "Disabled")
|
||||
|
||||
# Stage 2: Fetch from server
|
||||
|
||||
# Inbox sync - simulate finding new messages
|
||||
tracker.start_task("inbox", 100)
|
||||
for i in range(0, 101, 20):
|
||||
tracker.update_task("inbox", i, f"Fetching emails... {i}%")
|
||||
await asyncio.sleep(0.3)
|
||||
if tracker.is_task_enabled("inbox"):
|
||||
tracker.start_task("inbox", 100)
|
||||
for i in range(0, 101, 20):
|
||||
tracker.update_task("inbox", i, f"Fetching emails... {i}%")
|
||||
await asyncio.sleep(0.3)
|
||||
|
||||
new_message_count = random.randint(0, 5)
|
||||
if new_message_count > 0:
|
||||
tracker.complete_task("inbox", f"{new_message_count} new emails")
|
||||
if dashboard._notify:
|
||||
from src.utils.notifications import notify_new_emails
|
||||
new_message_count = random.randint(0, 5)
|
||||
if new_message_count > 0:
|
||||
tracker.complete_task("inbox", f"{new_message_count} new emails")
|
||||
if dashboard._notify:
|
||||
from src.utils.notifications import notify_new_emails
|
||||
|
||||
notify_new_emails(new_message_count, "")
|
||||
notify_new_emails(new_message_count, "")
|
||||
else:
|
||||
tracker.complete_task("inbox", "No new emails")
|
||||
else:
|
||||
tracker.complete_task("inbox", "No new emails")
|
||||
tracker.skip_task("inbox", "Disabled")
|
||||
|
||||
# Archive fetch
|
||||
if tracker.is_task_enabled("archive-fetch"):
|
||||
tracker.start_task("archive-fetch", 100)
|
||||
for i in range(0, 101, 25):
|
||||
tracker.update_task("archive-fetch", i, f"Fetching archive... {i}%")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.complete_task("archive-fetch", "Archive synced")
|
||||
else:
|
||||
tracker.skip_task("archive-fetch", "Disabled")
|
||||
|
||||
# Calendar sync
|
||||
tracker.start_task("calendar", 100)
|
||||
for i in range(0, 101, 25):
|
||||
tracker.update_task("calendar", i, f"Syncing events... {i}%")
|
||||
await asyncio.sleep(0.3)
|
||||
tracker.complete_task("calendar", "25 events synced")
|
||||
if tracker.is_task_enabled("calendar"):
|
||||
tracker.start_task("calendar", 100)
|
||||
for i in range(0, 101, 25):
|
||||
tracker.update_task("calendar", i, f"Syncing events... {i}%")
|
||||
await asyncio.sleep(0.3)
|
||||
tracker.complete_task("calendar", "25 events synced")
|
||||
else:
|
||||
tracker.skip_task("calendar", "Disabled")
|
||||
|
||||
# Stage 3: Task management
|
||||
|
||||
# Godspeed sync
|
||||
tracker.start_task("godspeed", 100)
|
||||
for i in range(0, 101, 33):
|
||||
tracker.update_task(
|
||||
"godspeed", min(i, 100), f"Syncing tasks... {min(i, 100)}%"
|
||||
)
|
||||
if tracker.is_task_enabled("godspeed"):
|
||||
tracker.start_task("godspeed", 100)
|
||||
for i in range(0, 101, 33):
|
||||
tracker.update_task(
|
||||
"godspeed", min(i, 100), f"Syncing tasks... {min(i, 100)}%"
|
||||
)
|
||||
await asyncio.sleep(0.3)
|
||||
tracker.complete_task("godspeed", "42 tasks synced")
|
||||
else:
|
||||
tracker.skip_task("godspeed", "Disabled")
|
||||
|
||||
# dstask sync
|
||||
if tracker.is_task_enabled("dstask"):
|
||||
tracker.start_task("dstask", 100)
|
||||
tracker.update_task("dstask", 30, "Running dstask sync...")
|
||||
await asyncio.sleep(0.3)
|
||||
tracker.complete_task("godspeed", "42 tasks synced")
|
||||
tracker.update_task("dstask", 70, "Pushing changes...")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.complete_task("dstask", "Sync completed")
|
||||
else:
|
||||
tracker.skip_task("dstask", "Disabled")
|
||||
|
||||
# Task sweep
|
||||
tracker.start_task("sweep")
|
||||
tracker.update_task("sweep", 50, "Scanning notes directory...")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.skip_task("sweep", "Before 6 PM, skipping daily sweep")
|
||||
if tracker.is_task_enabled("sweep"):
|
||||
tracker.start_task("sweep")
|
||||
tracker.update_task("sweep", 50, "Scanning notes directory...")
|
||||
await asyncio.sleep(0.2)
|
||||
tracker.skip_task("sweep", "Before 6 PM, skipping daily sweep")
|
||||
else:
|
||||
tracker.skip_task("sweep", "Disabled")
|
||||
|
||||
# Schedule next sync
|
||||
dashboard.schedule_next_sync()
|
||||
|
||||
# Notify all running TUI apps to refresh their data
|
||||
from src.utils.ipc import notify_all
|
||||
|
||||
await notify_all({"source": "sync_dashboard_demo"})
|
||||
|
||||
except Exception as e:
|
||||
tracker.error_task("archive", str(e))
|
||||
|
||||
@@ -893,6 +1056,7 @@ async def run_dashboard_sync(
|
||||
synchronize_maildir_async,
|
||||
process_outbox_async,
|
||||
fetch_mail_async,
|
||||
fetch_archive_mail_async,
|
||||
)
|
||||
from src.services.microsoft_graph.calendar import (
|
||||
fetch_calendar_events,
|
||||
@@ -911,6 +1075,7 @@ async def run_dashboard_sync(
|
||||
)
|
||||
from src.utils.calendar_utils import save_events_to_vdir, save_events_to_file
|
||||
from src.utils.notifications import notify_new_emails
|
||||
from src.utils.ipc import notify_all
|
||||
|
||||
config = dashboard._sync_config
|
||||
|
||||
@@ -946,32 +1111,52 @@ async def run_dashboard_sync(
|
||||
# ===== STAGE 1: Sync local changes to server =====
|
||||
|
||||
# Archive mail
|
||||
tracker.start_task("archive", 100)
|
||||
tracker.update_task("archive", 10, "Checking for archived messages...")
|
||||
try:
|
||||
archive_progress = DashboardProgressAdapter(tracker, "archive")
|
||||
await archive_mail_async(
|
||||
maildir_path, headers, archive_progress, None, dry_run
|
||||
)
|
||||
tracker.complete_task("archive", "Archive sync complete")
|
||||
except Exception as e:
|
||||
tracker.error_task("archive", str(e))
|
||||
if tracker.is_task_enabled("archive"):
|
||||
tracker.start_task("archive", 100)
|
||||
tracker.update_task("archive", 10, "Checking for archived messages...")
|
||||
try:
|
||||
archive_progress = DashboardProgressAdapter(tracker, "archive")
|
||||
await archive_mail_async(
|
||||
maildir_path,
|
||||
headers,
|
||||
archive_progress,
|
||||
None,
|
||||
dry_run,
|
||||
is_cancelled=lambda: not tracker.is_task_enabled("archive"),
|
||||
)
|
||||
if tracker.is_task_enabled("archive"):
|
||||
tracker.complete_task("archive", "Archive sync complete")
|
||||
else:
|
||||
tracker.skip_task("archive", "Cancelled")
|
||||
except Exception as e:
|
||||
tracker.error_task("archive", str(e))
|
||||
else:
|
||||
tracker.skip_task("archive", "Disabled")
|
||||
|
||||
# Process outbox (send pending emails)
|
||||
tracker.start_task("outbox", 100)
|
||||
tracker.update_task("outbox", 10, "Checking outbox...")
|
||||
try:
|
||||
outbox_progress = DashboardProgressAdapter(tracker, "outbox")
|
||||
result = await process_outbox_async(
|
||||
base_maildir_path, org, headers, outbox_progress, None, dry_run
|
||||
)
|
||||
sent_count, failed_count = result if result else (0, 0)
|
||||
if sent_count > 0:
|
||||
tracker.complete_task("outbox", f"{sent_count} emails sent")
|
||||
else:
|
||||
tracker.complete_task("outbox", "No pending emails")
|
||||
except Exception as e:
|
||||
tracker.error_task("outbox", str(e))
|
||||
if tracker.is_task_enabled("outbox"):
|
||||
tracker.start_task("outbox", 100)
|
||||
tracker.update_task("outbox", 10, "Checking outbox...")
|
||||
try:
|
||||
outbox_progress = DashboardProgressAdapter(tracker, "outbox")
|
||||
result = await process_outbox_async(
|
||||
base_maildir_path,
|
||||
org,
|
||||
headers,
|
||||
outbox_progress,
|
||||
None,
|
||||
dry_run,
|
||||
access_token=access_token,
|
||||
)
|
||||
sent_count, failed_count = result if result else (0, 0)
|
||||
if sent_count > 0:
|
||||
tracker.complete_task("outbox", f"{sent_count} emails sent")
|
||||
else:
|
||||
tracker.complete_task("outbox", "No pending emails")
|
||||
except Exception as e:
|
||||
tracker.error_task("outbox", str(e))
|
||||
else:
|
||||
tracker.skip_task("outbox", "Disabled")
|
||||
|
||||
# ===== STAGE 2: Fetch from server =====
|
||||
|
||||
@@ -985,146 +1170,223 @@ async def run_dashboard_sync(
|
||||
messages_before += len([f for f in os.listdir(cur_dir) if ".eml" in f])
|
||||
|
||||
# Inbox sync
|
||||
tracker.start_task("inbox", 100)
|
||||
tracker.update_task("inbox", 10, "Fetching emails from server...")
|
||||
try:
|
||||
inbox_progress = DashboardProgressAdapter(tracker, "inbox")
|
||||
await fetch_mail_async(
|
||||
maildir_path,
|
||||
attachments_dir,
|
||||
headers,
|
||||
inbox_progress,
|
||||
None,
|
||||
dry_run,
|
||||
download_attachments,
|
||||
)
|
||||
tracker.update_task("inbox", 80, "Processing messages...")
|
||||
|
||||
# Count new messages
|
||||
messages_after = 0
|
||||
if os.path.exists(new_dir):
|
||||
messages_after += len(
|
||||
[f for f in os.listdir(new_dir) if ".eml" in f]
|
||||
)
|
||||
if os.path.exists(cur_dir):
|
||||
messages_after += len(
|
||||
[f for f in os.listdir(cur_dir) if ".eml" in f]
|
||||
if tracker.is_task_enabled("inbox"):
|
||||
tracker.start_task("inbox", 100)
|
||||
tracker.update_task("inbox", 10, "Fetching emails from server...")
|
||||
try:
|
||||
inbox_progress = DashboardProgressAdapter(tracker, "inbox")
|
||||
await fetch_mail_async(
|
||||
maildir_path,
|
||||
attachments_dir,
|
||||
headers,
|
||||
inbox_progress,
|
||||
None,
|
||||
dry_run,
|
||||
download_attachments,
|
||||
is_cancelled=lambda: not tracker.is_task_enabled("inbox"),
|
||||
)
|
||||
|
||||
new_message_count = messages_after - messages_before
|
||||
# Check if cancelled before completing
|
||||
if not tracker.is_task_enabled("inbox"):
|
||||
tracker.skip_task("inbox", "Cancelled")
|
||||
else:
|
||||
tracker.update_task("inbox", 80, "Processing messages...")
|
||||
|
||||
if new_message_count > 0:
|
||||
tracker.complete_task("inbox", f"{new_message_count} new emails")
|
||||
if dashboard._notify and not dry_run:
|
||||
notify_new_emails(new_message_count, org)
|
||||
else:
|
||||
tracker.complete_task("inbox", "No new emails")
|
||||
except Exception as e:
|
||||
tracker.error_task("inbox", str(e))
|
||||
# Count new messages
|
||||
messages_after = 0
|
||||
if os.path.exists(new_dir):
|
||||
messages_after += len(
|
||||
[f for f in os.listdir(new_dir) if ".eml" in f]
|
||||
)
|
||||
if os.path.exists(cur_dir):
|
||||
messages_after += len(
|
||||
[f for f in os.listdir(cur_dir) if ".eml" in f]
|
||||
)
|
||||
|
||||
new_message_count = messages_after - messages_before
|
||||
|
||||
if new_message_count > 0:
|
||||
tracker.complete_task(
|
||||
"inbox", f"{new_message_count} new emails"
|
||||
)
|
||||
if dashboard._notify and not dry_run:
|
||||
notify_new_emails(new_message_count, org)
|
||||
else:
|
||||
tracker.complete_task("inbox", "No new emails")
|
||||
except Exception as e:
|
||||
tracker.error_task("inbox", str(e))
|
||||
else:
|
||||
tracker.skip_task("inbox", "Disabled")
|
||||
|
||||
# Archive sync (fetch archived messages from server)
|
||||
if tracker.is_task_enabled("archive-fetch"):
|
||||
tracker.start_task("archive-fetch", 100)
|
||||
tracker.update_task("archive-fetch", 10, "Fetching archived emails...")
|
||||
try:
|
||||
archive_progress = DashboardProgressAdapter(
|
||||
tracker, "archive-fetch"
|
||||
)
|
||||
await fetch_archive_mail_async(
|
||||
maildir_path,
|
||||
attachments_dir,
|
||||
headers,
|
||||
archive_progress,
|
||||
None,
|
||||
dry_run,
|
||||
download_attachments,
|
||||
is_cancelled=lambda: not tracker.is_task_enabled(
|
||||
"archive-fetch"
|
||||
),
|
||||
)
|
||||
if tracker.is_task_enabled("archive-fetch"):
|
||||
tracker.complete_task("archive-fetch", "Archive synced")
|
||||
else:
|
||||
tracker.skip_task("archive-fetch", "Cancelled")
|
||||
except Exception as e:
|
||||
tracker.error_task("archive-fetch", str(e))
|
||||
else:
|
||||
tracker.skip_task("archive-fetch", "Disabled")
|
||||
|
||||
# Calendar sync
|
||||
tracker.start_task("calendar", 100)
|
||||
tracker.update_task("calendar", 10, "Fetching calendar events...")
|
||||
try:
|
||||
events, total_events = await fetch_calendar_events(
|
||||
headers=headers, days_back=days_back, days_forward=days_forward
|
||||
)
|
||||
tracker.update_task(
|
||||
"calendar", 50, f"Processing {len(events)} events..."
|
||||
)
|
||||
if tracker.is_task_enabled("calendar"):
|
||||
tracker.start_task("calendar", 100)
|
||||
tracker.update_task("calendar", 10, "Fetching calendar events...")
|
||||
try:
|
||||
events, total_events = await fetch_calendar_events(
|
||||
headers=headers, days_back=days_back, days_forward=days_forward
|
||||
)
|
||||
tracker.update_task(
|
||||
"calendar", 50, f"Processing {len(events)} events..."
|
||||
)
|
||||
|
||||
if not dry_run:
|
||||
calendar_progress = DashboardProgressAdapter(tracker, "calendar")
|
||||
org_vdir_path = os.path.join(vdir, org) if vdir else None
|
||||
if vdir and org_vdir_path:
|
||||
save_events_to_vdir(
|
||||
events, org_vdir_path, calendar_progress, None, dry_run
|
||||
)
|
||||
elif icsfile:
|
||||
save_events_to_file(
|
||||
events,
|
||||
f"{icsfile}/events_latest.ics",
|
||||
calendar_progress,
|
||||
None,
|
||||
dry_run,
|
||||
if not dry_run:
|
||||
calendar_progress = DashboardProgressAdapter(
|
||||
tracker, "calendar"
|
||||
)
|
||||
org_vdir_path = os.path.join(vdir, org) if vdir else None
|
||||
if vdir and org_vdir_path:
|
||||
save_events_to_vdir(
|
||||
events, org_vdir_path, calendar_progress, None, dry_run
|
||||
)
|
||||
elif icsfile:
|
||||
save_events_to_file(
|
||||
events,
|
||||
f"{icsfile}/events_latest.ics",
|
||||
calendar_progress,
|
||||
None,
|
||||
dry_run,
|
||||
)
|
||||
|
||||
tracker.complete_task("calendar", f"{len(events)} events synced")
|
||||
except Exception as e:
|
||||
tracker.error_task("calendar", str(e))
|
||||
tracker.complete_task("calendar", f"{len(events)} events synced")
|
||||
except Exception as e:
|
||||
tracker.error_task("calendar", str(e))
|
||||
else:
|
||||
tracker.skip_task("calendar", "Disabled")
|
||||
|
||||
# ===== STAGE 3: Godspeed operations =====
|
||||
|
||||
# Godspeed sync (runs every 15 minutes)
|
||||
tracker.start_task("godspeed", 100)
|
||||
if should_run_godspeed_sync():
|
||||
tracker.update_task("godspeed", 10, "Syncing with Godspeed...")
|
||||
try:
|
||||
email, password, token = get_godspeed_credentials()
|
||||
if token or (email and password):
|
||||
from src.services.godspeed.client import GodspeedClient
|
||||
from src.services.godspeed.sync import GodspeedSync
|
||||
if tracker.is_task_enabled("godspeed"):
|
||||
tracker.start_task("godspeed", 100)
|
||||
if should_run_godspeed_sync():
|
||||
tracker.update_task("godspeed", 10, "Syncing with Godspeed...")
|
||||
try:
|
||||
email, password, token = get_godspeed_credentials()
|
||||
if token or (email and password):
|
||||
from src.services.godspeed.client import GodspeedClient
|
||||
from src.services.godspeed.sync import GodspeedSync
|
||||
|
||||
sync_dir = get_godspeed_sync_directory()
|
||||
client = GodspeedClient(
|
||||
email=email, password=password, token=token
|
||||
)
|
||||
sync_engine = GodspeedSync(client, sync_dir)
|
||||
sync_engine.sync_bidirectional()
|
||||
sync_dir = get_godspeed_sync_directory()
|
||||
client = GodspeedClient(
|
||||
email=email, password=password, token=token
|
||||
)
|
||||
sync_engine = GodspeedSync(client, sync_dir)
|
||||
sync_engine.sync_bidirectional()
|
||||
|
||||
state = load_sync_state()
|
||||
state["last_godspeed_sync"] = time.time()
|
||||
save_sync_state(state)
|
||||
state = load_sync_state()
|
||||
state["last_godspeed_sync"] = time.time()
|
||||
save_sync_state(state)
|
||||
|
||||
tracker.complete_task("godspeed", "Sync completed")
|
||||
else:
|
||||
tracker.skip_task("godspeed", "No credentials configured")
|
||||
except Exception as e:
|
||||
tracker.error_task("godspeed", str(e))
|
||||
tracker.complete_task("godspeed", "Sync completed")
|
||||
else:
|
||||
tracker.skip_task("godspeed", "No credentials configured")
|
||||
except Exception as e:
|
||||
tracker.error_task("godspeed", str(e))
|
||||
else:
|
||||
tracker.skip_task("godspeed", "Not due yet (every 15 min)")
|
||||
else:
|
||||
tracker.skip_task("godspeed", "Not due yet (every 15 min)")
|
||||
tracker.skip_task("godspeed", "Disabled")
|
||||
|
||||
# dstask sync
|
||||
if tracker.is_task_enabled("dstask"):
|
||||
tracker.start_task("dstask", 100)
|
||||
try:
|
||||
from src.services.dstask.client import DstaskClient
|
||||
|
||||
dstask_client = DstaskClient()
|
||||
if dstask_client.is_available():
|
||||
tracker.update_task("dstask", 30, "Running dstask sync...")
|
||||
success = dstask_client.sync()
|
||||
if success:
|
||||
tracker.complete_task("dstask", "Sync completed")
|
||||
else:
|
||||
tracker.error_task("dstask", "Sync failed")
|
||||
else:
|
||||
tracker.skip_task("dstask", "dstask not installed")
|
||||
except Exception as e:
|
||||
tracker.error_task("dstask", str(e))
|
||||
else:
|
||||
tracker.skip_task("dstask", "Disabled")
|
||||
|
||||
# Task sweep (runs once daily after 6 PM)
|
||||
tracker.start_task("sweep", 100)
|
||||
if should_run_sweep():
|
||||
tracker.update_task("sweep", 10, "Sweeping tasks from notes...")
|
||||
try:
|
||||
from src.cli.godspeed import TaskSweeper
|
||||
if tracker.is_task_enabled("sweep"):
|
||||
tracker.start_task("sweep", 100)
|
||||
if should_run_sweep():
|
||||
tracker.update_task("sweep", 10, "Sweeping tasks from notes...")
|
||||
try:
|
||||
from src.cli.godspeed import TaskSweeper
|
||||
from datetime import datetime
|
||||
|
||||
notes_dir_env = os.getenv("NOTES_DIR")
|
||||
if notes_dir_env and Path(notes_dir_env).exists():
|
||||
godspeed_dir = get_godspeed_sync_directory()
|
||||
sweeper = TaskSweeper(
|
||||
Path(notes_dir_env), godspeed_dir, dry_run=dry_run
|
||||
)
|
||||
result = sweeper.sweep_tasks()
|
||||
|
||||
state = load_sync_state()
|
||||
state["last_sweep_date"] = datetime.now().strftime(
|
||||
"%Y-%m-%d"
|
||||
)
|
||||
save_sync_state(state)
|
||||
|
||||
swept = result.get("swept_tasks", 0)
|
||||
if swept > 0:
|
||||
tracker.complete_task("sweep", f"{swept} tasks swept")
|
||||
else:
|
||||
tracker.complete_task("sweep", "No tasks to sweep")
|
||||
else:
|
||||
tracker.skip_task("sweep", "$NOTES_DIR not configured")
|
||||
except Exception as e:
|
||||
tracker.error_task("sweep", str(e))
|
||||
else:
|
||||
from datetime import datetime
|
||||
|
||||
notes_dir_env = os.getenv("NOTES_DIR")
|
||||
if notes_dir_env and Path(notes_dir_env).exists():
|
||||
godspeed_dir = get_godspeed_sync_directory()
|
||||
sweeper = TaskSweeper(
|
||||
Path(notes_dir_env), godspeed_dir, dry_run=dry_run
|
||||
)
|
||||
result = sweeper.sweep_tasks()
|
||||
|
||||
state = load_sync_state()
|
||||
state["last_sweep_date"] = datetime.now().strftime("%Y-%m-%d")
|
||||
save_sync_state(state)
|
||||
|
||||
swept = result.get("swept_tasks", 0)
|
||||
if swept > 0:
|
||||
tracker.complete_task("sweep", f"{swept} tasks swept")
|
||||
else:
|
||||
tracker.complete_task("sweep", "No tasks to sweep")
|
||||
current_hour = datetime.now().hour
|
||||
if current_hour < 18:
|
||||
tracker.skip_task("sweep", "Before 6 PM")
|
||||
else:
|
||||
tracker.skip_task("sweep", "$NOTES_DIR not configured")
|
||||
except Exception as e:
|
||||
tracker.error_task("sweep", str(e))
|
||||
tracker.skip_task("sweep", "Already completed today")
|
||||
else:
|
||||
from datetime import datetime
|
||||
|
||||
current_hour = datetime.now().hour
|
||||
if current_hour < 18:
|
||||
tracker.skip_task("sweep", "Before 6 PM")
|
||||
else:
|
||||
tracker.skip_task("sweep", "Already completed today")
|
||||
tracker.skip_task("sweep", "Disabled")
|
||||
|
||||
# Schedule next sync
|
||||
dashboard.schedule_next_sync()
|
||||
|
||||
# Notify all running TUI apps to refresh their data
|
||||
await notify_all({"source": "sync_dashboard"})
|
||||
|
||||
except Exception as e:
|
||||
# If we fail early (e.g., auth), log to the first pending task
|
||||
for task_id in [
|
||||
@@ -1133,6 +1395,7 @@ async def run_dashboard_sync(
|
||||
"inbox",
|
||||
"calendar",
|
||||
"godspeed",
|
||||
"dstask",
|
||||
"sweep",
|
||||
]:
|
||||
if task_id in dashboard._task_items:
|
||||
|
||||
11
src/cli/tasks.py
Normal file
11
src/cli/tasks.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""CLI command for Tasks TUI."""
|
||||
|
||||
import click
|
||||
|
||||
|
||||
@click.command()
|
||||
def tasks():
|
||||
"""Launch the Tasks TUI for managing tasks via dstask."""
|
||||
from src.tasks import run_app
|
||||
|
||||
run_app()
|
||||
1
src/mail/__init__.py
Normal file
1
src/mail/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Initialize the mail package
|
||||
490
src/mail/actions/calendar_invite.py
Normal file
490
src/mail/actions/calendar_invite.py
Normal file
@@ -0,0 +1,490 @@
|
||||
"""Calendar invite actions for mail app.
|
||||
|
||||
Allows responding to calendar invites directly from email using ICS/SMTP.
|
||||
|
||||
Uses the iTIP (iCalendar Transport-Independent Interoperability Protocol)
|
||||
standard to send REPLY messages via email instead of requiring Calendar.ReadWrite
|
||||
API permissions.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from src.mail.utils.calendar_parser import ParsedCalendarEvent
|
||||
|
||||
# Set up dedicated RSVP logger
|
||||
rsvp_logger = logging.getLogger("calendar_rsvp")
|
||||
rsvp_logger.setLevel(logging.DEBUG)
|
||||
|
||||
# Create file handler if not already set up
|
||||
if not rsvp_logger.handlers:
|
||||
log_dir = os.path.expanduser("~/.local/share/luk")
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
log_file = os.path.join(log_dir, "calendar_rsvp.log")
|
||||
handler = logging.FileHandler(log_file)
|
||||
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
|
||||
rsvp_logger.addHandler(handler)
|
||||
|
||||
|
||||
def _get_user_email() -> Optional[str]:
|
||||
"""Get the current user's email address from MSAL cache.
|
||||
|
||||
Returns:
|
||||
User's email address if found, None otherwise.
|
||||
"""
|
||||
import msal
|
||||
|
||||
client_id = os.getenv("AZURE_CLIENT_ID")
|
||||
tenant_id = os.getenv("AZURE_TENANT_ID")
|
||||
|
||||
if not client_id or not tenant_id:
|
||||
rsvp_logger.warning("Azure credentials not configured")
|
||||
return None
|
||||
|
||||
cache_file = os.path.expanduser("~/.local/share/luk/token_cache.bin")
|
||||
if not os.path.exists(cache_file):
|
||||
rsvp_logger.warning("Token cache file not found")
|
||||
return None
|
||||
|
||||
try:
|
||||
cache = msal.SerializableTokenCache()
|
||||
cache.deserialize(open(cache_file, "r").read())
|
||||
authority = f"https://login.microsoftonline.com/{tenant_id}"
|
||||
app = msal.PublicClientApplication(
|
||||
client_id, authority=authority, token_cache=cache
|
||||
)
|
||||
accounts = app.get_accounts()
|
||||
|
||||
if accounts:
|
||||
# The username field contains the user's email
|
||||
return accounts[0].get("username")
|
||||
return None
|
||||
except Exception as e:
|
||||
rsvp_logger.error(f"Failed to get user email from MSAL: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def _get_user_display_name() -> Optional[str]:
|
||||
"""Get the current user's display name from MSAL cache.
|
||||
|
||||
Returns:
|
||||
User's display name if found, None otherwise.
|
||||
"""
|
||||
import msal
|
||||
|
||||
client_id = os.getenv("AZURE_CLIENT_ID")
|
||||
tenant_id = os.getenv("AZURE_TENANT_ID")
|
||||
|
||||
if not client_id or not tenant_id:
|
||||
return None
|
||||
|
||||
cache_file = os.path.expanduser("~/.local/share/luk/token_cache.bin")
|
||||
if not os.path.exists(cache_file):
|
||||
return None
|
||||
|
||||
try:
|
||||
cache = msal.SerializableTokenCache()
|
||||
cache.deserialize(open(cache_file, "r").read())
|
||||
authority = f"https://login.microsoftonline.com/{tenant_id}"
|
||||
app = msal.PublicClientApplication(
|
||||
client_id, authority=authority, token_cache=cache
|
||||
)
|
||||
accounts = app.get_accounts()
|
||||
|
||||
if accounts:
|
||||
# Try to get name from account, fallback to username
|
||||
name = accounts[0].get("name")
|
||||
if name:
|
||||
return name
|
||||
# Fallback: construct name from email
|
||||
username = accounts[0].get("username", "")
|
||||
if "@" in username:
|
||||
local_part = username.split("@")[0]
|
||||
# Convert firstname.lastname to Firstname Lastname
|
||||
parts = local_part.replace(".", " ").replace("_", " ").split()
|
||||
return " ".join(p.capitalize() for p in parts)
|
||||
return None
|
||||
except Exception as e:
|
||||
rsvp_logger.debug(f"Failed to get display name: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def generate_ics_reply(
|
||||
event: ParsedCalendarEvent,
|
||||
response: str,
|
||||
attendee_email: str,
|
||||
attendee_name: Optional[str] = None,
|
||||
) -> str:
|
||||
"""Generate an iCalendar REPLY for a calendar invite.
|
||||
|
||||
Args:
|
||||
event: The parsed calendar event from the original invite
|
||||
response: Response type - 'ACCEPTED', 'TENTATIVE', or 'DECLINED'
|
||||
attendee_email: The attendee's email address
|
||||
attendee_name: The attendee's display name (optional)
|
||||
|
||||
Returns:
|
||||
ICS content string formatted as an iTIP REPLY
|
||||
"""
|
||||
# Map response to PARTSTAT value
|
||||
partstat_map = {
|
||||
"accept": "ACCEPTED",
|
||||
"tentativelyAccept": "TENTATIVE",
|
||||
"decline": "DECLINED",
|
||||
}
|
||||
partstat = partstat_map.get(response, "ACCEPTED")
|
||||
|
||||
# Generate DTSTAMP in UTC format
|
||||
dtstamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
||||
|
||||
# Build attendee line with proper formatting
|
||||
if attendee_name:
|
||||
attendee_line = (
|
||||
f'ATTENDEE;PARTSTAT={partstat};CN="{attendee_name}":MAILTO:{attendee_email}'
|
||||
)
|
||||
else:
|
||||
attendee_line = f"ATTENDEE;PARTSTAT={partstat}:MAILTO:{attendee_email}"
|
||||
|
||||
# Build organizer line
|
||||
if event.organizer_name:
|
||||
organizer_line = (
|
||||
f'ORGANIZER;CN="{event.organizer_name}":MAILTO:{event.organizer_email}'
|
||||
)
|
||||
else:
|
||||
organizer_line = f"ORGANIZER:MAILTO:{event.organizer_email}"
|
||||
|
||||
# Build the response subject prefix
|
||||
response_prefix = {
|
||||
"accept": "Accepted",
|
||||
"tentativelyAccept": "Tentative",
|
||||
"decline": "Declined",
|
||||
}.get(response, "Accepted")
|
||||
|
||||
summary = f"{response_prefix}: {event.summary or '(no subject)'}"
|
||||
|
||||
# Build the ICS content following iTIP REPLY standard
|
||||
ics_lines = [
|
||||
"BEGIN:VCALENDAR",
|
||||
"VERSION:2.0",
|
||||
"PRODID:-//LUK Mail//Calendar Reply//EN",
|
||||
"METHOD:REPLY",
|
||||
"BEGIN:VEVENT",
|
||||
f"UID:{event.uid}",
|
||||
f"DTSTAMP:{dtstamp}",
|
||||
organizer_line,
|
||||
attendee_line,
|
||||
f"SEQUENCE:{event.sequence}",
|
||||
f"SUMMARY:{summary}",
|
||||
"END:VEVENT",
|
||||
"END:VCALENDAR",
|
||||
]
|
||||
|
||||
return "\r\n".join(ics_lines)
|
||||
|
||||
|
||||
def build_calendar_reply_email(
|
||||
event: ParsedCalendarEvent,
|
||||
response: str,
|
||||
from_email: str,
|
||||
to_email: str,
|
||||
from_name: Optional[str] = None,
|
||||
) -> str:
|
||||
"""Build a MIME email with calendar REPLY attachment.
|
||||
|
||||
The email is formatted according to iTIP/iMIP standards so that
|
||||
Exchange/Outlook will recognize it as a calendar action.
|
||||
|
||||
Args:
|
||||
event: The parsed calendar event from the original invite
|
||||
response: Response type - 'accept', 'tentativelyAccept', or 'decline'
|
||||
from_email: Sender's email address
|
||||
to_email: Recipient's email address (the organizer)
|
||||
from_name: Sender's display name (optional)
|
||||
|
||||
Returns:
|
||||
Complete RFC 5322 email as string
|
||||
"""
|
||||
# Generate the ICS reply content
|
||||
ics_content = generate_ics_reply(event, response, from_email, from_name)
|
||||
|
||||
# Build response text for email body
|
||||
response_text = {
|
||||
"accept": "accepted",
|
||||
"tentativelyAccept": "tentatively accepted",
|
||||
"decline": "declined",
|
||||
}.get(response, "accepted")
|
||||
|
||||
subject_prefix = {
|
||||
"accept": "Accepted",
|
||||
"tentativelyAccept": "Tentative",
|
||||
"decline": "Declined",
|
||||
}.get(response, "Accepted")
|
||||
|
||||
subject = f"{subject_prefix}: {event.summary or '(no subject)'}"
|
||||
|
||||
# Create the email message
|
||||
msg = MIMEMultipart("mixed")
|
||||
|
||||
# Set headers
|
||||
if from_name:
|
||||
msg["From"] = f'"{from_name}" <{from_email}>'
|
||||
else:
|
||||
msg["From"] = from_email
|
||||
|
||||
msg["To"] = to_email
|
||||
msg["Subject"] = subject
|
||||
|
||||
# Add Content-Class header for Exchange compatibility
|
||||
msg["Content-Class"] = "urn:content-classes:calendarmessage"
|
||||
|
||||
# Create text body
|
||||
body_text = f"This meeting has been {response_text}."
|
||||
text_part = MIMEText(body_text, "plain", "utf-8")
|
||||
msg.attach(text_part)
|
||||
|
||||
# Create calendar part with proper iTIP headers
|
||||
# The content-type must include method=REPLY for Exchange to recognize it
|
||||
calendar_part = MIMEText(ics_content, "calendar", "utf-8")
|
||||
calendar_part.set_param("method", "REPLY")
|
||||
calendar_part.add_header("Content-Disposition", "attachment", filename="invite.ics")
|
||||
msg.attach(calendar_part)
|
||||
|
||||
return msg.as_string()
|
||||
|
||||
|
||||
def queue_calendar_reply(
|
||||
event: ParsedCalendarEvent,
|
||||
response: str,
|
||||
from_email: str,
|
||||
to_email: str,
|
||||
from_name: Optional[str] = None,
|
||||
) -> Tuple[bool, str]:
|
||||
"""Queue a calendar reply email for sending via the outbox.
|
||||
|
||||
Args:
|
||||
event: The parsed calendar event from the original invite
|
||||
response: Response type - 'accept', 'tentativelyAccept', or 'decline'
|
||||
from_email: Sender's email address
|
||||
to_email: Recipient's email address (the organizer)
|
||||
from_name: Sender's display name (optional)
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
# Build the email
|
||||
email_content = build_calendar_reply_email(
|
||||
event, response, from_email, to_email, from_name
|
||||
)
|
||||
|
||||
# Determine organization from email domain
|
||||
org = "default"
|
||||
if "@" in from_email:
|
||||
domain = from_email.split("@")[1].lower()
|
||||
# Map known domains to org names (matching sendmail script logic)
|
||||
domain_to_org = {
|
||||
"corteva.com": "corteva",
|
||||
}
|
||||
org = domain_to_org.get(domain, domain.split(".")[0])
|
||||
|
||||
# Queue the email in the outbox
|
||||
base_path = os.path.expanduser(os.getenv("MAILDIR_PATH", "~/Mail"))
|
||||
outbox_path = os.path.join(base_path, org, "outbox")
|
||||
|
||||
# Ensure directories exist
|
||||
for subdir in ["new", "cur", "tmp", "failed"]:
|
||||
dir_path = os.path.join(outbox_path, subdir)
|
||||
os.makedirs(dir_path, exist_ok=True)
|
||||
|
||||
# Generate unique filename
|
||||
timestamp = str(int(time.time() * 1000000))
|
||||
hostname = os.uname().nodename
|
||||
filename = f"{timestamp}.{os.getpid()}.{hostname}"
|
||||
|
||||
# Write to tmp first, then move to new (atomic operation)
|
||||
tmp_path = os.path.join(outbox_path, "tmp", filename)
|
||||
new_path = os.path.join(outbox_path, "new", filename)
|
||||
|
||||
with open(tmp_path, "w", encoding="utf-8") as f:
|
||||
f.write(email_content)
|
||||
|
||||
os.rename(tmp_path, new_path)
|
||||
|
||||
response_text = {
|
||||
"accept": "accepted",
|
||||
"tentativelyAccept": "tentatively accepted",
|
||||
"decline": "declined",
|
||||
}.get(response, "accepted")
|
||||
|
||||
rsvp_logger.info(
|
||||
f"Queued calendar reply: {response_text} for '{event.summary}' to {event.organizer_email}"
|
||||
)
|
||||
|
||||
return True, f"Response queued - will be sent on next sync"
|
||||
|
||||
except Exception as e:
|
||||
rsvp_logger.error(f"Failed to queue calendar reply: {e}", exc_info=True)
|
||||
return False, f"Failed to queue response: {str(e)}"
|
||||
|
||||
|
||||
def send_calendar_reply_via_apple_mail(
|
||||
event: ParsedCalendarEvent,
|
||||
response: str,
|
||||
from_email: str,
|
||||
to_email: str,
|
||||
from_name: Optional[str] = None,
|
||||
auto_send: bool = False,
|
||||
) -> Tuple[bool, str]:
|
||||
"""Send a calendar reply immediately via Apple Mail.
|
||||
|
||||
Args:
|
||||
event: The parsed calendar event from the original invite
|
||||
response: Response type - 'accept', 'tentativelyAccept', or 'decline'
|
||||
from_email: Sender's email address
|
||||
to_email: Recipient's email address (the organizer)
|
||||
from_name: Sender's display name (optional)
|
||||
auto_send: If True, automatically send via AppleScript
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
from src.mail.utils.apple_mail import open_eml_in_apple_mail
|
||||
|
||||
try:
|
||||
# Build the email
|
||||
email_content = build_calendar_reply_email(
|
||||
event, response, from_email, to_email, from_name
|
||||
)
|
||||
|
||||
response_text = {
|
||||
"accept": "accepted",
|
||||
"tentativelyAccept": "tentatively accepted",
|
||||
"decline": "declined",
|
||||
}.get(response, "accepted")
|
||||
|
||||
subject = f"{response_text.capitalize()}: {event.summary or '(no subject)'}"
|
||||
|
||||
# Open in Apple Mail (and optionally auto-send)
|
||||
success, message = open_eml_in_apple_mail(
|
||||
email_content, auto_send=auto_send, subject=subject
|
||||
)
|
||||
|
||||
if success:
|
||||
rsvp_logger.info(
|
||||
f"Calendar reply via Apple Mail: {response_text} for '{event.summary}' to {to_email}"
|
||||
)
|
||||
|
||||
return success, message
|
||||
|
||||
except Exception as e:
|
||||
rsvp_logger.error(
|
||||
f"Failed to send calendar reply via Apple Mail: {e}", exc_info=True
|
||||
)
|
||||
return False, f"Failed to send response: {str(e)}"
|
||||
|
||||
|
||||
def action_accept_invite(app):
|
||||
"""Accept the current calendar invite."""
|
||||
_respond_to_current_invite(app, "accept")
|
||||
|
||||
|
||||
def action_decline_invite(app):
|
||||
"""Decline the current calendar invite."""
|
||||
_respond_to_current_invite(app, "decline")
|
||||
|
||||
|
||||
def action_tentative_invite(app):
|
||||
"""Tentatively accept the current calendar invite."""
|
||||
_respond_to_current_invite(app, "tentativelyAccept")
|
||||
|
||||
|
||||
def _respond_to_current_invite(app, response: str):
|
||||
"""Helper to respond to the current message's calendar invite.
|
||||
|
||||
Sends the response immediately via Apple Mail instead of queuing for sync.
|
||||
"""
|
||||
from src.mail.widgets.ContentContainer import ContentContainer
|
||||
from src.mail.config import get_config
|
||||
|
||||
rsvp_logger.info(f"Starting invite response: {response}")
|
||||
|
||||
current_message_id = app.current_message_id
|
||||
if not current_message_id:
|
||||
rsvp_logger.warning("No message selected")
|
||||
app.notify("No message selected", severity="warning")
|
||||
return
|
||||
|
||||
# Get user's email from MSAL cache
|
||||
user_email = _get_user_email()
|
||||
if not user_email:
|
||||
rsvp_logger.error("Could not determine user email - run 'luk sync' first")
|
||||
app.notify(
|
||||
"Could not determine your email. Run 'luk sync' first.", severity="error"
|
||||
)
|
||||
return
|
||||
|
||||
user_name = _get_user_display_name()
|
||||
rsvp_logger.debug(f"User: {user_name} <{user_email}>")
|
||||
|
||||
# Get the parsed calendar event from ContentContainer
|
||||
calendar_event = None
|
||||
try:
|
||||
content_container = app.query_one(ContentContainer)
|
||||
calendar_event = content_container.current_calendar_event
|
||||
except Exception as e:
|
||||
rsvp_logger.error(f"Failed to get ContentContainer: {e}")
|
||||
|
||||
if not calendar_event:
|
||||
rsvp_logger.warning("No calendar event data found in current message")
|
||||
app.notify("No calendar invite found in this message", severity="warning")
|
||||
return
|
||||
|
||||
event_uid = calendar_event.uid
|
||||
event_summary = calendar_event.summary or "(no subject)"
|
||||
organizer_email = calendar_event.organizer_email
|
||||
|
||||
rsvp_logger.info(
|
||||
f"Calendar event: {event_summary}, UID: {event_uid}, Organizer: {organizer_email}"
|
||||
)
|
||||
|
||||
if not event_uid:
|
||||
rsvp_logger.warning("No UID found in calendar event")
|
||||
app.notify("Calendar invite missing UID - cannot respond", severity="warning")
|
||||
return
|
||||
|
||||
if not organizer_email:
|
||||
rsvp_logger.warning("No organizer email found in calendar event")
|
||||
app.notify(
|
||||
"Calendar invite missing organizer - cannot respond", severity="warning"
|
||||
)
|
||||
return
|
||||
|
||||
# Get config for auto-send preference
|
||||
config = get_config()
|
||||
auto_send = config.mail.auto_send_via_applescript
|
||||
|
||||
# Send immediately via Apple Mail
|
||||
success, message = send_calendar_reply_via_apple_mail(
|
||||
calendar_event,
|
||||
response,
|
||||
user_email,
|
||||
organizer_email,
|
||||
user_name,
|
||||
auto_send=auto_send,
|
||||
)
|
||||
|
||||
severity = "information" if success else "error"
|
||||
app.notify(message, severity=severity)
|
||||
|
||||
if success:
|
||||
response_text = {
|
||||
"accept": "Accepted",
|
||||
"tentativelyAccept": "Tentatively accepted",
|
||||
"decline": "Declined",
|
||||
}.get(response, "Responded to")
|
||||
rsvp_logger.info(f"{response_text} invite: {event_summary}")
|
||||
170
src/mail/actions/compose.py
Normal file
170
src/mail/actions/compose.py
Normal file
@@ -0,0 +1,170 @@
|
||||
"""Compose, reply, and forward email actions for mail app.
|
||||
|
||||
Uses Apple Mail for composing and sending emails.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from typing import Optional
|
||||
|
||||
from src.mail.utils.apple_mail import (
|
||||
compose_new_email,
|
||||
reply_to_email,
|
||||
forward_email,
|
||||
)
|
||||
from src.services.himalaya import client as himalaya_client
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Module-level temp directory for exported messages (persists across calls)
|
||||
_temp_dir: Optional[tempfile.TemporaryDirectory] = None
|
||||
|
||||
|
||||
def _get_temp_dir() -> str:
|
||||
"""Get or create a persistent temp directory for exported messages."""
|
||||
global _temp_dir
|
||||
if _temp_dir is None:
|
||||
_temp_dir = tempfile.TemporaryDirectory(prefix="luk_mail_")
|
||||
return _temp_dir.name
|
||||
|
||||
|
||||
async def _export_current_message(app) -> Optional[str]:
|
||||
"""Export the currently selected message to a temp .eml file.
|
||||
|
||||
Args:
|
||||
app: The mail app instance
|
||||
|
||||
Returns:
|
||||
Path to the exported .eml file, or None if export failed
|
||||
"""
|
||||
current_message_id = app.current_message_id
|
||||
if not current_message_id:
|
||||
return None
|
||||
|
||||
# Use himalaya to export the raw message
|
||||
raw_content, success = await himalaya_client.get_raw_message(current_message_id)
|
||||
if not success or not raw_content:
|
||||
logger.error(f"Failed to export message {current_message_id}")
|
||||
return None
|
||||
|
||||
# Save to a temp file
|
||||
temp_dir = _get_temp_dir()
|
||||
eml_path = os.path.join(temp_dir, f"message_{current_message_id}.eml")
|
||||
|
||||
try:
|
||||
with open(eml_path, "w", encoding="utf-8") as f:
|
||||
f.write(raw_content)
|
||||
return eml_path
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to write temp .eml file: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def _get_user_email() -> Optional[str]:
|
||||
"""Get the current user's email address from MSAL cache."""
|
||||
import msal
|
||||
|
||||
client_id = os.getenv("AZURE_CLIENT_ID")
|
||||
tenant_id = os.getenv("AZURE_TENANT_ID")
|
||||
|
||||
if not client_id or not tenant_id:
|
||||
return None
|
||||
|
||||
cache_file = os.path.expanduser("~/.local/share/luk/token_cache.bin")
|
||||
if not os.path.exists(cache_file):
|
||||
return None
|
||||
|
||||
try:
|
||||
cache = msal.SerializableTokenCache()
|
||||
cache.deserialize(open(cache_file, "r").read())
|
||||
authority = f"https://login.microsoftonline.com/{tenant_id}"
|
||||
app = msal.PublicClientApplication(
|
||||
client_id, authority=authority, token_cache=cache
|
||||
)
|
||||
accounts = app.get_accounts()
|
||||
|
||||
if accounts:
|
||||
return accounts[0].get("username")
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def action_compose(app):
|
||||
"""Open a new compose window in Apple Mail."""
|
||||
user_email = _get_user_email()
|
||||
|
||||
success, message = compose_new_email(
|
||||
to="",
|
||||
subject="",
|
||||
body="",
|
||||
)
|
||||
|
||||
if success:
|
||||
app.notify("Compose window opened in Mail", severity="information")
|
||||
else:
|
||||
app.notify(f"Failed to open compose: {message}", severity="error")
|
||||
|
||||
|
||||
async def action_reply(app):
|
||||
"""Reply to the current message in Apple Mail."""
|
||||
if not app.current_message_id:
|
||||
app.notify("No message selected", severity="warning")
|
||||
return
|
||||
|
||||
app.notify("Exporting message...", severity="information")
|
||||
message_path = await _export_current_message(app)
|
||||
|
||||
if not message_path:
|
||||
app.notify("Failed to export message", severity="error")
|
||||
return
|
||||
|
||||
success, message = reply_to_email(message_path, reply_all=False)
|
||||
|
||||
if success:
|
||||
app.notify("Reply window opened in Mail", severity="information")
|
||||
else:
|
||||
app.notify(f"Failed to open reply: {message}", severity="error")
|
||||
|
||||
|
||||
async def action_reply_all(app):
|
||||
"""Reply to all on the current message in Apple Mail."""
|
||||
if not app.current_message_id:
|
||||
app.notify("No message selected", severity="warning")
|
||||
return
|
||||
|
||||
app.notify("Exporting message...", severity="information")
|
||||
message_path = await _export_current_message(app)
|
||||
|
||||
if not message_path:
|
||||
app.notify("Failed to export message", severity="error")
|
||||
return
|
||||
|
||||
success, message = reply_to_email(message_path, reply_all=True)
|
||||
|
||||
if success:
|
||||
app.notify("Reply-all window opened in Mail", severity="information")
|
||||
else:
|
||||
app.notify(f"Failed to open reply-all: {message}", severity="error")
|
||||
|
||||
|
||||
async def action_forward(app):
|
||||
"""Forward the current message in Apple Mail."""
|
||||
if not app.current_message_id:
|
||||
app.notify("No message selected", severity="warning")
|
||||
return
|
||||
|
||||
app.notify("Exporting message...", severity="information")
|
||||
message_path = await _export_current_message(app)
|
||||
|
||||
if not message_path:
|
||||
app.notify("Failed to export message", severity="error")
|
||||
return
|
||||
|
||||
success, message = forward_email(message_path)
|
||||
|
||||
if success:
|
||||
app.notify("Forward window opened in Mail", severity="information")
|
||||
else:
|
||||
app.notify(f"Failed to open forward: {message}", severity="error")
|
||||
@@ -22,7 +22,11 @@ async def delete_current(app):
|
||||
next_id, next_idx = app.message_store.find_prev_valid_id(current_index)
|
||||
|
||||
# Delete the message using our Himalaya client module
|
||||
success = await himalaya_client.delete_message(current_message_id)
|
||||
folder = app.folder if app.folder else None
|
||||
account = app.current_account if app.current_account else None
|
||||
message, success = await himalaya_client.delete_message(
|
||||
current_message_id, folder=folder, account=account
|
||||
)
|
||||
|
||||
if success:
|
||||
app.show_status(f"Message {current_message_id} deleted.", "success")
|
||||
@@ -38,4 +42,6 @@ async def delete_current(app):
|
||||
app.current_message_id = 0
|
||||
app.show_status("No more messages available.", "warning")
|
||||
else:
|
||||
app.show_status(f"Failed to delete message {current_message_id}.", "error")
|
||||
app.show_status(
|
||||
f"Failed to delete message {current_message_id}: {message}", "error"
|
||||
)
|
||||
1322
src/mail/app.py
Normal file
1322
src/mail/app.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
"""Configuration system for MaildirGTD email reader using Pydantic."""
|
||||
"""Configuration system for Mail email reader using Pydantic."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
@@ -66,8 +66,9 @@ class KeybindingsConfig(BaseModel):
|
||||
create_task: str = "t"
|
||||
reload: str = "%"
|
||||
toggle_sort: str = "s"
|
||||
toggle_selection: str = "x"
|
||||
toggle_selection: str = "space"
|
||||
clear_selection: str = "escape"
|
||||
scroll_page_down: str = "pagedown"
|
||||
scroll_page_down: str = "space"
|
||||
scroll_page_up: str = "b"
|
||||
toggle_main_content: str = "w"
|
||||
@@ -81,6 +82,14 @@ class ContentDisplayConfig(BaseModel):
|
||||
# View mode: "markdown" for pretty rendering, "html" for raw/plain display
|
||||
default_view_mode: Literal["markdown", "html"] = "markdown"
|
||||
|
||||
# URL compression: shorten long URLs for better readability
|
||||
compress_urls: bool = True
|
||||
max_url_length: int = 50 # Maximum length before URL is compressed
|
||||
|
||||
# Notification compression: compress notification emails into summaries
|
||||
compress_notifications: bool = True
|
||||
notification_compression_mode: Literal["summary", "detailed", "off"] = "summary"
|
||||
|
||||
|
||||
class LinkPanelConfig(BaseModel):
|
||||
"""Configuration for the link panel."""
|
||||
@@ -89,14 +98,30 @@ class LinkPanelConfig(BaseModel):
|
||||
close_on_open: bool = False
|
||||
|
||||
|
||||
class MailOperationsConfig(BaseModel):
|
||||
"""Configuration for mail operations."""
|
||||
|
||||
# Folder to move messages to when archiving
|
||||
archive_folder: str = "Archive"
|
||||
|
||||
# Enable SMTP OAuth2 sending (requires IT to enable SMTP AUTH for your mailbox)
|
||||
# When disabled, calendar replies will open in your default mail client instead
|
||||
enable_smtp_send: bool = False
|
||||
|
||||
# Auto-send emails opened in Apple Mail via AppleScript
|
||||
# When True, calendar replies will be sent automatically after opening in Mail
|
||||
# When False, the email will be opened for manual review before sending
|
||||
auto_send_via_applescript: bool = False
|
||||
|
||||
|
||||
class ThemeConfig(BaseModel):
|
||||
"""Theme/appearance settings."""
|
||||
|
||||
theme_name: str = "monokai"
|
||||
|
||||
|
||||
class MaildirGTDConfig(BaseModel):
|
||||
"""Main configuration for MaildirGTD email reader."""
|
||||
class MailAppConfig(BaseModel):
|
||||
"""Main configuration for Mail email reader."""
|
||||
|
||||
task: TaskBackendConfig = Field(default_factory=TaskBackendConfig)
|
||||
envelope_display: EnvelopeDisplayConfig = Field(
|
||||
@@ -104,6 +129,7 @@ class MaildirGTDConfig(BaseModel):
|
||||
)
|
||||
content_display: ContentDisplayConfig = Field(default_factory=ContentDisplayConfig)
|
||||
link_panel: LinkPanelConfig = Field(default_factory=LinkPanelConfig)
|
||||
mail: MailOperationsConfig = Field(default_factory=MailOperationsConfig)
|
||||
keybindings: KeybindingsConfig = Field(default_factory=KeybindingsConfig)
|
||||
theme: ThemeConfig = Field(default_factory=ThemeConfig)
|
||||
|
||||
@@ -111,15 +137,15 @@ class MaildirGTDConfig(BaseModel):
|
||||
def get_config_path(cls) -> Path:
|
||||
"""Get the path to the config file."""
|
||||
# Check environment variable first
|
||||
env_path = os.getenv("MAILDIR_GTD_CONFIG")
|
||||
env_path = os.getenv("LUK_MAIL_CONFIG")
|
||||
if env_path:
|
||||
return Path(env_path)
|
||||
|
||||
# Default to ~/.config/luk/maildir_gtd.toml
|
||||
return Path.home() / ".config" / "luk" / "maildir_gtd.toml"
|
||||
# Default to ~/.config/luk/mail.toml
|
||||
return Path.home() / ".config" / "luk" / "mail.toml"
|
||||
|
||||
@classmethod
|
||||
def load(cls, config_path: Optional[Path] = None) -> "MaildirGTDConfig":
|
||||
def load(cls, config_path: Optional[Path] = None) -> "MailAppConfig":
|
||||
"""Load config from TOML file with defaults for missing values."""
|
||||
if config_path is None:
|
||||
config_path = cls.get_config_path()
|
||||
@@ -152,19 +178,19 @@ class MaildirGTDConfig(BaseModel):
|
||||
|
||||
|
||||
# Global config instance (lazy-loaded)
|
||||
_config: Optional[MaildirGTDConfig] = None
|
||||
_config: Optional[MailAppConfig] = None
|
||||
|
||||
|
||||
def get_config() -> MaildirGTDConfig:
|
||||
def get_config() -> MailAppConfig:
|
||||
"""Get the global config instance, loading it if necessary."""
|
||||
global _config
|
||||
if _config is None:
|
||||
_config = MaildirGTDConfig.load()
|
||||
_config = MailAppConfig.load()
|
||||
return _config
|
||||
|
||||
|
||||
def reload_config() -> MaildirGTDConfig:
|
||||
def reload_config() -> MailAppConfig:
|
||||
"""Force reload of the config from disk."""
|
||||
global _config
|
||||
_config = MaildirGTDConfig.load()
|
||||
_config = MailAppConfig.load()
|
||||
return _config
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#main_content, .list_view {
|
||||
scrollbar-size: 1 1;
|
||||
border: round rgb(117, 106, 129);
|
||||
border: round $border;
|
||||
height: 1fr;
|
||||
}
|
||||
|
||||
@@ -11,17 +11,23 @@
|
||||
width: 1fr
|
||||
}
|
||||
|
||||
.list_view {
|
||||
height: 3;
|
||||
}
|
||||
|
||||
#main_content {
|
||||
width: 2fr;
|
||||
|
||||
}
|
||||
|
||||
|
||||
.envelope-selected {
|
||||
tint: $accent 20%;
|
||||
}
|
||||
|
||||
#sidebar:focus-within {
|
||||
background: $panel;
|
||||
|
||||
.list_view:blur {
|
||||
height: 3;
|
||||
}
|
||||
@@ -30,20 +36,25 @@
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#envelopes_list {
|
||||
height: 2fr;
|
||||
}
|
||||
|
||||
#main_content:focus, .list_view:focus {
|
||||
border: round $secondary;
|
||||
background: rgb(55, 53, 57);
|
||||
background: $surface;
|
||||
border-title-style: bold;
|
||||
}
|
||||
|
||||
Label#task_prompt {
|
||||
padding: 1;
|
||||
color: rgb(128,128,128);
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
Label#task_prompt_label {
|
||||
padding: 1;
|
||||
color: rgb(255, 216, 102);
|
||||
color: $warning;
|
||||
}
|
||||
|
||||
Label#message_label {
|
||||
@@ -55,15 +66,41 @@ StatusTitle {
|
||||
width: 100%;
|
||||
height: 1;
|
||||
color: $text;
|
||||
background: rgb(64, 62, 65);
|
||||
background: $panel;
|
||||
content-align: center middle;
|
||||
}
|
||||
|
||||
EnvelopeHeader {
|
||||
dock: top;
|
||||
width: 100%;
|
||||
max-height: 2;
|
||||
tint: $primary 10%;
|
||||
height: auto;
|
||||
min-height: 4;
|
||||
max-height: 6;
|
||||
padding: 0 1;
|
||||
background: $surface-darken-1;
|
||||
scrollbar-size: 1 1;
|
||||
}
|
||||
|
||||
/* Full headers mode - allow more height for scrolling */
|
||||
EnvelopeHeader.full-headers {
|
||||
max-height: 12;
|
||||
}
|
||||
|
||||
#content_scroll {
|
||||
height: 1fr;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Header labels should be single line with truncation */
|
||||
EnvelopeHeader Label {
|
||||
width: 100%;
|
||||
height: 1;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
/* Full headers mode - allow wrapping */
|
||||
EnvelopeHeader.full-headers Label {
|
||||
height: auto;
|
||||
text-overflow: clip;
|
||||
}
|
||||
|
||||
Markdown {
|
||||
@@ -102,8 +139,8 @@ EnvelopeListItem .envelope-row-3 {
|
||||
}
|
||||
|
||||
EnvelopeListItem .status-icon {
|
||||
width: 3;
|
||||
padding: 0 1 0 0;
|
||||
width: 2;
|
||||
padding: 0;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
@@ -112,8 +149,8 @@ EnvelopeListItem .status-icon.unread {
|
||||
}
|
||||
|
||||
EnvelopeListItem .checkbox {
|
||||
width: 2;
|
||||
padding: 0 1 0 0;
|
||||
width: 1;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
EnvelopeListItem .sender-name {
|
||||
@@ -128,12 +165,12 @@ EnvelopeListItem .message-datetime {
|
||||
|
||||
EnvelopeListItem .email-subject {
|
||||
width: 1fr;
|
||||
padding: 0 4;
|
||||
padding: 0 3;
|
||||
}
|
||||
|
||||
EnvelopeListItem .email-preview {
|
||||
width: 1fr;
|
||||
padding: 0 4;
|
||||
padding: 0 3;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
@@ -146,20 +183,39 @@ EnvelopeListItem.unread .email-subject {
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
/* Selected message styling */
|
||||
/* Selected/checked message styling (for multi-select) */
|
||||
EnvelopeListItem.selected {
|
||||
tint: $accent 20%;
|
||||
}
|
||||
|
||||
/* Currently highlighted/focused item styling - more prominent */
|
||||
EnvelopeListItem.highlighted {
|
||||
background: $primary-darken-1;
|
||||
}
|
||||
|
||||
EnvelopeListItem.highlighted .sender-name {
|
||||
color: $text;
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
EnvelopeListItem.highlighted .email-subject {
|
||||
color: $text;
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
EnvelopeListItem.highlighted .message-datetime {
|
||||
color: $secondary-lighten-2;
|
||||
}
|
||||
|
||||
/* GroupHeader - date group separator */
|
||||
GroupHeader {
|
||||
height: 1;
|
||||
width: 1fr;
|
||||
background: rgb(64, 62, 65);
|
||||
background: $panel;
|
||||
}
|
||||
|
||||
GroupHeader .group-header-label {
|
||||
color: rgb(160, 160, 160);
|
||||
color: $text-muted;
|
||||
text-style: bold;
|
||||
padding: 0 1;
|
||||
width: 1fr;
|
||||
@@ -211,18 +267,36 @@ GroupHeader .group-header-label {
|
||||
|
||||
#envelopes_list {
|
||||
ListItem:odd {
|
||||
background: rgb(45, 45, 46);
|
||||
background: $surface;
|
||||
}
|
||||
ListItem:even {
|
||||
background: rgb(50, 50, 56);
|
||||
background: $surface-darken-1;
|
||||
}
|
||||
|
||||
& > ListItem {
|
||||
&.-highlight, .selection {
|
||||
color: $block-cursor-blurred-foreground;
|
||||
background: $block-cursor-blurred-background;
|
||||
text-style: $block-cursor-blurred-text-style;
|
||||
}
|
||||
/* Currently highlighted/focused item - make it very visible */
|
||||
& > ListItem.-highlight {
|
||||
background: $primary-darken-2;
|
||||
color: $text;
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
/* Highlighted item's child elements */
|
||||
& > ListItem.-highlight EnvelopeListItem {
|
||||
tint: $primary 30%;
|
||||
}
|
||||
|
||||
& > ListItem.-highlight .sender-name {
|
||||
color: $text;
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
& > ListItem.-highlight .email-subject {
|
||||
color: $text;
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
& > ListItem.-highlight .message-datetime {
|
||||
color: $secondary-lighten-2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,9 +332,9 @@ GroupHeader .group-header-label {
|
||||
}
|
||||
|
||||
Label.group_header {
|
||||
color: rgb(140, 140, 140);
|
||||
color: $text-muted;
|
||||
text-style: bold;
|
||||
background: rgb(64, 62, 65);
|
||||
background: $panel;
|
||||
width: 100%;
|
||||
padding: 0 1;
|
||||
}
|
||||
@@ -289,6 +363,3 @@ ContentContainer {
|
||||
width: 100%;
|
||||
height: 1fr;
|
||||
}
|
||||
.checkbox {
|
||||
padding-right: 1;
|
||||
}
|
||||
208
src/mail/invite_compressor.py
Normal file
208
src/mail/invite_compressor.py
Normal file
@@ -0,0 +1,208 @@
|
||||
"""Calendar invite compressor for terminal-friendly display."""
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
from .utils.calendar_parser import (
|
||||
ParsedCalendarEvent,
|
||||
parse_calendar_from_raw_message,
|
||||
format_event_time,
|
||||
is_cancelled_event,
|
||||
is_event_request,
|
||||
)
|
||||
from .notification_detector import is_calendar_email
|
||||
|
||||
|
||||
class InviteCompressor:
|
||||
"""Compress calendar invite emails into terminal-friendly summaries."""
|
||||
|
||||
# Nerdfont icons
|
||||
ICON_CALENDAR = "\uf073" # calendar icon
|
||||
ICON_CANCELLED = "\uf057" # times-circle
|
||||
ICON_INVITE = "\uf0e0" # envelope
|
||||
ICON_REPLY = "\uf3e5" # reply
|
||||
ICON_LOCATION = "\uf3c5" # map-marker-alt
|
||||
ICON_CLOCK = "\uf017" # clock
|
||||
ICON_USER = "\uf007" # user
|
||||
ICON_USERS = "\uf0c0" # users
|
||||
|
||||
def __init__(self, mode: str = "summary"):
|
||||
"""Initialize compressor.
|
||||
|
||||
Args:
|
||||
mode: Compression mode - "summary", "detailed", or "off"
|
||||
"""
|
||||
self.mode = mode
|
||||
|
||||
def should_compress(self, envelope: dict[str, Any]) -> bool:
|
||||
"""Check if email should be compressed as calendar invite.
|
||||
|
||||
Args:
|
||||
envelope: Email envelope metadata
|
||||
|
||||
Returns:
|
||||
True if email is a calendar invite that should be compressed
|
||||
"""
|
||||
if self.mode == "off":
|
||||
return False
|
||||
|
||||
return is_calendar_email(envelope)
|
||||
|
||||
def compress(
|
||||
self, raw_message: str, envelope: dict[str, Any]
|
||||
) -> tuple[str, Optional[ParsedCalendarEvent]]:
|
||||
"""Compress calendar invite email content.
|
||||
|
||||
Args:
|
||||
raw_message: Raw email MIME content
|
||||
envelope: Email envelope metadata
|
||||
|
||||
Returns:
|
||||
Tuple of (compressed content, parsed event or None)
|
||||
"""
|
||||
if not self.should_compress(envelope):
|
||||
return "", None
|
||||
|
||||
# Parse the ICS content from raw message
|
||||
event = parse_calendar_from_raw_message(raw_message)
|
||||
|
||||
if not event:
|
||||
return "", None
|
||||
|
||||
# Format as markdown
|
||||
compressed = self._format_as_markdown(event, envelope)
|
||||
|
||||
return compressed, event
|
||||
|
||||
def _format_as_markdown(
|
||||
self,
|
||||
event: ParsedCalendarEvent,
|
||||
envelope: dict[str, Any],
|
||||
) -> str:
|
||||
"""Format event as markdown for terminal display.
|
||||
|
||||
Args:
|
||||
event: Parsed calendar event
|
||||
envelope: Email envelope metadata
|
||||
|
||||
Returns:
|
||||
Markdown-formatted compressed invite
|
||||
"""
|
||||
lines = []
|
||||
|
||||
# Determine event type and icon
|
||||
if is_cancelled_event(event):
|
||||
icon = self.ICON_CANCELLED
|
||||
type_label = "CANCELLED"
|
||||
type_style = "~~" # strikethrough
|
||||
elif is_event_request(event):
|
||||
icon = self.ICON_INVITE
|
||||
type_label = "MEETING INVITE"
|
||||
type_style = "**"
|
||||
else:
|
||||
icon = self.ICON_CALENDAR
|
||||
type_label = event.method or "CALENDAR"
|
||||
type_style = ""
|
||||
|
||||
# Header
|
||||
lines.append(f"## {icon} {type_label}")
|
||||
lines.append("")
|
||||
|
||||
# Event title
|
||||
title = event.summary or envelope.get("subject", "Untitled Event")
|
||||
if is_cancelled_event(event):
|
||||
# Remove "Canceled: " prefix if present
|
||||
if title.lower().startswith("canceled:"):
|
||||
title = title[9:].strip()
|
||||
elif title.lower().startswith("cancelled:"):
|
||||
title = title[10:].strip()
|
||||
lines.append(f"~~{title}~~")
|
||||
else:
|
||||
lines.append(f"**{title}**")
|
||||
lines.append("")
|
||||
|
||||
# Time
|
||||
time_str = format_event_time(event)
|
||||
lines.append(f"{self.ICON_CLOCK} {time_str}")
|
||||
lines.append("")
|
||||
|
||||
# Location
|
||||
if event.location:
|
||||
lines.append(f"{self.ICON_LOCATION} {event.location}")
|
||||
lines.append("")
|
||||
|
||||
# Organizer
|
||||
if event.organizer_name or event.organizer_email:
|
||||
organizer = event.organizer_name or event.organizer_email
|
||||
lines.append(f"{self.ICON_USER} **Organizer:** {organizer}")
|
||||
lines.append("")
|
||||
|
||||
# Attendees (compressed)
|
||||
if event.attendees:
|
||||
attendee_summary = self._compress_attendees(event.attendees)
|
||||
lines.append(f"{self.ICON_USERS} **Attendees:** {attendee_summary}")
|
||||
lines.append("")
|
||||
|
||||
# Actions hint
|
||||
if is_event_request(event):
|
||||
lines.append("---")
|
||||
lines.append("")
|
||||
lines.append("*Press `A` to Accept, `T` for Tentative, `D` to Decline*")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
def _compress_attendees(self, attendees: list[str], max_shown: int = 3) -> str:
|
||||
"""Compress attendee list to a short summary.
|
||||
|
||||
Args:
|
||||
attendees: List of attendee strings (name <email> or just email)
|
||||
max_shown: Maximum number of attendees to show before truncating
|
||||
|
||||
Returns:
|
||||
Compressed attendee summary like "Alice, Bob, Carol... (+12 more)"
|
||||
"""
|
||||
if not attendees:
|
||||
return "None"
|
||||
|
||||
# Extract just names from attendees
|
||||
names = []
|
||||
for att in attendees:
|
||||
# Handle "Name <email>" format
|
||||
if "<" in att:
|
||||
name = att.split("<")[0].strip()
|
||||
if name:
|
||||
# Get just first name for brevity
|
||||
first_name = (
|
||||
name.split(",")[0].strip() if "," in name else name.split()[0]
|
||||
)
|
||||
names.append(first_name)
|
||||
else:
|
||||
names.append(att.split("<")[1].rstrip(">").split("@")[0])
|
||||
else:
|
||||
# Just email, use local part
|
||||
names.append(att.split("@")[0])
|
||||
|
||||
total = len(names)
|
||||
|
||||
if total <= max_shown:
|
||||
return ", ".join(names)
|
||||
else:
|
||||
shown = ", ".join(names[:max_shown])
|
||||
remaining = total - max_shown
|
||||
return f"{shown}... (+{remaining} more)"
|
||||
|
||||
|
||||
def compress_invite(
|
||||
raw_message: str, envelope: dict[str, Any], mode: str = "summary"
|
||||
) -> tuple[str, Optional[ParsedCalendarEvent]]:
|
||||
"""Convenience function to compress a calendar invite.
|
||||
|
||||
Args:
|
||||
raw_message: Raw email MIME content
|
||||
envelope: Email envelope metadata
|
||||
mode: Compression mode
|
||||
|
||||
Returns:
|
||||
Tuple of (compressed content, parsed event or None)
|
||||
"""
|
||||
compressor = InviteCompressor(mode=mode)
|
||||
return compressor.compress(raw_message, envelope)
|
||||
@@ -83,7 +83,17 @@ class MessageStore:
|
||||
self, current_index: int
|
||||
) -> Tuple[Optional[int], Optional[int]]:
|
||||
"""Find the next valid message ID and its index"""
|
||||
if not self.envelopes or current_index >= len(self.envelopes) - 1:
|
||||
if not self.envelopes:
|
||||
return None, None
|
||||
|
||||
# Clamp current_index to valid range in case list shrunk during async operations
|
||||
if current_index >= len(self.envelopes):
|
||||
current_index = len(self.envelopes) - 1
|
||||
if current_index < 0:
|
||||
current_index = 0
|
||||
|
||||
# If already at or past the end, no next item
|
||||
if current_index >= len(self.envelopes) - 1:
|
||||
return None, None
|
||||
|
||||
# Start from current index + 1
|
||||
@@ -99,7 +109,17 @@ class MessageStore:
|
||||
self, current_index: int
|
||||
) -> Tuple[Optional[int], Optional[int]]:
|
||||
"""Find the previous valid message ID and its index"""
|
||||
if not self.envelopes or current_index <= 0:
|
||||
if not self.envelopes:
|
||||
return None, None
|
||||
|
||||
# Clamp current_index to valid range in case list shrunk during async operations
|
||||
if current_index >= len(self.envelopes):
|
||||
current_index = len(self.envelopes) - 1
|
||||
if current_index < 0:
|
||||
current_index = 0
|
||||
|
||||
# If at the beginning, no previous item
|
||||
if current_index <= 0:
|
||||
return None, None
|
||||
|
||||
# Start from current index - 1
|
||||
@@ -148,3 +168,69 @@ class MessageStore:
|
||||
self.total_messages = len(self.metadata_by_id)
|
||||
else:
|
||||
logging.warning(f"Invalid index {index} for message ID {message_id}")
|
||||
|
||||
def filter_by_query(self, query: str) -> List[Dict[str, Any]]:
|
||||
"""Filter envelopes by search query.
|
||||
|
||||
Searches subject, from name, from address, to name, and to address.
|
||||
Returns a new list of filtered envelopes (with headers regenerated).
|
||||
"""
|
||||
if not query or not query.strip():
|
||||
return self.envelopes
|
||||
|
||||
query_lower = query.lower().strip()
|
||||
filtered = []
|
||||
current_month = None
|
||||
|
||||
for item in self.envelopes:
|
||||
if item is None:
|
||||
continue
|
||||
|
||||
# Skip headers - we'll regenerate them
|
||||
if item.get("type") == "header":
|
||||
continue
|
||||
|
||||
# Check if envelope matches query
|
||||
# Use "or ''" to handle None values (key exists but value is None)
|
||||
subject = (item.get("subject") or "").lower()
|
||||
from_info = item.get("from", {})
|
||||
from_name = (
|
||||
(from_info.get("name") or "").lower()
|
||||
if isinstance(from_info, dict)
|
||||
else ""
|
||||
)
|
||||
from_addr = (
|
||||
(from_info.get("addr") or "").lower()
|
||||
if isinstance(from_info, dict)
|
||||
else ""
|
||||
)
|
||||
to_info = item.get("to", {})
|
||||
to_name = (
|
||||
(to_info.get("name") or "").lower() if isinstance(to_info, dict) else ""
|
||||
)
|
||||
to_addr = (
|
||||
(to_info.get("addr") or "").lower() if isinstance(to_info, dict) else ""
|
||||
)
|
||||
|
||||
if (
|
||||
query_lower in subject
|
||||
or query_lower in from_name
|
||||
or query_lower in from_addr
|
||||
or query_lower in to_name
|
||||
or query_lower in to_addr
|
||||
):
|
||||
# Regenerate month header if needed
|
||||
date_str = item.get("date", "")
|
||||
try:
|
||||
date = datetime.fromisoformat(date_str.replace("Z", "+00:00"))
|
||||
month_key = date.strftime("%B %Y")
|
||||
except (ValueError, TypeError):
|
||||
month_key = "Unknown Date"
|
||||
|
||||
if month_key != current_month:
|
||||
current_month = month_key
|
||||
filtered.append({"type": "header", "label": month_key})
|
||||
|
||||
filtered.append(item)
|
||||
|
||||
return filtered
|
||||
219
src/mail/notification_compressor.py
Normal file
219
src/mail/notification_compressor.py
Normal file
@@ -0,0 +1,219 @@
|
||||
"""Notification email compressor for terminal-friendly display."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from .notification_detector import (
|
||||
NotificationType,
|
||||
classify_notification,
|
||||
extract_notification_summary,
|
||||
is_notification_email,
|
||||
)
|
||||
|
||||
|
||||
class NotificationCompressor:
|
||||
"""Compress notification emails into terminal-friendly summaries."""
|
||||
|
||||
def __init__(self, mode: str = "summary"):
|
||||
"""Initialize compressor.
|
||||
|
||||
Args:
|
||||
mode: Compression mode - "summary", "detailed", or "off"
|
||||
"""
|
||||
self.mode = mode
|
||||
|
||||
def should_compress(self, envelope: dict[str, Any]) -> bool:
|
||||
"""Check if email should be compressed.
|
||||
|
||||
Args:
|
||||
envelope: Email envelope metadata
|
||||
|
||||
Returns:
|
||||
True if email should be compressed
|
||||
"""
|
||||
if self.mode == "off":
|
||||
return False
|
||||
|
||||
return is_notification_email(envelope)
|
||||
|
||||
def compress(
|
||||
self, content: str, envelope: dict[str, Any]
|
||||
) -> tuple[str, NotificationType | None]:
|
||||
"""Compress notification email content.
|
||||
|
||||
Args:
|
||||
content: Raw email content
|
||||
envelope: Email envelope metadata
|
||||
|
||||
Returns:
|
||||
Tuple of (compressed content, notification_type)
|
||||
"""
|
||||
|
||||
if not self.should_compress(envelope):
|
||||
return content, None
|
||||
|
||||
# Classify notification type
|
||||
notif_type = classify_notification(envelope, content)
|
||||
|
||||
# Extract summary
|
||||
summary = extract_notification_summary(content, notif_type)
|
||||
|
||||
# Format as markdown
|
||||
compressed = self._format_as_markdown(summary, envelope, notif_type)
|
||||
|
||||
return compressed, notif_type
|
||||
|
||||
def _format_as_markdown(
|
||||
self,
|
||||
summary: dict[str, Any],
|
||||
envelope: dict[str, Any],
|
||||
notif_type: NotificationType | None,
|
||||
) -> str:
|
||||
"""Format summary as markdown for terminal display.
|
||||
|
||||
Args:
|
||||
summary: Extracted summary data
|
||||
envelope: Email envelope metadata
|
||||
notif_type: Classified notification type
|
||||
|
||||
Returns:
|
||||
Markdown-formatted compressed email
|
||||
"""
|
||||
|
||||
from_addr = envelope.get("from", {}).get("name") or envelope.get(
|
||||
"from", {}
|
||||
).get("addr", "")
|
||||
subject = envelope.get("subject", "")
|
||||
|
||||
# Get icon
|
||||
icon = notif_type.icon if notif_type else "\uf0f3"
|
||||
|
||||
# Build markdown
|
||||
lines = []
|
||||
|
||||
# Header with icon
|
||||
if notif_type:
|
||||
lines.append(f"## {icon} {notif_type.name.title()} Notification")
|
||||
else:
|
||||
lines.append(f"## {icon} Notification")
|
||||
|
||||
lines.append("")
|
||||
|
||||
# Title/subject
|
||||
if summary.get("title"):
|
||||
lines.append(f"**{summary['title']}**")
|
||||
else:
|
||||
lines.append(f"**{subject}**")
|
||||
lines.append("")
|
||||
|
||||
# Metadata section
|
||||
if summary.get("metadata"):
|
||||
lines.append("### Details")
|
||||
for key, value in summary["metadata"].items():
|
||||
# Format key nicely
|
||||
key_formatted = key.replace("_", " ").title()
|
||||
lines.append(f"- **{key_formatted}**: {value}")
|
||||
lines.append("")
|
||||
|
||||
# Action items
|
||||
if summary.get("action_items"):
|
||||
lines.append("### Actions")
|
||||
for i, action in enumerate(summary["action_items"], 1):
|
||||
lines.append(f"{i}. {action}")
|
||||
lines.append("")
|
||||
|
||||
# Add footer
|
||||
lines.append("---")
|
||||
lines.append("")
|
||||
lines.append(f"*From: {from_addr}*")
|
||||
lines.append(
|
||||
"*This is a compressed notification. Press `m` to see full email.*"
|
||||
)
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
class DetailedCompressor(NotificationCompressor):
|
||||
"""Compressor that includes more detail in summaries."""
|
||||
|
||||
def _format_as_markdown(
|
||||
self,
|
||||
summary: dict[str, Any],
|
||||
envelope: dict[str, Any],
|
||||
notif_type: NotificationType | None,
|
||||
) -> str:
|
||||
"""Format summary with more detail."""
|
||||
|
||||
from_addr = envelope.get("from", {}).get("name") or envelope.get(
|
||||
"from", {}
|
||||
).get("addr", "")
|
||||
subject = envelope.get("subject", "")
|
||||
date = envelope.get("date", "")
|
||||
|
||||
icon = notif_type.icon if notif_type else "\uf0f3"
|
||||
|
||||
lines = []
|
||||
|
||||
# Header
|
||||
lines.append(
|
||||
f"## {icon} {notif_type.name.title()} Notification"
|
||||
if notif_type
|
||||
else f"## {icon} Notification"
|
||||
)
|
||||
lines.append("")
|
||||
|
||||
# Subject and from
|
||||
lines.append(f"**Subject:** {subject}")
|
||||
lines.append(f"**From:** {from_addr}")
|
||||
lines.append(f"**Date:** {date}")
|
||||
lines.append("")
|
||||
|
||||
# Summary title
|
||||
if summary.get("title"):
|
||||
lines.append(f"### {summary['title']}")
|
||||
lines.append("")
|
||||
|
||||
# Metadata table
|
||||
if summary.get("metadata"):
|
||||
lines.append("| Property | Value |")
|
||||
lines.append("|----------|-------|")
|
||||
for key, value in summary["metadata"].items():
|
||||
key_formatted = key.replace("_", " ").title()
|
||||
lines.append(f"| {key_formatted} | {value} |")
|
||||
lines.append("")
|
||||
|
||||
# Action items
|
||||
if summary.get("action_items"):
|
||||
lines.append("### Action Items")
|
||||
for i, action in enumerate(summary["action_items"], 1):
|
||||
lines.append(f"- [ ] {action}")
|
||||
lines.append("")
|
||||
|
||||
# Key links
|
||||
if summary.get("key_links"):
|
||||
lines.append("### Important Links")
|
||||
for link in summary["key_links"]:
|
||||
lines.append(f"- [{link.get('text', 'Link')}]({link.get('url', '#')})")
|
||||
lines.append("")
|
||||
|
||||
# Footer
|
||||
lines.append("---")
|
||||
lines.append(
|
||||
"*This is a compressed notification view. Press `m` to toggle full view.*"
|
||||
)
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def create_compressor(mode: str) -> NotificationCompressor:
|
||||
"""Factory function to create appropriate compressor.
|
||||
|
||||
Args:
|
||||
mode: Compression mode - "summary", "detailed", or "off"
|
||||
|
||||
Returns:
|
||||
NotificationCompressor instance
|
||||
"""
|
||||
|
||||
if mode == "detailed":
|
||||
return DetailedCompressor(mode=mode)
|
||||
return NotificationCompressor(mode=mode)
|
||||
443
src/mail/notification_detector.py
Normal file
443
src/mail/notification_detector.py
Normal file
@@ -0,0 +1,443 @@
|
||||
"""Email notification detection utilities."""
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class NotificationType:
|
||||
"""Classification of notification email types."""
|
||||
|
||||
name: str
|
||||
patterns: list[str]
|
||||
domains: list[str]
|
||||
icon: str
|
||||
|
||||
def matches(self, envelope: dict[str, Any], content: str | None = None) -> bool:
|
||||
"""Check if envelope matches this notification type."""
|
||||
|
||||
# Check sender domain (more specific check)
|
||||
from_addr = envelope.get("from", {}).get("addr", "").lower()
|
||||
for domain in self.domains:
|
||||
# For atlassian.net, check if it's specifically jira or confluence in the address
|
||||
if domain == "atlassian.net":
|
||||
if "jira@" in from_addr:
|
||||
return self.name == "jira"
|
||||
if "confluence@" in from_addr:
|
||||
return self.name == "confluence"
|
||||
elif domain in from_addr:
|
||||
return True
|
||||
|
||||
# Check subject patterns
|
||||
subject = envelope.get("subject", "").lower()
|
||||
if any(re.search(pattern, subject, re.IGNORECASE) for pattern in self.patterns):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
# Common notification types
|
||||
NOTIFICATION_TYPES = [
|
||||
NotificationType(
|
||||
name="gitlab",
|
||||
patterns=[r"\[gitlab\]", r"pipeline", r"merge request", r"mention.*you"],
|
||||
domains=["gitlab.com", "@gitlab"],
|
||||
icon="\uf296",
|
||||
),
|
||||
NotificationType(
|
||||
name="github",
|
||||
patterns=[r"\[github\]", r"pr #", r"pull request", r"issue #", r"mention"],
|
||||
domains=["github.com", "noreply@github.com"],
|
||||
icon="\uf09b",
|
||||
),
|
||||
NotificationType(
|
||||
name="jira",
|
||||
patterns=[r"\[jira\]", r"[a-z]+-\d+", r"issue updated", r"comment added"],
|
||||
domains=["atlassian.net", "jira"],
|
||||
icon="\uf1b3",
|
||||
),
|
||||
NotificationType(
|
||||
name="confluence",
|
||||
patterns=[r"\[confluence\]", r"page created", r"page updated", r"comment"],
|
||||
domains=["atlassian.net", "confluence"],
|
||||
icon="\uf298",
|
||||
),
|
||||
NotificationType(
|
||||
name="datadog",
|
||||
patterns=[r"alert", r"monitor", r"incident", r"downtime"],
|
||||
domains=["datadoghq.com", "datadog"],
|
||||
icon="\uf1b0",
|
||||
),
|
||||
NotificationType(
|
||||
name="renovate",
|
||||
patterns=[r"renovate", r"dependency update", r"lock file"],
|
||||
domains=["renovate", "renovatebot"],
|
||||
icon="\uf1e6",
|
||||
),
|
||||
NotificationType(
|
||||
name="general",
|
||||
patterns=[r"\[.*?\]", r"notification", r"digest", r"summary"],
|
||||
domains=["noreply@", "no-reply@", "notifications@"],
|
||||
icon="\uf0f3",
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def is_notification_email(envelope: dict[str, Any], content: str | None = None) -> bool:
|
||||
"""Check if an email is a notification-style email.
|
||||
|
||||
Args:
|
||||
envelope: Email envelope metadata from himalaya
|
||||
content: Optional email content for content-based detection
|
||||
|
||||
Returns:
|
||||
True if email appears to be a notification
|
||||
"""
|
||||
|
||||
# Check against known notification types
|
||||
for notif_type in NOTIFICATION_TYPES:
|
||||
if notif_type.matches(envelope, content):
|
||||
return True
|
||||
|
||||
# Check for generic notification indicators
|
||||
subject = envelope.get("subject", "").lower()
|
||||
from_addr = envelope.get("from", {}).get("addr", "").lower()
|
||||
|
||||
# Generic notification patterns
|
||||
generic_patterns = [
|
||||
r"^\[.*?\]", # Brackets at start
|
||||
r"weekly|daily|monthly.*report|digest|summary",
|
||||
r"you were mentioned",
|
||||
r"this is an automated message",
|
||||
r"do not reply|don't reply",
|
||||
]
|
||||
|
||||
if any(re.search(pattern, subject, re.IGNORECASE) for pattern in generic_patterns):
|
||||
return True
|
||||
|
||||
# Check for notification senders
|
||||
notification_senders = ["noreply", "no-reply", "notifications", "robot", "bot"]
|
||||
if any(sender in from_addr for sender in notification_senders):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def classify_notification(
|
||||
envelope: dict[str, Any], content: str | None = None
|
||||
) -> NotificationType | None:
|
||||
"""Classify the type of notification email.
|
||||
|
||||
Args:
|
||||
envelope: Email envelope metadata from himalaya
|
||||
content: Optional email content for content-based detection
|
||||
|
||||
Returns:
|
||||
NotificationType if classified, None if not a notification
|
||||
"""
|
||||
|
||||
for notif_type in NOTIFICATION_TYPES:
|
||||
if notif_type.matches(envelope, content):
|
||||
return notif_type
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def extract_notification_summary(
|
||||
content: str, notification_type: NotificationType | None = None
|
||||
) -> dict[str, Any]:
|
||||
"""Extract structured summary from notification email content.
|
||||
|
||||
Args:
|
||||
content: Email body content
|
||||
notification_type: Classified notification type (optional)
|
||||
|
||||
Returns:
|
||||
Dictionary with extracted summary fields
|
||||
"""
|
||||
|
||||
summary = {
|
||||
"title": None,
|
||||
"action_items": [],
|
||||
"key_links": [],
|
||||
"metadata": {},
|
||||
}
|
||||
|
||||
# Extract based on notification type
|
||||
if notification_type and notification_type.name == "gitlab":
|
||||
summary.update(_extract_gitlab_summary(content))
|
||||
elif notification_type and notification_type.name == "github":
|
||||
summary.update(_extract_github_summary(content))
|
||||
elif notification_type and notification_type.name == "jira":
|
||||
summary.update(_extract_jira_summary(content))
|
||||
elif notification_type and notification_type.name == "confluence":
|
||||
summary.update(_extract_confluence_summary(content))
|
||||
elif notification_type and notification_type.name == "datadog":
|
||||
summary.update(_extract_datadog_summary(content))
|
||||
elif notification_type and notification_type.name == "renovate":
|
||||
summary.update(_extract_renovate_summary(content))
|
||||
else:
|
||||
summary.update(_extract_general_notification_summary(content))
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def _extract_gitlab_summary(content: str) -> dict[str, Any]:
|
||||
"""Extract summary from GitLab notification."""
|
||||
summary = {"action_items": [], "key_links": [], "metadata": {}}
|
||||
|
||||
# Pipeline patterns
|
||||
pipeline_match = re.search(
|
||||
r"Pipeline #(\d+).*?(?:failed|passed|canceled) by (.+?)[\n\r]",
|
||||
content,
|
||||
re.IGNORECASE,
|
||||
)
|
||||
if pipeline_match:
|
||||
summary["metadata"]["pipeline_id"] = pipeline_match.group(1)
|
||||
summary["metadata"]["triggered_by"] = pipeline_match.group(2)
|
||||
summary["title"] = f"Pipeline #{pipeline_match.group(1)}"
|
||||
|
||||
# Merge request patterns
|
||||
mr_match = re.search(r"Merge request #(\d+):\s*(.+?)[\n\r]", content, re.IGNORECASE)
|
||||
if mr_match:
|
||||
summary["metadata"]["mr_id"] = mr_match.group(1)
|
||||
summary["metadata"]["mr_title"] = mr_match.group(2)
|
||||
summary["title"] = f"MR #{mr_match.group(1)}: {mr_match.group(2)}"
|
||||
|
||||
# Mention patterns
|
||||
mention_match = re.search(
|
||||
r"<@(.+?)> mentioned you in (?:#|@)(.+?)[\n\r]", content, re.IGNORECASE
|
||||
)
|
||||
if mention_match:
|
||||
summary["metadata"]["mentioned_by"] = mention_match.group(1)
|
||||
summary["metadata"]["mentioned_in"] = mention_match.group(2)
|
||||
summary["title"] = f"Mention by {mention_match.group(1)}"
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def _extract_github_summary(content: str) -> dict[str, Any]:
|
||||
"""Extract summary from GitHub notification."""
|
||||
summary = {"action_items": [], "key_links": [], "metadata": {}}
|
||||
|
||||
# PR/Issue patterns
|
||||
pr_match = re.search(r"(?:PR|Issue) #(\d+):\s*(.+?)[\n\r]", content, re.IGNORECASE)
|
||||
if pr_match:
|
||||
summary["metadata"]["number"] = pr_match.group(1)
|
||||
summary["metadata"]["title"] = pr_match.group(2)
|
||||
summary["title"] = f"#{pr_match.group(1)}: {pr_match.group(2)}"
|
||||
|
||||
# Review requested
|
||||
if "review requested" in content.lower():
|
||||
summary["action_items"].append("Review requested")
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def _extract_jira_summary(content: str) -> dict[str, Any]:
|
||||
"""Extract summary from Jira notification."""
|
||||
summary = {"action_items": [], "key_links": [], "metadata": {}}
|
||||
|
||||
# Issue patterns
|
||||
issue_match = re.search(r"([A-Z]+-\d+):\s*(.+?)[\n\r]", content, re.IGNORECASE)
|
||||
if issue_match:
|
||||
summary["metadata"]["issue_key"] = issue_match.group(1)
|
||||
summary["metadata"]["issue_title"] = issue_match.group(2)
|
||||
summary["title"] = f"{issue_match.group(1)}: {issue_match.group(2)}"
|
||||
|
||||
# Status changes
|
||||
if "status changed" in content.lower():
|
||||
status_match = re.search(
|
||||
r"status changed from (.+?) to (.+)", content, re.IGNORECASE
|
||||
)
|
||||
if status_match:
|
||||
summary["metadata"]["status_from"] = status_match.group(1)
|
||||
summary["metadata"]["status_to"] = status_match.group(2)
|
||||
summary["action_items"].append(
|
||||
f"Status: {status_match.group(1)} → {status_match.group(2)}"
|
||||
)
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def _extract_confluence_summary(content: str) -> dict[str, Any]:
|
||||
"""Extract summary from Confluence notification."""
|
||||
summary = {"action_items": [], "key_links": [], "metadata": {}}
|
||||
|
||||
# Page patterns
|
||||
page_match = re.search(r"Page \"(.+?)\"", content, re.IGNORECASE)
|
||||
if page_match:
|
||||
summary["metadata"]["page_title"] = page_match.group(1)
|
||||
summary["title"] = f"Page: {page_match.group(1)}"
|
||||
|
||||
# Author
|
||||
author_match = re.search(
|
||||
r"(?:created|updated) by (.+?)[\n\r]", content, re.IGNORECASE
|
||||
)
|
||||
if author_match:
|
||||
summary["metadata"]["author"] = author_match.group(1)
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def _extract_datadog_summary(content: str) -> dict[str, Any]:
|
||||
"""Extract summary from Datadog notification."""
|
||||
summary = {"action_items": [], "key_links": [], "metadata": {}}
|
||||
|
||||
# Alert status
|
||||
if "triggered" in content.lower():
|
||||
summary["metadata"]["status"] = "Triggered"
|
||||
summary["action_items"].append("Alert triggered - investigate")
|
||||
elif "recovered" in content.lower():
|
||||
summary["metadata"]["status"] = "Recovered"
|
||||
|
||||
# Monitor name
|
||||
monitor_match = re.search(r"Monitor: (.+?)[\n\r]", content, re.IGNORECASE)
|
||||
if monitor_match:
|
||||
summary["metadata"]["monitor"] = monitor_match.group(1)
|
||||
summary["title"] = f"Alert: {monitor_match.group(1)}"
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def _extract_renovate_summary(content: str) -> dict[str, Any]:
|
||||
"""Extract summary from Renovate notification."""
|
||||
summary = {"action_items": [], "key_links": [], "metadata": {}}
|
||||
|
||||
# Dependency patterns
|
||||
dep_match = re.search(
|
||||
r"Update (?:.+) dependency (.+?) to (v?\d+\.\d+\.?\d*)", content, re.IGNORECASE
|
||||
)
|
||||
if dep_match:
|
||||
summary["metadata"]["dependency"] = dep_match.group(2)
|
||||
summary["metadata"]["version"] = dep_match.group(3)
|
||||
summary["title"] = f"Update {dep_match.group(2)} to {dep_match.group(3)}"
|
||||
summary["action_items"].append("Review and merge dependency update")
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def _extract_general_notification_summary(content: str) -> dict[str, Any]:
|
||||
"""Extract summary from general notification."""
|
||||
summary = {"action_items": [], "key_links": [], "metadata": {}}
|
||||
|
||||
# Look for action-oriented phrases
|
||||
action_patterns = [
|
||||
r"you need to (.+)",
|
||||
r"please (.+)",
|
||||
r"action required",
|
||||
r"review requested",
|
||||
r"approval needed",
|
||||
]
|
||||
|
||||
for pattern in action_patterns:
|
||||
matches = re.findall(pattern, content, re.IGNORECASE)
|
||||
summary["action_items"].extend(matches)
|
||||
|
||||
# Limit action items
|
||||
summary["action_items"] = summary["action_items"][:5]
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
# Calendar email patterns
|
||||
CALENDAR_SUBJECT_PATTERNS = [
|
||||
r"^canceled:",
|
||||
r"^cancelled:",
|
||||
r"^accepted:",
|
||||
r"^declined:",
|
||||
r"^tentative:",
|
||||
r"^updated:",
|
||||
r"^invitation:",
|
||||
r"^meeting\s+(request|update|cancel)",
|
||||
r"^\[calendar\]",
|
||||
r"invite\s+you\s+to",
|
||||
r"has\s+invited\s+you",
|
||||
]
|
||||
|
||||
|
||||
def _decode_mime_content(raw_content: str) -> str:
|
||||
"""Decode base64 parts from MIME content for text searching.
|
||||
|
||||
Args:
|
||||
raw_content: Raw MIME message content
|
||||
|
||||
Returns:
|
||||
Decoded text content for searching
|
||||
"""
|
||||
import base64
|
||||
|
||||
decoded_parts = [raw_content] # Include raw content for non-base64 parts
|
||||
|
||||
# Find and decode base64 text parts
|
||||
b64_pattern = re.compile(
|
||||
r"Content-Type:\s*text/(?:plain|html)[^\n]*\n"
|
||||
r"(?:[^\n]+\n)*?" # Other headers
|
||||
r"Content-Transfer-Encoding:\s*base64[^\n]*\n"
|
||||
r"(?:[^\n]+\n)*?" # Other headers
|
||||
r"\n" # Empty line before content
|
||||
r"([A-Za-z0-9+/=\s]+)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
for match in b64_pattern.finditer(raw_content):
|
||||
try:
|
||||
b64_content = (
|
||||
match.group(1).replace("\n", "").replace("\r", "").replace(" ", "")
|
||||
)
|
||||
decoded = base64.b64decode(b64_content).decode("utf-8", errors="replace")
|
||||
decoded_parts.append(decoded)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return " ".join(decoded_parts)
|
||||
|
||||
|
||||
def is_calendar_email(envelope: dict[str, Any], content: str | None = None) -> bool:
|
||||
"""Check if an email is a calendar invite/update/cancellation.
|
||||
|
||||
Args:
|
||||
envelope: Email envelope metadata from himalaya
|
||||
content: Optional message content to check for calendar indicators
|
||||
|
||||
Returns:
|
||||
True if email appears to be a calendar-related email
|
||||
"""
|
||||
subject = envelope.get("subject", "").lower().strip()
|
||||
|
||||
# Check subject patterns
|
||||
for pattern in CALENDAR_SUBJECT_PATTERNS:
|
||||
if re.search(pattern, subject, re.IGNORECASE):
|
||||
return True
|
||||
|
||||
# Check for meeting-related keywords in subject
|
||||
meeting_keywords = ["meeting", "appointment", "calendar", "invite", "rsvp"]
|
||||
if any(keyword in subject for keyword in meeting_keywords):
|
||||
return True
|
||||
|
||||
# Check for forwarded meeting invites (FW: or Fwd:) with calendar keywords
|
||||
if re.match(r"^(fw|fwd):", subject, re.IGNORECASE):
|
||||
# Check for Teams/calendar-related terms that might indicate forwarded invite
|
||||
forward_meeting_keywords = ["connect", "sync", "call", "discussion", "review"]
|
||||
if any(keyword in subject for keyword in forward_meeting_keywords):
|
||||
return True
|
||||
|
||||
# If content is provided, check for calendar indicators
|
||||
if content:
|
||||
# Decode base64 parts for proper text searching
|
||||
decoded_content = _decode_mime_content(content).lower()
|
||||
|
||||
# Teams meeting indicators
|
||||
if "microsoft teams meeting" in decoded_content:
|
||||
return True
|
||||
if "join the meeting" in decoded_content:
|
||||
return True
|
||||
# ICS content indicator (check raw content for MIME headers)
|
||||
if "text/calendar" in content.lower():
|
||||
return True
|
||||
# VCALENDAR block
|
||||
if "begin:vcalendar" in content.lower():
|
||||
return True
|
||||
|
||||
return False
|
||||
108
src/mail/screens/ConfirmDialog.py
Normal file
108
src/mail/screens/ConfirmDialog.py
Normal file
@@ -0,0 +1,108 @@
|
||||
"""Confirmation dialog screen for destructive actions."""
|
||||
|
||||
from textual import on
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Horizontal, Vertical, Container
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Button, Label, Static
|
||||
|
||||
|
||||
class ConfirmDialog(ModalScreen[bool]):
|
||||
"""A modal confirmation dialog that returns True if confirmed, False otherwise."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("enter", "confirm", "Confirm"),
|
||||
Binding("y", "confirm", "Yes"),
|
||||
Binding("n", "cancel", "No"),
|
||||
]
|
||||
|
||||
DEFAULT_CSS = """
|
||||
ConfirmDialog {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
ConfirmDialog #confirm-container {
|
||||
width: 50;
|
||||
height: auto;
|
||||
min-height: 7;
|
||||
background: $surface;
|
||||
border: thick $primary;
|
||||
padding: 1 2;
|
||||
}
|
||||
|
||||
ConfirmDialog #confirm-title {
|
||||
text-style: bold;
|
||||
width: 1fr;
|
||||
height: 1;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
ConfirmDialog #confirm-message {
|
||||
width: 1fr;
|
||||
height: 1;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
ConfirmDialog #confirm-buttons {
|
||||
width: 1fr;
|
||||
height: 3;
|
||||
align: center middle;
|
||||
margin-top: 1;
|
||||
}
|
||||
|
||||
ConfirmDialog Button {
|
||||
margin: 0 1;
|
||||
}
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
title: str = "Confirm",
|
||||
message: str = "Are you sure?",
|
||||
confirm_label: str = "Yes",
|
||||
cancel_label: str = "No",
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the confirmation dialog.
|
||||
|
||||
Args:
|
||||
title: The dialog title
|
||||
message: The confirmation message
|
||||
confirm_label: Label for the confirm button
|
||||
cancel_label: Label for the cancel button
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._title = title
|
||||
self._message = message
|
||||
self._confirm_label = confirm_label
|
||||
self._cancel_label = cancel_label
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Container(id="confirm-container"):
|
||||
yield Label(self._title, id="confirm-title")
|
||||
yield Label(self._message, id="confirm-message")
|
||||
with Horizontal(id="confirm-buttons"):
|
||||
yield Button(self._cancel_label, id="cancel", variant="default")
|
||||
yield Button(self._confirm_label, id="confirm", variant="error")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the cancel button by default (safer option)."""
|
||||
self.query_one("#cancel", Button).focus()
|
||||
|
||||
@on(Button.Pressed, "#confirm")
|
||||
def handle_confirm(self) -> None:
|
||||
self.dismiss(True)
|
||||
|
||||
@on(Button.Pressed, "#cancel")
|
||||
def handle_cancel(self) -> None:
|
||||
self.dismiss(False)
|
||||
|
||||
def action_confirm(self) -> None:
|
||||
self.dismiss(True)
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
self.dismiss(False)
|
||||
@@ -1,10 +1,12 @@
|
||||
import logging
|
||||
import asyncio
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Input, Label, Button, ListView, ListItem
|
||||
from textual.containers import Vertical, Horizontal, Container
|
||||
from textual.binding import Binding
|
||||
from textual import on, work
|
||||
from src.services.task_client import create_task, get_backend_info
|
||||
from src.utils.ipc import notify_refresh
|
||||
|
||||
|
||||
class CreateTaskScreen(ModalScreen):
|
||||
@@ -208,6 +210,8 @@ class CreateTaskScreen(ModalScreen):
|
||||
|
||||
if success:
|
||||
self.app.show_status(f"Task created: {subject}", "success")
|
||||
# Notify the tasks app to refresh
|
||||
asyncio.create_task(notify_refresh("tasks", {"source": "mail"}))
|
||||
self.dismiss()
|
||||
else:
|
||||
self.app.show_status(f"Failed to create task: {result}", "error")
|
||||
150
src/mail/screens/HelpScreen.py
Normal file
150
src/mail/screens/HelpScreen.py
Normal file
@@ -0,0 +1,150 @@
|
||||
"""Help screen modal for mail app."""
|
||||
|
||||
from textual.screen import Screen
|
||||
from textual.containers import Vertical, Horizontal, Center, ScrollableContainer
|
||||
from textual.widgets import Static, Button, Footer
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
|
||||
|
||||
class HelpScreen(Screen):
|
||||
"""Help screen showing all keyboard shortcuts and app information."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "pop_screen", "Close", show=False),
|
||||
Binding("q", "pop_screen", "Close", show=False),
|
||||
Binding("?", "pop_screen", "Close", show=False),
|
||||
]
|
||||
|
||||
def __init__(self, app_bindings: list[Binding], **kwargs):
|
||||
"""Initialize help screen with app bindings.
|
||||
|
||||
Args:
|
||||
app_bindings: List of bindings from the main app
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self.app_bindings = app_bindings
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Compose the help screen."""
|
||||
|
||||
with Vertical(id="help_container"):
|
||||
# Header
|
||||
yield Static(
|
||||
"╔══════════════════════════════════════════════════════════════════╗\n"
|
||||
"║" + " " * 68 + "║\n"
|
||||
"║" + " LUK Mail - Keyboard Shortcuts & Help".center(68) + " ║\n"
|
||||
"╚════════════════════════════════════════════════════════════════════╝"
|
||||
)
|
||||
|
||||
# Custom instructions section
|
||||
yield Static("", id="spacer_1")
|
||||
yield Static("[b cyan]Quick Actions[/b cyan]", id="instructions_title")
|
||||
yield Static("─" * 70, id="instructions_separator")
|
||||
yield Static("")
|
||||
yield Static(
|
||||
" The mail app automatically compresses notification emails from:"
|
||||
)
|
||||
yield Static(" • GitLab (pipelines, MRs, mentions)")
|
||||
yield Static(" • GitHub (PRs, issues, reviews)")
|
||||
yield Static(" • Jira (issues, status changes)")
|
||||
yield Static(" • Confluence (page updates, comments)")
|
||||
yield Static(" • Datadog (alerts, incidents)")
|
||||
yield Static(" • Renovate (dependency updates)")
|
||||
yield Static("")
|
||||
yield Static(
|
||||
" [yellow]Tip:[/yellow] Toggle between compressed and full view with [b]m[/b]"
|
||||
)
|
||||
yield Static("")
|
||||
|
||||
# Auto-generated keybindings section
|
||||
yield Static("", id="spacer_2")
|
||||
yield Static("[b cyan]Keyboard Shortcuts[/b cyan]", id="bindings_title")
|
||||
yield Static("─" * 70, id="bindings_separator")
|
||||
yield Static("")
|
||||
yield Static("[b green]Navigation[/b green]")
|
||||
yield Static(" j/k - Next/Previous message")
|
||||
yield Static(" g - Go to oldest message")
|
||||
yield Static(" G - Go to newest message")
|
||||
yield Static(" b - Scroll page up")
|
||||
yield Static(" PageDown/PageUp - Scroll page down/up")
|
||||
yield Static("")
|
||||
|
||||
yield Static("[b green]Message Actions[/b green]")
|
||||
yield Static(" o - Open message externally")
|
||||
yield Static(" # - Delete message(s)")
|
||||
yield Static(" e - Archive message(s)")
|
||||
yield Static(" u - Toggle read/unread")
|
||||
yield Static(" t - Create task from message")
|
||||
yield Static(" l - Show links in message")
|
||||
yield Static("")
|
||||
|
||||
yield Static("[b green]View Options[/b green]")
|
||||
yield Static(" w - Toggle message view window")
|
||||
yield Static(
|
||||
" m - Toggle markdown/html view (or compressed/html for notifications)"
|
||||
)
|
||||
yield Static(" h - Toggle full/compressed envelope headers")
|
||||
yield Static("")
|
||||
|
||||
yield Static("[b green]Search & Filter[/b green]")
|
||||
yield Static(" / - Search messages")
|
||||
yield Static(" s - Toggle sort order")
|
||||
yield Static(" x - Toggle selection mode")
|
||||
yield Static(" Space - Select/deselect message")
|
||||
yield Static(" Escape - Clear selection")
|
||||
yield Static("")
|
||||
|
||||
yield Static("[b green]Calendar Actions (when applicable)[/b green]")
|
||||
yield Static(" A - Accept invite")
|
||||
yield Static(" D - Decline invite")
|
||||
yield Static(" T - Tentative")
|
||||
yield Static("")
|
||||
|
||||
yield Static("[b green]Application[/b green]")
|
||||
yield Static(" r - Reload message list")
|
||||
yield Static(
|
||||
" 1-4 - Focus panel (Accounts, Folders, Messages, Content)"
|
||||
)
|
||||
yield Static(" q - Quit application")
|
||||
yield Static("")
|
||||
|
||||
# Notification compression section
|
||||
yield Static("", id="spacer_3")
|
||||
yield Static(
|
||||
"[b cyan]Notification Email Compression[/b cyan]",
|
||||
id="compression_title",
|
||||
)
|
||||
yield Static("─" * 70, id="compression_separator")
|
||||
yield Static("")
|
||||
yield Static(
|
||||
" Notification emails are automatically detected and compressed"
|
||||
)
|
||||
yield Static(" into terminal-friendly summaries showing:")
|
||||
yield Static(" • Notification type and icon")
|
||||
yield Static(" • Key details (ID, title, status)")
|
||||
yield Static(" • Action items")
|
||||
yield Static(" • Important links")
|
||||
yield Static("")
|
||||
yield Static(" [yellow]Configuration:[/yellow]")
|
||||
yield Static(" Edit ~/.config/luk/mail.toml to customize:")
|
||||
yield Static(" [dim]compress_notifications = true[/dim]")
|
||||
yield Static(" [dim]notification_compression_mode = 'summary'[/dim]")
|
||||
yield Static(" # Options: 'summary', 'detailed', 'off'")
|
||||
yield Static("")
|
||||
|
||||
# Footer
|
||||
yield Static("─" * 70, id="footer_separator")
|
||||
yield Static(
|
||||
"[dim]Press [b]ESC[/b], [b]q[/b], or [b]?[/b] to close this help screen[/dim]",
|
||||
id="footer_text",
|
||||
)
|
||||
|
||||
# Close button at bottom
|
||||
with Horizontal(id="button_container"):
|
||||
yield Button("Close", id="close_button", variant="primary")
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle button press to close help screen."""
|
||||
if event.button.id == "close_button":
|
||||
self.dismiss()
|
||||
@@ -13,7 +13,7 @@ from textual.containers import Container, Vertical
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Label, ListView, ListItem, Static
|
||||
|
||||
from src.maildir_gtd.config import get_config
|
||||
from src.mail.config import get_config
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -86,6 +86,9 @@ class LinkItem:
|
||||
- Keeping first and last path segments, eliding middle only if needed
|
||||
- Adapting to available width
|
||||
"""
|
||||
# Nerdfont chevron separator (nf-cod-chevron_right)
|
||||
sep = " \ueab6 "
|
||||
|
||||
# Special handling for common sites
|
||||
path = path.strip("/")
|
||||
|
||||
@@ -95,26 +98,26 @@ class LinkItem:
|
||||
if match:
|
||||
repo, type_, num = match.groups()
|
||||
icon = "#" if type_ == "issues" else "PR#"
|
||||
return f"{domain} > {repo} {icon}{num}"
|
||||
return f"{domain}{sep}{repo} {icon}{num}"
|
||||
|
||||
match = re.match(r"([^/]+/[^/]+)", path)
|
||||
if match:
|
||||
return f"{domain} > {match.group(1)}"
|
||||
return f"{domain}{sep}{match.group(1)}"
|
||||
|
||||
# Google Docs
|
||||
if "docs.google.com" in domain:
|
||||
if "/document/" in path:
|
||||
return f"{domain} > Document"
|
||||
return f"{domain}{sep}Document"
|
||||
if "/spreadsheets/" in path:
|
||||
return f"{domain} > Spreadsheet"
|
||||
return f"{domain}{sep}Spreadsheet"
|
||||
if "/presentation/" in path:
|
||||
return f"{domain} > Slides"
|
||||
return f"{domain}{sep}Slides"
|
||||
|
||||
# Jira/Atlassian
|
||||
if "atlassian.net" in domain or "jira" in domain.lower():
|
||||
match = re.search(r"([A-Z]+-\d+)", path)
|
||||
if match:
|
||||
return f"{domain} > {match.group(1)}"
|
||||
return f"{domain}{sep}{match.group(1)}"
|
||||
|
||||
# GitLab
|
||||
if "gitlab" in domain.lower():
|
||||
@@ -122,7 +125,7 @@ class LinkItem:
|
||||
if match:
|
||||
repo, type_, num = match.groups()
|
||||
icon = "#" if type_ == "issues" else "MR!"
|
||||
return f"{domain} > {repo} {icon}{num}"
|
||||
return f"{domain}{sep}{repo} {icon}{num}"
|
||||
|
||||
# Generic shortening - keep URL readable
|
||||
if len(url) <= max_len:
|
||||
@@ -136,31 +139,31 @@ class LinkItem:
|
||||
|
||||
# Try to fit the full path first
|
||||
full_path = "/".join(path_parts)
|
||||
result = f"{domain} > {full_path}"
|
||||
result = f"{domain}{sep}{full_path}"
|
||||
if len(result) <= max_len:
|
||||
return result
|
||||
|
||||
# Keep first segment + last two segments if possible
|
||||
if len(path_parts) >= 3:
|
||||
short_path = f"{path_parts[0]}/.../{path_parts[-2]}/{path_parts[-1]}"
|
||||
result = f"{domain} > {short_path}"
|
||||
result = f"{domain}{sep}{short_path}"
|
||||
if len(result) <= max_len:
|
||||
return result
|
||||
|
||||
# Keep first + last segment
|
||||
if len(path_parts) >= 2:
|
||||
short_path = f"{path_parts[0]}/.../{path_parts[-1]}"
|
||||
result = f"{domain} > {short_path}"
|
||||
result = f"{domain}{sep}{short_path}"
|
||||
if len(result) <= max_len:
|
||||
return result
|
||||
|
||||
# Just last segment
|
||||
result = f"{domain} > .../{path_parts[-1]}"
|
||||
result = f"{domain}{sep}.../{path_parts[-1]}"
|
||||
if len(result) <= max_len:
|
||||
return result
|
||||
|
||||
# Truncate with ellipsis as last resort
|
||||
result = f"{domain} > {path_parts[-1]}"
|
||||
result = f"{domain}{sep}{path_parts[-1]}"
|
||||
if len(result) > max_len:
|
||||
result = result[: max_len - 3] + "..."
|
||||
|
||||
@@ -411,6 +414,10 @@ class LinkPanel(ModalScreen):
|
||||
self._mnemonic_map: dict[str, LinkItem] = {
|
||||
link.mnemonic: link for link in links if link.mnemonic
|
||||
}
|
||||
self._key_buffer: str = ""
|
||||
self._key_timer = None
|
||||
# Check if we have any multi-char mnemonics
|
||||
self._has_multi_char = any(len(m) > 1 for m in self._mnemonic_map.keys())
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Container(id="link-panel-container"):
|
||||
@@ -436,18 +443,68 @@ class LinkPanel(ModalScreen):
|
||||
self.query_one("#link-list").focus()
|
||||
|
||||
def on_key(self, event) -> None:
|
||||
"""Handle mnemonic key presses."""
|
||||
"""Handle mnemonic key presses with buffering for multi-char mnemonics."""
|
||||
key = event.key.lower()
|
||||
|
||||
# Check for single-char mnemonic
|
||||
if key in self._mnemonic_map:
|
||||
self._open_link(self._mnemonic_map[key])
|
||||
# Only buffer alphabetic keys
|
||||
if not key.isalpha() or len(key) != 1:
|
||||
return
|
||||
|
||||
# Cancel any pending timer
|
||||
if self._key_timer:
|
||||
self._key_timer.stop()
|
||||
self._key_timer = None
|
||||
|
||||
# Add key to buffer
|
||||
self._key_buffer += key
|
||||
|
||||
# Check for exact match with buffered keys
|
||||
if self._key_buffer in self._mnemonic_map:
|
||||
# If no multi-char mnemonics exist, open immediately
|
||||
if not self._has_multi_char:
|
||||
self._open_link(self._mnemonic_map[self._key_buffer])
|
||||
self._key_buffer = ""
|
||||
event.prevent_default()
|
||||
return
|
||||
|
||||
# Check if any longer mnemonic starts with our buffer
|
||||
has_longer_match = any(
|
||||
m.startswith(self._key_buffer) and len(m) > len(self._key_buffer)
|
||||
for m in self._mnemonic_map.keys()
|
||||
)
|
||||
|
||||
if has_longer_match:
|
||||
# Wait for possible additional keys
|
||||
self._key_timer = self.set_timer(0.4, self._flush_key_buffer)
|
||||
else:
|
||||
# No longer matches possible, open immediately
|
||||
self._open_link(self._mnemonic_map[self._key_buffer])
|
||||
self._key_buffer = ""
|
||||
|
||||
event.prevent_default()
|
||||
return
|
||||
|
||||
# Check for two-char mnemonics (accumulate?)
|
||||
# For simplicity, we'll just support single-char for now
|
||||
# A more sophisticated approach would use a timeout buffer
|
||||
# Check if buffer could still match something
|
||||
could_match = any(
|
||||
m.startswith(self._key_buffer) for m in self._mnemonic_map.keys()
|
||||
)
|
||||
|
||||
if could_match:
|
||||
# Wait for more keys
|
||||
self._key_timer = self.set_timer(0.4, self._flush_key_buffer)
|
||||
event.prevent_default()
|
||||
else:
|
||||
# No possible match, clear buffer
|
||||
self._key_buffer = ""
|
||||
|
||||
def _flush_key_buffer(self) -> None:
|
||||
"""Called after timeout to process buffered keys."""
|
||||
self._key_timer = None
|
||||
|
||||
if self._key_buffer and self._key_buffer in self._mnemonic_map:
|
||||
self._open_link(self._mnemonic_map[self._key_buffer])
|
||||
|
||||
self._key_buffer = ""
|
||||
|
||||
def action_open_selected(self) -> None:
|
||||
"""Open the currently selected link."""
|
||||
491
src/mail/screens/SearchPanel.py
Normal file
491
src/mail/screens/SearchPanel.py
Normal file
@@ -0,0 +1,491 @@
|
||||
"""Docked search panel for mail app with live search.
|
||||
|
||||
Provides a search input docked to the top of the window with:
|
||||
- Live search with 1 second debounce
|
||||
- Cancel button to restore previous state
|
||||
- Help button showing Himalaya search syntax
|
||||
- Date picker for date/before/after keywords
|
||||
"""
|
||||
|
||||
from datetime import date
|
||||
from typing import Optional, List, Dict, Any
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.message import Message
|
||||
from textual.reactive import reactive
|
||||
from textual.screen import ModalScreen
|
||||
from textual.timer import Timer
|
||||
from textual.widget import Widget
|
||||
from textual.widgets import Button, Input, Label, Static
|
||||
from textual.suggester import SuggestFromList
|
||||
|
||||
from src.calendar.widgets.MonthCalendar import MonthCalendar
|
||||
|
||||
# Himalaya search keywords for autocomplete
|
||||
HIMALAYA_KEYWORDS = [
|
||||
"from ",
|
||||
"to ",
|
||||
"subject ",
|
||||
"body ",
|
||||
"date ",
|
||||
"before ",
|
||||
"after ",
|
||||
"flag ",
|
||||
"not ",
|
||||
"and ",
|
||||
"or ",
|
||||
"order by ",
|
||||
"order by date ",
|
||||
"order by date asc",
|
||||
"order by date desc",
|
||||
"order by from ",
|
||||
"order by to ",
|
||||
"order by subject ",
|
||||
"flag seen",
|
||||
"flag flagged",
|
||||
"not flag seen",
|
||||
]
|
||||
|
||||
HIMALAYA_SEARCH_HELP = """
|
||||
## Himalaya Search Query Syntax
|
||||
|
||||
A filter query is composed of operators and conditions:
|
||||
|
||||
### Operators
|
||||
- `not <condition>` - filter envelopes that do NOT match the condition
|
||||
- `<condition> and <condition>` - filter envelopes matching BOTH conditions
|
||||
- `<condition> or <condition>` - filter envelopes matching EITHER condition
|
||||
|
||||
### Conditions
|
||||
- `date <yyyy-mm-dd>` - match the given date
|
||||
- `before <yyyy-mm-dd>` - date strictly before the given date
|
||||
- `after <yyyy-mm-dd>` - date strictly after the given date
|
||||
- `from <pattern>` - senders matching the pattern
|
||||
- `to <pattern>` - recipients matching the pattern
|
||||
- `subject <pattern>` - subject matching the pattern
|
||||
- `body <pattern>` - text body matching the pattern
|
||||
- `flag <flag>` - envelopes with the given flag (e.g., `seen`, `flagged`)
|
||||
|
||||
### Examples
|
||||
- `from john` - emails from anyone named John
|
||||
- `subject meeting and after 2025-01-01` - meetings after Jan 1st
|
||||
- `not flag seen` - unread emails
|
||||
- `from boss or from manager` - emails from boss or manager
|
||||
- `body urgent and before 2025-12-01` - urgent emails before Dec 1st
|
||||
|
||||
### Sort Query
|
||||
Start with `order by`, followed by:
|
||||
- `date [asc|desc]`
|
||||
- `from [asc|desc]`
|
||||
- `to [asc|desc]`
|
||||
- `subject [asc|desc]`
|
||||
|
||||
Example: `from john order by date desc`
|
||||
""".strip()
|
||||
|
||||
|
||||
class SearchHelpModal(ModalScreen[None]):
|
||||
"""Modal showing Himalaya search syntax help."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
SearchHelpModal {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
SearchHelpModal > Vertical {
|
||||
width: 80;
|
||||
max-width: 90%;
|
||||
height: auto;
|
||||
max-height: 80%;
|
||||
border: solid $primary;
|
||||
background: $surface;
|
||||
padding: 1 2;
|
||||
}
|
||||
|
||||
SearchHelpModal > Vertical > Static {
|
||||
height: auto;
|
||||
max-height: 100%;
|
||||
}
|
||||
|
||||
SearchHelpModal > Vertical > Horizontal {
|
||||
height: auto;
|
||||
align: center middle;
|
||||
margin-top: 1;
|
||||
}
|
||||
"""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "close", "Close"),
|
||||
]
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
from textual.widgets import Markdown
|
||||
|
||||
with Vertical():
|
||||
yield Markdown(HIMALAYA_SEARCH_HELP)
|
||||
with Horizontal():
|
||||
yield Button("Close", variant="primary", id="close-btn")
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
if event.button.id == "close-btn":
|
||||
self.dismiss(None)
|
||||
|
||||
def action_close(self) -> None:
|
||||
self.dismiss(None)
|
||||
|
||||
|
||||
class DatePickerModal(ModalScreen[Optional[date]]):
|
||||
"""Modal with a calendar for selecting a date."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
DatePickerModal {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
DatePickerModal > Vertical {
|
||||
width: 30;
|
||||
height: auto;
|
||||
border: solid $primary;
|
||||
background: $surface;
|
||||
padding: 1 2;
|
||||
}
|
||||
|
||||
DatePickerModal > Vertical > Label {
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
DatePickerModal > Vertical > Horizontal {
|
||||
height: auto;
|
||||
align: center middle;
|
||||
margin-top: 1;
|
||||
}
|
||||
|
||||
DatePickerModal > Vertical > Horizontal > Button {
|
||||
margin: 0 1;
|
||||
}
|
||||
"""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("left", "prev_month", "Previous month", show=False),
|
||||
Binding("right", "next_month", "Next month", show=False),
|
||||
Binding("enter", "select_date", "Select date", show=False),
|
||||
]
|
||||
|
||||
def __init__(self, keyword: str = "date") -> None:
|
||||
super().__init__()
|
||||
self.keyword = keyword
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical():
|
||||
yield Label(f"Select date for '{self.keyword}':", id="picker-title")
|
||||
yield MonthCalendar(id="date-picker-calendar")
|
||||
with Horizontal():
|
||||
yield Button("Today", variant="default", id="today-btn")
|
||||
yield Button("Select", variant="primary", id="select-btn")
|
||||
yield Button("Cancel", variant="warning", id="cancel-btn")
|
||||
|
||||
def on_month_calendar_date_selected(
|
||||
self, event: MonthCalendar.DateSelected
|
||||
) -> None:
|
||||
"""Handle date selection from calendar click."""
|
||||
self.dismiss(event.date)
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
if event.button.id == "select-btn":
|
||||
calendar = self.query_one("#date-picker-calendar", MonthCalendar)
|
||||
self.dismiss(calendar.selected_date)
|
||||
elif event.button.id == "today-btn":
|
||||
calendar = self.query_one("#date-picker-calendar", MonthCalendar)
|
||||
today = date.today()
|
||||
calendar.selected_date = today
|
||||
calendar.display_month = today.replace(day=1)
|
||||
calendar.refresh()
|
||||
elif event.button.id == "cancel-btn":
|
||||
self.dismiss(None)
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
self.dismiss(None)
|
||||
|
||||
def action_prev_month(self) -> None:
|
||||
calendar = self.query_one("#date-picker-calendar", MonthCalendar)
|
||||
calendar.prev_month()
|
||||
|
||||
def action_next_month(self) -> None:
|
||||
calendar = self.query_one("#date-picker-calendar", MonthCalendar)
|
||||
calendar.next_month()
|
||||
|
||||
def action_select_date(self) -> None:
|
||||
calendar = self.query_one("#date-picker-calendar", MonthCalendar)
|
||||
self.dismiss(calendar.selected_date)
|
||||
|
||||
|
||||
class SearchPanel(Widget):
|
||||
"""Docked search panel with live search capability."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
SearchPanel {
|
||||
dock: top;
|
||||
height: auto;
|
||||
width: 100%;
|
||||
background: $surface;
|
||||
border-bottom: solid $primary;
|
||||
padding: 0 1;
|
||||
display: none;
|
||||
}
|
||||
|
||||
SearchPanel.visible {
|
||||
display: block;
|
||||
}
|
||||
|
||||
SearchPanel > Horizontal {
|
||||
height: 3;
|
||||
width: 100%;
|
||||
align: left middle;
|
||||
}
|
||||
|
||||
SearchPanel .search-label {
|
||||
width: auto;
|
||||
padding: 0 1;
|
||||
color: $primary;
|
||||
}
|
||||
|
||||
SearchPanel Input {
|
||||
width: 1fr;
|
||||
margin: 0 1;
|
||||
}
|
||||
|
||||
SearchPanel Button {
|
||||
width: auto;
|
||||
min-width: 8;
|
||||
margin: 0 0 0 1;
|
||||
}
|
||||
|
||||
SearchPanel .search-status {
|
||||
width: auto;
|
||||
padding: 0 1;
|
||||
color: $text-muted;
|
||||
}
|
||||
"""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel search", show=False),
|
||||
]
|
||||
|
||||
# Reactive to track search state
|
||||
is_searching: reactive[bool] = reactive(False)
|
||||
result_count: reactive[int] = reactive(-1) # -1 = no search yet
|
||||
|
||||
class SearchRequested(Message):
|
||||
"""Fired when a search should be performed."""
|
||||
|
||||
def __init__(self, query: str) -> None:
|
||||
super().__init__()
|
||||
self.query = query
|
||||
|
||||
class SearchCancelled(Message):
|
||||
"""Fired when the search is cancelled."""
|
||||
|
||||
pass
|
||||
|
||||
class SearchConfirmed(Message):
|
||||
"""Fired when user presses Enter to confirm search and focus results."""
|
||||
|
||||
def __init__(self, query: str) -> None:
|
||||
super().__init__()
|
||||
self.query = query
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
classes: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
self._debounce_timer: Optional[Timer] = None
|
||||
self._last_query: str = ""
|
||||
self._pending_date_keyword: Optional[str] = None # Track keyword awaiting date
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Horizontal():
|
||||
yield Label("Search:", classes="search-label")
|
||||
yield Input(
|
||||
placeholder="from <name> or subject <text> or body <text>...",
|
||||
id="search-input",
|
||||
suggester=SuggestFromList(HIMALAYA_KEYWORDS, case_sensitive=False),
|
||||
)
|
||||
yield Label("", classes="search-status", id="search-status")
|
||||
yield Button("?", variant="default", id="help-btn")
|
||||
yield Button("Cancel", variant="warning", id="cancel-btn")
|
||||
|
||||
def _has_suggestion(self) -> bool:
|
||||
"""Check if the search input currently has an autocomplete suggestion."""
|
||||
try:
|
||||
input_widget = self.query_one("#search-input", Input)
|
||||
return bool(input_widget._suggestion and input_widget._cursor_at_end)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _accept_suggestion(self) -> bool:
|
||||
"""Accept the current autocomplete suggestion if present. Returns True if accepted."""
|
||||
try:
|
||||
input_widget = self.query_one("#search-input", Input)
|
||||
if input_widget._suggestion and input_widget._cursor_at_end:
|
||||
input_widget.value = input_widget._suggestion
|
||||
input_widget.cursor_position = len(input_widget.value)
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
def on_key(self, event) -> None:
|
||||
"""Handle key events to intercept Tab for autocomplete."""
|
||||
if event.key == "tab":
|
||||
# Try to accept suggestion; if successful, prevent default tab behavior
|
||||
if self._accept_suggestion():
|
||||
event.prevent_default()
|
||||
event.stop()
|
||||
|
||||
def show(self, initial_query: str = "") -> None:
|
||||
"""Show the search panel and focus the input."""
|
||||
self.add_class("visible")
|
||||
input_widget = self.query_one("#search-input", Input)
|
||||
input_widget.value = initial_query
|
||||
self._last_query = initial_query
|
||||
input_widget.focus()
|
||||
|
||||
def hide(self) -> None:
|
||||
"""Hide the search panel."""
|
||||
self.remove_class("visible")
|
||||
self._cancel_debounce()
|
||||
self.result_count = -1
|
||||
|
||||
def focus_input(self) -> None:
|
||||
"""Focus the search input field."""
|
||||
input_widget = self.query_one("#search-input", Input)
|
||||
input_widget.focus()
|
||||
|
||||
@property
|
||||
def is_visible(self) -> bool:
|
||||
"""Check if the panel is visible."""
|
||||
return self.has_class("visible")
|
||||
|
||||
@property
|
||||
def search_query(self) -> str:
|
||||
"""Get the current search query."""
|
||||
return self.query_one("#search-input", Input).value
|
||||
|
||||
def _cancel_debounce(self) -> None:
|
||||
"""Cancel any pending debounced search."""
|
||||
if self._debounce_timer:
|
||||
self._debounce_timer.stop()
|
||||
self._debounce_timer = None
|
||||
|
||||
def _trigger_search(self) -> None:
|
||||
"""Trigger the actual search after debounce."""
|
||||
# Don't search if an autocomplete suggestion is visible
|
||||
if self._has_suggestion():
|
||||
return
|
||||
|
||||
query = self.query_one("#search-input", Input).value.strip()
|
||||
if query and query != self._last_query:
|
||||
self._last_query = query
|
||||
self.is_searching = True
|
||||
self.post_message(self.SearchRequested(query))
|
||||
|
||||
def _check_date_keyword(self, value: str) -> Optional[str]:
|
||||
"""Check if the input ends with a date keyword that needs a date picker.
|
||||
|
||||
Returns the keyword (date/before/after) if found, None otherwise.
|
||||
"""
|
||||
value_lower = value.lower()
|
||||
for keyword in ("date ", "before ", "after "):
|
||||
if value_lower.endswith(keyword):
|
||||
return keyword.strip()
|
||||
return None
|
||||
|
||||
def _show_date_picker(self, keyword: str) -> None:
|
||||
"""Show the date picker modal for the given keyword."""
|
||||
self._pending_date_keyword = keyword
|
||||
|
||||
def on_date_selected(selected_date: Optional[date]) -> None:
|
||||
if selected_date:
|
||||
# Insert the date into the search input
|
||||
input_widget = self.query_one("#search-input", Input)
|
||||
date_str = selected_date.strftime("%Y-%m-%d")
|
||||
input_widget.value = input_widget.value + date_str
|
||||
input_widget.cursor_position = len(input_widget.value)
|
||||
self._pending_date_keyword = None
|
||||
# Refocus the input
|
||||
self.query_one("#search-input", Input).focus()
|
||||
|
||||
self.app.push_screen(DatePickerModal(keyword), on_date_selected)
|
||||
|
||||
def on_input_changed(self, event: Input.Changed) -> None:
|
||||
"""Handle input changes with debounce."""
|
||||
if event.input.id != "search-input":
|
||||
return
|
||||
|
||||
# Cancel any existing timer
|
||||
self._cancel_debounce()
|
||||
|
||||
# Don't search empty queries
|
||||
if not event.value.strip():
|
||||
return
|
||||
|
||||
# Check for date keywords and show picker
|
||||
date_keyword = self._check_date_keyword(event.value)
|
||||
if date_keyword:
|
||||
self._show_date_picker(date_keyword)
|
||||
return
|
||||
|
||||
# Set up new debounce timer (1 second)
|
||||
self._debounce_timer = self.set_timer(1.0, self._trigger_search)
|
||||
|
||||
def on_input_submitted(self, event: Input.Submitted) -> None:
|
||||
"""Handle Enter key - trigger search immediately and confirm."""
|
||||
if event.input.id != "search-input":
|
||||
return
|
||||
|
||||
self._cancel_debounce()
|
||||
query = event.value.strip()
|
||||
if query:
|
||||
self._last_query = query
|
||||
self.is_searching = True
|
||||
self.post_message(self.SearchConfirmed(query))
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle button presses."""
|
||||
if event.button.id == "cancel-btn":
|
||||
self.action_cancel()
|
||||
elif event.button.id == "help-btn":
|
||||
self.app.push_screen(SearchHelpModal())
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
"""Cancel the search and restore previous state."""
|
||||
self._cancel_debounce()
|
||||
self.hide()
|
||||
self.post_message(self.SearchCancelled())
|
||||
|
||||
def update_status(self, count: int, searching: bool = False) -> None:
|
||||
"""Update the search status display."""
|
||||
self.is_searching = searching
|
||||
self.result_count = count
|
||||
|
||||
status = self.query_one("#search-status", Label)
|
||||
if searching:
|
||||
status.update("Searching...")
|
||||
elif count >= 0:
|
||||
status.update(f"{count} result{'s' if count != 1 else ''}")
|
||||
else:
|
||||
status.update("")
|
||||
|
||||
def watch_is_searching(self, searching: bool) -> None:
|
||||
"""Update UI when searching state changes."""
|
||||
status = self.query_one("#search-status", Label)
|
||||
if searching:
|
||||
status.update("Searching...")
|
||||
@@ -1,8 +1,11 @@
|
||||
# Initialize the screens package
|
||||
# Initialize screens package
|
||||
from .CreateTask import CreateTaskScreen
|
||||
from .OpenMessage import OpenMessageScreen
|
||||
from .DocumentViewer import DocumentViewerScreen
|
||||
from .LinkPanel import LinkPanel, LinkItem, extract_links_from_content
|
||||
from .ConfirmDialog import ConfirmDialog
|
||||
from .SearchPanel import SearchPanel, SearchHelpModal
|
||||
from .HelpScreen import HelpScreen
|
||||
|
||||
__all__ = [
|
||||
"CreateTaskScreen",
|
||||
@@ -11,4 +14,8 @@ __all__ = [
|
||||
"LinkPanel",
|
||||
"LinkItem",
|
||||
"extract_links_from_content",
|
||||
"ConfirmDialog",
|
||||
"SearchPanel",
|
||||
"SearchHelpModal",
|
||||
"HelpScreen",
|
||||
]
|
||||
16
src/mail/utils/__init__.py
Normal file
16
src/mail/utils/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""Mail utilities module."""
|
||||
|
||||
from .calendar_parser import (
|
||||
parse_calendar_part,
|
||||
parse_calendar_attachment,
|
||||
is_cancelled_event,
|
||||
is_event_request,
|
||||
ParsedCalendarEvent,
|
||||
)
|
||||
|
||||
from .apple_mail import (
|
||||
open_eml_in_apple_mail,
|
||||
compose_new_email,
|
||||
reply_to_email,
|
||||
forward_email,
|
||||
)
|
||||
255
src/mail/utils/apple_mail.py
Normal file
255
src/mail/utils/apple_mail.py
Normal file
@@ -0,0 +1,255 @@
|
||||
"""Apple Mail integration utilities.
|
||||
|
||||
Provides functions for opening emails in Apple Mail and optionally
|
||||
auto-sending them via AppleScript.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
from typing import Optional, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def open_eml_in_apple_mail(
|
||||
email_content: str,
|
||||
auto_send: bool = False,
|
||||
subject: str = "",
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
Open an email in Apple Mail, optionally auto-sending it.
|
||||
|
||||
Args:
|
||||
email_content: The raw email content (RFC 5322 format)
|
||||
auto_send: If True, automatically send the email after opening
|
||||
subject: Email subject for logging purposes
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
# Create a temp .eml file
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".eml", delete=False, encoding="utf-8"
|
||||
) as tmp:
|
||||
tmp.write(email_content)
|
||||
tmp_path = tmp.name
|
||||
|
||||
logger.info(f"Created temp .eml file: {tmp_path}")
|
||||
|
||||
# Open with Apple Mail
|
||||
result = subprocess.run(
|
||||
["open", "-a", "Mail", tmp_path], capture_output=True, text=True
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"Failed to open Mail: {result.stderr}")
|
||||
return False, f"Failed to open Mail: {result.stderr}"
|
||||
|
||||
if auto_send:
|
||||
# Wait for Mail to open the message
|
||||
time.sleep(1.5)
|
||||
|
||||
# Use AppleScript to send the frontmost message
|
||||
success, message = _applescript_send_frontmost_message()
|
||||
if success:
|
||||
logger.info(f"Auto-sent email: {subject}")
|
||||
# Clean up temp file after sending
|
||||
try:
|
||||
os.unlink(tmp_path)
|
||||
except OSError:
|
||||
pass
|
||||
return True, "Email sent successfully"
|
||||
else:
|
||||
logger.warning(
|
||||
f"Auto-send failed, email opened for manual sending: {message}"
|
||||
)
|
||||
return True, f"Email opened (auto-send failed: {message})"
|
||||
else:
|
||||
logger.info(f"Opened email in Mail for manual sending: {subject}")
|
||||
return True, "Email opened in Mail - please send manually"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error opening email in Apple Mail: {e}", exc_info=True)
|
||||
return False, f"Error: {str(e)}"
|
||||
|
||||
|
||||
def _applescript_send_frontmost_message() -> Tuple[bool, str]:
|
||||
"""
|
||||
Use AppleScript to send the frontmost message in Apple Mail.
|
||||
|
||||
When an .eml file is opened, Mail shows it as a "view" not a compose window.
|
||||
We need to use Message > Send Again to convert it to a compose window,
|
||||
then send it.
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
# AppleScript to:
|
||||
# 1. Activate Mail
|
||||
# 2. Use "Send Again" menu item to convert viewed message to compose
|
||||
# 3. Send the message with Cmd+Shift+D
|
||||
applescript = """
|
||||
tell application "Mail"
|
||||
activate
|
||||
delay 0.3
|
||||
end tell
|
||||
|
||||
tell application "System Events"
|
||||
tell process "Mail"
|
||||
-- First, trigger "Send Again" from Message menu to convert to compose window
|
||||
-- Menu: Message > Send Again (Cmd+Shift+D also works for this in some contexts)
|
||||
try
|
||||
click menu item "Send Again" of menu "Message" of menu bar 1
|
||||
delay 0.5
|
||||
on error
|
||||
-- If Send Again fails, window might already be a compose window
|
||||
end try
|
||||
|
||||
-- Now send the message with Cmd+Shift+D
|
||||
keystroke "d" using {command down, shift down}
|
||||
delay 0.3
|
||||
|
||||
return "sent"
|
||||
end tell
|
||||
end tell
|
||||
"""
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["osascript", "-e", applescript], capture_output=True, text=True, timeout=15
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
output = result.stdout.strip()
|
||||
if output == "sent":
|
||||
return True, "Message sent"
|
||||
else:
|
||||
return False, output
|
||||
else:
|
||||
return False, result.stderr.strip()
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
return False, "AppleScript timed out"
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
|
||||
def compose_new_email(
|
||||
to: str = "",
|
||||
subject: str = "",
|
||||
body: str = "",
|
||||
auto_send: bool = False,
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
Open a new compose window in Apple Mail using mailto: URL.
|
||||
|
||||
Args:
|
||||
to: Recipient email address
|
||||
subject: Email subject
|
||||
body: Email body text
|
||||
auto_send: Ignored - no AppleScript automation for compose
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
import urllib.parse
|
||||
|
||||
try:
|
||||
# Build mailto: URL
|
||||
params = {}
|
||||
if subject:
|
||||
params["subject"] = subject
|
||||
if body:
|
||||
params["body"] = body
|
||||
|
||||
query_string = urllib.parse.urlencode(params)
|
||||
mailto_url = f"mailto:{to}"
|
||||
if query_string:
|
||||
mailto_url += f"?{query_string}"
|
||||
|
||||
# Open mailto: URL - this will open the default mail client
|
||||
result = subprocess.run(["open", mailto_url], capture_output=True, text=True)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"Failed to open mailto: {result.stderr}")
|
||||
return False, f"Failed to open compose: {result.stderr}"
|
||||
|
||||
return True, "Compose window opened"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error composing email: {e}", exc_info=True)
|
||||
return False, str(e)
|
||||
|
||||
|
||||
def reply_to_email(
|
||||
original_message_path: str,
|
||||
reply_all: bool = False,
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
Open an email in Apple Mail for the user to manually reply.
|
||||
|
||||
This just opens the .eml file in Mail. The user can then use
|
||||
Mail's Reply button (Cmd+R) or Reply All (Cmd+Shift+R) themselves.
|
||||
|
||||
Args:
|
||||
original_message_path: Path to the original .eml file
|
||||
reply_all: Ignored - user will manually choose reply type
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
# Just open the message in Mail - no AppleScript automation
|
||||
result = subprocess.run(
|
||||
["open", "-a", "Mail", original_message_path],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
return False, f"Failed to open message: {result.stderr}"
|
||||
|
||||
reply_type = "Reply All" if reply_all else "Reply"
|
||||
return (
|
||||
True,
|
||||
f"Message opened - use {reply_type} (Cmd+{'Shift+' if reply_all else ''}R)",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error opening message for reply: {e}", exc_info=True)
|
||||
return False, str(e)
|
||||
|
||||
|
||||
def forward_email(original_message_path: str) -> Tuple[bool, str]:
|
||||
"""
|
||||
Open an email in Apple Mail for the user to manually forward.
|
||||
|
||||
This just opens the .eml file in Mail. The user can then use
|
||||
Mail's Forward button (Cmd+Shift+F) themselves.
|
||||
|
||||
Args:
|
||||
original_message_path: Path to the original .eml file
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
# Just open the message in Mail - no AppleScript automation
|
||||
result = subprocess.run(
|
||||
["open", "-a", "Mail", original_message_path],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
return False, f"Failed to open message: {result.stderr}"
|
||||
|
||||
return True, "Message opened - use Forward (Cmd+Shift+F)"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error opening message for forward: {e}", exc_info=True)
|
||||
return False, str(e)
|
||||
427
src/mail/utils/calendar_parser.py
Normal file
427
src/mail/utils/calendar_parser.py
Normal file
@@ -0,0 +1,427 @@
|
||||
"""Calendar ICS file parser utilities."""
|
||||
|
||||
import base64
|
||||
import re
|
||||
from typing import Optional, List
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
try:
|
||||
from icalendar import Calendar
|
||||
except ImportError:
|
||||
Calendar = None # type: ignore
|
||||
|
||||
|
||||
@dataclass
|
||||
class ParsedCalendarEvent:
|
||||
"""Parsed calendar event from ICS file."""
|
||||
|
||||
# Core event properties
|
||||
summary: Optional[str] = None
|
||||
location: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
start: Optional[datetime] = None
|
||||
end: Optional[datetime] = None
|
||||
all_day: bool = False
|
||||
|
||||
# Calendar method (REQUEST, CANCEL, REPLY, etc.)
|
||||
method: Optional[str] = None
|
||||
|
||||
# Organizer
|
||||
organizer_name: Optional[str] = None
|
||||
organizer_email: Optional[str] = None
|
||||
|
||||
# Attendees
|
||||
attendees: List[str] = field(default_factory=list)
|
||||
|
||||
# Status (CONFIRMED, TENTATIVE, CANCELLED)
|
||||
status: Optional[str] = None
|
||||
|
||||
# UID for matching with Graph API
|
||||
uid: Optional[str] = None
|
||||
|
||||
# Sequence number for iTIP REPLY
|
||||
sequence: int = 0
|
||||
|
||||
|
||||
def extract_ics_from_mime(raw_message: str) -> Optional[str]:
|
||||
"""Extract ICS calendar content from raw MIME message.
|
||||
|
||||
Looks for text/calendar parts and base64-decoded .ics attachments.
|
||||
|
||||
Args:
|
||||
raw_message: Full raw email in EML/MIME format
|
||||
|
||||
Returns:
|
||||
ICS content string if found, None otherwise
|
||||
"""
|
||||
# Pattern 1: Look for inline text/calendar content
|
||||
# Content-Type: text/calendar followed by the ICS content
|
||||
calendar_pattern = re.compile(
|
||||
r"Content-Type:\s*text/calendar[^\n]*\n"
|
||||
r"(?:Content-Transfer-Encoding:\s*(\w+)[^\n]*\n)?"
|
||||
r"(?:[^\n]+\n)*?" # Other headers
|
||||
r"\n" # Empty line before content
|
||||
r"(BEGIN:VCALENDAR.*?END:VCALENDAR)",
|
||||
re.DOTALL | re.IGNORECASE,
|
||||
)
|
||||
|
||||
match = calendar_pattern.search(raw_message)
|
||||
if match:
|
||||
encoding = match.group(1)
|
||||
ics_content = match.group(2)
|
||||
|
||||
if encoding and encoding.lower() == "base64":
|
||||
try:
|
||||
# Remove line breaks and decode
|
||||
ics_bytes = base64.b64decode(
|
||||
ics_content.replace("\n", "").replace("\r", "")
|
||||
)
|
||||
return ics_bytes.decode("utf-8", errors="replace")
|
||||
except Exception as e:
|
||||
logging.debug(f"Failed to decode base64 ICS: {e}")
|
||||
else:
|
||||
return ics_content
|
||||
|
||||
# Pattern 2: Look for base64-encoded text/calendar
|
||||
base64_pattern = re.compile(
|
||||
r"Content-Type:\s*text/calendar[^\n]*\n"
|
||||
r"Content-Transfer-Encoding:\s*base64[^\n]*\n"
|
||||
r"(?:[^\n]+\n)*?" # Other headers
|
||||
r"\n" # Empty line before content
|
||||
r"([A-Za-z0-9+/=\s]+)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
match = base64_pattern.search(raw_message)
|
||||
if match:
|
||||
try:
|
||||
b64_content = (
|
||||
match.group(1).replace("\n", "").replace("\r", "").replace(" ", "")
|
||||
)
|
||||
ics_bytes = base64.b64decode(b64_content)
|
||||
return ics_bytes.decode("utf-8", errors="replace")
|
||||
except Exception as e:
|
||||
logging.debug(f"Failed to decode base64 calendar: {e}")
|
||||
|
||||
# Pattern 3: Just look for raw VCALENDAR block
|
||||
vcal_pattern = re.compile(r"(BEGIN:VCALENDAR.*?END:VCALENDAR)", re.DOTALL)
|
||||
match = vcal_pattern.search(raw_message)
|
||||
if match:
|
||||
return match.group(1)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def parse_ics_content(ics_content: str) -> Optional[ParsedCalendarEvent]:
|
||||
"""Parse ICS calendar content into a ParsedCalendarEvent.
|
||||
|
||||
Args:
|
||||
ics_content: Raw ICS/iCalendar content string
|
||||
|
||||
Returns:
|
||||
ParsedCalendarEvent if parsing succeeded, None otherwise
|
||||
"""
|
||||
if Calendar is None:
|
||||
logging.warning("icalendar library not installed, cannot parse ICS")
|
||||
return None
|
||||
|
||||
try:
|
||||
# Handle bytes input
|
||||
if isinstance(ics_content, bytes):
|
||||
ics_content = ics_content.decode("utf-8", errors="replace")
|
||||
|
||||
calendar = Calendar.from_ical(ics_content)
|
||||
|
||||
# METHOD is a calendar-level property, not event-level
|
||||
method = str(calendar.get("method", "")).upper() or None
|
||||
|
||||
# Get first VEVENT component
|
||||
events = [c for c in calendar.walk() if c.name == "VEVENT"]
|
||||
if not events:
|
||||
logging.debug("No VEVENT found in calendar")
|
||||
return None
|
||||
|
||||
event = events[0]
|
||||
|
||||
# Extract organizer info
|
||||
organizer_name = None
|
||||
organizer_email = None
|
||||
organizer = event.get("organizer")
|
||||
if organizer:
|
||||
# Organizer can be a vCalAddress object
|
||||
organizer_name = (
|
||||
str(organizer.params.get("CN", ""))
|
||||
if hasattr(organizer, "params")
|
||||
else None
|
||||
)
|
||||
# Extract email from mailto: URI
|
||||
organizer_str = str(organizer)
|
||||
if organizer_str.lower().startswith("mailto:"):
|
||||
organizer_email = organizer_str[7:]
|
||||
else:
|
||||
organizer_email = organizer_str
|
||||
|
||||
# Extract attendees
|
||||
attendees = []
|
||||
attendee_list = event.get("attendee")
|
||||
if attendee_list:
|
||||
# Can be a single attendee or a list
|
||||
if not isinstance(attendee_list, list):
|
||||
attendee_list = [attendee_list]
|
||||
for att in attendee_list:
|
||||
att_str = str(att)
|
||||
if att_str.lower().startswith("mailto:"):
|
||||
att_email = att_str[7:]
|
||||
else:
|
||||
att_email = att_str
|
||||
att_name = (
|
||||
str(att.params.get("CN", "")) if hasattr(att, "params") else None
|
||||
)
|
||||
if att_name and att_email:
|
||||
attendees.append(f"{att_name} <{att_email}>")
|
||||
elif att_email:
|
||||
attendees.append(att_email)
|
||||
|
||||
# Extract start/end times
|
||||
start_dt = None
|
||||
end_dt = None
|
||||
all_day = False
|
||||
|
||||
dtstart = event.get("dtstart")
|
||||
if dtstart:
|
||||
dt_val = dtstart.dt
|
||||
if hasattr(dt_val, "hour"):
|
||||
start_dt = dt_val
|
||||
else:
|
||||
# Date only = all day event
|
||||
start_dt = dt_val
|
||||
all_day = True
|
||||
|
||||
dtend = event.get("dtend")
|
||||
if dtend:
|
||||
end_dt = dtend.dt
|
||||
|
||||
# Extract sequence number (defaults to 0)
|
||||
sequence = 0
|
||||
seq_val = event.get("sequence")
|
||||
if seq_val is not None:
|
||||
try:
|
||||
sequence = int(seq_val)
|
||||
except (ValueError, TypeError):
|
||||
sequence = 0
|
||||
|
||||
return ParsedCalendarEvent(
|
||||
summary=str(event.get("summary", "")) or None,
|
||||
location=str(event.get("location", "")) or None,
|
||||
description=str(event.get("description", "")) or None,
|
||||
start=start_dt,
|
||||
end=end_dt,
|
||||
all_day=all_day,
|
||||
method=method,
|
||||
organizer_name=organizer_name,
|
||||
organizer_email=organizer_email,
|
||||
attendees=attendees,
|
||||
status=str(event.get("status", "")).upper() or None,
|
||||
uid=str(event.get("uid", "")) or None,
|
||||
sequence=sequence,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error parsing calendar ICS: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def _decode_mime_text(raw_message: str) -> str:
|
||||
"""Decode base64 text parts from MIME message.
|
||||
|
||||
Args:
|
||||
raw_message: Raw MIME message
|
||||
|
||||
Returns:
|
||||
Decoded text content
|
||||
"""
|
||||
decoded_parts = []
|
||||
|
||||
# Find and decode base64 text parts
|
||||
b64_pattern = re.compile(
|
||||
r"Content-Type:\s*text/(?:plain|html)[^\n]*\n"
|
||||
r"(?:[^\n]+\n)*?"
|
||||
r"Content-Transfer-Encoding:\s*base64[^\n]*\n"
|
||||
r"(?:[^\n]+\n)*?"
|
||||
r"\n"
|
||||
r"([A-Za-z0-9+/=\s]+)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
for match in b64_pattern.finditer(raw_message):
|
||||
try:
|
||||
b64_content = (
|
||||
match.group(1).replace("\n", "").replace("\r", "").replace(" ", "")
|
||||
)
|
||||
decoded = base64.b64decode(b64_content).decode("utf-8", errors="replace")
|
||||
decoded_parts.append(decoded)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return "\n".join(decoded_parts) if decoded_parts else raw_message
|
||||
|
||||
|
||||
def extract_teams_meeting_info(raw_message: str) -> Optional[ParsedCalendarEvent]:
|
||||
"""Extract Teams meeting info from email body when no ICS is present.
|
||||
|
||||
This handles emails that contain Teams meeting details in the body
|
||||
but don't have an ICS calendar attachment.
|
||||
|
||||
Args:
|
||||
raw_message: Full raw email in EML/MIME format
|
||||
|
||||
Returns:
|
||||
ParsedCalendarEvent with Teams meeting info, or None if not a Teams meeting
|
||||
"""
|
||||
# Decode the message content
|
||||
content = _decode_mime_text(raw_message)
|
||||
content_lower = content.lower()
|
||||
|
||||
# Check if this is a Teams meeting email
|
||||
if (
|
||||
"microsoft teams" not in content_lower
|
||||
and "join the meeting" not in content_lower
|
||||
):
|
||||
return None
|
||||
|
||||
# Extract Teams meeting URL
|
||||
teams_url_pattern = re.compile(
|
||||
r"https://teams\.microsoft\.com/l/meetup-join/[^\s<>\"']+",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
teams_url_match = teams_url_pattern.search(content)
|
||||
teams_url = teams_url_match.group(0) if teams_url_match else None
|
||||
|
||||
# Extract meeting ID
|
||||
meeting_id_pattern = re.compile(r"Meeting ID:\s*([\d\s]+)", re.IGNORECASE)
|
||||
meeting_id_match = meeting_id_pattern.search(content)
|
||||
meeting_id = meeting_id_match.group(1).strip() if meeting_id_match else None
|
||||
|
||||
# Extract subject from email headers
|
||||
subject = None
|
||||
subject_match = re.search(
|
||||
r"^Subject:\s*(.+)$", raw_message, re.MULTILINE | re.IGNORECASE
|
||||
)
|
||||
if subject_match:
|
||||
subject = subject_match.group(1).strip()
|
||||
|
||||
# Extract organizer from From header
|
||||
organizer_email = None
|
||||
organizer_name = None
|
||||
from_match = re.search(r"^From:\s*(.+)$", raw_message, re.MULTILINE | re.IGNORECASE)
|
||||
if from_match:
|
||||
from_value = from_match.group(1).strip()
|
||||
# Parse "Name <email>" format
|
||||
email_match = re.search(r"<([^>]+)>", from_value)
|
||||
if email_match:
|
||||
organizer_email = email_match.group(1)
|
||||
organizer_name = from_value.split("<")[0].strip().strip('"')
|
||||
else:
|
||||
organizer_email = from_value
|
||||
|
||||
# Create location string with Teams info
|
||||
location = teams_url if teams_url else "Microsoft Teams Meeting"
|
||||
if meeting_id:
|
||||
location = f"Teams Meeting (ID: {meeting_id})"
|
||||
|
||||
return ParsedCalendarEvent(
|
||||
summary=subject or "Teams Meeting",
|
||||
location=location,
|
||||
description=f"Join: {teams_url}" if teams_url else None,
|
||||
method="TEAMS", # Custom method to indicate this is extracted, not from ICS
|
||||
organizer_name=organizer_name,
|
||||
organizer_email=organizer_email,
|
||||
)
|
||||
|
||||
|
||||
def parse_calendar_from_raw_message(raw_message: str) -> Optional[ParsedCalendarEvent]:
|
||||
"""Extract and parse calendar event from raw email message.
|
||||
|
||||
First tries to extract ICS content from the message. If no ICS is found,
|
||||
falls back to extracting Teams meeting info from the email body.
|
||||
|
||||
Args:
|
||||
raw_message: Full raw email in EML/MIME format
|
||||
|
||||
Returns:
|
||||
ParsedCalendarEvent if found and parsed, None otherwise
|
||||
"""
|
||||
# First try to extract ICS content
|
||||
ics_content = extract_ics_from_mime(raw_message)
|
||||
if ics_content:
|
||||
event = parse_ics_content(ics_content)
|
||||
if event:
|
||||
return event
|
||||
|
||||
# Fall back to extracting Teams meeting info from body
|
||||
return extract_teams_meeting_info(raw_message)
|
||||
|
||||
|
||||
# Legacy function names for compatibility
|
||||
def parse_calendar_part(content: str) -> Optional[ParsedCalendarEvent]:
|
||||
"""Parse calendar MIME part content. Legacy wrapper for parse_ics_content."""
|
||||
return parse_ics_content(content)
|
||||
|
||||
|
||||
def parse_calendar_attachment(attachment_content: str) -> Optional[ParsedCalendarEvent]:
|
||||
"""Parse base64-encoded calendar file attachment."""
|
||||
try:
|
||||
decoded = base64.b64decode(attachment_content)
|
||||
return parse_ics_content(decoded.decode("utf-8", errors="replace"))
|
||||
except Exception as e:
|
||||
logging.error(f"Error decoding calendar attachment: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def is_cancelled_event(event: ParsedCalendarEvent) -> bool:
|
||||
"""Check if event is a cancellation."""
|
||||
return event.method == "CANCEL" or event.status == "CANCELLED"
|
||||
|
||||
|
||||
def is_event_request(event: ParsedCalendarEvent) -> bool:
|
||||
"""Check if event is an invite request."""
|
||||
return event.method == "REQUEST"
|
||||
|
||||
|
||||
def format_event_time(event: ParsedCalendarEvent) -> str:
|
||||
"""Format event time for display.
|
||||
|
||||
Returns a human-readable string like:
|
||||
- "Mon, Dec 30, 2025 2:00 PM - 3:00 PM"
|
||||
- "All day: Mon, Dec 30, 2025"
|
||||
"""
|
||||
if not event.start:
|
||||
return "Time not specified"
|
||||
|
||||
if event.all_day:
|
||||
if hasattr(event.start, "strftime"):
|
||||
return f"All day: {event.start.strftime('%a, %b %d, %Y')}"
|
||||
return f"All day: {event.start}"
|
||||
|
||||
try:
|
||||
start_str = (
|
||||
event.start.strftime("%a, %b %d, %Y %I:%M %p")
|
||||
if hasattr(event.start, "strftime")
|
||||
else str(event.start)
|
||||
)
|
||||
if event.end and hasattr(event.end, "strftime"):
|
||||
# Same day? Just show end time
|
||||
if (
|
||||
hasattr(event.start, "date")
|
||||
and hasattr(event.end, "date")
|
||||
and event.start.date() == event.end.date()
|
||||
):
|
||||
end_str = event.end.strftime("%I:%M %p")
|
||||
else:
|
||||
end_str = event.end.strftime("%a, %b %d, %Y %I:%M %p")
|
||||
return f"{start_str} - {end_str}"
|
||||
return start_str
|
||||
except Exception:
|
||||
return str(event.start)
|
||||
219
src/mail/widgets/CalendarInvitePanel.py
Normal file
219
src/mail/widgets/CalendarInvitePanel.py
Normal file
@@ -0,0 +1,219 @@
|
||||
"""Calendar invite panel widget for displaying calendar event details with actions."""
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.widgets import Static, Button, Label
|
||||
from textual.reactive import reactive
|
||||
from textual.message import Message
|
||||
|
||||
from src.mail.utils.calendar_parser import (
|
||||
ParsedCalendarEvent,
|
||||
is_cancelled_event,
|
||||
is_event_request,
|
||||
format_event_time,
|
||||
)
|
||||
|
||||
|
||||
class CalendarInvitePanel(Vertical):
|
||||
"""Panel displaying calendar invite details with accept/decline/tentative actions.
|
||||
|
||||
This widget shows at the top of the ContentContainer when viewing a calendar email.
|
||||
"""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
CalendarInvitePanel {
|
||||
height: auto;
|
||||
max-height: 14;
|
||||
padding: 1;
|
||||
margin-bottom: 1;
|
||||
background: $surface;
|
||||
border: solid $primary;
|
||||
}
|
||||
|
||||
CalendarInvitePanel.cancelled {
|
||||
border: solid $error;
|
||||
}
|
||||
|
||||
CalendarInvitePanel.request {
|
||||
border: solid $success;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .event-badge {
|
||||
padding: 0 1;
|
||||
margin-right: 1;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .event-badge.cancelled {
|
||||
background: $error;
|
||||
color: $text;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .event-badge.request {
|
||||
background: $success;
|
||||
color: $text;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .event-badge.reply {
|
||||
background: $warning;
|
||||
color: $text;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .event-title {
|
||||
text-style: bold;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .event-detail {
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .action-buttons {
|
||||
height: auto;
|
||||
margin-top: 1;
|
||||
}
|
||||
|
||||
CalendarInvitePanel .action-buttons Button {
|
||||
margin-right: 1;
|
||||
}
|
||||
"""
|
||||
|
||||
class InviteAction(Message):
|
||||
"""Message sent when user takes an action on the invite."""
|
||||
|
||||
def __init__(self, action: str, event: ParsedCalendarEvent) -> None:
|
||||
self.action = action # "accept", "decline", "tentative"
|
||||
self.event = event
|
||||
super().__init__()
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
event: ParsedCalendarEvent,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.event = event
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Compose the calendar invite panel."""
|
||||
# Determine badge and styling based on method
|
||||
badge_text, badge_class = self._get_badge_info()
|
||||
|
||||
with Horizontal():
|
||||
yield Label(badge_text, classes=f"event-badge {badge_class}")
|
||||
yield Label(
|
||||
self.event.summary or "Calendar Event",
|
||||
classes="event-title",
|
||||
)
|
||||
|
||||
# Event time
|
||||
time_str = format_event_time(self.event)
|
||||
yield Static(f"\uf017 {time_str}", classes="event-detail") # nf-fa-clock_o
|
||||
|
||||
# Location if present
|
||||
if self.event.location:
|
||||
yield Static(
|
||||
f"\uf041 {self.event.location}", # nf-fa-map_marker
|
||||
classes="event-detail",
|
||||
)
|
||||
|
||||
# Organizer
|
||||
if self.event.organizer_name or self.event.organizer_email:
|
||||
organizer = self.event.organizer_name or self.event.organizer_email
|
||||
yield Static(
|
||||
f"\uf007 {organizer}", # nf-fa-user
|
||||
classes="event-detail",
|
||||
)
|
||||
|
||||
# Attendees count
|
||||
if self.event.attendees:
|
||||
count = len(self.event.attendees)
|
||||
yield Static(
|
||||
f"\uf0c0 {count} attendee{'s' if count != 1 else ''}", # nf-fa-users
|
||||
classes="event-detail",
|
||||
)
|
||||
|
||||
# Action buttons (only for REQUEST method, not for CANCEL or TEAMS)
|
||||
if is_event_request(self.event):
|
||||
with Horizontal(classes="action-buttons"):
|
||||
yield Button(
|
||||
"\uf00c Accept", # nf-fa-check
|
||||
id="btn-accept",
|
||||
variant="success",
|
||||
)
|
||||
yield Button(
|
||||
"? Tentative",
|
||||
id="btn-tentative",
|
||||
variant="warning",
|
||||
)
|
||||
yield Button(
|
||||
"\uf00d Decline", # nf-fa-times
|
||||
id="btn-decline",
|
||||
variant="error",
|
||||
)
|
||||
elif self.event.method == "TEAMS":
|
||||
# Teams meeting extracted from email body (no ICS)
|
||||
# Show join button if we have a URL in the description
|
||||
if self.event.description and "Join:" in self.event.description:
|
||||
with Horizontal(classes="action-buttons"):
|
||||
yield Button(
|
||||
"\uf0c1 Join Meeting", # nf-fa-link
|
||||
id="btn-join",
|
||||
variant="primary",
|
||||
)
|
||||
yield Static(
|
||||
"[dim]Teams meeting - no calendar invite attached[/dim]",
|
||||
classes="event-detail",
|
||||
)
|
||||
elif is_cancelled_event(self.event):
|
||||
yield Static(
|
||||
"[dim]This meeting has been cancelled[/dim]",
|
||||
classes="event-detail",
|
||||
)
|
||||
|
||||
def _get_badge_info(self) -> tuple[str, str]:
|
||||
"""Get badge text and CSS class based on event method."""
|
||||
method = self.event.method or ""
|
||||
|
||||
if method == "CANCEL" or self.event.status == "CANCELLED":
|
||||
return "CANCELLED", "cancelled"
|
||||
elif method == "REQUEST":
|
||||
return "INVITE", "request"
|
||||
elif method == "TEAMS":
|
||||
return "TEAMS", "request"
|
||||
elif method == "REPLY":
|
||||
return "REPLY", "reply"
|
||||
elif method == "COUNTER":
|
||||
return "COUNTER", "reply"
|
||||
else:
|
||||
return "EVENT", ""
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Apply styling based on event type."""
|
||||
if is_cancelled_event(self.event):
|
||||
self.add_class("cancelled")
|
||||
elif is_event_request(self.event):
|
||||
self.add_class("request")
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle action button presses."""
|
||||
button_id = event.button.id
|
||||
|
||||
if button_id == "btn-accept":
|
||||
self.post_message(self.InviteAction("accept", self.event))
|
||||
elif button_id == "btn-tentative":
|
||||
self.post_message(self.InviteAction("tentative", self.event))
|
||||
elif button_id == "btn-decline":
|
||||
self.post_message(self.InviteAction("decline", self.event))
|
||||
elif button_id == "btn-join":
|
||||
# Open Teams meeting URL
|
||||
if self.event.description and "Join:" in self.event.description:
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
url_match = re.search(
|
||||
r"Join:\s*(https://[^\s]+)", self.event.description
|
||||
)
|
||||
if url_match:
|
||||
url = url_match.group(1)
|
||||
subprocess.run(["open", url], capture_output=True)
|
||||
self.app.notify("Opening Teams meeting...", severity="information")
|
||||
946
src/mail/widgets/ContentContainer.py
Normal file
946
src/mail/widgets/ContentContainer.py
Normal file
@@ -0,0 +1,946 @@
|
||||
from markitdown import MarkItDown
|
||||
from textual import work
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Vertical, ScrollableContainer
|
||||
from textual.widgets import Static, Markdown, Label
|
||||
from textual.reactive import reactive
|
||||
from src.services.himalaya import client as himalaya_client
|
||||
from src.mail.config import get_config
|
||||
from src.mail.screens.LinkPanel import (
|
||||
extract_links_from_content,
|
||||
LinkItem,
|
||||
LinkItem as LinkItemClass,
|
||||
)
|
||||
from src.mail.notification_compressor import create_compressor
|
||||
from src.mail.notification_detector import NotificationType, is_calendar_email
|
||||
from src.mail.utils.calendar_parser import (
|
||||
ParsedCalendarEvent,
|
||||
parse_calendar_from_raw_message,
|
||||
parse_ics_content,
|
||||
)
|
||||
from src.mail.widgets.CalendarInvitePanel import CalendarInvitePanel
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Literal, List, Dict, Any, Optional
|
||||
from urllib.parse import urlparse
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Add the parent directory to the system path to resolve relative imports
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
def compress_urls_in_content(content: str, max_url_len: int = 50) -> str:
|
||||
"""Compress long URLs in markdown/text content for better readability.
|
||||
|
||||
Replaces long URLs with shortened versions using the same algorithm
|
||||
as LinkPanel._shorten_url. Preserves markdown link syntax.
|
||||
|
||||
Args:
|
||||
content: The markdown/text content to process
|
||||
max_url_len: Maximum length for displayed URLs (default 50)
|
||||
|
||||
Returns:
|
||||
Content with compressed URLs
|
||||
"""
|
||||
|
||||
# Pattern for markdown links: [text](url)
|
||||
def replace_md_link(match):
|
||||
anchor_text = match.group(1)
|
||||
url = match.group(2)
|
||||
|
||||
# Don't compress if URL is already short
|
||||
if len(url) <= max_url_len:
|
||||
return match.group(0)
|
||||
|
||||
# Use LinkItem's shortening algorithm
|
||||
short_url = LinkItemClass._shorten_url(
|
||||
url,
|
||||
urlparse(url).netloc.replace("www.", ""),
|
||||
urlparse(url).path,
|
||||
max_url_len,
|
||||
)
|
||||
|
||||
# Keep original anchor text, but if it's the same as URL, use short version
|
||||
if anchor_text == url or anchor_text.startswith("http"):
|
||||
return f"[\uf0c1 {short_url}]({url})"
|
||||
else:
|
||||
return match.group(0) # Keep original if anchor text is meaningful
|
||||
|
||||
# Pattern for bare URLs (not inside markdown links)
|
||||
def replace_bare_url(match):
|
||||
url = match.group(0)
|
||||
|
||||
# Don't compress if URL is already short
|
||||
if len(url) <= max_url_len:
|
||||
return url
|
||||
|
||||
parsed = urlparse(url)
|
||||
short_url = LinkItemClass._shorten_url(
|
||||
url, parsed.netloc.replace("www.", ""), parsed.path, max_url_len
|
||||
)
|
||||
|
||||
# Return as markdown link with icon
|
||||
return f"[\uf0c1 {short_url}]({url})"
|
||||
|
||||
# First, process markdown links
|
||||
md_link_pattern = r"\[([^\]]+)\]\((https?://[^)]+)\)"
|
||||
content = re.sub(md_link_pattern, replace_md_link, content)
|
||||
|
||||
# Then process bare URLs that aren't already in markdown links
|
||||
# This regex matches URLs not preceded by ]( which would indicate markdown link
|
||||
bare_url_pattern = r'(?<!\]\()https?://[^\s<>"\'\)]+[^\s<>"\'\.\,\)\]]'
|
||||
|
||||
# Use a more careful approach to avoid double-processing
|
||||
# Split content, process bare URLs, rejoin
|
||||
result = []
|
||||
last_end = 0
|
||||
|
||||
for match in re.finditer(bare_url_pattern, content):
|
||||
# Check if this URL is inside a markdown link (preceded by "](")
|
||||
prefix_start = max(0, match.start() - 2)
|
||||
prefix = content[prefix_start : match.start()]
|
||||
if prefix.endswith("]("):
|
||||
continue # Skip URLs that are already markdown link targets
|
||||
|
||||
result.append(content[last_end : match.start()])
|
||||
result.append(replace_bare_url(match))
|
||||
last_end = match.end()
|
||||
|
||||
result.append(content[last_end:])
|
||||
|
||||
return "".join(result)
|
||||
|
||||
|
||||
class EnvelopeHeader(ScrollableContainer):
|
||||
"""Email envelope header with compressible To/CC fields.
|
||||
|
||||
Scrollable when in full-headers mode to handle long recipient lists.
|
||||
"""
|
||||
|
||||
# Maximum recipients to show before truncating
|
||||
MAX_RECIPIENTS_SHOWN = 2
|
||||
# Show full headers when toggled
|
||||
show_full_headers: bool = False
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.subject_label = Label("", id="header_subject")
|
||||
self.from_label = Label("", id="header_from")
|
||||
self.to_label = Label("", id="header_to")
|
||||
self.date_label = Label("", id="header_date")
|
||||
self.cc_label = Label("", id="header_cc")
|
||||
# Store full values for toggle
|
||||
self._full_subject = ""
|
||||
self._full_to = ""
|
||||
self._full_cc = ""
|
||||
self._full_from = ""
|
||||
|
||||
def on_mount(self):
|
||||
self.mount(self.subject_label)
|
||||
self.mount(self.from_label)
|
||||
self.mount(self.to_label)
|
||||
self.mount(self.cc_label)
|
||||
self.mount(self.date_label)
|
||||
# Add bottom margin to subject for visual separation from metadata
|
||||
self.subject_label.styles.margin = (0, 0, 1, 0)
|
||||
# Hide CC label by default (shown when CC is present)
|
||||
self.cc_label.styles.display = "none"
|
||||
# Set initial placeholder content
|
||||
self.subject_label.update("[dim]Select a message to view[/dim]")
|
||||
self.from_label.update("[b]From:[/b] -")
|
||||
self.to_label.update("[b]To:[/b] -")
|
||||
self.date_label.update("[b]Date:[/b] -")
|
||||
|
||||
def _compress_recipients(self, recipients_str: str, max_shown: int = 2) -> str:
|
||||
"""Compress a list of recipients to a single line with truncation.
|
||||
|
||||
Args:
|
||||
recipients_str: Comma-separated list of recipients
|
||||
max_shown: Maximum number of recipients to show
|
||||
|
||||
Returns:
|
||||
Compressed string like "Alice, Bob... (+15 more)"
|
||||
"""
|
||||
if not recipients_str:
|
||||
return ""
|
||||
|
||||
# Split by comma, handling "Name <email>" format
|
||||
# Use regex to split on ", " only when not inside < >
|
||||
parts = []
|
||||
current = ""
|
||||
in_angle = False
|
||||
for char in recipients_str:
|
||||
if char == "<":
|
||||
in_angle = True
|
||||
elif char == ">":
|
||||
in_angle = False
|
||||
elif char == "," and not in_angle:
|
||||
if current.strip():
|
||||
parts.append(current.strip())
|
||||
current = ""
|
||||
continue
|
||||
current += char
|
||||
if current.strip():
|
||||
parts.append(current.strip())
|
||||
|
||||
total = len(parts)
|
||||
|
||||
if total <= max_shown:
|
||||
return recipients_str
|
||||
|
||||
# Extract short names from first few recipients
|
||||
short_names = []
|
||||
for part in parts[:max_shown]:
|
||||
# Handle "Last, First <email>" or just "email@example.com"
|
||||
if "<" in part:
|
||||
name = part.split("<")[0].strip()
|
||||
if name:
|
||||
# Get first name for brevity (handle "Last, First" format)
|
||||
if "," in name:
|
||||
# "Last, First" -> "First"
|
||||
name_parts = name.split(",")
|
||||
if len(name_parts) >= 2:
|
||||
name = name_parts[1].strip().split()[0]
|
||||
else:
|
||||
name = name_parts[0].strip()
|
||||
else:
|
||||
# "First Last" -> "First"
|
||||
name = name.split()[0]
|
||||
short_names.append(name)
|
||||
else:
|
||||
# No name, use email local part
|
||||
email = part.split("<")[1].rstrip(">").split("@")[0]
|
||||
short_names.append(email)
|
||||
else:
|
||||
# Just email address
|
||||
short_names.append(part.split("@")[0])
|
||||
|
||||
remaining = total - max_shown
|
||||
return f"{', '.join(short_names)}... (+{remaining} more)"
|
||||
|
||||
def toggle_full_headers(self) -> None:
|
||||
"""Toggle between compressed and full header view."""
|
||||
self.show_full_headers = not self.show_full_headers
|
||||
# Update CSS class for styling
|
||||
if self.show_full_headers:
|
||||
self.add_class("full-headers")
|
||||
else:
|
||||
self.remove_class("full-headers")
|
||||
self._refresh_display()
|
||||
|
||||
def _refresh_display(self) -> None:
|
||||
"""Refresh the display based on current mode."""
|
||||
if self.show_full_headers:
|
||||
# Full view - show complete text
|
||||
self.subject_label.update(
|
||||
f"[b bright_white]{self._full_subject}[/b bright_white]"
|
||||
)
|
||||
self.from_label.update(f"[b]From:[/b] {self._full_from}")
|
||||
self.to_label.update(f"[b]To:[/b] {self._full_to}")
|
||||
if self._full_cc:
|
||||
self.cc_label.update(f"[b]CC:[/b] {self._full_cc}")
|
||||
self.cc_label.styles.display = "block"
|
||||
else:
|
||||
# Compressed view - truncate for single line display
|
||||
self.subject_label.update(
|
||||
f"[b bright_white]{self._full_subject}[/b bright_white]"
|
||||
)
|
||||
self.from_label.update(
|
||||
f"[b]From:[/b] {self._compress_recipients(self._full_from, max_shown=1)}"
|
||||
)
|
||||
self.to_label.update(
|
||||
f"[b]To:[/b] {self._compress_recipients(self._full_to)}"
|
||||
)
|
||||
if self._full_cc:
|
||||
self.cc_label.update(
|
||||
f"[b]CC:[/b] {self._compress_recipients(self._full_cc)}"
|
||||
)
|
||||
self.cc_label.styles.display = "block"
|
||||
|
||||
def update(self, subject, from_, to, date, cc=None):
|
||||
# Store full values
|
||||
self._full_subject = subject or ""
|
||||
self._full_from = from_ or ""
|
||||
self._full_to = to or ""
|
||||
self._full_cc = cc or ""
|
||||
|
||||
# Format the date for better readability
|
||||
if date:
|
||||
try:
|
||||
# Try to convert the date string to a datetime object
|
||||
date_obj = datetime.fromisoformat(date.replace("Z", "+00:00"))
|
||||
formatted_date = date_obj.strftime("%a, %d %b %Y %H:%M:%S %Z")
|
||||
self.date_label.update(f"[b]Date:[/b] {formatted_date}")
|
||||
except (ValueError, TypeError):
|
||||
# If parsing fails, just use the original date string
|
||||
self.date_label.update(f"[b]Date:[/b] {date}")
|
||||
else:
|
||||
self.date_label.update("[b]Date:[/b] Unknown")
|
||||
|
||||
if not cc:
|
||||
self.cc_label.styles.display = "none"
|
||||
|
||||
# Apply current display mode
|
||||
self._refresh_display()
|
||||
|
||||
|
||||
class ContentContainer(Vertical):
|
||||
"""Container for displaying email content with toggleable view modes.
|
||||
|
||||
Uses a Vertical layout with:
|
||||
- EnvelopeHeader (fixed at top, non-scrolling)
|
||||
- ScrollableContainer for the message content (scrollable)
|
||||
"""
|
||||
|
||||
can_focus = True
|
||||
|
||||
# Reactive to track view mode and update UI
|
||||
current_mode: reactive[Literal["markdown", "html"]] = reactive("markdown")
|
||||
|
||||
BINDINGS = []
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.md = MarkItDown()
|
||||
self.header = EnvelopeHeader(id="envelope_header")
|
||||
self.scroll_container = ScrollableContainer(id="content_scroll")
|
||||
self.content = Markdown("", id="markdown_content")
|
||||
self.html_content = Static("", id="html_content", markup=False)
|
||||
self.current_content = None
|
||||
self.current_raw_content = None # Store original uncompressed content
|
||||
self.current_message_id = None
|
||||
self.current_folder: str | None = None
|
||||
self.current_account: str | None = None
|
||||
self.content_worker = None
|
||||
self.current_envelope: Optional[Dict[str, Any]] = None
|
||||
self.current_notification_type: Optional[NotificationType] = None
|
||||
self.is_compressed_view: bool = False
|
||||
self.compression_enabled: bool = True # Toggle for compression on/off
|
||||
|
||||
# Calendar invite state
|
||||
self.calendar_panel: Optional[CalendarInvitePanel] = None
|
||||
self.current_calendar_event: Optional[ParsedCalendarEvent] = None
|
||||
|
||||
# Load default view mode and notification compression from config
|
||||
config = get_config()
|
||||
self.current_mode = config.content_display.default_view_mode
|
||||
self.compressor = create_compressor(
|
||||
config.content_display.notification_compression_mode
|
||||
)
|
||||
|
||||
def compose(self):
|
||||
yield self.header
|
||||
with self.scroll_container:
|
||||
yield self.content
|
||||
yield self.html_content
|
||||
|
||||
def on_mount(self):
|
||||
# Set initial display based on config default
|
||||
self._apply_view_mode()
|
||||
self._update_mode_indicator()
|
||||
|
||||
def watch_current_mode(self, old_mode: str, new_mode: str) -> None:
|
||||
"""React to mode changes."""
|
||||
self._apply_view_mode()
|
||||
self._update_mode_indicator()
|
||||
|
||||
def _apply_view_mode(self) -> None:
|
||||
"""Apply the current view mode to widget visibility."""
|
||||
if self.current_mode == "markdown":
|
||||
self.html_content.styles.display = "none"
|
||||
self.content.styles.display = "block"
|
||||
else:
|
||||
self.content.styles.display = "none"
|
||||
self.html_content.styles.display = "block"
|
||||
|
||||
def _update_mode_indicator(self) -> None:
|
||||
"""Update the border subtitle to show current mode."""
|
||||
if self.current_mode == "markdown":
|
||||
if self.is_compressed_view:
|
||||
mode_label = "Compressed"
|
||||
mode_icon = "\uf066" # nf-fa-compress
|
||||
else:
|
||||
mode_label = "Markdown"
|
||||
mode_icon = "\ue73e" # nf-md-language_markdown
|
||||
else:
|
||||
mode_label = "HTML/Text"
|
||||
mode_icon = "\uf121" # nf-fa-code
|
||||
self.border_subtitle = f"{mode_icon} {mode_label}"
|
||||
|
||||
async def action_toggle_mode(self):
|
||||
"""Toggle between viewing modes.
|
||||
|
||||
For notification emails: cycles compressed → full markdown → HTML → compressed
|
||||
For regular emails: cycles between markdown and HTML.
|
||||
"""
|
||||
# Check if this is a compressible notification email
|
||||
is_notification = (
|
||||
self.compressor.mode != "off"
|
||||
and self.current_envelope
|
||||
and self.compressor.should_compress(self.current_envelope)
|
||||
)
|
||||
|
||||
if is_notification:
|
||||
# Three-state cycle for notifications: compressed → full → html → compressed
|
||||
if self.current_mode == "markdown" and self.compression_enabled:
|
||||
# Currently compressed markdown → show full markdown
|
||||
self.compression_enabled = False
|
||||
# Don't change mode, just re-display with compression off
|
||||
elif self.current_mode == "markdown" and not self.compression_enabled:
|
||||
# Currently full markdown → switch to HTML
|
||||
self.current_mode = "html"
|
||||
else:
|
||||
# Currently HTML → back to compressed markdown
|
||||
self.current_mode = "markdown"
|
||||
self.compression_enabled = True
|
||||
else:
|
||||
# Simple two-state toggle for regular emails
|
||||
if self.current_mode == "html":
|
||||
self.current_mode = "markdown"
|
||||
else:
|
||||
self.current_mode = "html"
|
||||
|
||||
# Re-display content with new mode/compression settings
|
||||
if self.current_raw_content is not None:
|
||||
# Use cached raw content instead of re-fetching
|
||||
self._update_content(self.current_raw_content)
|
||||
self._apply_view_mode()
|
||||
self._update_mode_indicator()
|
||||
elif self.current_message_id:
|
||||
# Fall back to re-fetching if no cached content
|
||||
self.display_content(
|
||||
self.current_message_id,
|
||||
folder=self.current_folder,
|
||||
account=self.current_account,
|
||||
envelope=self.current_envelope,
|
||||
)
|
||||
|
||||
def update_header(self, subject, from_, to, date, cc=None):
|
||||
self.header.update(subject, from_, to, date, cc)
|
||||
|
||||
@work(exclusive=True)
|
||||
async def fetch_message_content(self, message_id: int, format: str):
|
||||
"""Fetch message content using the Himalaya client module."""
|
||||
if not message_id:
|
||||
self.notify("No message ID provided.")
|
||||
return
|
||||
|
||||
# Always fetch raw message first to check for calendar data
|
||||
# This allows us to detect calendar invites even in forwarded emails
|
||||
raw_content, raw_success = await himalaya_client.get_raw_message(
|
||||
message_id, folder=self.current_folder, account=self.current_account
|
||||
)
|
||||
|
||||
# Check if this is a calendar email (using envelope + content for better detection)
|
||||
is_calendar = self.current_envelope and is_calendar_email(
|
||||
self.current_envelope, content=raw_content if raw_success else None
|
||||
)
|
||||
|
||||
calendar_event = None
|
||||
if is_calendar and raw_success and raw_content:
|
||||
calendar_event = parse_calendar_from_raw_message(raw_content)
|
||||
|
||||
# If attachments were skipped during sync, try to fetch ICS from Graph API
|
||||
# This handles both:
|
||||
# 1. TEAMS method (Teams meeting detected but no ICS in message)
|
||||
# 2. No calendar_event parsed but we detected calendar email patterns
|
||||
if "X-Attachments-Skipped" in raw_content:
|
||||
should_fetch_ics = (
|
||||
# No calendar event parsed at all
|
||||
calendar_event is None
|
||||
# Or we got a TEAMS fallback (no real ICS found)
|
||||
or calendar_event.method == "TEAMS"
|
||||
)
|
||||
|
||||
if should_fetch_ics:
|
||||
# Try to fetch ICS from Graph API
|
||||
ics_content = await self._fetch_ics_from_graph(raw_content)
|
||||
if ics_content:
|
||||
# Re-parse with the actual ICS
|
||||
real_event = parse_ics_content(ics_content)
|
||||
if real_event:
|
||||
calendar_event = real_event
|
||||
|
||||
if calendar_event:
|
||||
self._show_calendar_panel(calendar_event)
|
||||
else:
|
||||
self._hide_calendar_panel()
|
||||
else:
|
||||
self._hide_calendar_panel()
|
||||
|
||||
content, success = await himalaya_client.get_message_content(
|
||||
message_id, folder=self.current_folder, account=self.current_account
|
||||
)
|
||||
if success:
|
||||
self._update_content(content)
|
||||
else:
|
||||
self.notify(f"Failed to fetch content for message ID {message_id}.")
|
||||
|
||||
async def _fetch_ics_from_graph(self, raw_content: str) -> str | None:
|
||||
"""Fetch ICS attachment from Graph API using the message ID in headers.
|
||||
|
||||
Args:
|
||||
raw_content: Raw MIME content containing Message-ID header
|
||||
|
||||
Returns:
|
||||
ICS content string if found, None otherwise
|
||||
"""
|
||||
import re
|
||||
|
||||
# Extract Graph message ID from Message-ID header
|
||||
# Format: Message-ID: \n AAkALg...
|
||||
match = re.search(
|
||||
r"Message-ID:\s*\n?\s*([A-Za-z0-9+/=-]+)",
|
||||
raw_content,
|
||||
re.IGNORECASE,
|
||||
)
|
||||
if not match:
|
||||
return None
|
||||
|
||||
graph_message_id = match.group(1).strip()
|
||||
|
||||
try:
|
||||
# Get auth headers
|
||||
from src.services.microsoft_graph.auth import get_access_token
|
||||
from src.services.microsoft_graph.mail import fetch_message_ics_attachment
|
||||
|
||||
# Use Mail.Read scope for reading attachments
|
||||
scopes = ["https://graph.microsoft.com/Mail.Read"]
|
||||
token, _ = get_access_token(scopes)
|
||||
if not token:
|
||||
return None
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
ics_content, success = await fetch_message_ics_attachment(
|
||||
graph_message_id, headers
|
||||
)
|
||||
|
||||
if success and ics_content:
|
||||
return ics_content
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error fetching ICS from Graph: {e}")
|
||||
|
||||
return None
|
||||
|
||||
def display_content(
|
||||
self,
|
||||
message_id: int,
|
||||
folder: str | None = None,
|
||||
account: str | None = None,
|
||||
envelope: Optional[Dict[str, Any]] = None,
|
||||
) -> None:
|
||||
"""Display the content of a message."""
|
||||
if not message_id:
|
||||
return
|
||||
|
||||
self.current_message_id = message_id
|
||||
self.current_folder = folder
|
||||
self.current_account = account
|
||||
self.current_envelope = envelope
|
||||
|
||||
# Reset compression state for new message (start with compression enabled)
|
||||
self.compression_enabled = True
|
||||
self.current_raw_content = None
|
||||
|
||||
# Update the header with envelope data
|
||||
if envelope:
|
||||
subject = envelope.get("subject", "")
|
||||
|
||||
# Extract from - can be dict with name/addr or string
|
||||
from_info = envelope.get("from", {})
|
||||
if isinstance(from_info, dict):
|
||||
from_name = from_info.get("name") or ""
|
||||
from_addr = from_info.get("addr") or ""
|
||||
if from_name and from_addr:
|
||||
from_str = f"{from_name} <{from_addr}>"
|
||||
elif from_name:
|
||||
from_str = from_name
|
||||
else:
|
||||
from_str = from_addr
|
||||
else:
|
||||
from_str = str(from_info)
|
||||
|
||||
# Extract to - can be dict, list of dicts, or string
|
||||
to_info = envelope.get("to", {})
|
||||
to_str = self._format_recipients(to_info)
|
||||
|
||||
# Extract cc - can be dict, list of dicts, or string
|
||||
cc_info = envelope.get("cc", {})
|
||||
cc_str = self._format_recipients(cc_info) if cc_info else None
|
||||
|
||||
date = envelope.get("date", "")
|
||||
|
||||
self.header.update(subject, from_str, to_str, date, cc_str)
|
||||
|
||||
# Immediately show a loading message
|
||||
if self.current_mode == "markdown":
|
||||
self.content.update("Loading...")
|
||||
else:
|
||||
self.html_content.update("Loading...")
|
||||
|
||||
# Cancel any existing content fetch operations
|
||||
if self.content_worker:
|
||||
self.content_worker.cancel()
|
||||
|
||||
# Fetch content in the current mode
|
||||
format_type = "text" if self.current_mode == "markdown" else "html"
|
||||
self.content_worker = self.fetch_message_content(message_id, format_type)
|
||||
|
||||
def _strip_headers_from_content(self, content: str) -> str:
|
||||
"""Strip email headers and multipart MIME noise from content.
|
||||
|
||||
Email content from himalaya may include:
|
||||
1. Headers at the top (From, To, Subject, etc.) - shown in EnvelopeHeader
|
||||
2. Additional full headers after a blank line (Received, etc.)
|
||||
3. MIME multipart boundaries and part headers
|
||||
4. Base64 encoded content (attachments, calendar data)
|
||||
|
||||
This extracts just the readable plain text content.
|
||||
"""
|
||||
lines = content.split("\n")
|
||||
result_lines = []
|
||||
in_base64_block = False
|
||||
in_calendar_block = False
|
||||
in_header_block = True # Start assuming we're in headers
|
||||
|
||||
# Common email header patterns (case insensitive)
|
||||
header_pattern = re.compile(
|
||||
r"^(From|To|Subject|Date|CC|BCC|Reply-To|Message-ID|Received|"
|
||||
r"Content-Type|Content-Transfer-Encoding|Content-Disposition|"
|
||||
r"Content-Language|MIME-Version|Thread-Topic|Thread-Index|"
|
||||
r"Importance|X-Priority|Accept-Language|X-MS-|x-ms-|"
|
||||
r"x-microsoft-|x-forefront-|authentication-results).*:",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
line = lines[i]
|
||||
stripped = line.strip()
|
||||
|
||||
# Skip MIME boundary lines (--boundary or --boundary--)
|
||||
if stripped.startswith("--") and len(stripped) > 10:
|
||||
in_base64_block = False
|
||||
in_header_block = False # After boundary, might be content
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# Check for Content-Type to detect base64/calendar sections
|
||||
if stripped.lower().startswith("content-type:"):
|
||||
if (
|
||||
"base64" in stripped.lower() or "base64" in lines[i + 1].lower()
|
||||
if i + 1 < len(lines)
|
||||
else False
|
||||
):
|
||||
in_base64_block = True
|
||||
if "text/calendar" in stripped.lower():
|
||||
in_calendar_block = True
|
||||
# Skip this header and any continuation lines
|
||||
i += 1
|
||||
while i < len(lines) and (
|
||||
lines[i].startswith(" ") or lines[i].startswith("\t")
|
||||
):
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# Skip Content-Transfer-Encoding header
|
||||
if stripped.lower().startswith("content-transfer-encoding:"):
|
||||
if "base64" in stripped.lower():
|
||||
in_base64_block = True
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# Skip email headers (matches header pattern)
|
||||
if header_pattern.match(line):
|
||||
# Skip this header and any continuation lines
|
||||
i += 1
|
||||
while i < len(lines) and (
|
||||
lines[i].startswith(" ") or lines[i].startswith("\t")
|
||||
):
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# Blank line - could be end of headers or part separator
|
||||
if stripped == "":
|
||||
# If we haven't collected any content yet, keep skipping
|
||||
if not result_lines:
|
||||
i += 1
|
||||
continue
|
||||
# Otherwise keep the blank line (paragraph separator in body)
|
||||
result_lines.append(line)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# Detect and skip base64 encoded blocks
|
||||
if in_base64_block:
|
||||
# Check if line looks like base64 (long string of base64 chars)
|
||||
if len(stripped) > 20 and re.match(r"^[A-Za-z0-9+/=]+$", stripped):
|
||||
i += 1
|
||||
continue
|
||||
else:
|
||||
# End of base64 block
|
||||
in_base64_block = False
|
||||
|
||||
# Skip calendar/ICS content (BEGIN:VCALENDAR to END:VCALENDAR)
|
||||
if stripped.startswith("BEGIN:VCALENDAR"):
|
||||
in_calendar_block = True
|
||||
i += 1
|
||||
continue
|
||||
if in_calendar_block:
|
||||
if stripped.startswith("END:VCALENDAR"):
|
||||
in_calendar_block = False
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# This looks like actual content - add it
|
||||
result_lines.append(line)
|
||||
i += 1
|
||||
|
||||
return "\n".join(result_lines).strip()
|
||||
|
||||
return "\n".join(result_lines).strip()
|
||||
|
||||
def _format_recipients(self, recipients_info) -> str:
|
||||
"""Format recipients info (dict, list of dicts, or string) to a string."""
|
||||
if not recipients_info:
|
||||
return ""
|
||||
|
||||
if isinstance(recipients_info, str):
|
||||
return recipients_info
|
||||
|
||||
if isinstance(recipients_info, dict):
|
||||
# Single recipient
|
||||
name = recipients_info.get("name") or ""
|
||||
addr = recipients_info.get("addr") or ""
|
||||
if name and addr:
|
||||
return f"{name} <{addr}>"
|
||||
elif name:
|
||||
return name
|
||||
else:
|
||||
return addr
|
||||
|
||||
if isinstance(recipients_info, list):
|
||||
# Multiple recipients
|
||||
parts = []
|
||||
for r in recipients_info:
|
||||
if isinstance(r, dict):
|
||||
name = r.get("name") or ""
|
||||
addr = r.get("addr") or ""
|
||||
if name and addr:
|
||||
parts.append(f"{name} <{addr}>")
|
||||
elif name:
|
||||
parts.append(name)
|
||||
elif addr:
|
||||
parts.append(addr)
|
||||
elif isinstance(r, str):
|
||||
parts.append(r)
|
||||
return ", ".join(parts)
|
||||
|
||||
return str(recipients_info)
|
||||
|
||||
def clear_content(self) -> None:
|
||||
"""Clear the message content display."""
|
||||
self.content.update("")
|
||||
self.html_content.update("")
|
||||
self.current_content = None
|
||||
self.current_message_id = None
|
||||
self.border_title = "No message selected"
|
||||
|
||||
def _parse_headers_from_content(self, content: str) -> Dict[str, str]:
|
||||
"""Parse email headers from message content.
|
||||
|
||||
Returns a dict with keys: from, to, subject, date, cc
|
||||
"""
|
||||
headers = {}
|
||||
lines = content.split("\n")
|
||||
current_header = None
|
||||
current_value = ""
|
||||
|
||||
for line in lines:
|
||||
# Blank line marks end of headers
|
||||
if line.strip() == "":
|
||||
if current_header:
|
||||
headers[current_header] = current_value.strip()
|
||||
break
|
||||
|
||||
# Check for header continuation (line starts with whitespace)
|
||||
if line.startswith(" ") or line.startswith("\t"):
|
||||
if current_header:
|
||||
current_value += " " + line.strip()
|
||||
continue
|
||||
|
||||
# Save previous header if any
|
||||
if current_header:
|
||||
headers[current_header] = current_value.strip()
|
||||
|
||||
# Parse new header
|
||||
if ":" in line:
|
||||
header_name, _, value = line.partition(":")
|
||||
header_lower = header_name.lower().strip()
|
||||
if header_lower in ("from", "to", "subject", "date", "cc"):
|
||||
current_header = header_lower
|
||||
current_value = value.strip()
|
||||
else:
|
||||
current_header = None
|
||||
else:
|
||||
# Line doesn't look like a header, we've reached body
|
||||
break
|
||||
|
||||
return headers
|
||||
|
||||
def _update_content(self, content: str | None) -> None:
|
||||
"""Update the content widgets with the fetched content."""
|
||||
if content is None:
|
||||
content = "(No content)"
|
||||
|
||||
# Parse headers from content to update the EnvelopeHeader
|
||||
# (himalaya envelope list doesn't include full To/CC info)
|
||||
parsed_headers = self._parse_headers_from_content(content)
|
||||
if parsed_headers:
|
||||
# Update header with parsed values, falling back to envelope data
|
||||
subject = parsed_headers.get("subject") or (
|
||||
self.current_envelope.get("subject", "")
|
||||
if self.current_envelope
|
||||
else ""
|
||||
)
|
||||
from_str = parsed_headers.get("from") or ""
|
||||
to_str = parsed_headers.get("to") or ""
|
||||
date_str = parsed_headers.get("date") or (
|
||||
self.current_envelope.get("date", "") if self.current_envelope else ""
|
||||
)
|
||||
cc_str = parsed_headers.get("cc") or None
|
||||
|
||||
self.header.update(subject, from_str, to_str, date_str, cc_str)
|
||||
|
||||
# Strip headers from content (they're shown in EnvelopeHeader)
|
||||
content = self._strip_headers_from_content(content)
|
||||
|
||||
# Store the raw content for link extraction and for toggle mode
|
||||
self.current_content = content
|
||||
self.current_raw_content = content # Keep original for mode toggling
|
||||
|
||||
# Apply notification compression if enabled AND compression toggle is on
|
||||
display_content = content
|
||||
if (
|
||||
self.compressor.mode != "off"
|
||||
and self.current_envelope
|
||||
and self.compression_enabled
|
||||
):
|
||||
compressed_content, notif_type = self.compressor.compress(
|
||||
content, self.current_envelope
|
||||
)
|
||||
self.current_notification_type = notif_type
|
||||
if notif_type is not None:
|
||||
# Only use compressed content if compression was actually applied
|
||||
display_content = compressed_content
|
||||
self.is_compressed_view = True
|
||||
else:
|
||||
self.is_compressed_view = False
|
||||
else:
|
||||
self.current_notification_type = None
|
||||
self.is_compressed_view = False
|
||||
|
||||
# Get URL compression settings from config
|
||||
config = get_config()
|
||||
compress_urls = config.content_display.compress_urls
|
||||
max_url_len = config.content_display.max_url_length
|
||||
|
||||
try:
|
||||
if self.current_mode == "markdown":
|
||||
# For markdown mode, use the Markdown widget
|
||||
final_content = display_content
|
||||
if compress_urls and not self.is_compressed_view:
|
||||
# Don't compress URLs in notification summaries (they're already formatted)
|
||||
final_content = compress_urls_in_content(
|
||||
display_content, max_url_len
|
||||
)
|
||||
self.content.update(final_content)
|
||||
else:
|
||||
# For HTML mode, use the Static widget with markup
|
||||
# First, try to extract the body content if it's HTML
|
||||
body_match = re.search(
|
||||
r"<body[^>]*>(.*?)</body>", content, re.DOTALL | re.IGNORECASE
|
||||
)
|
||||
if body_match:
|
||||
content = body_match.group(1)
|
||||
|
||||
# Replace some common HTML elements with Textual markup
|
||||
content = content.replace("<b>", "[b]").replace("</b>", "[/b]")
|
||||
content = content.replace("<i>", "[i]").replace("</i>", "[/i]")
|
||||
content = content.replace("<u>", "[u]").replace("</u>", "[/u]")
|
||||
|
||||
# Convert links to a readable format
|
||||
content = re.sub(
|
||||
r'<a href="([^"]+)"[^>]*>([^<]+)</a>', r"[\2](\1)", content
|
||||
)
|
||||
|
||||
# Add CSS for better readability
|
||||
self.html_content.update(content)
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error updating content: {e}")
|
||||
if self.current_mode == "markdown":
|
||||
self.content.update(f"Error displaying content: {e}")
|
||||
else:
|
||||
self.html_content.update(f"Error displaying content: {e}")
|
||||
|
||||
def get_links(self) -> List[LinkItem]:
|
||||
"""Extract and return links from the current message content."""
|
||||
if not self.current_content:
|
||||
return []
|
||||
return extract_links_from_content(self.current_content)
|
||||
|
||||
def _show_calendar_panel(self, event: ParsedCalendarEvent) -> None:
|
||||
"""Show the calendar invite panel at the top of the scrollable content."""
|
||||
# Remove existing panel if any
|
||||
self._hide_calendar_panel()
|
||||
|
||||
# Store the calendar event for RSVP actions
|
||||
self.current_calendar_event = event
|
||||
|
||||
# Create and mount new panel at the beginning of the scroll container
|
||||
# Don't use a fixed ID to avoid DuplicateIds errors when panels are
|
||||
# removed asynchronously
|
||||
self.calendar_panel = CalendarInvitePanel(event)
|
||||
self.scroll_container.mount(self.calendar_panel, before=0)
|
||||
|
||||
def _hide_calendar_panel(self) -> None:
|
||||
"""Hide/remove the calendar invite panel."""
|
||||
self.current_calendar_event = None
|
||||
|
||||
# Remove the panel via instance reference (more reliable than ID query)
|
||||
if self.calendar_panel is not None:
|
||||
try:
|
||||
self.calendar_panel.remove()
|
||||
except Exception:
|
||||
pass # Already removed or not mounted
|
||||
self.calendar_panel = None
|
||||
|
||||
# Also remove any orphaned CalendarInvitePanel widgets
|
||||
try:
|
||||
for panel in self.query(CalendarInvitePanel):
|
||||
panel.remove()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def on_calendar_invite_panel_invite_action(
|
||||
self, event: CalendarInvitePanel.InviteAction
|
||||
) -> None:
|
||||
"""Handle calendar invite actions from the panel.
|
||||
|
||||
Bubbles the action up to the app level for processing.
|
||||
"""
|
||||
# Get the app and call the appropriate action
|
||||
action = event.action
|
||||
calendar_event = event.event
|
||||
|
||||
if action == "accept":
|
||||
self.app.action_accept_invite()
|
||||
elif action == "decline":
|
||||
self.app.action_decline_invite()
|
||||
elif action == "tentative":
|
||||
self.app.action_tentative_invite()
|
||||
@@ -7,7 +7,7 @@ from textual.app import ComposeResult
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.widgets import Label, Static
|
||||
|
||||
from src.maildir_gtd.config import EnvelopeDisplayConfig, get_config
|
||||
from src.mail.config import EnvelopeDisplayConfig, get_config
|
||||
|
||||
|
||||
class EnvelopeListItem(Static):
|
||||
@@ -44,28 +44,29 @@ class EnvelopeListItem(Static):
|
||||
|
||||
EnvelopeListItem .status-icon {
|
||||
width: 2;
|
||||
padding: 0 1 0 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
EnvelopeListItem .checkbox {
|
||||
width: 2;
|
||||
padding: 0 1 0 0;
|
||||
width: 1;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
EnvelopeListItem .sender-name {
|
||||
width: 1fr;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
EnvelopeListItem .message-datetime {
|
||||
width: auto;
|
||||
padding: 0 1;
|
||||
color: $text-muted;
|
||||
color: $text-disabled;
|
||||
}
|
||||
|
||||
EnvelopeListItem .email-subject {
|
||||
width: 1fr;
|
||||
padding: 0 3;
|
||||
text-style: bold;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
EnvelopeListItem .email-preview {
|
||||
@@ -76,10 +77,16 @@ class EnvelopeListItem(Static):
|
||||
|
||||
EnvelopeListItem.unread .sender-name {
|
||||
text-style: bold;
|
||||
color: $text;
|
||||
}
|
||||
|
||||
EnvelopeListItem.unread .message-datetime {
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
EnvelopeListItem.unread .email-subject {
|
||||
text-style: bold;
|
||||
color: $text;
|
||||
}
|
||||
"""
|
||||
|
||||
@@ -147,6 +154,9 @@ class EnvelopeListItem(Static):
|
||||
date_str = date_str.replace("Z", "+00:00")
|
||||
dt = datetime.fromisoformat(date_str)
|
||||
|
||||
# Convert to local timezone
|
||||
dt = dt.astimezone()
|
||||
|
||||
parts = []
|
||||
if self.config.show_date:
|
||||
parts.append(dt.strftime(self.config.date_format))
|
||||
@@ -1 +0,0 @@
|
||||
# Initialize the maildir_gtd package
|
||||
@@ -1,649 +0,0 @@
|
||||
from .config import get_config, MaildirGTDConfig
|
||||
from .message_store import MessageStore
|
||||
from .widgets.ContentContainer import ContentContainer
|
||||
from .widgets.EnvelopeListItem import EnvelopeListItem, GroupHeader
|
||||
from .screens.LinkPanel import LinkPanel
|
||||
from .actions.task import action_create_task
|
||||
from .actions.open import action_open
|
||||
from .actions.delete import delete_current
|
||||
from src.services.taskwarrior import client as taskwarrior_client
|
||||
from src.services.himalaya import client as himalaya_client
|
||||
from textual.containers import Container, ScrollableContainer, Vertical, Horizontal
|
||||
from textual.timer import Timer
|
||||
from textual.binding import Binding
|
||||
from textual.reactive import reactive, Reactive
|
||||
from textual.widgets import Footer, Static, Label, Markdown, ListView, ListItem
|
||||
from textual.screen import Screen
|
||||
from textual.logging import TextualHandler
|
||||
from textual.app import App, ComposeResult, SystemCommand, RenderResult
|
||||
from textual.worker import Worker
|
||||
from textual import work
|
||||
import sys
|
||||
import os
|
||||
from datetime import UTC, datetime
|
||||
import logging
|
||||
from typing import Iterable, Optional, List, Dict, Any, Generator, Tuple
|
||||
|
||||
# Add the parent directory to the system path to resolve relative imports
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
# Import our new API modules
|
||||
|
||||
# Updated imports with correct relative paths
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
level="NOTSET",
|
||||
handlers=[TextualHandler()],
|
||||
)
|
||||
|
||||
|
||||
class StatusTitle(Static):
|
||||
total_messages: Reactive[int] = reactive(0)
|
||||
current_message_index: Reactive[int] = reactive(0)
|
||||
current_message_id: Reactive[int] = reactive(1)
|
||||
folder: Reactive[str] = reactive("INBOX")
|
||||
|
||||
def render(self) -> RenderResult:
|
||||
return f"{self.folder} | ID: {self.current_message_id} | [b]{self.current_message_index}[/b]/{self.total_messages}"
|
||||
|
||||
|
||||
class EmailViewerApp(App):
|
||||
"""A simple email viewer app using the Himalaya CLI."""
|
||||
|
||||
CSS_PATH = "email_viewer.tcss"
|
||||
title = "Maildir GTD Reader"
|
||||
current_message_id: Reactive[int] = reactive(0)
|
||||
current_message_index: Reactive[int] = reactive(0)
|
||||
highlighted_message_index: Reactive[int] = reactive(0)
|
||||
folder = reactive("INBOX")
|
||||
header_expanded = reactive(False)
|
||||
reload_needed = reactive(True)
|
||||
message_store = MessageStore()
|
||||
oldest_id: Reactive[int] = reactive(0)
|
||||
newest_id: Reactive[int] = reactive(0)
|
||||
msg_worker: Worker | None = None
|
||||
total_messages: Reactive[int] = reactive(0)
|
||||
status_title = reactive("Message View")
|
||||
sort_order_ascending: Reactive[bool] = reactive(True)
|
||||
selected_messages: Reactive[set[int]] = reactive(set())
|
||||
main_content_visible: Reactive[bool] = reactive(True)
|
||||
|
||||
def get_system_commands(self, screen: Screen) -> Iterable[SystemCommand]:
|
||||
yield from super().get_system_commands(screen)
|
||||
yield SystemCommand("Next Message", "Navigate to Next ID", self.action_next)
|
||||
yield SystemCommand(
|
||||
"Previous Message", "Navigate to Previous ID", self.action_previous
|
||||
)
|
||||
yield SystemCommand(
|
||||
"Delete Message", "Delete the current message", self.action_delete
|
||||
)
|
||||
yield SystemCommand(
|
||||
"Archive Message", "Archive the current message", self.action_archive
|
||||
)
|
||||
yield SystemCommand(
|
||||
"Open Message", "Open a specific message by ID", self.action_open
|
||||
)
|
||||
yield SystemCommand(
|
||||
"Create Task", "Create a task using the task CLI", self.action_create_task
|
||||
)
|
||||
yield SystemCommand(
|
||||
"Oldest Message", "Show the oldest message", self.action_oldest
|
||||
)
|
||||
yield SystemCommand(
|
||||
"Newest Message", "Show the newest message", self.action_newest
|
||||
)
|
||||
yield SystemCommand("Reload", "Reload the message list", self.fetch_envelopes)
|
||||
|
||||
BINDINGS = [
|
||||
Binding("j", "next", "Next message"),
|
||||
Binding("k", "previous", "Previous message"),
|
||||
Binding("#", "delete", "Delete message"),
|
||||
Binding("e", "archive", "Archive message"),
|
||||
Binding("o", "open", "Open message", show=False),
|
||||
Binding("q", "quit", "Quit application"),
|
||||
Binding("h", "toggle_header", "Toggle Envelope Header"),
|
||||
Binding("t", "create_task", "Create Task"),
|
||||
Binding("l", "open_links", "Show Links"),
|
||||
Binding("%", "reload", "Reload message list"),
|
||||
Binding("1", "focus_1", "Focus Accounts Panel"),
|
||||
Binding("2", "focus_2", "Focus Folders Panel"),
|
||||
Binding("3", "focus_3", "Focus Envelopes Panel"),
|
||||
Binding("4", "focus_4", "Focus Main Content"),
|
||||
Binding("w", "toggle_main_content", "Toggle Message View Window"),
|
||||
]
|
||||
|
||||
BINDINGS.extend(
|
||||
[
|
||||
Binding("space", "scroll_page_down", "Scroll page down"),
|
||||
Binding("b", "scroll_page_up", "Scroll page up"),
|
||||
Binding("s", "toggle_sort_order", "Toggle Sort Order"),
|
||||
Binding("x", "toggle_selection", "Toggle selection"),
|
||||
Binding("escape", "clear_selection", "Clear selection"),
|
||||
]
|
||||
)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Horizontal(
|
||||
Vertical(
|
||||
ListView(
|
||||
ListItem(Label("All emails...")),
|
||||
id="envelopes_list",
|
||||
classes="list_view",
|
||||
initial_index=0,
|
||||
),
|
||||
ListView(id="accounts_list", classes="list_view"),
|
||||
ListView(id="folders_list", classes="list_view"),
|
||||
id="sidebar",
|
||||
),
|
||||
ContentContainer(id="main_content"),
|
||||
id="outer-wrapper",
|
||||
)
|
||||
yield Footer()
|
||||
|
||||
async def on_mount(self) -> None:
|
||||
self.alert_timer: Timer | None = None # Timer to throttle alerts
|
||||
self.theme = "monokai"
|
||||
self.title = "MaildirGTD"
|
||||
self.query_one("#main_content").border_title = self.status_title
|
||||
sort_indicator = "↑" if self.sort_order_ascending else "↓"
|
||||
self.query_one("#envelopes_list").border_title = f"1️⃣ Emails {sort_indicator}"
|
||||
self.query_one("#accounts_list").border_title = "2️⃣ Accounts"
|
||||
|
||||
self.query_one("#folders_list").border_title = "3️⃣ Folders"
|
||||
|
||||
self.fetch_accounts()
|
||||
self.fetch_folders()
|
||||
worker = self.fetch_envelopes()
|
||||
await worker.wait()
|
||||
self.query_one("#envelopes_list").focus()
|
||||
self.action_oldest()
|
||||
|
||||
def compute_status_title(self):
|
||||
metadata = self.message_store.get_metadata(self.current_message_id)
|
||||
message_date = metadata["date"] if metadata else "N/A"
|
||||
return f"✉️ Message ID: {self.current_message_id} | Date: {message_date}"
|
||||
|
||||
def watch_status_title(self, old_status_title: str, new_status_title: str) -> None:
|
||||
self.query_one(ContentContainer).border_title = new_status_title
|
||||
|
||||
def watch_sort_order_ascending(self, old_value: bool, new_value: bool) -> None:
|
||||
"""Update the border title of the envelopes list when the sort order changes."""
|
||||
sort_indicator = "↑" if new_value else "↓"
|
||||
self.query_one("#envelopes_list").border_title = f"1️⃣ Emails {sort_indicator}"
|
||||
|
||||
def watch_current_message_index(self, old_index: int, new_index: int) -> None:
|
||||
if new_index < 0:
|
||||
new_index = 0
|
||||
self.current_message_index = new_index
|
||||
if new_index > self.total_messages:
|
||||
new_index = self.total_messages
|
||||
self.current_message_index = new_index
|
||||
|
||||
self._update_list_view_subtitle()
|
||||
self.query_one("#envelopes_list", ListView).index = new_index
|
||||
|
||||
def watch_selected_messages(
|
||||
self, old_messages: set[int], new_messages: set[int]
|
||||
) -> None:
|
||||
self._update_list_view_subtitle()
|
||||
|
||||
def _update_list_view_subtitle(self) -> None:
|
||||
subtitle = f"[b]{self.current_message_index}[/b]/{self.total_messages}"
|
||||
if self.selected_messages:
|
||||
subtitle = f"(✓{len(self.selected_messages)}) {subtitle}"
|
||||
self.query_one("#envelopes_list").border_subtitle = subtitle
|
||||
|
||||
def watch_total_messages(self, old_total: int, new_total: int) -> None:
|
||||
"""Called when the total_messages reactive attribute changes."""
|
||||
self._update_list_view_subtitle()
|
||||
|
||||
def watch_reload_needed(
|
||||
self, old_reload_needed: bool, new_reload_needed: bool
|
||||
) -> None:
|
||||
logging.info(f"Reload needed: {new_reload_needed}")
|
||||
if not old_reload_needed and new_reload_needed:
|
||||
self.fetch_envelopes()
|
||||
|
||||
def watch_current_message_id(
|
||||
self, old_message_id: int, new_message_id: int
|
||||
) -> None:
|
||||
"""Called when the current message ID changes."""
|
||||
logging.info(
|
||||
f"Current message ID changed from {old_message_id} to {new_message_id}"
|
||||
)
|
||||
if new_message_id == old_message_id:
|
||||
return
|
||||
|
||||
# If the main content view is not visible, don't load the message
|
||||
if not self.main_content_visible:
|
||||
return
|
||||
|
||||
# Cancel any existing message loading worker
|
||||
if self.msg_worker:
|
||||
self.msg_worker.cancel()
|
||||
|
||||
# Start a new worker to load the message content
|
||||
self.msg_worker = self.load_message_content(new_message_id)
|
||||
|
||||
@work(exclusive=True)
|
||||
async def load_message_content(self, message_id: int) -> None:
|
||||
"""Worker to load message content asynchronously."""
|
||||
content_container = self.query_one(ContentContainer)
|
||||
content_container.display_content(message_id)
|
||||
|
||||
metadata = self.message_store.get_metadata(message_id)
|
||||
if metadata:
|
||||
message_date = metadata["date"]
|
||||
if self.current_message_index != metadata["index"]:
|
||||
self.current_message_index = metadata["index"]
|
||||
|
||||
list_view = self.query_one("#envelopes_list", ListView)
|
||||
if list_view.index != metadata["index"]:
|
||||
list_view.index = metadata["index"]
|
||||
else:
|
||||
logging.warning(f"Message ID {message_id} not found in metadata.")
|
||||
|
||||
def on_list_view_selected(self, event: ListView.Selected) -> None:
|
||||
"""Called when an item in the list view is selected."""
|
||||
if event.list_view.index is None:
|
||||
return
|
||||
|
||||
selected_index = event.list_view.index
|
||||
|
||||
current_item = self.message_store.envelopes[selected_index]
|
||||
|
||||
if current_item is None or current_item.get("type") == "header":
|
||||
return
|
||||
|
||||
message_id = int(current_item["id"])
|
||||
self.current_message_id = message_id
|
||||
self.current_message_index = selected_index
|
||||
|
||||
def on_list_view_highlighted(self, event: ListView.Highlighted) -> None:
|
||||
"""Called when an item in the list view is highlighted (e.g., via arrow keys)."""
|
||||
if event.list_view.index is None:
|
||||
return
|
||||
|
||||
highlighted_index = event.list_view.index
|
||||
|
||||
current_item = self.message_store.envelopes[highlighted_index]
|
||||
|
||||
if current_item is None or current_item.get("type") == "header":
|
||||
return
|
||||
|
||||
# message_id = int(current_item["id"])
|
||||
# self.current_message_id = message_id
|
||||
self.highlighted_message_index = highlighted_index
|
||||
|
||||
@work(exclusive=False)
|
||||
async def fetch_envelopes(self) -> None:
|
||||
msglist = self.query_one("#envelopes_list", ListView)
|
||||
try:
|
||||
msglist.loading = True
|
||||
|
||||
# Use the Himalaya client to fetch envelopes
|
||||
envelopes, success = await himalaya_client.list_envelopes()
|
||||
|
||||
if success:
|
||||
self.reload_needed = False
|
||||
# Ensure envelopes is a list, even if it's None from the client
|
||||
envelopes_list = envelopes if envelopes is not None else []
|
||||
self.message_store.load(envelopes_list, self.sort_order_ascending)
|
||||
self.total_messages = self.message_store.total_messages
|
||||
|
||||
# Use the centralized refresh method to update the ListView
|
||||
self._populate_list_view()
|
||||
|
||||
# Restore the current index
|
||||
msglist.index = self.current_message_index
|
||||
else:
|
||||
self.show_status("Failed to fetch envelopes.", "error")
|
||||
except Exception as e:
|
||||
self.show_status(f"Error fetching message list: {e}", "error")
|
||||
finally:
|
||||
msglist.loading = False
|
||||
|
||||
@work(exclusive=False)
|
||||
async def fetch_accounts(self) -> None:
|
||||
accounts_list = self.query_one("#accounts_list", ListView)
|
||||
try:
|
||||
accounts_list.loading = True
|
||||
|
||||
# Use the Himalaya client to fetch accounts
|
||||
accounts, success = await himalaya_client.list_accounts()
|
||||
|
||||
if success and accounts:
|
||||
for account in accounts:
|
||||
item = ListItem(
|
||||
Label(
|
||||
str(account["name"]).strip(),
|
||||
classes="account_name",
|
||||
markup=False,
|
||||
)
|
||||
)
|
||||
accounts_list.append(item)
|
||||
else:
|
||||
self.show_status("Failed to fetch accounts.", "error")
|
||||
except Exception as e:
|
||||
self.show_status(f"Error fetching account list: {e}", "error")
|
||||
finally:
|
||||
accounts_list.loading = False
|
||||
|
||||
@work(exclusive=False)
|
||||
async def fetch_folders(self) -> None:
|
||||
folders_list = self.query_one("#folders_list", ListView)
|
||||
folders_list.clear()
|
||||
folders_list.append(
|
||||
ListItem(Label("INBOX", classes="folder_name", markup=False))
|
||||
)
|
||||
try:
|
||||
folders_list.loading = True
|
||||
|
||||
# Use the Himalaya client to fetch folders
|
||||
folders, success = await himalaya_client.list_folders()
|
||||
|
||||
if success and folders:
|
||||
for folder in folders:
|
||||
item = ListItem(
|
||||
Label(
|
||||
str(folder["name"]).strip(),
|
||||
classes="folder_name",
|
||||
markup=False,
|
||||
)
|
||||
)
|
||||
folders_list.append(item)
|
||||
else:
|
||||
self.show_status("Failed to fetch folders.", "error")
|
||||
except Exception as e:
|
||||
self.show_status(f"Error fetching folder list: {e}", "error")
|
||||
finally:
|
||||
folders_list.loading = False
|
||||
|
||||
def _populate_list_view(self) -> None:
|
||||
"""Populate the ListView with new items using the new EnvelopeListItem widget."""
|
||||
envelopes_list = self.query_one("#envelopes_list", ListView)
|
||||
envelopes_list.clear()
|
||||
|
||||
config = get_config()
|
||||
|
||||
for item in self.message_store.envelopes:
|
||||
if item and item.get("type") == "header":
|
||||
# Use the new GroupHeader widget for date groupings
|
||||
envelopes_list.append(ListItem(GroupHeader(label=item["label"])))
|
||||
elif item:
|
||||
# Use the new EnvelopeListItem widget
|
||||
message_id = int(item.get("id", 0))
|
||||
is_selected = message_id in self.selected_messages
|
||||
envelope_widget = EnvelopeListItem(
|
||||
envelope=item,
|
||||
config=config.envelope_display,
|
||||
is_selected=is_selected,
|
||||
)
|
||||
envelopes_list.append(ListItem(envelope_widget))
|
||||
|
||||
self.refresh_list_view_items() # Initial refresh of item states
|
||||
|
||||
def refresh_list_view_items(self) -> None:
|
||||
"""Update the visual state of existing ListItems without clearing the list."""
|
||||
envelopes_list = self.query_one("#envelopes_list", ListView)
|
||||
for i, list_item in enumerate(envelopes_list.children):
|
||||
if isinstance(list_item, ListItem):
|
||||
item_data = self.message_store.envelopes[i]
|
||||
|
||||
if item_data and item_data.get("type") != "header":
|
||||
message_id = int(item_data["id"])
|
||||
is_selected = message_id in self.selected_messages or False
|
||||
list_item.set_class(is_selected, "selection")
|
||||
|
||||
# Try to update the EnvelopeListItem's selection state
|
||||
try:
|
||||
envelope_widget = list_item.query_one(EnvelopeListItem)
|
||||
envelope_widget.set_selected(is_selected)
|
||||
except Exception:
|
||||
pass # Widget may not exist or be of old type
|
||||
|
||||
def show_message(self, message_id: int, new_index=None) -> None:
|
||||
if new_index:
|
||||
self.current_message_index = new_index
|
||||
self.action_focus_4()
|
||||
self.current_message_id = message_id
|
||||
|
||||
def show_status(self, message: str, severity: str = "information") -> None:
|
||||
"""Display a status message using the built-in notify function."""
|
||||
self.notify(
|
||||
message, title="Status", severity=severity, timeout=2.6, markup=True
|
||||
)
|
||||
|
||||
async def action_toggle_sort_order(self) -> None:
|
||||
"""Toggle the sort order of the envelope list."""
|
||||
self.sort_order_ascending = not self.sort_order_ascending
|
||||
worker = self.fetch_envelopes()
|
||||
await worker.wait()
|
||||
|
||||
if self.sort_order_ascending:
|
||||
self.action_oldest()
|
||||
else:
|
||||
self.action_newest()
|
||||
|
||||
async def action_toggle_mode(self) -> None:
|
||||
"""Toggle the content mode between plaintext and markdown."""
|
||||
content_container = self.query_one(ContentContainer)
|
||||
await content_container.action_toggle_mode()
|
||||
|
||||
def action_next(self) -> None:
|
||||
if not self.current_message_index >= 0:
|
||||
return
|
||||
|
||||
next_id, next_idx = self.message_store.find_next_valid_id(
|
||||
self.current_message_index
|
||||
)
|
||||
if next_id is not None and next_idx is not None:
|
||||
self.current_message_id = next_id
|
||||
self.current_message_index = next_idx
|
||||
|
||||
def action_previous(self) -> None:
|
||||
if not self.current_message_index >= 0:
|
||||
return
|
||||
|
||||
prev_id, prev_idx = self.message_store.find_prev_valid_id(
|
||||
self.current_message_index
|
||||
)
|
||||
if prev_id is not None and prev_idx is not None:
|
||||
self.current_message_id = prev_id
|
||||
self.current_message_index = prev_idx
|
||||
|
||||
async def action_delete(self) -> None:
|
||||
"""Delete the current message and update UI consistently."""
|
||||
# Call the delete_current function which uses our Himalaya client module
|
||||
worker = delete_current(self)
|
||||
await worker.wait()
|
||||
|
||||
async def action_archive(self) -> None:
|
||||
"""Archive the current or selected messages and update UI consistently."""
|
||||
next_id_to_select = None
|
||||
|
||||
if self.selected_messages:
|
||||
# --- Multi-message archive ---
|
||||
message_ids_to_archive = list(self.selected_messages)
|
||||
|
||||
if message_ids_to_archive:
|
||||
highest_archived_id = max(message_ids_to_archive)
|
||||
metadata = self.message_store.get_metadata(highest_archived_id)
|
||||
if metadata:
|
||||
next_id, _ = self.message_store.find_next_valid_id(
|
||||
metadata["index"]
|
||||
)
|
||||
if next_id is None:
|
||||
next_id, _ = self.message_store.find_prev_valid_id(
|
||||
metadata["index"]
|
||||
)
|
||||
next_id_to_select = next_id
|
||||
|
||||
message, success = await himalaya_client.archive_messages(
|
||||
[str(mid) for mid in message_ids_to_archive]
|
||||
)
|
||||
|
||||
if success:
|
||||
self.show_status(message or "Success archived")
|
||||
self.selected_messages.clear()
|
||||
else:
|
||||
self.show_status(f"Failed to archive messages: {message}", "error")
|
||||
return
|
||||
|
||||
else:
|
||||
# --- Single message archive ---
|
||||
if not self.current_message_id:
|
||||
self.show_status("No message selected to archive.", "error")
|
||||
return
|
||||
|
||||
current_id = self.current_message_id
|
||||
current_idx = self.current_message_index
|
||||
|
||||
next_id, _ = self.message_store.find_next_valid_id(current_idx)
|
||||
if next_id is None:
|
||||
next_id, _ = self.message_store.find_prev_valid_id(current_idx)
|
||||
next_id_to_select = next_id
|
||||
|
||||
message, success = await himalaya_client.archive_messages([str(current_id)])
|
||||
|
||||
if success:
|
||||
self.show_status(message or "Archived")
|
||||
else:
|
||||
self.show_status(
|
||||
f"Failed to archive message {current_id}: {message}", "error"
|
||||
)
|
||||
return
|
||||
|
||||
# Refresh the envelope list
|
||||
worker = self.fetch_envelopes()
|
||||
await worker.wait()
|
||||
|
||||
# After refresh, select the next message
|
||||
if next_id_to_select:
|
||||
new_metadata = self.message_store.get_metadata(next_id_to_select)
|
||||
if new_metadata:
|
||||
self.current_message_id = next_id_to_select
|
||||
else:
|
||||
self.action_oldest()
|
||||
else:
|
||||
self.action_oldest()
|
||||
|
||||
def action_open(self) -> None:
|
||||
action_open(self)
|
||||
|
||||
def action_create_task(self) -> None:
|
||||
action_create_task(self)
|
||||
|
||||
def action_open_links(self) -> None:
|
||||
"""Open the link panel showing links from the current message."""
|
||||
content_container = self.query_one(ContentContainer)
|
||||
links = content_container.get_links()
|
||||
self.push_screen(LinkPanel(links))
|
||||
|
||||
def action_scroll_down(self) -> None:
|
||||
"""Scroll the main content down."""
|
||||
self.query_one("#main_content").scroll_down()
|
||||
|
||||
def action_scroll_up(self) -> None:
|
||||
"""Scroll the main content up."""
|
||||
self.query_one("#main_content").scroll_up()
|
||||
|
||||
def action_scroll_page_down(self) -> None:
|
||||
"""Scroll the main content down by a page."""
|
||||
self.query_one("#main_content").scroll_page_down()
|
||||
|
||||
def action_scroll_page_up(self) -> None:
|
||||
"""Scroll the main content up by a page."""
|
||||
self.query_one("#main_content").scroll_page_up()
|
||||
|
||||
def action_toggle_main_content(self) -> None:
|
||||
"""Toggle the visibility of the main content pane."""
|
||||
self.main_content_visible = not self.main_content_visible
|
||||
|
||||
def watch_main_content_visible(self, visible: bool) -> None:
|
||||
"""Called when main_content_visible changes."""
|
||||
main_content = self.query_one("#main_content")
|
||||
accounts_list = self.query_one("#accounts_list")
|
||||
folders_list = self.query_one("#folders_list")
|
||||
|
||||
main_content.display = visible
|
||||
accounts_list.display = visible
|
||||
folders_list.display = visible
|
||||
|
||||
self.query_one("#envelopes_list").focus()
|
||||
|
||||
def action_quit(self) -> None:
|
||||
self.exit()
|
||||
|
||||
def action_toggle_selection(self) -> None:
|
||||
"""Toggle selection for the current message."""
|
||||
current_item_data = self.message_store.envelopes[self.highlighted_message_index]
|
||||
if current_item_data and current_item_data.get("type") != "header":
|
||||
message_id = int(current_item_data["id"])
|
||||
envelopes_list = self.query_one("#envelopes_list", ListView)
|
||||
current_list_item = envelopes_list.children[self.highlighted_message_index]
|
||||
|
||||
# Toggle selection state
|
||||
if message_id in self.selected_messages:
|
||||
self.selected_messages.remove(message_id)
|
||||
is_selected = False
|
||||
else:
|
||||
self.selected_messages.add(message_id)
|
||||
is_selected = True
|
||||
|
||||
# Update the EnvelopeListItem widget
|
||||
try:
|
||||
envelope_widget = current_list_item.query_one(EnvelopeListItem)
|
||||
envelope_widget.set_selected(is_selected)
|
||||
except Exception:
|
||||
# Fallback for old-style widgets
|
||||
try:
|
||||
checkbox_label = current_list_item.query_one(".checkbox", Label)
|
||||
if is_selected:
|
||||
checkbox_label.add_class("x-list")
|
||||
checkbox_label.update("\uf4a7")
|
||||
else:
|
||||
checkbox_label.remove_class("x-list")
|
||||
checkbox_label.update("\ue640")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self._update_list_view_subtitle()
|
||||
|
||||
def action_clear_selection(self) -> None:
|
||||
"""Clear all selected messages."""
|
||||
self.selected_messages.clear()
|
||||
self.refresh_list_view_items() # Refresh all items to uncheck checkboxes
|
||||
self._update_list_view_subtitle()
|
||||
|
||||
def action_oldest(self) -> None:
|
||||
self.fetch_envelopes() if self.reload_needed else None
|
||||
self.show_message(self.message_store.get_oldest_id())
|
||||
|
||||
def action_newest(self) -> None:
|
||||
self.fetch_envelopes() if self.reload_needed else None
|
||||
self.show_message(self.message_store.get_newest_id())
|
||||
|
||||
def action_focus_1(self) -> None:
|
||||
self.query_one("#envelopes_list").focus()
|
||||
|
||||
def action_focus_2(self) -> None:
|
||||
self.query_one("#accounts_list").focus()
|
||||
|
||||
def action_focus_3(self) -> None:
|
||||
self.query_one("#folders_list").focus()
|
||||
|
||||
def action_focus_4(self) -> None:
|
||||
self.query_one("#main_content").focus()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = EmailViewerApp()
|
||||
app.run()
|
||||
|
||||
|
||||
def launch_email_viewer():
|
||||
app = EmailViewerApp()
|
||||
app.run()
|
||||
@@ -1,213 +0,0 @@
|
||||
from markitdown import MarkItDown
|
||||
from textual import work
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Vertical, ScrollableContainer
|
||||
from textual.widgets import Static, Markdown, Label
|
||||
from textual.reactive import reactive
|
||||
from src.services.himalaya import client as himalaya_client
|
||||
from src.maildir_gtd.config import get_config
|
||||
from src.maildir_gtd.screens.LinkPanel import extract_links_from_content, LinkItem
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Literal, List
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Add the parent directory to the system path to resolve relative imports
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
class EnvelopeHeader(Vertical):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.subject_label = Label("")
|
||||
self.from_label = Label("")
|
||||
self.to_label = Label("")
|
||||
self.date_label = Label("")
|
||||
self.cc_label = Label("")
|
||||
|
||||
def on_mount(self):
|
||||
self.styles.height = "auto"
|
||||
self.mount(self.subject_label)
|
||||
self.mount(self.from_label)
|
||||
self.mount(self.to_label)
|
||||
self.mount(self.cc_label)
|
||||
self.mount(self.date_label)
|
||||
|
||||
def update(self, subject, from_, to, date, cc=None):
|
||||
self.subject_label.update(f"[b]Subject:[/b] {subject}")
|
||||
self.from_label.update(f"[b]From:[/b] {from_}")
|
||||
self.to_label.update(f"[b]To:[/b] {to}")
|
||||
|
||||
# Format the date for better readability
|
||||
if date:
|
||||
try:
|
||||
# Try to convert the date string to a datetime object
|
||||
date_obj = datetime.fromisoformat(date.replace("Z", "+00:00"))
|
||||
formatted_date = date_obj.strftime("%a, %d %b %Y %H:%M:%S %Z")
|
||||
self.date_label.update(f"[b]Date:[/b] {formatted_date}")
|
||||
except (ValueError, TypeError):
|
||||
# If parsing fails, just use the original date string
|
||||
self.date_label.update(f"[b]Date:[/b] {date}")
|
||||
else:
|
||||
self.date_label.update("[b]Date:[/b] Unknown")
|
||||
|
||||
if cc:
|
||||
self.cc_label.update(f"[b]CC:[/b] {cc}")
|
||||
self.cc_label.styles.display = "block"
|
||||
else:
|
||||
self.cc_label.styles.display = "none"
|
||||
|
||||
|
||||
class ContentContainer(ScrollableContainer):
|
||||
"""Container for displaying email content with toggleable view modes."""
|
||||
|
||||
can_focus = True
|
||||
|
||||
# Reactive to track view mode and update UI
|
||||
current_mode: reactive[Literal["markdown", "html"]] = reactive("markdown")
|
||||
|
||||
BINDINGS = [
|
||||
Binding("m", "toggle_mode", "Toggle View Mode"),
|
||||
]
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.md = MarkItDown()
|
||||
self.header = EnvelopeHeader(id="envelope_header")
|
||||
self.content = Markdown("", id="markdown_content")
|
||||
self.html_content = Static("", id="html_content", markup=False)
|
||||
self.current_content = None
|
||||
self.current_message_id = None
|
||||
self.content_worker = None
|
||||
|
||||
# Load default view mode from config
|
||||
config = get_config()
|
||||
self.current_mode = config.content_display.default_view_mode
|
||||
|
||||
def compose(self):
|
||||
yield self.content
|
||||
yield self.html_content
|
||||
|
||||
def on_mount(self):
|
||||
# Set initial display based on config default
|
||||
self._apply_view_mode()
|
||||
self._update_mode_indicator()
|
||||
|
||||
def watch_current_mode(self, old_mode: str, new_mode: str) -> None:
|
||||
"""React to mode changes."""
|
||||
self._apply_view_mode()
|
||||
self._update_mode_indicator()
|
||||
|
||||
def _apply_view_mode(self) -> None:
|
||||
"""Apply the current view mode to widget visibility."""
|
||||
if self.current_mode == "markdown":
|
||||
self.html_content.styles.display = "none"
|
||||
self.content.styles.display = "block"
|
||||
else:
|
||||
self.content.styles.display = "none"
|
||||
self.html_content.styles.display = "block"
|
||||
|
||||
def _update_mode_indicator(self) -> None:
|
||||
"""Update the border subtitle to show current mode."""
|
||||
mode_label = "Markdown" if self.current_mode == "markdown" else "HTML/Text"
|
||||
mode_icon = (
|
||||
"\ue73e" if self.current_mode == "markdown" else "\uf121"
|
||||
) # nf-md-language_markdown / nf-fa-code
|
||||
self.border_subtitle = f"{mode_icon} {mode_label}"
|
||||
|
||||
async def action_toggle_mode(self):
|
||||
"""Toggle between markdown and HTML viewing modes."""
|
||||
if self.current_mode == "html":
|
||||
self.current_mode = "markdown"
|
||||
else:
|
||||
self.current_mode = "html"
|
||||
|
||||
# Reload the content if we have a message ID
|
||||
if self.current_message_id:
|
||||
self.display_content(self.current_message_id)
|
||||
|
||||
def update_header(self, subject, from_, to, date, cc=None):
|
||||
self.header.update(subject, from_, to, date, cc)
|
||||
|
||||
@work(exclusive=True)
|
||||
async def fetch_message_content(self, message_id: int, format: str):
|
||||
"""Fetch message content using the Himalaya client module."""
|
||||
if not message_id:
|
||||
self.notify("No message ID provided.")
|
||||
return
|
||||
|
||||
content, success = await himalaya_client.get_message_content(message_id)
|
||||
if success:
|
||||
self._update_content(content)
|
||||
else:
|
||||
self.notify(f"Failed to fetch content for message ID {message_id}.")
|
||||
|
||||
def display_content(self, message_id: int) -> None:
|
||||
"""Display the content of a message."""
|
||||
if not message_id:
|
||||
return
|
||||
|
||||
self.current_message_id = message_id
|
||||
|
||||
# Immediately show a loading message
|
||||
if self.current_mode == "markdown":
|
||||
self.content.update("Loading...")
|
||||
else:
|
||||
self.html_content.update("Loading...")
|
||||
|
||||
# Cancel any existing content fetch operations
|
||||
if self.content_worker:
|
||||
self.content_worker.cancel()
|
||||
|
||||
# Fetch content in the current mode
|
||||
format_type = "text" if self.current_mode == "markdown" else "html"
|
||||
self.content_worker = self.fetch_message_content(message_id, format_type)
|
||||
|
||||
def _update_content(self, content: str | None) -> None:
|
||||
"""Update the content widgets with the fetched content."""
|
||||
if content is None:
|
||||
content = "(No content)"
|
||||
|
||||
# Store the raw content for link extraction
|
||||
self.current_content = content
|
||||
|
||||
try:
|
||||
if self.current_mode == "markdown":
|
||||
# For markdown mode, use the Markdown widget
|
||||
self.content.update(content)
|
||||
else:
|
||||
# For HTML mode, use the Static widget with markup
|
||||
# First, try to extract the body content if it's HTML
|
||||
body_match = re.search(
|
||||
r"<body[^>]*>(.*?)</body>", content, re.DOTALL | re.IGNORECASE
|
||||
)
|
||||
if body_match:
|
||||
content = body_match.group(1)
|
||||
|
||||
# Replace some common HTML elements with Textual markup
|
||||
content = content.replace("<b>", "[b]").replace("</b>", "[/b]")
|
||||
content = content.replace("<i>", "[i]").replace("</i>", "[/i]")
|
||||
content = content.replace("<u>", "[u]").replace("</u>", "[/u]")
|
||||
|
||||
# Convert links to a readable format
|
||||
content = re.sub(
|
||||
r'<a href="([^"]+)"[^>]*>([^<]+)</a>', r"[\2](\1)", content
|
||||
)
|
||||
|
||||
# Add CSS for better readability
|
||||
self.html_content.update(content)
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error updating content: {e}")
|
||||
if self.current_mode == "markdown":
|
||||
self.content.update(f"Error displaying content: {e}")
|
||||
else:
|
||||
self.html_content.update(f"Error displaying content: {e}")
|
||||
|
||||
def get_links(self) -> List[LinkItem]:
|
||||
"""Extract and return links from the current message content."""
|
||||
if not self.current_content:
|
||||
return []
|
||||
return extract_links_from_content(self.current_content)
|
||||
5
src/services/dstask/__init__.py
Normal file
5
src/services/dstask/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""dstask client service for Tasks TUI."""
|
||||
|
||||
from .client import DstaskClient
|
||||
|
||||
__all__ = ["DstaskClient"]
|
||||
399
src/services/dstask/client.py
Normal file
399
src/services/dstask/client.py
Normal file
@@ -0,0 +1,399 @@
|
||||
"""dstask CLI client implementation.
|
||||
|
||||
This module implements the TaskBackend interface for dstask,
|
||||
a local-first task manager with Git sync support.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from src.tasks.backend import (
|
||||
Project,
|
||||
Task,
|
||||
TaskBackend,
|
||||
TaskPriority,
|
||||
TaskStatus,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DstaskClient(TaskBackend):
|
||||
"""Client for interacting with dstask CLI."""
|
||||
|
||||
def __init__(self, dstask_path: Optional[str] = None):
|
||||
"""Initialize dstask client.
|
||||
|
||||
Args:
|
||||
dstask_path: Path to dstask binary. Defaults to ~/.local/bin/dstask
|
||||
"""
|
||||
if dstask_path is None:
|
||||
dstask_path = str(Path.home() / ".local" / "bin" / "dstask")
|
||||
self.dstask_path = dstask_path
|
||||
|
||||
def is_available(self) -> bool:
|
||||
"""Check if dstask binary is available and executable."""
|
||||
path = Path(self.dstask_path)
|
||||
return path.exists() and path.is_file()
|
||||
|
||||
def _run_command(
|
||||
self, args: list[str], capture_output: bool = True
|
||||
) -> subprocess.CompletedProcess:
|
||||
"""Run a dstask command.
|
||||
|
||||
Args:
|
||||
args: Command arguments (without dstask binary)
|
||||
capture_output: Whether to capture stdout/stderr
|
||||
|
||||
Returns:
|
||||
CompletedProcess result
|
||||
"""
|
||||
cmd = [self.dstask_path] + args
|
||||
logger.debug(f"Running: {' '.join(cmd)}")
|
||||
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=capture_output,
|
||||
text=True,
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.warning(f"dstask command failed: {result.stderr}")
|
||||
|
||||
return result
|
||||
|
||||
def _parse_datetime(self, value: str) -> Optional[datetime]:
|
||||
"""Parse datetime from dstask JSON format."""
|
||||
if not value:
|
||||
return None
|
||||
|
||||
# dstask uses RFC3339 format
|
||||
try:
|
||||
# Handle Z suffix
|
||||
if value.endswith("Z"):
|
||||
value = value[:-1] + "+00:00"
|
||||
dt = datetime.fromisoformat(value)
|
||||
# dstask uses year 1 (0001-01-01) to indicate no date set
|
||||
if dt.year == 1:
|
||||
return None
|
||||
return dt
|
||||
except ValueError:
|
||||
logger.warning(f"Failed to parse datetime: {value}")
|
||||
return None
|
||||
|
||||
def _parse_task(self, data: dict) -> Task:
|
||||
"""Parse a task from dstask JSON output."""
|
||||
# Map dstask status to TaskStatus
|
||||
status_map = {
|
||||
"pending": TaskStatus.PENDING,
|
||||
"active": TaskStatus.ACTIVE,
|
||||
"resolved": TaskStatus.DONE,
|
||||
"deleted": TaskStatus.DELETED,
|
||||
}
|
||||
status = status_map.get(data.get("status", "pending"), TaskStatus.PENDING)
|
||||
|
||||
# Parse priority
|
||||
priority_str = data.get("priority", "P2")
|
||||
priority = TaskPriority.from_string(priority_str)
|
||||
|
||||
return Task(
|
||||
uuid=data.get("uuid", ""),
|
||||
id=data.get("id", 0),
|
||||
summary=data.get("summary", ""),
|
||||
status=status,
|
||||
priority=priority,
|
||||
project=data.get("project", ""),
|
||||
tags=data.get("tags", []) or [],
|
||||
notes=data.get("notes", ""),
|
||||
due=self._parse_datetime(data.get("due", "")),
|
||||
created=self._parse_datetime(data.get("created", "")),
|
||||
resolved=self._parse_datetime(data.get("resolved", "")),
|
||||
)
|
||||
|
||||
def _get_tasks_json(self, command: str = "show-open") -> list[Task]:
|
||||
"""Get tasks using a dstask command that outputs JSON."""
|
||||
result = self._run_command([command])
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"Failed to get tasks: {result.stderr}")
|
||||
return []
|
||||
|
||||
try:
|
||||
data = json.loads(result.stdout)
|
||||
return [self._parse_task(t) for t in data]
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Failed to parse dstask output: {e}")
|
||||
return []
|
||||
|
||||
def get_tasks(
|
||||
self,
|
||||
project: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
status: Optional[TaskStatus] = None,
|
||||
) -> list[Task]:
|
||||
"""Get tasks, optionally filtered by project, tags, or status."""
|
||||
# Build filter arguments
|
||||
args = ["show-open"]
|
||||
|
||||
if project:
|
||||
args.append(f"project:{project}")
|
||||
|
||||
if tags:
|
||||
for tag in tags:
|
||||
args.append(f"+{tag}")
|
||||
|
||||
result = self._run_command(args)
|
||||
|
||||
if result.returncode != 0:
|
||||
return []
|
||||
|
||||
try:
|
||||
data = json.loads(result.stdout)
|
||||
tasks = [self._parse_task(t) for t in data]
|
||||
|
||||
# Filter by status if specified
|
||||
if status:
|
||||
tasks = [t for t in tasks if t.status == status]
|
||||
|
||||
return tasks
|
||||
except json.JSONDecodeError:
|
||||
return []
|
||||
|
||||
def get_next_tasks(self) -> list[Task]:
|
||||
"""Get the 'next' tasks to work on."""
|
||||
return self._get_tasks_json("next")
|
||||
|
||||
def get_task(self, task_id: str) -> Optional[Task]:
|
||||
"""Get a single task by ID or UUID."""
|
||||
result = self._run_command(["show-open"])
|
||||
|
||||
if result.returncode != 0:
|
||||
return None
|
||||
|
||||
try:
|
||||
data = json.loads(result.stdout)
|
||||
for t in data:
|
||||
if str(t.get("id")) == task_id or t.get("uuid") == task_id:
|
||||
return self._parse_task(t)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
def add_task(
|
||||
self,
|
||||
summary: str,
|
||||
project: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
priority: Optional[TaskPriority] = None,
|
||||
due: Optional[datetime] = None,
|
||||
notes: Optional[str] = None,
|
||||
) -> Task:
|
||||
"""Create a new task.
|
||||
|
||||
Notes are added using dstask's / syntax during creation, where
|
||||
everything after / becomes the note content. Each word must be
|
||||
a separate argument for this to work.
|
||||
"""
|
||||
args = ["add", summary]
|
||||
|
||||
if project:
|
||||
args.append(f"project:{project}")
|
||||
|
||||
if tags:
|
||||
for tag in tags:
|
||||
args.append(f"+{tag}")
|
||||
|
||||
if priority:
|
||||
args.append(priority.value)
|
||||
|
||||
if due:
|
||||
# dstask uses various date formats
|
||||
args.append(f"due:{due.strftime('%Y-%m-%d')}")
|
||||
|
||||
# Add notes using / syntax - each word must be a separate argument
|
||||
# dstask interprets everything after "/" as note content
|
||||
if notes:
|
||||
args.append("/")
|
||||
# Split notes into words to pass as separate arguments
|
||||
args.extend(notes.split())
|
||||
|
||||
result = self._run_command(args)
|
||||
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"Failed to add task: {result.stderr}")
|
||||
|
||||
# Get the newly created task (it should be the last one)
|
||||
tasks = self.get_next_tasks()
|
||||
if tasks:
|
||||
# Find task by summary (best effort)
|
||||
for task in reversed(tasks):
|
||||
if task.summary == summary:
|
||||
return task
|
||||
|
||||
# Return a placeholder if we can't find it
|
||||
return Task(
|
||||
uuid="",
|
||||
id=0,
|
||||
summary=summary,
|
||||
project=project or "",
|
||||
tags=tags or [],
|
||||
priority=priority or TaskPriority.P2,
|
||||
notes=notes or "",
|
||||
due=due,
|
||||
)
|
||||
|
||||
def complete_task(self, task_id: str) -> bool:
|
||||
"""Mark a task as complete."""
|
||||
result = self._run_command(["done", task_id])
|
||||
return result.returncode == 0
|
||||
|
||||
def delete_task(self, task_id: str) -> bool:
|
||||
"""Delete a task."""
|
||||
# dstask uses 'remove' for deletion
|
||||
result = self._run_command(["remove", task_id])
|
||||
return result.returncode == 0
|
||||
|
||||
def start_task(self, task_id: str) -> bool:
|
||||
"""Start working on a task (mark as active)."""
|
||||
result = self._run_command(["start", task_id])
|
||||
return result.returncode == 0
|
||||
|
||||
def stop_task(self, task_id: str) -> bool:
|
||||
"""Stop working on a task (mark as pending)."""
|
||||
result = self._run_command(["stop", task_id])
|
||||
return result.returncode == 0
|
||||
|
||||
def modify_task(
|
||||
self,
|
||||
task_id: str,
|
||||
summary: Optional[str] = None,
|
||||
project: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
priority: Optional[TaskPriority] = None,
|
||||
due: Optional[datetime] = None,
|
||||
notes: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""Modify a task."""
|
||||
args = ["modify", task_id]
|
||||
|
||||
if summary:
|
||||
args.append(summary)
|
||||
|
||||
if project:
|
||||
args.append(f"project:{project}")
|
||||
|
||||
if tags:
|
||||
for tag in tags:
|
||||
args.append(f"+{tag}")
|
||||
|
||||
if priority:
|
||||
args.append(priority.value)
|
||||
|
||||
if due:
|
||||
args.append(f"due:{due.strftime('%Y-%m-%d')}")
|
||||
|
||||
result = self._run_command(args)
|
||||
|
||||
# Handle notes separately
|
||||
if notes is not None and result.returncode == 0:
|
||||
self._run_command(["note", task_id, notes])
|
||||
|
||||
return result.returncode == 0
|
||||
|
||||
def get_projects(self) -> list[Project]:
|
||||
"""Get all projects."""
|
||||
result = self._run_command(["show-projects"])
|
||||
|
||||
if result.returncode != 0:
|
||||
return []
|
||||
|
||||
try:
|
||||
data = json.loads(result.stdout)
|
||||
projects = []
|
||||
for p in data:
|
||||
priority = TaskPriority.from_string(p.get("priority", "P2"))
|
||||
projects.append(
|
||||
Project(
|
||||
name=p.get("name", ""),
|
||||
task_count=p.get("taskCount", 0),
|
||||
resolved_count=p.get("resolvedCount", 0),
|
||||
active=p.get("active", True),
|
||||
priority=priority,
|
||||
)
|
||||
)
|
||||
return projects
|
||||
except json.JSONDecodeError:
|
||||
return []
|
||||
|
||||
def get_tags(self) -> list[str]:
|
||||
"""Get all tags."""
|
||||
result = self._run_command(["show-tags"])
|
||||
|
||||
if result.returncode != 0:
|
||||
return []
|
||||
|
||||
# show-tags outputs plain text, one tag per line
|
||||
tags = result.stdout.strip().split("\n")
|
||||
return [t.strip() for t in tags if t.strip()]
|
||||
|
||||
def sync(self) -> bool:
|
||||
"""Sync tasks with Git remote."""
|
||||
result = self._run_command(["sync"])
|
||||
return result.returncode == 0
|
||||
|
||||
def edit_task_interactive(self, task_id: str) -> bool:
|
||||
"""Open task in editor for interactive editing."""
|
||||
# This needs to run without capturing output
|
||||
result = self._run_command(["edit", task_id], capture_output=False)
|
||||
return result.returncode == 0
|
||||
|
||||
def edit_note_interactive(self, task_id: str) -> bool:
|
||||
"""Open task notes in editor for interactive editing."""
|
||||
# This needs to run without capturing output
|
||||
result = self._run_command(["note", task_id], capture_output=False)
|
||||
return result.returncode == 0
|
||||
|
||||
def get_context(self) -> Optional[str]:
|
||||
"""Get the current context filter.
|
||||
|
||||
Returns:
|
||||
Current context string, or None if no context is set
|
||||
"""
|
||||
result = self._run_command(["context"])
|
||||
if result.returncode == 0:
|
||||
context = result.stdout.strip()
|
||||
return context if context else None
|
||||
return None
|
||||
|
||||
def set_context(self, context: Optional[str]) -> bool:
|
||||
"""Set the context filter.
|
||||
|
||||
Args:
|
||||
context: Context string (e.g., "+work", "project:foo") or None to clear
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
if context is None or context.lower() == "none" or context == "":
|
||||
result = self._run_command(["context", "none"])
|
||||
else:
|
||||
result = self._run_command(["context", context])
|
||||
return result.returncode == 0
|
||||
|
||||
def get_contexts(self) -> list[str]:
|
||||
"""Get available contexts based on tags.
|
||||
|
||||
For dstask, contexts are typically tag-based filters like "+work".
|
||||
We derive available contexts from the existing tags.
|
||||
|
||||
Returns:
|
||||
List of context strings (tag-based)
|
||||
"""
|
||||
# Get all tags and convert to context format
|
||||
tags = self.get_tags()
|
||||
return [f"+{tag}" for tag in tags if tag]
|
||||
@@ -4,12 +4,20 @@ import json
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
from src.mail.config import get_config
|
||||
|
||||
async def list_envelopes(limit: int = 9999) -> Tuple[List[Dict[str, Any]], bool]:
|
||||
|
||||
async def list_envelopes(
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
limit: int = 9999,
|
||||
) -> Tuple[List[Dict[str, Any]], bool]:
|
||||
"""
|
||||
Retrieve a list of email envelopes using the Himalaya CLI.
|
||||
|
||||
Args:
|
||||
folder: The folder to list envelopes from (defaults to INBOX)
|
||||
account: The account to use (defaults to default account)
|
||||
limit: Maximum number of envelopes to retrieve
|
||||
|
||||
Returns:
|
||||
@@ -18,8 +26,14 @@ async def list_envelopes(limit: int = 9999) -> Tuple[List[Dict[str, Any]], bool]
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
try:
|
||||
cmd = f"himalaya envelope list -o json -s {limit}"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
f"himalaya envelope list -o json -s {limit}",
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
@@ -64,18 +78,27 @@ async def list_accounts() -> Tuple[List[Dict[str, Any]], bool]:
|
||||
return [], False
|
||||
|
||||
|
||||
async def list_folders() -> Tuple[List[Dict[str, Any]], bool]:
|
||||
async def list_folders(
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[List[Dict[str, Any]], bool]:
|
||||
"""
|
||||
Retrieve a list of folders available in Himalaya.
|
||||
|
||||
Args:
|
||||
account: The account to list folders for (defaults to default account)
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
- List of folder dictionaries
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
try:
|
||||
cmd = "himalaya folder list -o json"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
"himalaya folder list -o json",
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
@@ -92,28 +115,88 @@ async def list_folders() -> Tuple[List[Dict[str, Any]], bool]:
|
||||
return [], False
|
||||
|
||||
|
||||
async def delete_message(message_id: int) -> bool:
|
||||
async def get_folder_count(
|
||||
folder: str,
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[int, bool]:
|
||||
"""
|
||||
Delete a message by its ID.
|
||||
Get the count of messages in a folder.
|
||||
|
||||
Args:
|
||||
message_id: The ID of the message to delete
|
||||
folder: The folder to count messages in
|
||||
account: The account to use (defaults to default account)
|
||||
|
||||
Returns:
|
||||
True if deletion was successful, False otherwise
|
||||
Tuple containing:
|
||||
- Message count
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
try:
|
||||
# Use a high limit to get all messages, then count them
|
||||
# This is the most reliable way with himalaya
|
||||
cmd = f"himalaya envelope list -o json -s 9999 -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
f"himalaya message delete {message_id}",
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
return process.returncode == 0
|
||||
if process.returncode == 0:
|
||||
envelopes = json.loads(stdout.decode())
|
||||
return len(envelopes), True
|
||||
else:
|
||||
logging.error(f"Error getting folder count: {stderr.decode()}")
|
||||
return 0, False
|
||||
except Exception as e:
|
||||
logging.error(f"Exception during folder count: {e}")
|
||||
return 0, False
|
||||
|
||||
|
||||
async def delete_message(
|
||||
message_id: int,
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[Optional[str], bool]:
|
||||
"""
|
||||
Delete a message by its ID.
|
||||
|
||||
Args:
|
||||
message_id: The ID of the message to delete
|
||||
folder: The folder containing the message
|
||||
account: The account to use
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
- Result message or error
|
||||
- Success status (True if deletion was successful)
|
||||
"""
|
||||
try:
|
||||
cmd = f"himalaya message delete {message_id}"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
return stdout.decode().strip() or "Deleted successfully", True
|
||||
else:
|
||||
error_msg = stderr.decode().strip()
|
||||
logging.error(f"Error deleting message: {error_msg}")
|
||||
return error_msg or "Unknown error", False
|
||||
except Exception as e:
|
||||
logging.error(f"Exception during message deletion: {e}")
|
||||
return False
|
||||
return str(e), False
|
||||
|
||||
|
||||
# async def archive_message(message_id: int) -> [str, bool]:
|
||||
@@ -140,19 +223,31 @@ async def delete_message(message_id: int) -> bool:
|
||||
# return False
|
||||
|
||||
|
||||
async def archive_messages(message_ids: List[str]) -> Tuple[Optional[str], bool]:
|
||||
async def archive_messages(
|
||||
message_ids: List[str],
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[Optional[str], bool]:
|
||||
"""
|
||||
Archive multiple messages by their IDs.
|
||||
|
||||
Args:
|
||||
message_ids: A list of message IDs to archive.
|
||||
folder: The source folder containing the messages
|
||||
account: The account to use
|
||||
|
||||
Returns:
|
||||
A tuple containing an optional output string and a boolean indicating success.
|
||||
"""
|
||||
try:
|
||||
config = get_config()
|
||||
archive_folder = config.mail.archive_folder
|
||||
ids_str = " ".join(message_ids)
|
||||
cmd = f"himalaya message move Archives {ids_str}"
|
||||
cmd = f"himalaya message move {archive_folder} {ids_str}"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
@@ -162,21 +257,28 @@ async def archive_messages(message_ids: List[str]) -> Tuple[Optional[str], bool]
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
return stdout.decode(), True
|
||||
return stdout.decode().strip() or "Archived successfully", True
|
||||
else:
|
||||
logging.error(f"Error archiving messages: {stderr.decode()}")
|
||||
return None, False
|
||||
error_msg = stderr.decode().strip()
|
||||
logging.error(f"Error archiving messages: {error_msg}")
|
||||
return error_msg or "Unknown error", False
|
||||
except Exception as e:
|
||||
logging.error(f"Exception during message archiving: {e}")
|
||||
return None, False
|
||||
return str(e), False
|
||||
|
||||
|
||||
async def get_message_content(message_id: int) -> Tuple[Optional[str], bool]:
|
||||
async def get_message_content(
|
||||
message_id: int,
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[Optional[str], bool]:
|
||||
"""
|
||||
Retrieve the content of a message by its ID.
|
||||
|
||||
Args:
|
||||
message_id: The ID of the message to retrieve
|
||||
folder: The folder containing the message
|
||||
account: The account to use
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
@@ -184,7 +286,13 @@ async def get_message_content(message_id: int) -> Tuple[Optional[str], bool]:
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
try:
|
||||
# Don't use --no-headers - we parse headers for EnvelopeHeader display
|
||||
# and strip them from the body content ourselves
|
||||
cmd = f"himalaya message read {message_id}"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
@@ -204,6 +312,171 @@ async def get_message_content(message_id: int) -> Tuple[Optional[str], bool]:
|
||||
return None, False
|
||||
|
||||
|
||||
async def mark_as_read(
|
||||
message_id: int,
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[Optional[str], bool]:
|
||||
"""
|
||||
Mark a message as read by adding the 'seen' flag.
|
||||
|
||||
Args:
|
||||
message_id: The ID of the message to mark as read
|
||||
folder: The folder containing the message
|
||||
account: The account to use
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
- Result message or error
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
try:
|
||||
cmd = f"himalaya flag add seen {message_id}"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
return stdout.decode().strip() or "Marked as read", True
|
||||
else:
|
||||
error_msg = stderr.decode().strip()
|
||||
logging.error(f"Error marking message as read: {error_msg}")
|
||||
return error_msg or "Unknown error", False
|
||||
except Exception as e:
|
||||
logging.error(f"Exception during marking message as read: {e}")
|
||||
return str(e), False
|
||||
|
||||
|
||||
async def mark_as_unread(
|
||||
message_id: int,
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[Optional[str], bool]:
|
||||
"""
|
||||
Mark a message as unread by removing the 'seen' flag.
|
||||
|
||||
Args:
|
||||
message_id: The ID of the message to mark as unread
|
||||
folder: The folder containing the message
|
||||
account: The account to use
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
- Result message or error
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
try:
|
||||
cmd = f"himalaya flag remove seen {message_id}"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
return stdout.decode().strip() or "Marked as unread", True
|
||||
else:
|
||||
error_msg = stderr.decode().strip()
|
||||
logging.error(f"Error marking message as unread: {error_msg}")
|
||||
return error_msg or "Unknown error", False
|
||||
except Exception as e:
|
||||
logging.error(f"Exception during marking message as unread: {e}")
|
||||
return str(e), False
|
||||
|
||||
|
||||
async def search_envelopes(
|
||||
query: str,
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
limit: int = 100,
|
||||
) -> Tuple[List[Dict[str, Any]], bool]:
|
||||
"""
|
||||
Search for envelopes matching a query using Himalaya CLI.
|
||||
|
||||
The query is searched across from, to, subject, and body fields.
|
||||
|
||||
Args:
|
||||
query: The search term to look for
|
||||
folder: The folder to search in (defaults to INBOX)
|
||||
account: The account to use (defaults to default account)
|
||||
limit: Maximum number of results to return
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
- List of matching envelope dictionaries
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
try:
|
||||
# Himalaya query keywords that indicate the user is writing a raw query
|
||||
query_keywords = (
|
||||
"from ",
|
||||
"to ",
|
||||
"subject ",
|
||||
"body ",
|
||||
"date ",
|
||||
"before ",
|
||||
"after ",
|
||||
"flag ",
|
||||
"not ",
|
||||
"order by ",
|
||||
)
|
||||
|
||||
# Check if user is using raw query syntax
|
||||
query_lower = query.lower()
|
||||
is_raw_query = any(query_lower.startswith(kw) for kw in query_keywords)
|
||||
|
||||
if is_raw_query:
|
||||
# Pass through as-is (user knows what they're doing)
|
||||
search_query = query
|
||||
else:
|
||||
# Build a compound query to search from, to, subject, and body
|
||||
# Himalaya query syntax: from <pattern> or to <pattern> or subject <pattern> or body <pattern>
|
||||
search_query = (
|
||||
f"from {query} or to {query} or subject {query} or body {query}"
|
||||
)
|
||||
|
||||
# Build command with options before the query (query must be at the end, quoted)
|
||||
cmd = "himalaya envelope list -o json"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
cmd += f" -s {limit}"
|
||||
# Query must be quoted and at the end of the command
|
||||
cmd += f' "{search_query}"'
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
envelopes = json.loads(stdout.decode())
|
||||
return envelopes, True
|
||||
else:
|
||||
logging.error(f"Error searching envelopes: {stderr.decode()}")
|
||||
return [], False
|
||||
except Exception as e:
|
||||
logging.error(f"Exception during envelope search: {e}")
|
||||
return [], False
|
||||
|
||||
|
||||
def sync_himalaya():
|
||||
"""This command does not exist. Halucinated by AI."""
|
||||
try:
|
||||
@@ -211,3 +484,62 @@ def sync_himalaya():
|
||||
print("Himalaya sync completed successfully.")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error during Himalaya sync: {e}")
|
||||
|
||||
|
||||
async def get_raw_message(
|
||||
message_id: int,
|
||||
folder: Optional[str] = None,
|
||||
account: Optional[str] = None,
|
||||
) -> Tuple[Optional[str], bool]:
|
||||
"""
|
||||
Retrieve the full raw message (EML format) by its ID.
|
||||
|
||||
This exports the complete MIME message including all parts (text, HTML,
|
||||
attachments like ICS calendar files). Useful for parsing calendar invites.
|
||||
|
||||
Args:
|
||||
message_id: The ID of the message to retrieve
|
||||
folder: The folder containing the message
|
||||
account: The account to use
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
- Raw message content (EML format) or None if retrieval failed
|
||||
- Success status (True if operation was successful)
|
||||
"""
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
try:
|
||||
# Create a temporary directory for the export
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
eml_path = os.path.join(tmpdir, f"message_{message_id}.eml")
|
||||
|
||||
cmd = f"himalaya message export -F -d '{eml_path}' {message_id}"
|
||||
if folder:
|
||||
cmd += f" -f '{folder}'"
|
||||
if account:
|
||||
cmd += f" -a '{account}'"
|
||||
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
# Read the exported EML file
|
||||
if os.path.exists(eml_path):
|
||||
with open(eml_path, "r", encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
return content, True
|
||||
else:
|
||||
logging.error(f"EML file not created at {eml_path}")
|
||||
return None, False
|
||||
else:
|
||||
logging.error(f"Error exporting raw message: {stderr.decode()}")
|
||||
return None, False
|
||||
except Exception as e:
|
||||
logging.error(f"Exception during raw message export: {e}")
|
||||
return None, False
|
||||
|
||||
5
src/services/khal/__init__.py
Normal file
5
src/services/khal/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Khal service package for calendar operations."""
|
||||
|
||||
from .client import KhalClient
|
||||
|
||||
__all__ = ["KhalClient"]
|
||||
373
src/services/khal/client.py
Normal file
373
src/services/khal/client.py
Normal file
@@ -0,0 +1,373 @@
|
||||
"""Khal CLI client for calendar operations.
|
||||
|
||||
This module provides a client that uses the khal CLI tool to interact with
|
||||
calendar data stored in vdir format.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import logging
|
||||
from datetime import datetime, date, timedelta
|
||||
from typing import Optional, List
|
||||
|
||||
from src.calendar.backend import CalendarBackend, Event
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class KhalClient(CalendarBackend):
|
||||
"""Calendar backend using khal CLI."""
|
||||
|
||||
def __init__(self, config_path: Optional[str] = None):
|
||||
"""Initialize the Khal client.
|
||||
|
||||
Args:
|
||||
config_path: Optional path to khal config file
|
||||
"""
|
||||
self.config_path = config_path
|
||||
|
||||
def _run_khal(
|
||||
self, args: List[str], capture_output: bool = True
|
||||
) -> subprocess.CompletedProcess:
|
||||
"""Run a khal command.
|
||||
|
||||
Args:
|
||||
args: Command arguments (after 'khal')
|
||||
capture_output: Whether to capture stdout/stderr
|
||||
|
||||
Returns:
|
||||
CompletedProcess result
|
||||
"""
|
||||
cmd = ["khal"] + args
|
||||
if self.config_path:
|
||||
cmd.extend(["-c", self.config_path])
|
||||
|
||||
logger.debug(f"Running khal command: {' '.join(cmd)}")
|
||||
|
||||
return subprocess.run(
|
||||
cmd,
|
||||
capture_output=capture_output,
|
||||
text=True,
|
||||
)
|
||||
|
||||
def _parse_event_line(
|
||||
self, line: str, day_header_date: Optional[date] = None
|
||||
) -> Optional[Event]:
|
||||
"""Parse a single event line from khal list output.
|
||||
|
||||
Expected format: title|start-time|end-time|start|end|location|uid|description|organizer|url|categories|status|recurring
|
||||
|
||||
Args:
|
||||
line: The line to parse
|
||||
day_header_date: Current day being parsed (from day headers)
|
||||
|
||||
Returns:
|
||||
Event if successfully parsed, None otherwise
|
||||
"""
|
||||
# Skip empty lines and day headers
|
||||
if not line or "|" not in line:
|
||||
return None
|
||||
|
||||
parts = line.split("|")
|
||||
if len(parts) < 5:
|
||||
return None
|
||||
|
||||
try:
|
||||
title = parts[0].strip()
|
||||
start_str = parts[3].strip() # Full datetime
|
||||
end_str = parts[4].strip() # Full datetime
|
||||
location = parts[5].strip() if len(parts) > 5 else ""
|
||||
uid = parts[6].strip() if len(parts) > 6 else ""
|
||||
description = parts[7].strip() if len(parts) > 7 else ""
|
||||
organizer = parts[8].strip() if len(parts) > 8 else ""
|
||||
url = parts[9].strip() if len(parts) > 9 else ""
|
||||
categories = parts[10].strip() if len(parts) > 10 else ""
|
||||
status = parts[11].strip() if len(parts) > 11 else ""
|
||||
recurring_symbol = parts[12].strip() if len(parts) > 12 else ""
|
||||
|
||||
# Parse datetimes (format: YYYY-MM-DD HH:MM)
|
||||
start = datetime.strptime(start_str, "%Y-%m-%d %H:%M")
|
||||
end = datetime.strptime(end_str, "%Y-%m-%d %H:%M")
|
||||
|
||||
# Check for all-day events (typically start at 00:00 and end at 00:00 next day)
|
||||
all_day = (
|
||||
start.hour == 0
|
||||
and start.minute == 0
|
||||
and end.hour == 0
|
||||
and end.minute == 0
|
||||
and (end.date() - start.date()).days >= 1
|
||||
)
|
||||
|
||||
# Check if event is recurring (repeat symbol is typically a loop arrow)
|
||||
recurring = bool(recurring_symbol)
|
||||
|
||||
return Event(
|
||||
uid=uid or f"{title}_{start_str}",
|
||||
title=title,
|
||||
start=start,
|
||||
end=end,
|
||||
location=location,
|
||||
description=description,
|
||||
organizer=organizer,
|
||||
url=url,
|
||||
categories=categories,
|
||||
status=status,
|
||||
all_day=all_day,
|
||||
recurring=recurring,
|
||||
)
|
||||
except (ValueError, IndexError) as e:
|
||||
logger.warning(f"Failed to parse event line '{line}': {e}")
|
||||
return None
|
||||
|
||||
def get_events(
|
||||
self,
|
||||
start_date: date,
|
||||
end_date: date,
|
||||
calendar: Optional[str] = None,
|
||||
) -> List[Event]:
|
||||
"""Get events in a date range.
|
||||
|
||||
Args:
|
||||
start_date: Start of range (inclusive)
|
||||
end_date: End of range (inclusive)
|
||||
calendar: Optional calendar name to filter by
|
||||
|
||||
Returns:
|
||||
List of events in the range, sorted by start time
|
||||
"""
|
||||
# Format dates for khal
|
||||
start_str = start_date.strftime("%Y-%m-%d")
|
||||
# Add one day to end_date to make it inclusive
|
||||
end_dt = end_date + timedelta(days=1)
|
||||
end_str = end_dt.strftime("%Y-%m-%d")
|
||||
|
||||
# Build command
|
||||
# Format: title|start-time|end-time|start|end|location|uid|description|organizer|url|categories|status|recurring
|
||||
format_str = "{title}|{start-time}|{end-time}|{start}|{end}|{location}|{uid}|{description}|{organizer}|{url}|{categories}|{status}|{repeat-symbol}"
|
||||
args = ["list", "-f", format_str, start_str, end_str]
|
||||
|
||||
if calendar:
|
||||
args.extend(["-a", calendar])
|
||||
|
||||
result = self._run_khal(args)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"khal list failed: {result.stderr}")
|
||||
return []
|
||||
|
||||
events = []
|
||||
current_day: Optional[date] = None
|
||||
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# Check for day headers (e.g., "Today, 2025-12-18" or "Monday, 2025-12-22")
|
||||
if ", " in line and "|" not in line:
|
||||
try:
|
||||
# Extract date from header
|
||||
date_part = line.split(", ")[-1]
|
||||
current_day = datetime.strptime(date_part, "%Y-%m-%d").date()
|
||||
except ValueError:
|
||||
pass
|
||||
continue
|
||||
|
||||
event = self._parse_event_line(line, current_day)
|
||||
if event:
|
||||
events.append(event)
|
||||
|
||||
# Sort by start time
|
||||
events.sort(key=lambda e: e.start)
|
||||
|
||||
return events
|
||||
|
||||
def get_event(self, uid: str) -> Optional[Event]:
|
||||
"""Get a single event by UID.
|
||||
|
||||
Args:
|
||||
uid: Event unique identifier
|
||||
|
||||
Returns:
|
||||
Event if found, None otherwise
|
||||
"""
|
||||
# khal doesn't have a direct "get by uid" command
|
||||
# We search for it instead
|
||||
result = self._run_khal(["search", uid])
|
||||
|
||||
if result.returncode != 0 or not result.stdout.strip():
|
||||
return None
|
||||
|
||||
# Parse the first result
|
||||
# Search output format is different, so we need to handle it
|
||||
lines = result.stdout.strip().split("\n")
|
||||
if lines:
|
||||
# For now, return None - would need more parsing
|
||||
# This is a limitation of khal's CLI
|
||||
return None
|
||||
|
||||
return None
|
||||
|
||||
def get_calendars(self) -> List[str]:
|
||||
"""Get list of available calendar names.
|
||||
|
||||
Returns:
|
||||
List of calendar names
|
||||
"""
|
||||
result = self._run_khal(["printcalendars"])
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"khal printcalendars failed: {result.stderr}")
|
||||
return []
|
||||
|
||||
calendars = []
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
line = line.strip()
|
||||
if line:
|
||||
calendars.append(line)
|
||||
|
||||
return calendars
|
||||
|
||||
def create_event(
|
||||
self,
|
||||
title: str,
|
||||
start: datetime,
|
||||
end: datetime,
|
||||
calendar: Optional[str] = None,
|
||||
location: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
all_day: bool = False,
|
||||
) -> Event:
|
||||
"""Create a new event.
|
||||
|
||||
Args:
|
||||
title: Event title
|
||||
start: Start datetime
|
||||
end: End datetime
|
||||
calendar: Calendar to add event to
|
||||
location: Event location
|
||||
description: Event description
|
||||
all_day: Whether this is an all-day event
|
||||
|
||||
Returns:
|
||||
The created event
|
||||
"""
|
||||
# Build khal new command
|
||||
# Format: khal new [-a calendar] start end title [:: description] [-l location]
|
||||
if all_day:
|
||||
start_str = start.strftime("%Y-%m-%d")
|
||||
end_str = end.strftime("%Y-%m-%d")
|
||||
else:
|
||||
start_str = start.strftime("%Y-%m-%d %H:%M")
|
||||
end_str = end.strftime("%H:%M") # End time only if same day
|
||||
if end.date() != start.date():
|
||||
end_str = end.strftime("%Y-%m-%d %H:%M")
|
||||
|
||||
args = ["new"]
|
||||
if calendar:
|
||||
args.extend(["-a", calendar])
|
||||
if location:
|
||||
args.extend(["-l", location])
|
||||
|
||||
args.extend([start_str, end_str, title])
|
||||
|
||||
if description:
|
||||
args.extend(["::", description])
|
||||
|
||||
result = self._run_khal(args)
|
||||
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"Failed to create event: {result.stderr}")
|
||||
|
||||
# Return a constructed event (khal doesn't return the created event)
|
||||
return Event(
|
||||
uid=f"new_{title}_{start.isoformat()}",
|
||||
title=title,
|
||||
start=start,
|
||||
end=end,
|
||||
location=location or "",
|
||||
description=description or "",
|
||||
calendar=calendar or "",
|
||||
all_day=all_day,
|
||||
)
|
||||
|
||||
def delete_event(self, uid: str) -> bool:
|
||||
"""Delete an event.
|
||||
|
||||
Args:
|
||||
uid: Event unique identifier
|
||||
|
||||
Returns:
|
||||
True if deleted successfully
|
||||
"""
|
||||
# khal edit with --delete flag
|
||||
# This is tricky because khal edit is interactive
|
||||
# We might need to use khal's Python API directly for this
|
||||
logger.warning("delete_event not fully implemented for khal CLI")
|
||||
return False
|
||||
|
||||
def update_event(
|
||||
self,
|
||||
uid: str,
|
||||
title: Optional[str] = None,
|
||||
start: Optional[datetime] = None,
|
||||
end: Optional[datetime] = None,
|
||||
location: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
) -> Optional[Event]:
|
||||
"""Update an existing event.
|
||||
|
||||
Args:
|
||||
uid: Event unique identifier
|
||||
title: New title (if provided)
|
||||
start: New start time (if provided)
|
||||
end: New end time (if provided)
|
||||
location: New location (if provided)
|
||||
description: New description (if provided)
|
||||
|
||||
Returns:
|
||||
Updated event if successful, None otherwise
|
||||
"""
|
||||
# khal edit is interactive, so this is limited via CLI
|
||||
logger.warning("update_event not fully implemented for khal CLI")
|
||||
return None
|
||||
|
||||
def search_events(self, query: str) -> List[Event]:
|
||||
"""Search for events matching a query string.
|
||||
|
||||
Args:
|
||||
query: Search string to match against event titles and descriptions
|
||||
|
||||
Returns:
|
||||
List of matching events
|
||||
"""
|
||||
if not query:
|
||||
return []
|
||||
|
||||
# Use khal search with custom format
|
||||
format_str = "{title}|{start-time}|{end-time}|{start}|{end}|{location}|{uid}|{description}|{organizer}|{url}|{categories}|{status}|{repeat-symbol}"
|
||||
args = ["search", "-f", format_str, query]
|
||||
|
||||
result = self._run_khal(args)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"khal search failed: {result.stderr}")
|
||||
return []
|
||||
|
||||
events = []
|
||||
for line in result.stdout.strip().split("\n"):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# Skip day headers
|
||||
if ", " in line and "|" not in line:
|
||||
continue
|
||||
|
||||
event = self._parse_event_line(line)
|
||||
if event:
|
||||
events.append(event)
|
||||
|
||||
# Sort by start time
|
||||
events.sort(key=lambda e: e.start)
|
||||
|
||||
return events
|
||||
@@ -19,12 +19,85 @@ logging.getLogger("asyncio").setLevel(logging.ERROR)
|
||||
logging.getLogger("azure").setLevel(logging.ERROR)
|
||||
logging.getLogger("azure.core").setLevel(logging.ERROR)
|
||||
|
||||
# Token cache location - use consistent path regardless of working directory
|
||||
TOKEN_CACHE_DIR = os.path.expanduser("~/.local/share/luk")
|
||||
TOKEN_CACHE_FILE = os.path.join(TOKEN_CACHE_DIR, "token_cache.bin")
|
||||
|
||||
# Legacy cache file (in current working directory) - for migration
|
||||
LEGACY_CACHE_FILE = "token_cache.bin"
|
||||
|
||||
|
||||
def ensure_directory_exists(path):
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
|
||||
|
||||
def _get_cache_file():
|
||||
"""Get the token cache file path, migrating from legacy location if needed."""
|
||||
ensure_directory_exists(TOKEN_CACHE_DIR)
|
||||
|
||||
# If new location exists, use it
|
||||
if os.path.exists(TOKEN_CACHE_FILE):
|
||||
return TOKEN_CACHE_FILE
|
||||
|
||||
# If legacy location exists, migrate it
|
||||
if os.path.exists(LEGACY_CACHE_FILE):
|
||||
try:
|
||||
import shutil
|
||||
|
||||
shutil.copy2(LEGACY_CACHE_FILE, TOKEN_CACHE_FILE)
|
||||
os.remove(LEGACY_CACHE_FILE)
|
||||
except Exception:
|
||||
pass # If migration fails, just use new location
|
||||
return TOKEN_CACHE_FILE
|
||||
|
||||
# Default to new location
|
||||
return TOKEN_CACHE_FILE
|
||||
|
||||
|
||||
def has_valid_cached_token(scopes=None):
|
||||
"""
|
||||
Check if we have a valid cached token (without triggering auth flow).
|
||||
|
||||
Args:
|
||||
scopes: List of scopes to check. If None, uses default scopes.
|
||||
|
||||
Returns:
|
||||
bool: True if a valid cached token exists, False otherwise.
|
||||
"""
|
||||
if scopes is None:
|
||||
scopes = ["https://graph.microsoft.com/Mail.Read"]
|
||||
|
||||
client_id = os.getenv("AZURE_CLIENT_ID")
|
||||
tenant_id = os.getenv("AZURE_TENANT_ID")
|
||||
|
||||
if not client_id or not tenant_id:
|
||||
return False
|
||||
|
||||
cache = msal.SerializableTokenCache()
|
||||
cache_file = _get_cache_file()
|
||||
|
||||
if not os.path.exists(cache_file):
|
||||
return False
|
||||
|
||||
try:
|
||||
cache.deserialize(open(cache_file, "r").read())
|
||||
authority = f"https://login.microsoftonline.com/{tenant_id}"
|
||||
app = msal.PublicClientApplication(
|
||||
client_id, authority=authority, token_cache=cache
|
||||
)
|
||||
accounts = app.get_accounts()
|
||||
|
||||
if not accounts:
|
||||
return False
|
||||
|
||||
# Try silent auth - this will return None if token is expired
|
||||
token_response = app.acquire_token_silent(scopes, account=accounts[0])
|
||||
return token_response is not None and "access_token" in token_response
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def get_access_token(scopes):
|
||||
"""
|
||||
Authenticate with Microsoft Graph API and obtain an access token.
|
||||
@@ -49,9 +122,9 @@ def get_access_token(scopes):
|
||||
"Please set the AZURE_CLIENT_ID and AZURE_TENANT_ID environment variables."
|
||||
)
|
||||
|
||||
# Token cache
|
||||
# Token cache - use consistent location
|
||||
cache = msal.SerializableTokenCache()
|
||||
cache_file = "token_cache.bin"
|
||||
cache_file = _get_cache_file()
|
||||
|
||||
if os.path.exists(cache_file):
|
||||
cache.deserialize(open(cache_file, "r").read())
|
||||
@@ -113,3 +186,78 @@ def get_access_token(scopes):
|
||||
}
|
||||
|
||||
return access_token, headers
|
||||
|
||||
|
||||
def get_smtp_access_token(silent_only: bool = False):
|
||||
"""
|
||||
Get an access token specifically for SMTP sending via Outlook.
|
||||
|
||||
SMTP OAuth2 requires a token with the outlook.office.com resource,
|
||||
which is different from the graph.microsoft.com resource used for
|
||||
other operations.
|
||||
|
||||
Args:
|
||||
silent_only: If True, only attempt silent auth (no interactive prompts).
|
||||
Use this when calling from within a TUI to avoid blocking.
|
||||
|
||||
Returns:
|
||||
str: Access token for SMTP, or None if authentication fails.
|
||||
"""
|
||||
client_id = os.getenv("AZURE_CLIENT_ID")
|
||||
tenant_id = os.getenv("AZURE_TENANT_ID")
|
||||
|
||||
if not client_id or not tenant_id:
|
||||
return None
|
||||
|
||||
# Token cache - use consistent location
|
||||
cache = msal.SerializableTokenCache()
|
||||
cache_file = _get_cache_file()
|
||||
|
||||
if os.path.exists(cache_file):
|
||||
cache.deserialize(open(cache_file, "r").read())
|
||||
|
||||
authority = f"https://login.microsoftonline.com/{tenant_id}"
|
||||
app = msal.PublicClientApplication(
|
||||
client_id, authority=authority, token_cache=cache
|
||||
)
|
||||
accounts = app.get_accounts()
|
||||
|
||||
if not accounts:
|
||||
return None
|
||||
|
||||
# Request token for Outlook SMTP scope
|
||||
smtp_scopes = ["https://outlook.office.com/SMTP.Send"]
|
||||
token_response = app.acquire_token_silent(smtp_scopes, account=accounts[0])
|
||||
|
||||
if token_response and "access_token" in token_response:
|
||||
# Save updated cache
|
||||
with open(cache_file, "w") as f:
|
||||
f.write(cache.serialize())
|
||||
return token_response["access_token"]
|
||||
|
||||
# If silent auth failed and we're not in silent_only mode, try interactive flow
|
||||
if not silent_only:
|
||||
try:
|
||||
flow = app.initiate_device_flow(scopes=smtp_scopes)
|
||||
if "user_code" not in flow:
|
||||
return None
|
||||
|
||||
print(
|
||||
Panel(
|
||||
flow["message"],
|
||||
border_style="magenta",
|
||||
padding=2,
|
||||
title="SMTP Authentication Required",
|
||||
)
|
||||
)
|
||||
|
||||
token_response = app.acquire_token_by_device_flow(flow)
|
||||
|
||||
if token_response and "access_token" in token_response:
|
||||
with open(cache_file, "w") as f:
|
||||
f.write(cache.serialize())
|
||||
return token_response["access_token"]
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
@@ -468,3 +468,76 @@ async def sync_local_calendar_changes(
|
||||
)
|
||||
|
||||
return created_count, deleted_count
|
||||
|
||||
|
||||
async def fetch_pending_invites(headers, days_forward=30):
|
||||
"""
|
||||
Fetch calendar invites that need a response (pending/tentative).
|
||||
|
||||
Args:
|
||||
headers (dict): Headers including authentication.
|
||||
days_forward (int): Number of days to look forward.
|
||||
|
||||
Returns:
|
||||
list: List of invite dictionaries with response status info.
|
||||
"""
|
||||
start_date = datetime.now()
|
||||
end_date = start_date + timedelta(days=days_forward)
|
||||
|
||||
start_date_str = start_date.strftime("%Y-%m-%dT00:00:00Z")
|
||||
end_date_str = end_date.strftime("%Y-%m-%dT23:59:59Z")
|
||||
|
||||
# Fetch events with response status
|
||||
calendar_url = (
|
||||
f"https://graph.microsoft.com/v1.0/me/calendarView?"
|
||||
f"startDateTime={start_date_str}&endDateTime={end_date_str}&"
|
||||
f"$select=id,subject,organizer,start,end,location,isAllDay,responseStatus,isCancelled&"
|
||||
f"$filter=responseStatus/response eq 'notResponded' or responseStatus/response eq 'tentativelyAccepted'&"
|
||||
f"$orderby=start/dateTime"
|
||||
)
|
||||
|
||||
invites = []
|
||||
|
||||
try:
|
||||
response_data = await fetch_with_aiohttp(calendar_url, headers)
|
||||
invites.extend(response_data.get("value", []))
|
||||
|
||||
# Handle pagination
|
||||
next_link = response_data.get("@odata.nextLink")
|
||||
while next_link:
|
||||
response_data = await fetch_with_aiohttp(next_link, headers)
|
||||
invites.extend(response_data.get("value", []))
|
||||
next_link = response_data.get("@odata.nextLink")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error fetching pending invites: {e}")
|
||||
|
||||
return invites
|
||||
|
||||
|
||||
async def respond_to_invite(headers, event_id, response):
|
||||
"""
|
||||
Respond to a calendar invite.
|
||||
|
||||
Args:
|
||||
headers (dict): Authentication headers
|
||||
event_id (str): The ID of the event to respond to
|
||||
response (str): Response type - 'accept', 'tentativelyAccept', or 'decline'
|
||||
|
||||
Returns:
|
||||
bool: True if response was successful
|
||||
"""
|
||||
valid_responses = ["accept", "tentativelyAccept", "decline"]
|
||||
if response not in valid_responses:
|
||||
print(f"Invalid response type: {response}. Must be one of {valid_responses}")
|
||||
return False
|
||||
|
||||
try:
|
||||
response_url = (
|
||||
f"https://graph.microsoft.com/v1.0/me/events/{event_id}/{response}"
|
||||
)
|
||||
status = await post_with_aiohttp(response_url, headers, {})
|
||||
return status in (200, 202)
|
||||
except Exception as e:
|
||||
print(f"Error responding to invite: {e}")
|
||||
return False
|
||||
|
||||
@@ -13,8 +13,8 @@ logging.getLogger("aiohttp.access").setLevel(logging.ERROR)
|
||||
logging.getLogger("urllib3").setLevel(logging.ERROR)
|
||||
logging.getLogger("asyncio").setLevel(logging.ERROR)
|
||||
|
||||
# Define a global semaphore for throttling - reduced for better compliance
|
||||
semaphore = asyncio.Semaphore(2)
|
||||
# Define a global semaphore for throttling - increased for better parallelization
|
||||
semaphore = asyncio.Semaphore(5)
|
||||
|
||||
|
||||
async def _handle_throttling_retry(func, *args, max_retries=3):
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
Mail operations for Microsoft Graph API.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import os
|
||||
import re
|
||||
import glob
|
||||
import json
|
||||
import asyncio
|
||||
from email.parser import Parser
|
||||
from email.utils import getaddresses
|
||||
from typing import List, Dict, Any
|
||||
from typing import List, Dict, Any, Set
|
||||
|
||||
from .client import (
|
||||
fetch_with_aiohttp,
|
||||
@@ -27,6 +29,7 @@ async def fetch_mail_async(
|
||||
task_id,
|
||||
dry_run=False,
|
||||
download_attachments=False,
|
||||
is_cancelled=None,
|
||||
):
|
||||
"""
|
||||
Fetch mail from Microsoft Graph API and save to Maildir.
|
||||
@@ -39,6 +42,7 @@ async def fetch_mail_async(
|
||||
task_id: ID of the task in the progress bar.
|
||||
dry_run (bool): If True, don't actually make changes.
|
||||
download_attachments (bool): If True, download email attachments.
|
||||
is_cancelled (callable, optional): Callback that returns True if task should stop.
|
||||
|
||||
Returns:
|
||||
None
|
||||
@@ -105,59 +109,135 @@ async def fetch_mail_async(
|
||||
|
||||
# Update progress to reflect only the messages we actually need to download
|
||||
progress.update(task_id, total=len(messages_to_download), completed=0)
|
||||
downloaded_count = 0
|
||||
|
||||
for message in messages_to_download:
|
||||
progress.console.print(
|
||||
f"Processing message: {message.get('subject', 'No Subject')}", end="\r"
|
||||
)
|
||||
await save_mime_to_maildir_async(
|
||||
maildir_path,
|
||||
message,
|
||||
attachments_dir,
|
||||
headers,
|
||||
progress,
|
||||
dry_run,
|
||||
download_attachments,
|
||||
)
|
||||
progress.update(task_id, advance=1)
|
||||
progress.update(task_id, completed=len(messages_to_download))
|
||||
progress.console.print(
|
||||
f"\nFinished downloading {len(messages_to_download)} new messages."
|
||||
)
|
||||
# Download messages in parallel batches for better performance
|
||||
# Using 10 concurrent downloads with connection pooling for better throughput
|
||||
BATCH_SIZE = 10
|
||||
|
||||
for i in range(0, len(messages_to_download), BATCH_SIZE):
|
||||
# Check if task was cancelled/disabled
|
||||
if is_cancelled and is_cancelled():
|
||||
progress.console.print("Task cancelled, stopping inbox fetch")
|
||||
break
|
||||
|
||||
batch = messages_to_download[i : i + BATCH_SIZE]
|
||||
|
||||
# Create tasks for parallel download
|
||||
async def download_message(message):
|
||||
progress.console.print(
|
||||
f"Processing message: {message.get('subject', 'No Subject')[:50]}",
|
||||
end="\r",
|
||||
)
|
||||
await save_mime_to_maildir_async(
|
||||
maildir_path,
|
||||
message,
|
||||
attachments_dir,
|
||||
headers,
|
||||
progress,
|
||||
dry_run,
|
||||
download_attachments,
|
||||
)
|
||||
return 1
|
||||
|
||||
# Execute batch in parallel
|
||||
tasks = [download_message(msg) for msg in batch]
|
||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||
|
||||
# Count successful downloads
|
||||
batch_success = sum(1 for r in results if r == 1)
|
||||
downloaded_count += batch_success
|
||||
progress.update(task_id, advance=len(batch))
|
||||
|
||||
# Log any errors
|
||||
for idx, result in enumerate(results):
|
||||
if isinstance(result, Exception):
|
||||
progress.console.print(f"Error downloading message: {result}")
|
||||
|
||||
progress.update(task_id, completed=downloaded_count)
|
||||
progress.console.print(f"\nFinished downloading {downloaded_count} new messages.")
|
||||
progress.console.print(
|
||||
f"Total messages on server: {len(messages)}, Already local: {len(local_msg_ids)}"
|
||||
)
|
||||
|
||||
|
||||
async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=False):
|
||||
def _get_archive_sync_state_path(maildir_path: str) -> str:
|
||||
"""Get the path to the archive sync state file."""
|
||||
return os.path.join(maildir_path, ".Archive", ".sync_state.json")
|
||||
|
||||
|
||||
def _load_archive_sync_state(maildir_path: str) -> Set[str]:
|
||||
"""Load the set of message IDs that have been synced to server."""
|
||||
state_path = _get_archive_sync_state_path(maildir_path)
|
||||
if os.path.exists(state_path):
|
||||
try:
|
||||
with open(state_path, "r") as f:
|
||||
data = json.load(f)
|
||||
return set(data.get("synced_to_server", []))
|
||||
except Exception:
|
||||
pass
|
||||
return set()
|
||||
|
||||
|
||||
def _save_archive_sync_state(maildir_path: str, synced_ids: Set[str]) -> None:
|
||||
"""Save the set of message IDs that have been synced to server."""
|
||||
state_path = _get_archive_sync_state_path(maildir_path)
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
with open(state_path, "w") as f:
|
||||
json.dump({"synced_to_server": list(synced_ids)}, f, indent=2)
|
||||
|
||||
|
||||
async def archive_mail_async(
|
||||
maildir_path, headers, progress, task_id, dry_run=False, is_cancelled=None
|
||||
):
|
||||
"""
|
||||
Archive mail from Maildir to Microsoft Graph API archive folder using batch operations.
|
||||
|
||||
Messages are moved to the server's Archive folder, but local copies are kept.
|
||||
A sync state file tracks which messages have already been synced to avoid
|
||||
re-processing them on subsequent runs.
|
||||
|
||||
Args:
|
||||
maildir_path (str): Path to the Maildir.
|
||||
headers (dict): Headers including authentication.
|
||||
progress: Progress instance for updating progress bars.
|
||||
task_id: ID of the task in the progress bar.
|
||||
dry_run (bool): If True, don't actually make changes.
|
||||
is_cancelled (callable, optional): Callback that returns True if task should stop.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
# Check both possible archive folder names locally
|
||||
# Load already-synced message IDs
|
||||
synced_ids = _load_archive_sync_state(maildir_path)
|
||||
|
||||
# Check both possible archive folder names locally (prefer .Archive)
|
||||
archive_files = []
|
||||
for archive_folder_name in [".Archives", ".Archive"]:
|
||||
for archive_folder_name in [".Archive", ".Archives"]:
|
||||
archive_dir = os.path.join(maildir_path, archive_folder_name)
|
||||
if os.path.exists(archive_dir):
|
||||
archive_files.extend(
|
||||
glob.glob(os.path.join(archive_dir, "**", "*.eml*"), recursive=True)
|
||||
)
|
||||
|
||||
if not archive_files:
|
||||
# Filter out already-synced messages
|
||||
files_to_sync = []
|
||||
for filepath in archive_files:
|
||||
message_id = os.path.basename(filepath).split(".")[0]
|
||||
if message_id not in synced_ids:
|
||||
files_to_sync.append(filepath)
|
||||
|
||||
if not files_to_sync:
|
||||
progress.update(task_id, total=0, completed=0)
|
||||
progress.console.print("No messages to archive")
|
||||
progress.console.print(
|
||||
f"No new messages to archive ({len(archive_files)} already synced)"
|
||||
)
|
||||
return
|
||||
|
||||
progress.update(task_id, total=len(archive_files))
|
||||
progress.update(task_id, total=len(files_to_sync))
|
||||
progress.console.print(
|
||||
f"Found {len(files_to_sync)} new messages to sync to server Archive"
|
||||
)
|
||||
|
||||
# Get archive folder ID from server
|
||||
folder_response = await fetch_with_aiohttp(
|
||||
@@ -179,9 +259,15 @@ async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=F
|
||||
# Process files in batches of 20 (Microsoft Graph batch limit)
|
||||
batch_size = 20
|
||||
successful_moves = []
|
||||
newly_synced_ids: Set[str] = set()
|
||||
|
||||
for i in range(0, len(archive_files), batch_size):
|
||||
batch_files = archive_files[i : i + batch_size]
|
||||
for i in range(0, len(files_to_sync), batch_size):
|
||||
# Check if task was cancelled/disabled
|
||||
if is_cancelled and is_cancelled():
|
||||
progress.console.print("Task cancelled, stopping archive sync")
|
||||
break
|
||||
|
||||
batch_files = files_to_sync[i : i + batch_size]
|
||||
|
||||
# Add small delay between batches to respect API limits
|
||||
if i > 0:
|
||||
@@ -216,23 +302,22 @@ async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=F
|
||||
status = response["status"]
|
||||
|
||||
if status == 201: # 201 Created indicates successful move
|
||||
os.remove(
|
||||
filepath
|
||||
) # Remove the local file since it's now archived on server
|
||||
# Keep local file, just mark as synced
|
||||
newly_synced_ids.add(message_id)
|
||||
successful_moves.append(message_id)
|
||||
progress.console.print(
|
||||
f"Moved message to 'Archive': {message_id}"
|
||||
f"Moved message to server Archive: {message_id}"
|
||||
)
|
||||
elif status == 404:
|
||||
os.remove(
|
||||
filepath
|
||||
) # Remove the file from local archive if not found on server
|
||||
# Message not in Inbox (maybe already archived or deleted on server)
|
||||
# Mark as synced so we don't retry, but keep local copy
|
||||
newly_synced_ids.add(message_id)
|
||||
progress.console.print(
|
||||
f"Message not found on server, removed local copy: {message_id}"
|
||||
f"Message not in Inbox (already archived?): {message_id}"
|
||||
)
|
||||
else:
|
||||
progress.console.print(
|
||||
f"Failed to move message to 'Archive': {message_id}, status: {status}"
|
||||
f"Failed to move message to Archive: {message_id}, status: {status}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
@@ -247,19 +332,19 @@ async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=F
|
||||
{"destinationId": archive_folder_id},
|
||||
)
|
||||
if status == 201:
|
||||
os.remove(filepath)
|
||||
newly_synced_ids.add(message_id)
|
||||
successful_moves.append(message_id)
|
||||
progress.console.print(
|
||||
f"Moved message to 'Archive' (fallback): {message_id}"
|
||||
f"Moved message to server Archive (fallback): {message_id}"
|
||||
)
|
||||
elif status == 404:
|
||||
os.remove(filepath)
|
||||
newly_synced_ids.add(message_id)
|
||||
progress.console.print(
|
||||
f"Message not found on server, removed local copy: {message_id}"
|
||||
f"Message not in Inbox (already archived?): {message_id}"
|
||||
)
|
||||
else:
|
||||
progress.console.print(
|
||||
f"Failed to move message to 'Archive': {message_id}, status: {status}"
|
||||
f"Failed to move message to Archive: {message_id}, status: {status}"
|
||||
)
|
||||
except Exception as individual_error:
|
||||
progress.console.print(
|
||||
@@ -270,18 +355,205 @@ async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=F
|
||||
for filepath in batch_files:
|
||||
message_id = os.path.basename(filepath).split(".")[0]
|
||||
progress.console.print(
|
||||
f"[DRY-RUN] Would move message to 'Archive' folder: {message_id}"
|
||||
f"[DRY-RUN] Would move message to server Archive: {message_id}"
|
||||
)
|
||||
|
||||
progress.advance(task_id, len(batch_files))
|
||||
|
||||
if not dry_run:
|
||||
# Save sync state after each batch for resilience
|
||||
if not dry_run and newly_synced_ids:
|
||||
synced_ids.update(newly_synced_ids)
|
||||
_save_archive_sync_state(maildir_path, synced_ids)
|
||||
|
||||
# Final summary
|
||||
if not dry_run and successful_moves:
|
||||
progress.console.print(
|
||||
f"Successfully archived {len(successful_moves)} messages in batches"
|
||||
f"Successfully synced {len(successful_moves)} messages to server Archive (kept local copies)"
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
async def fetch_archive_mail_async(
|
||||
maildir_path,
|
||||
attachments_dir,
|
||||
headers,
|
||||
progress,
|
||||
task_id,
|
||||
dry_run=False,
|
||||
download_attachments=False,
|
||||
max_messages=None,
|
||||
is_cancelled=None,
|
||||
):
|
||||
"""
|
||||
Fetch archived mail from Microsoft Graph API Archive folder and save to local .Archive Maildir.
|
||||
|
||||
Args:
|
||||
maildir_path (str): Path to the Maildir.
|
||||
attachments_dir (str): Path to save attachments.
|
||||
headers (dict): Headers including authentication.
|
||||
progress: Progress instance for updating progress bars.
|
||||
task_id: ID of the task in the progress bar.
|
||||
dry_run (bool): If True, don't actually make changes.
|
||||
download_attachments (bool): If True, download email attachments.
|
||||
max_messages (int, optional): Maximum number of messages to fetch. None = all.
|
||||
is_cancelled (callable, optional): Callback that returns True if task should stop.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
from src.utils.mail_utils.maildir import save_mime_to_maildir_async
|
||||
|
||||
# Use the well-known 'archive' folder name
|
||||
mail_url = "https://graph.microsoft.com/v1.0/me/mailFolders/archive/messages?$top=100&$orderby=receivedDateTime desc&$select=id,subject,from,toRecipients,ccRecipients,receivedDateTime,isRead"
|
||||
messages = []
|
||||
|
||||
# Fetch the total count of messages in the archive
|
||||
archive_info_url = "https://graph.microsoft.com/v1.0/me/mailFolders/archive"
|
||||
|
||||
try:
|
||||
response = await fetch_with_aiohttp(archive_info_url, headers)
|
||||
total_messages = response.get("totalItemCount", 0) if response else 0
|
||||
except Exception as e:
|
||||
progress.console.print(f"Error fetching archive folder info: {e}")
|
||||
total_messages = 0
|
||||
|
||||
# Apply max_messages limit if specified
|
||||
effective_total = (
|
||||
min(total_messages, max_messages) if max_messages else total_messages
|
||||
)
|
||||
progress.update(task_id, total=effective_total)
|
||||
progress.console.print(
|
||||
f"Archive folder has {total_messages} messages"
|
||||
+ (f", fetching up to {max_messages}" if max_messages else "")
|
||||
)
|
||||
|
||||
# Fetch messages from archive
|
||||
fetched_count = 0
|
||||
while mail_url:
|
||||
try:
|
||||
response_data = await fetch_with_aiohttp(mail_url, headers)
|
||||
except Exception as e:
|
||||
progress.console.print(f"Error fetching archive messages: {e}")
|
||||
break
|
||||
|
||||
batch = response_data.get("value", []) if response_data else []
|
||||
|
||||
# Apply max_messages limit
|
||||
if max_messages and fetched_count + len(batch) > max_messages:
|
||||
batch = batch[: max_messages - fetched_count]
|
||||
messages.extend(batch)
|
||||
fetched_count += len(batch)
|
||||
break
|
||||
|
||||
messages.extend(batch)
|
||||
fetched_count += len(batch)
|
||||
progress.advance(task_id, len(batch))
|
||||
|
||||
# Get the next page URL from @odata.nextLink
|
||||
mail_url = response_data.get("@odata.nextLink") if response_data else None
|
||||
|
||||
# Set up local archive directory paths
|
||||
archive_dir = os.path.join(maildir_path, ".Archive")
|
||||
cur_dir = os.path.join(archive_dir, "cur")
|
||||
new_dir = os.path.join(archive_dir, "new")
|
||||
|
||||
# Ensure directories exist
|
||||
os.makedirs(cur_dir, exist_ok=True)
|
||||
os.makedirs(new_dir, exist_ok=True)
|
||||
os.makedirs(os.path.join(archive_dir, "tmp"), exist_ok=True)
|
||||
|
||||
# Get local message IDs in archive
|
||||
cur_files = set(glob.glob(os.path.join(cur_dir, "*.eml*")))
|
||||
new_files = set(glob.glob(os.path.join(new_dir, "*.eml*")))
|
||||
|
||||
local_msg_ids = set()
|
||||
for filename in set.union(cur_files, new_files):
|
||||
message_id = os.path.basename(filename).split(".")[0]
|
||||
local_msg_ids.add(message_id)
|
||||
|
||||
# Filter messages to only include those not already local
|
||||
messages_to_download = [msg for msg in messages if msg["id"] not in local_msg_ids]
|
||||
|
||||
progress.console.print(
|
||||
f"Found {len(messages)} messages on server Archive, {len(local_msg_ids)} already local"
|
||||
)
|
||||
progress.console.print(
|
||||
f"Downloading {len(messages_to_download)} new archived messages"
|
||||
)
|
||||
|
||||
# Update progress to reflect only the messages we actually need to download
|
||||
progress.update(task_id, total=len(messages_to_download), completed=0)
|
||||
|
||||
# Load sync state once, we'll update it after each batch for resilience
|
||||
synced_ids = _load_archive_sync_state(maildir_path) if not dry_run else set()
|
||||
downloaded_count = 0
|
||||
|
||||
# Download messages in parallel batches for better performance
|
||||
# Using 10 concurrent downloads with connection pooling for better throughput
|
||||
BATCH_SIZE = 10
|
||||
|
||||
for i in range(0, len(messages_to_download), BATCH_SIZE):
|
||||
# Check if task was cancelled/disabled
|
||||
if is_cancelled and is_cancelled():
|
||||
progress.console.print("Task cancelled, stopping archive fetch")
|
||||
break
|
||||
|
||||
batch = messages_to_download[i : i + BATCH_SIZE]
|
||||
batch_msg_ids = []
|
||||
|
||||
# Create tasks for parallel download
|
||||
async def download_message(message):
|
||||
progress.console.print(
|
||||
f"Processing archived message: {message.get('subject', 'No Subject')[:50]}",
|
||||
end="\r",
|
||||
)
|
||||
# Save to .Archive folder instead of main maildir
|
||||
await save_mime_to_maildir_async(
|
||||
archive_dir, # Use archive_dir instead of maildir_path
|
||||
message,
|
||||
attachments_dir,
|
||||
headers,
|
||||
progress,
|
||||
dry_run,
|
||||
download_attachments,
|
||||
)
|
||||
return message["id"]
|
||||
|
||||
# Execute batch in parallel
|
||||
tasks = [download_message(msg) for msg in batch]
|
||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||
|
||||
# Process results and collect successful message IDs
|
||||
for result in results:
|
||||
if isinstance(result, Exception):
|
||||
progress.console.print(f"Error downloading archived message: {result}")
|
||||
elif result:
|
||||
batch_msg_ids.append(result)
|
||||
downloaded_count += 1
|
||||
|
||||
progress.update(task_id, advance=len(batch))
|
||||
|
||||
# Update sync state after each batch (not each message) for resilience + performance
|
||||
if not dry_run and batch_msg_ids:
|
||||
synced_ids.update(batch_msg_ids)
|
||||
_save_archive_sync_state(maildir_path, synced_ids)
|
||||
|
||||
progress.update(task_id, completed=downloaded_count)
|
||||
progress.console.print(
|
||||
f"\nFinished downloading {downloaded_count} archived messages."
|
||||
)
|
||||
progress.console.print(
|
||||
f"Total in server Archive: {total_messages}, Already local: {len(local_msg_ids)}"
|
||||
)
|
||||
|
||||
# Also add any messages we already had locally (from the full server list)
|
||||
# to ensure they're marked as synced
|
||||
if not dry_run and messages:
|
||||
for msg in messages:
|
||||
synced_ids.add(msg["id"])
|
||||
_save_archive_sync_state(maildir_path, synced_ids)
|
||||
|
||||
|
||||
async def delete_mail_async(maildir_path, headers, progress, task_id, dry_run=False):
|
||||
"""
|
||||
Delete mail from Maildir and Microsoft Graph API using batch operations.
|
||||
@@ -589,30 +861,90 @@ def parse_email_for_graph_api(email_content: str) -> Dict[str, Any]:
|
||||
cc_recipients = parse_recipients(msg.get("Cc", ""))
|
||||
bcc_recipients = parse_recipients(msg.get("Bcc", ""))
|
||||
|
||||
# Get body content
|
||||
# Get body content and attachments
|
||||
body_content = ""
|
||||
body_type = "text"
|
||||
attachments: List[Dict[str, Any]] = []
|
||||
|
||||
if msg.is_multipart():
|
||||
for part in msg.walk():
|
||||
if part.get_content_type() == "text/plain":
|
||||
body_content = part.get_payload(decode=True).decode(
|
||||
"utf-8", errors="ignore"
|
||||
)
|
||||
body_type = "text"
|
||||
break
|
||||
elif part.get_content_type() == "text/html":
|
||||
body_content = part.get_payload(decode=True).decode(
|
||||
"utf-8", errors="ignore"
|
||||
)
|
||||
body_type = "html"
|
||||
content_type = part.get_content_type()
|
||||
content_disposition = part.get("Content-Disposition", "")
|
||||
|
||||
# Skip multipart containers
|
||||
if content_type.startswith("multipart/"):
|
||||
continue
|
||||
|
||||
# Handle text/plain body
|
||||
if content_type == "text/plain" and "attachment" not in content_disposition:
|
||||
payload = part.get_payload(decode=True)
|
||||
if payload:
|
||||
body_content = payload.decode("utf-8", errors="ignore")
|
||||
body_type = "text"
|
||||
|
||||
# Handle text/html body
|
||||
elif (
|
||||
content_type == "text/html" and "attachment" not in content_disposition
|
||||
):
|
||||
payload = part.get_payload(decode=True)
|
||||
if payload:
|
||||
body_content = payload.decode("utf-8", errors="ignore")
|
||||
body_type = "html"
|
||||
|
||||
# Handle calendar attachments (text/calendar)
|
||||
elif content_type == "text/calendar":
|
||||
payload = part.get_payload(decode=True)
|
||||
if payload:
|
||||
# Get filename from Content-Disposition or use default
|
||||
filename = part.get_filename() or "invite.ics"
|
||||
|
||||
# Base64 encode the content for Graph API
|
||||
content_bytes = (
|
||||
payload
|
||||
if isinstance(payload, bytes)
|
||||
else payload.encode("utf-8")
|
||||
)
|
||||
|
||||
attachments.append(
|
||||
{
|
||||
"@odata.type": "#microsoft.graph.fileAttachment",
|
||||
"name": filename,
|
||||
"contentType": "text/calendar; method=REPLY",
|
||||
"contentBytes": base64.b64encode(content_bytes).decode(
|
||||
"ascii"
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
# Handle other attachments
|
||||
elif "attachment" in content_disposition or part.get_filename():
|
||||
payload = part.get_payload(decode=True)
|
||||
if payload:
|
||||
filename = part.get_filename() or "attachment"
|
||||
content_bytes = (
|
||||
payload
|
||||
if isinstance(payload, bytes)
|
||||
else payload.encode("utf-8")
|
||||
)
|
||||
attachments.append(
|
||||
{
|
||||
"@odata.type": "#microsoft.graph.fileAttachment",
|
||||
"name": filename,
|
||||
"contentType": content_type,
|
||||
"contentBytes": base64.b64encode(content_bytes).decode(
|
||||
"ascii"
|
||||
),
|
||||
}
|
||||
)
|
||||
else:
|
||||
body_content = msg.get_payload(decode=True).decode("utf-8", errors="ignore")
|
||||
if msg.get_content_type() == "text/html":
|
||||
body_type = "html"
|
||||
payload = msg.get_payload(decode=True)
|
||||
if payload:
|
||||
body_content = payload.decode("utf-8", errors="ignore")
|
||||
if msg.get_content_type() == "text/html":
|
||||
body_type = "html"
|
||||
|
||||
# Build Graph API message
|
||||
message = {
|
||||
message: Dict[str, Any] = {
|
||||
"subject": msg.get("Subject", ""),
|
||||
"body": {"contentType": body_type, "content": body_content},
|
||||
"toRecipients": to_recipients,
|
||||
@@ -620,6 +952,10 @@ def parse_email_for_graph_api(email_content: str) -> Dict[str, Any]:
|
||||
"bccRecipients": bcc_recipients,
|
||||
}
|
||||
|
||||
# Add attachments if present
|
||||
if attachments:
|
||||
message["attachments"] = attachments
|
||||
|
||||
# Add reply-to if present
|
||||
reply_to = msg.get("Reply-To", "")
|
||||
if reply_to:
|
||||
@@ -701,6 +1037,189 @@ async def send_email_async(
|
||||
return False
|
||||
|
||||
|
||||
def send_email_smtp(
|
||||
email_content: str, access_token: str, from_email: str, dry_run: bool = False
|
||||
) -> bool:
|
||||
"""
|
||||
Send email using SMTP with OAuth2 XOAUTH2 authentication.
|
||||
|
||||
This uses Microsoft 365's SMTP AUTH with OAuth2, which requires the
|
||||
SMTP.Send scope (often available when Mail.ReadWrite is granted).
|
||||
|
||||
Args:
|
||||
email_content: Raw email content (RFC 5322 format)
|
||||
access_token: OAuth2 access token
|
||||
from_email: Sender's email address
|
||||
dry_run: If True, don't actually send the email
|
||||
|
||||
Returns:
|
||||
True if email was sent successfully, False otherwise
|
||||
"""
|
||||
import smtplib
|
||||
import logging
|
||||
from email.parser import Parser
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
handlers=[
|
||||
logging.FileHandler(os.path.expanduser("~/Mail/sendmail.log"), mode="a"),
|
||||
],
|
||||
)
|
||||
|
||||
try:
|
||||
# Parse email to get recipients
|
||||
parser = Parser()
|
||||
msg = parser.parsestr(email_content)
|
||||
|
||||
to_addrs = []
|
||||
for header in ["To", "Cc", "Bcc"]:
|
||||
if msg.get(header):
|
||||
addrs = getaddresses([msg.get(header)])
|
||||
to_addrs.extend([addr for name, addr in addrs if addr])
|
||||
|
||||
subject = msg.get("Subject", "(no subject)")
|
||||
|
||||
if dry_run:
|
||||
print(f"[DRY-RUN] Would send email via SMTP: {subject}")
|
||||
print(f"[DRY-RUN] To: {to_addrs}")
|
||||
return True
|
||||
|
||||
logging.info(f"Attempting SMTP send: {subject} to {to_addrs}")
|
||||
|
||||
# Build XOAUTH2 auth string
|
||||
# Format: base64("user=" + user + "\x01auth=Bearer " + token + "\x01\x01")
|
||||
auth_string = f"user={from_email}\x01auth=Bearer {access_token}\x01\x01"
|
||||
|
||||
# Connect to Office 365 SMTP
|
||||
with smtplib.SMTP("smtp.office365.com", 587) as server:
|
||||
server.set_debuglevel(0)
|
||||
server.ehlo()
|
||||
server.starttls()
|
||||
server.ehlo()
|
||||
|
||||
# Authenticate using XOAUTH2
|
||||
server.auth("XOAUTH2", lambda: auth_string)
|
||||
|
||||
# Send the email
|
||||
server.sendmail(from_email, to_addrs, email_content)
|
||||
|
||||
logging.info(f"Successfully sent email via SMTP: {subject}")
|
||||
return True
|
||||
|
||||
except smtplib.SMTPAuthenticationError as e:
|
||||
logging.error(f"SMTP authentication failed: {e}")
|
||||
return False
|
||||
except Exception as e:
|
||||
logging.error(f"SMTP send failed: {e}", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
async def send_email_smtp_async(
|
||||
email_content: str, access_token: str, from_email: str, dry_run: bool = False
|
||||
) -> bool:
|
||||
"""Async wrapper for send_email_smtp."""
|
||||
import asyncio
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(
|
||||
None, send_email_smtp, email_content, access_token, from_email, dry_run
|
||||
)
|
||||
|
||||
|
||||
async def open_email_in_client_async(email_path: str, subject: str) -> bool:
|
||||
"""
|
||||
Open an email file in the default mail client for manual sending.
|
||||
|
||||
This is used as a fallback when automated sending (Graph API, SMTP) fails.
|
||||
The email is copied to a .eml temp file and opened with the system default
|
||||
mail application.
|
||||
|
||||
Args:
|
||||
email_path: Path to the email file in maildir format
|
||||
subject: Email subject for logging/notification purposes
|
||||
|
||||
Returns:
|
||||
True if the email was successfully opened, False otherwise
|
||||
"""
|
||||
import asyncio
|
||||
import subprocess
|
||||
import tempfile
|
||||
import logging
|
||||
from email.parser import Parser
|
||||
from email.utils import parseaddr
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
handlers=[
|
||||
logging.FileHandler(os.path.expanduser("~/Mail/sendmail.log"), mode="a"),
|
||||
],
|
||||
)
|
||||
|
||||
try:
|
||||
# Read and parse the email
|
||||
with open(email_path, "r", encoding="utf-8") as f:
|
||||
email_content = f.read()
|
||||
|
||||
parser = Parser()
|
||||
msg = parser.parsestr(email_content)
|
||||
|
||||
# Extract headers
|
||||
to_header = msg.get("To", "")
|
||||
_, to_email = parseaddr(to_header)
|
||||
from_header = msg.get("From", "")
|
||||
|
||||
# Create a temp .eml file that mail clients can open
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".eml", delete=False, encoding="utf-8"
|
||||
) as tmp:
|
||||
tmp.write(email_content)
|
||||
tmp_path = tmp.name
|
||||
|
||||
# Try to open with Outlook first (better .eml support), fall back to default
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
# Try Outlook
|
||||
result = await loop.run_in_executor(
|
||||
None,
|
||||
lambda: subprocess.run(
|
||||
["open", "-a", "Microsoft Outlook", tmp_path], capture_output=True
|
||||
),
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
# Fall back to default mail client
|
||||
result = await loop.run_in_executor(
|
||||
None, lambda: subprocess.run(["open", tmp_path], capture_output=True)
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
logging.info(f"Opened email in mail client: {subject} (To: {to_email})")
|
||||
|
||||
# Send notification
|
||||
from src.utils.notifications import send_notification
|
||||
|
||||
send_notification(
|
||||
title="Calendar Reply Ready",
|
||||
message=f"To: {to_email}",
|
||||
subtitle=f"Please send: {subject}",
|
||||
sound="default",
|
||||
)
|
||||
return True
|
||||
else:
|
||||
logging.error(f"Failed to open email: {result.stderr.decode()}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error opening email in client: {e}", exc_info=True)
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error opening email in client: {e}", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
async def process_outbox_async(
|
||||
maildir_path: str,
|
||||
org: str,
|
||||
@@ -708,10 +1227,14 @@ async def process_outbox_async(
|
||||
progress,
|
||||
task_id,
|
||||
dry_run: bool = False,
|
||||
access_token: str | None = None,
|
||||
) -> tuple[int, int]:
|
||||
"""
|
||||
Process outbound emails in the outbox queue.
|
||||
|
||||
Tries Graph API first, falls back to SMTP OAuth2 if Graph API fails
|
||||
(e.g., when Mail.Send scope is not available but SMTP.Send is).
|
||||
|
||||
Args:
|
||||
maildir_path: Base maildir path
|
||||
org: Organization name
|
||||
@@ -719,6 +1242,7 @@ async def process_outbox_async(
|
||||
progress: Progress instance for updating progress bars
|
||||
task_id: ID of the task in the progress bar
|
||||
dry_run: If True, don't actually send emails
|
||||
access_token: OAuth2 access token for SMTP fallback
|
||||
|
||||
Returns:
|
||||
Tuple of (successful_sends, failed_sends)
|
||||
@@ -758,8 +1282,59 @@ async def process_outbox_async(
|
||||
with open(email_path, "r", encoding="utf-8") as f:
|
||||
email_content = f.read()
|
||||
|
||||
# Send email
|
||||
if await send_email_async(email_content, headers, dry_run):
|
||||
# Parse email to get from address for SMTP fallback
|
||||
parser = Parser()
|
||||
msg = parser.parsestr(email_content)
|
||||
from_header = msg.get("From", "")
|
||||
subject = msg.get("Subject", "Unknown")
|
||||
# Extract email from "Name <email@domain.com>" format
|
||||
from email.utils import parseaddr
|
||||
|
||||
_, from_email = parseaddr(from_header)
|
||||
|
||||
# Try Graph API first (will fail without Mail.Send scope)
|
||||
send_success = await send_email_async(email_content, headers, dry_run)
|
||||
|
||||
# If Graph API failed, check config for SMTP fallback
|
||||
if not send_success and from_email and not dry_run:
|
||||
import logging
|
||||
from src.mail.config import get_config
|
||||
|
||||
config = get_config()
|
||||
|
||||
if config.mail.enable_smtp_send:
|
||||
# SMTP sending is enabled in config
|
||||
from src.services.microsoft_graph.auth import get_smtp_access_token
|
||||
|
||||
logging.info(
|
||||
f"Graph API send failed, trying SMTP fallback for: {email_file}"
|
||||
)
|
||||
progress.console.print(f" Graph API failed, trying SMTP...")
|
||||
|
||||
# Get SMTP-specific token (different resource than Graph API)
|
||||
# Use silent_only=True to avoid blocking the TUI with auth prompts
|
||||
smtp_token = get_smtp_access_token(silent_only=True)
|
||||
if smtp_token:
|
||||
send_success = await send_email_smtp_async(
|
||||
email_content, smtp_token, from_email, dry_run
|
||||
)
|
||||
if send_success:
|
||||
logging.info(f"SMTP fallback succeeded for: {email_file}")
|
||||
else:
|
||||
logging.error("Failed to get SMTP access token")
|
||||
else:
|
||||
# SMTP disabled - open email in default mail client
|
||||
logging.info(
|
||||
f"Graph API send failed, opening in mail client: {email_file}"
|
||||
)
|
||||
progress.console.print(f" Opening in mail client...")
|
||||
|
||||
if await open_email_in_client_async(email_path, subject):
|
||||
# Mark as handled (move to cur) since user will send manually
|
||||
send_success = True
|
||||
logging.info(f"Opened email in mail client: {email_file}")
|
||||
|
||||
if send_success:
|
||||
# Move to cur directory on success
|
||||
if not dry_run:
|
||||
cur_path = os.path.join(cur_dir, email_file)
|
||||
@@ -778,14 +1353,13 @@ async def process_outbox_async(
|
||||
# Log the failure
|
||||
import logging
|
||||
|
||||
logging.error(f"Failed to send email: {email_file}")
|
||||
logging.error(
|
||||
f"Failed to send email via Graph API and SMTP: {email_file}"
|
||||
)
|
||||
|
||||
# Send notification about failure
|
||||
from src.utils.notifications import send_notification
|
||||
|
||||
parser = Parser()
|
||||
msg = parser.parsestr(email_content)
|
||||
subject = msg.get("Subject", "Unknown")
|
||||
send_notification(
|
||||
title="Email Send Failed",
|
||||
message=f"Failed to send: {subject}",
|
||||
@@ -832,3 +1406,98 @@ async def process_outbox_async(
|
||||
progress.console.print(f"✗ Failed to send {failed_sends} emails")
|
||||
|
||||
return successful_sends, failed_sends
|
||||
|
||||
|
||||
async def fetch_message_ics_attachment(
|
||||
graph_message_id: str,
|
||||
headers: Dict[str, str],
|
||||
) -> tuple[str | None, bool]:
|
||||
"""
|
||||
Fetch the ICS calendar attachment from a message via Microsoft Graph API.
|
||||
|
||||
Args:
|
||||
graph_message_id: The Microsoft Graph API message ID
|
||||
headers: Authentication headers for Microsoft Graph API
|
||||
|
||||
Returns:
|
||||
Tuple of (ICS content string or None, success boolean)
|
||||
"""
|
||||
from urllib.parse import quote
|
||||
|
||||
try:
|
||||
# URL-encode the message ID (may contain special chars like = + /)
|
||||
encoded_id = quote(graph_message_id, safe="")
|
||||
|
||||
# Fetch attachments list for the message
|
||||
attachments_url = (
|
||||
f"https://graph.microsoft.com/v1.0/me/messages/{encoded_id}/attachments"
|
||||
)
|
||||
|
||||
response = await fetch_with_aiohttp(attachments_url, headers)
|
||||
|
||||
attachments = response.get("value", [])
|
||||
|
||||
for attachment in attachments:
|
||||
content_type = (attachment.get("contentType") or "").lower()
|
||||
name = (attachment.get("name") or "").lower()
|
||||
|
||||
# Look for calendar attachments (text/calendar or application/ics)
|
||||
if "calendar" in content_type or name.endswith(".ics"):
|
||||
# contentBytes is base64-encoded
|
||||
content_bytes = attachment.get("contentBytes")
|
||||
if content_bytes:
|
||||
import base64
|
||||
|
||||
decoded = base64.b64decode(content_bytes)
|
||||
return decoded.decode("utf-8", errors="replace"), True
|
||||
|
||||
# No ICS attachment found
|
||||
return None, True
|
||||
|
||||
except Exception as e:
|
||||
import logging
|
||||
|
||||
logging.error(f"Error fetching ICS attachment: {e}")
|
||||
return None, False
|
||||
|
||||
|
||||
async def fetch_message_with_ics(
|
||||
graph_message_id: str,
|
||||
headers: Dict[str, str],
|
||||
) -> tuple[str | None, bool]:
|
||||
"""
|
||||
Fetch the full MIME content of a message including ICS attachment.
|
||||
|
||||
This fetches the raw $value of the message which includes all MIME parts.
|
||||
|
||||
Args:
|
||||
graph_message_id: The Microsoft Graph API message ID
|
||||
headers: Authentication headers for Microsoft Graph API
|
||||
|
||||
Returns:
|
||||
Tuple of (raw MIME content or None, success boolean)
|
||||
"""
|
||||
import aiohttp
|
||||
|
||||
try:
|
||||
# Fetch the raw MIME content
|
||||
mime_url = (
|
||||
f"https://graph.microsoft.com/v1.0/me/messages/{graph_message_id}/$value"
|
||||
)
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(mime_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
content = await response.text()
|
||||
return content, True
|
||||
else:
|
||||
import logging
|
||||
|
||||
logging.error(f"Failed to fetch MIME content: {response.status}")
|
||||
return None, False
|
||||
|
||||
except Exception as e:
|
||||
import logging
|
||||
|
||||
logging.error(f"Error fetching MIME content: {e}")
|
||||
return None, False
|
||||
|
||||
@@ -6,7 +6,7 @@ import logging
|
||||
import shlex
|
||||
from typing import Tuple, List, Dict, Any, Optional
|
||||
|
||||
from src.maildir_gtd.config import get_config
|
||||
from src.mail.config import get_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
18
src/tasks/__init__.py
Normal file
18
src/tasks/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""Tasks TUI module for managing tasks via dstask/taskwarrior."""
|
||||
|
||||
from .config import TasksAppConfig, get_config, reload_config
|
||||
from .backend import Task, TaskBackend, TaskPriority, TaskStatus, Project
|
||||
from .app import TasksApp, run_app
|
||||
|
||||
__all__ = [
|
||||
"TasksAppConfig",
|
||||
"get_config",
|
||||
"reload_config",
|
||||
"Task",
|
||||
"TaskBackend",
|
||||
"TaskPriority",
|
||||
"TaskStatus",
|
||||
"Project",
|
||||
"TasksApp",
|
||||
"run_app",
|
||||
]
|
||||
1039
src/tasks/app.py
Normal file
1039
src/tasks/app.py
Normal file
File diff suppressed because it is too large
Load Diff
304
src/tasks/backend.py
Normal file
304
src/tasks/backend.py
Normal file
@@ -0,0 +1,304 @@
|
||||
"""Task backend abstraction for Tasks TUI.
|
||||
|
||||
This module defines the abstract interface that all task backends must implement,
|
||||
allowing the TUI to work with different task management systems (dstask, taskwarrior, etc.)
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class TaskStatus(Enum):
|
||||
"""Task status values."""
|
||||
|
||||
PENDING = "pending"
|
||||
ACTIVE = "active"
|
||||
DONE = "done"
|
||||
DELETED = "deleted"
|
||||
|
||||
|
||||
class TaskPriority(Enum):
|
||||
"""Task priority levels (P0 = highest, P3 = lowest)."""
|
||||
|
||||
P0 = "P0" # Critical
|
||||
P1 = "P1" # High
|
||||
P2 = "P2" # Normal
|
||||
P3 = "P3" # Low
|
||||
|
||||
@classmethod
|
||||
def from_string(cls, value: str) -> "TaskPriority":
|
||||
"""Parse priority from string."""
|
||||
value = value.upper().strip()
|
||||
if value in ("P0", "0", "CRITICAL"):
|
||||
return cls.P0
|
||||
elif value in ("P1", "1", "HIGH", "H"):
|
||||
return cls.P1
|
||||
elif value in ("P2", "2", "NORMAL", "MEDIUM", "M"):
|
||||
return cls.P2
|
||||
elif value in ("P3", "3", "LOW", "L"):
|
||||
return cls.P3
|
||||
return cls.P2 # Default to normal
|
||||
|
||||
|
||||
@dataclass
|
||||
class Task:
|
||||
"""Unified task representation across backends."""
|
||||
|
||||
uuid: str
|
||||
id: int # Short numeric ID for display
|
||||
summary: str
|
||||
status: TaskStatus = TaskStatus.PENDING
|
||||
priority: TaskPriority = TaskPriority.P2
|
||||
project: str = ""
|
||||
tags: list[str] = field(default_factory=list)
|
||||
notes: str = ""
|
||||
due: Optional[datetime] = None
|
||||
created: Optional[datetime] = None
|
||||
resolved: Optional[datetime] = None
|
||||
|
||||
@property
|
||||
def is_overdue(self) -> bool:
|
||||
"""Check if task is overdue."""
|
||||
if self.due is None or self.status == TaskStatus.DONE:
|
||||
return False
|
||||
# Use timezone-aware now() if due date is timezone-aware
|
||||
now = datetime.now(timezone.utc) if self.due.tzinfo else datetime.now()
|
||||
return now > self.due
|
||||
|
||||
|
||||
@dataclass
|
||||
class Project:
|
||||
"""Project information."""
|
||||
|
||||
name: str
|
||||
task_count: int = 0
|
||||
resolved_count: int = 0
|
||||
active: bool = True
|
||||
priority: TaskPriority = TaskPriority.P2
|
||||
|
||||
|
||||
class TaskBackend(ABC):
|
||||
"""Abstract base class for task management backends."""
|
||||
|
||||
@abstractmethod
|
||||
def get_tasks(
|
||||
self,
|
||||
project: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
status: Optional[TaskStatus] = None,
|
||||
) -> list[Task]:
|
||||
"""Get tasks, optionally filtered by project, tags, or status.
|
||||
|
||||
Args:
|
||||
project: Filter by project name
|
||||
tags: Filter by tags (tasks must have all specified tags)
|
||||
status: Filter by status
|
||||
|
||||
Returns:
|
||||
List of matching tasks
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_next_tasks(self) -> list[Task]:
|
||||
"""Get the 'next' tasks to work on (priority-sorted actionable tasks)."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_task(self, task_id: str) -> Optional[Task]:
|
||||
"""Get a single task by ID or UUID.
|
||||
|
||||
Args:
|
||||
task_id: Task ID (numeric) or UUID
|
||||
|
||||
Returns:
|
||||
Task if found, None otherwise
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_task(
|
||||
self,
|
||||
summary: str,
|
||||
project: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
priority: Optional[TaskPriority] = None,
|
||||
due: Optional[datetime] = None,
|
||||
notes: Optional[str] = None,
|
||||
) -> Task:
|
||||
"""Create a new task.
|
||||
|
||||
Args:
|
||||
summary: Task description
|
||||
project: Project name
|
||||
tags: List of tags
|
||||
priority: Task priority
|
||||
due: Due date
|
||||
notes: Additional notes
|
||||
|
||||
Returns:
|
||||
The created task
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def complete_task(self, task_id: str) -> bool:
|
||||
"""Mark a task as complete.
|
||||
|
||||
Args:
|
||||
task_id: Task ID or UUID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def delete_task(self, task_id: str) -> bool:
|
||||
"""Delete a task.
|
||||
|
||||
Args:
|
||||
task_id: Task ID or UUID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def start_task(self, task_id: str) -> bool:
|
||||
"""Start working on a task (mark as active).
|
||||
|
||||
Args:
|
||||
task_id: Task ID or UUID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def stop_task(self, task_id: str) -> bool:
|
||||
"""Stop working on a task (mark as pending).
|
||||
|
||||
Args:
|
||||
task_id: Task ID or UUID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def modify_task(
|
||||
self,
|
||||
task_id: str,
|
||||
summary: Optional[str] = None,
|
||||
project: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
priority: Optional[TaskPriority] = None,
|
||||
due: Optional[datetime] = None,
|
||||
notes: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""Modify a task.
|
||||
|
||||
Args:
|
||||
task_id: Task ID or UUID
|
||||
summary: New summary (if provided)
|
||||
project: New project (if provided)
|
||||
tags: New tags (if provided)
|
||||
priority: New priority (if provided)
|
||||
due: New due date (if provided)
|
||||
notes: New notes (if provided)
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_projects(self) -> list[Project]:
|
||||
"""Get all projects.
|
||||
|
||||
Returns:
|
||||
List of projects with task counts
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_tags(self) -> list[str]:
|
||||
"""Get all tags.
|
||||
|
||||
Returns:
|
||||
List of tag names
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def sync(self) -> bool:
|
||||
"""Sync tasks with remote (if supported).
|
||||
|
||||
Returns:
|
||||
True if successful (or not applicable)
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def edit_task_interactive(self, task_id: str) -> bool:
|
||||
"""Open task in editor for interactive editing.
|
||||
|
||||
Args:
|
||||
task_id: Task ID or UUID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def edit_note_interactive(self, task_id: str) -> bool:
|
||||
"""Open task notes in editor for interactive editing.
|
||||
|
||||
Args:
|
||||
task_id: Task ID or UUID
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_context(self) -> Optional[str]:
|
||||
"""Get the current context filter.
|
||||
|
||||
Returns:
|
||||
Current context string, or None if no context is set
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_context(self, context: Optional[str]) -> bool:
|
||||
"""Set the context filter.
|
||||
|
||||
Args:
|
||||
context: Context string (e.g., "+work", "project:foo") or None to clear
|
||||
|
||||
Returns:
|
||||
True if successful
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_contexts(self) -> list[str]:
|
||||
"""Get available predefined contexts.
|
||||
|
||||
For taskwarrior, returns named contexts from config.
|
||||
For dstask, may return common tag-based contexts.
|
||||
|
||||
Returns:
|
||||
List of context names/filters
|
||||
"""
|
||||
pass
|
||||
207
src/tasks/config.py
Normal file
207
src/tasks/config.py
Normal file
@@ -0,0 +1,207 @@
|
||||
"""Configuration system for Tasks TUI using Pydantic."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Literal, Optional
|
||||
|
||||
import toml
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BackendConfig(BaseModel):
|
||||
"""Configuration for task management backend."""
|
||||
|
||||
# Which backend to use: dstask or taskwarrior
|
||||
backend: Literal["dstask", "taskwarrior"] = "dstask"
|
||||
|
||||
# Path to dstask binary
|
||||
dstask_path: str = Field(
|
||||
default_factory=lambda: str(Path.home() / ".local" / "bin" / "dstask")
|
||||
)
|
||||
|
||||
# Path to taskwarrior binary
|
||||
taskwarrior_path: str = "task"
|
||||
|
||||
|
||||
class DisplayConfig(BaseModel):
|
||||
"""Configuration for task list display."""
|
||||
|
||||
# Columns to show in the task table
|
||||
# Available: id, priority, project, tags, summary, due, status
|
||||
columns: list[str] = Field(
|
||||
default_factory=lambda: ["id", "priority", "summary", "due", "project", "tags"]
|
||||
)
|
||||
|
||||
# Column widths (0 = auto/flexible, takes remaining space)
|
||||
column_widths: dict[str, int] = Field(
|
||||
default_factory=lambda: {
|
||||
"id": 3,
|
||||
"priority": 5,
|
||||
"project": 12,
|
||||
"tags": 12,
|
||||
"summary": 0, # auto-expand to fill remaining space
|
||||
"due": 10,
|
||||
"status": 8,
|
||||
}
|
||||
)
|
||||
|
||||
# Date format for due dates
|
||||
date_format: str = "%Y-%m-%d"
|
||||
|
||||
# Show completed tasks
|
||||
show_completed: bool = False
|
||||
|
||||
# Default sort column
|
||||
default_sort: str = "priority"
|
||||
|
||||
# Sort direction (asc or desc)
|
||||
sort_direction: Literal["asc", "desc"] = "asc"
|
||||
|
||||
# Notes pane height as percentage (10-90)
|
||||
notes_pane_height: int = 50
|
||||
|
||||
# Notes editor mode: "external" uses $EDITOR, "builtin" uses TextArea widget
|
||||
notes_editor: Literal["external", "builtin"] = "external"
|
||||
|
||||
|
||||
class IconsConfig(BaseModel):
|
||||
"""NerdFont icons for task display."""
|
||||
|
||||
# Priority icons (P0 = highest, P3 = lowest)
|
||||
priority_p0: str = "\uf06a" # nf-fa-exclamation_circle (critical)
|
||||
priority_p1: str = "\uf062" # nf-fa-arrow_up (high)
|
||||
priority_p2: str = "\uf068" # nf-fa-minus (normal)
|
||||
priority_p3: str = "\uf063" # nf-fa-arrow_down (low)
|
||||
|
||||
# Status icons
|
||||
status_pending: str = "\uf10c" # nf-fa-circle_o (empty circle)
|
||||
status_active: str = "\uf192" # nf-fa-dot_circle_o (dot circle)
|
||||
status_done: str = "\uf058" # nf-fa-check_circle (checked)
|
||||
|
||||
# Other icons
|
||||
project: str = "\uf07b" # nf-fa-folder
|
||||
tag: str = "\uf02b" # nf-fa-tag
|
||||
due: str = "\uf073" # nf-fa-calendar
|
||||
overdue: str = "\uf071" # nf-fa-warning
|
||||
|
||||
|
||||
class KeybindingsConfig(BaseModel):
|
||||
"""Keybinding customization."""
|
||||
|
||||
# Navigation
|
||||
next_task: str = "j"
|
||||
prev_task: str = "k"
|
||||
first_task: str = "g"
|
||||
last_task: str = "G"
|
||||
|
||||
# Actions
|
||||
complete_task: str = "d"
|
||||
edit_task: str = "e"
|
||||
add_task: str = "a"
|
||||
delete_task: str = "x"
|
||||
start_task: str = "s"
|
||||
stop_task: str = "S"
|
||||
|
||||
# Filtering
|
||||
filter_project: str = "p"
|
||||
filter_tag: str = "t"
|
||||
clear_filters: str = "c"
|
||||
|
||||
# Other
|
||||
refresh: str = "r"
|
||||
sync: str = "y"
|
||||
quit: str = "q"
|
||||
help: str = "?"
|
||||
|
||||
|
||||
class ThemeConfig(BaseModel):
|
||||
"""Theme/appearance settings."""
|
||||
|
||||
# Priority colors (CSS color names or hex)
|
||||
color_p0: str = "red"
|
||||
color_p1: str = "orange"
|
||||
color_p2: str = "yellow"
|
||||
color_p3: str = "gray"
|
||||
|
||||
# Status colors
|
||||
color_pending: str = "white"
|
||||
color_active: str = "cyan"
|
||||
color_done: str = "green"
|
||||
|
||||
# Overdue color
|
||||
color_overdue: str = "red"
|
||||
|
||||
|
||||
class TasksAppConfig(BaseModel):
|
||||
"""Main configuration for Tasks TUI."""
|
||||
|
||||
backend: BackendConfig = Field(default_factory=BackendConfig)
|
||||
display: DisplayConfig = Field(default_factory=DisplayConfig)
|
||||
icons: IconsConfig = Field(default_factory=IconsConfig)
|
||||
keybindings: KeybindingsConfig = Field(default_factory=KeybindingsConfig)
|
||||
theme: ThemeConfig = Field(default_factory=ThemeConfig)
|
||||
|
||||
@classmethod
|
||||
def get_config_path(cls) -> Path:
|
||||
"""Get the path to the config file."""
|
||||
# Check environment variable first
|
||||
env_path = os.getenv("LUK_TASKS_CONFIG")
|
||||
if env_path:
|
||||
return Path(env_path)
|
||||
|
||||
# Default to ~/.config/luk/tasks.toml
|
||||
return Path.home() / ".config" / "luk" / "tasks.toml"
|
||||
|
||||
@classmethod
|
||||
def load(cls, config_path: Optional[Path] = None) -> "TasksAppConfig":
|
||||
"""Load config from TOML file with defaults for missing values."""
|
||||
if config_path is None:
|
||||
config_path = cls.get_config_path()
|
||||
|
||||
if config_path.exists():
|
||||
try:
|
||||
with open(config_path, "r") as f:
|
||||
data = toml.load(f)
|
||||
logger.info(f"Loaded config from {config_path}")
|
||||
return cls.model_validate(data)
|
||||
except Exception as e:
|
||||
logger.warning(f"Error loading config from {config_path}: {e}")
|
||||
logger.warning("Using default configuration")
|
||||
return cls()
|
||||
else:
|
||||
logger.info(f"No config file at {config_path}, using defaults")
|
||||
return cls()
|
||||
|
||||
def save(self, config_path: Optional[Path] = None) -> None:
|
||||
"""Save current config to TOML file."""
|
||||
if config_path is None:
|
||||
config_path = self.get_config_path()
|
||||
|
||||
# Ensure parent directory exists
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with open(config_path, "w") as f:
|
||||
toml.dump(self.model_dump(), f)
|
||||
logger.info(f"Saved config to {config_path}")
|
||||
|
||||
|
||||
# Global config instance (lazy-loaded)
|
||||
_config: Optional[TasksAppConfig] = None
|
||||
|
||||
|
||||
def get_config() -> TasksAppConfig:
|
||||
"""Get the global config instance, loading it if necessary."""
|
||||
global _config
|
||||
if _config is None:
|
||||
_config = TasksAppConfig.load()
|
||||
return _config
|
||||
|
||||
|
||||
def reload_config() -> TasksAppConfig:
|
||||
"""Force reload of the config from disk."""
|
||||
global _config
|
||||
_config = TasksAppConfig.load()
|
||||
return _config
|
||||
151
src/tasks/screens/AddTaskScreen.py
Normal file
151
src/tasks/screens/AddTaskScreen.py
Normal file
@@ -0,0 +1,151 @@
|
||||
"""Add Task modal screen for Tasks TUI."""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from textual import on
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Button, Input, Label
|
||||
|
||||
from src.tasks.widgets.AddTaskForm import AddTaskForm, TaskFormData
|
||||
|
||||
|
||||
class AddTaskScreen(ModalScreen[Optional[TaskFormData]]):
|
||||
"""Modal screen for adding a new task."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("ctrl+s", "submit", "Save"),
|
||||
]
|
||||
|
||||
DEFAULT_CSS = """
|
||||
AddTaskScreen {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
AddTaskScreen #add-task-container {
|
||||
width: 80%;
|
||||
height: auto;
|
||||
max-height: 85%;
|
||||
background: $surface;
|
||||
border: thick $primary;
|
||||
padding: 1;
|
||||
}
|
||||
|
||||
AddTaskScreen #add-task-title {
|
||||
text-style: bold;
|
||||
width: 100%;
|
||||
height: 1;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
AddTaskScreen #add-task-content {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
AddTaskScreen #add-task-form {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddTaskScreen #add-task-sidebar {
|
||||
width: 16;
|
||||
height: auto;
|
||||
padding: 1;
|
||||
align: center top;
|
||||
}
|
||||
|
||||
AddTaskScreen #add-task-sidebar Button {
|
||||
width: 100%;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
AddTaskScreen #help-text {
|
||||
width: 100%;
|
||||
height: 1;
|
||||
color: $text-muted;
|
||||
text-align: center;
|
||||
margin-top: 1;
|
||||
}
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
projects: list[str] | None = None,
|
||||
initial_data: TaskFormData | None = None,
|
||||
mail_link: str | None = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the add task screen.
|
||||
|
||||
Args:
|
||||
projects: List of available project names for the dropdown
|
||||
initial_data: Pre-populate form with this data
|
||||
mail_link: Optional mail link to prepend to notes
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._projects = projects or []
|
||||
self._initial_data = initial_data
|
||||
self._mail_link = mail_link
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical(id="add-task-container"):
|
||||
yield Label("Add New Task", id="add-task-title")
|
||||
|
||||
with Horizontal(id="add-task-content"):
|
||||
yield AddTaskForm(
|
||||
projects=self._projects,
|
||||
initial_data=self._initial_data,
|
||||
show_notes=True,
|
||||
mail_link=self._mail_link,
|
||||
id="add-task-form",
|
||||
)
|
||||
|
||||
with Vertical(id="add-task-sidebar"):
|
||||
yield Button("Create", id="create", variant="primary")
|
||||
yield Button("Cancel", id="cancel", variant="default")
|
||||
|
||||
yield Label("Ctrl+S to save, Escape to cancel", id="help-text")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the summary input."""
|
||||
try:
|
||||
form = self.query_one("#add-task-form", AddTaskForm)
|
||||
summary_input = form.query_one("#summary-input")
|
||||
summary_input.focus()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@on(Button.Pressed, "#create")
|
||||
def handle_create(self) -> None:
|
||||
"""Handle create button press."""
|
||||
self.action_submit()
|
||||
|
||||
@on(Button.Pressed, "#cancel")
|
||||
def handle_cancel(self) -> None:
|
||||
"""Handle cancel button press."""
|
||||
self.action_cancel()
|
||||
|
||||
@on(Input.Submitted, "#summary-input")
|
||||
def handle_summary_submit(self) -> None:
|
||||
"""Handle Enter key in summary input."""
|
||||
self.action_submit()
|
||||
|
||||
def action_submit(self) -> None:
|
||||
"""Validate and submit the form."""
|
||||
form = self.query_one("#add-task-form", AddTaskForm)
|
||||
is_valid, error = form.validate()
|
||||
|
||||
if not is_valid:
|
||||
self.notify(error, severity="error")
|
||||
return
|
||||
|
||||
data = form.get_form_data()
|
||||
self.dismiss(data)
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
"""Cancel and dismiss."""
|
||||
self.dismiss(None)
|
||||
390
src/tasks/screens/FilterScreens.py
Normal file
390
src/tasks/screens/FilterScreens.py
Normal file
@@ -0,0 +1,390 @@
|
||||
"""Filter selection screens for Tasks TUI."""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from textual import on
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Label, SelectionList, Button, RadioButton, RadioSet
|
||||
from textual.widgets.selection_list import Selection
|
||||
|
||||
|
||||
class ProjectFilterScreen(ModalScreen[Optional[str]]):
|
||||
"""Modal screen for selecting a project filter."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("enter", "select", "Select"),
|
||||
]
|
||||
|
||||
DEFAULT_CSS = """
|
||||
ProjectFilterScreen {
|
||||
align: center bottom;
|
||||
}
|
||||
|
||||
ProjectFilterScreen #filter-container {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
max-height: 12;
|
||||
background: $surface;
|
||||
border-top: thick $primary;
|
||||
padding: 1 2;
|
||||
layout: horizontal;
|
||||
}
|
||||
|
||||
ProjectFilterScreen #filter-title {
|
||||
text-style: bold;
|
||||
width: auto;
|
||||
height: 1;
|
||||
margin-right: 2;
|
||||
padding-top: 1;
|
||||
}
|
||||
|
||||
ProjectFilterScreen SelectionList {
|
||||
width: 1fr;
|
||||
height: auto;
|
||||
max-height: 8;
|
||||
}
|
||||
|
||||
ProjectFilterScreen #filter-buttons {
|
||||
width: auto;
|
||||
height: auto;
|
||||
layout: horizontal;
|
||||
align: center middle;
|
||||
margin-left: 2;
|
||||
}
|
||||
|
||||
ProjectFilterScreen Button {
|
||||
margin: 0 1;
|
||||
}
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
projects: list[tuple[str, int]], # List of (project_name, task_count)
|
||||
current_filter: Optional[str] = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the project filter screen.
|
||||
|
||||
Args:
|
||||
projects: List of (project_name, task_count) tuples
|
||||
current_filter: Currently selected project filter
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._projects = projects
|
||||
self._current_filter = current_filter
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Horizontal(id="filter-container"):
|
||||
yield Label("Project:", id="filter-title")
|
||||
|
||||
selections = [
|
||||
Selection(
|
||||
f"{name} ({count})",
|
||||
name,
|
||||
initial_state=name == self._current_filter,
|
||||
)
|
||||
for name, count in self._projects
|
||||
]
|
||||
|
||||
yield SelectionList[str](*selections, id="project-list")
|
||||
|
||||
with Horizontal(id="filter-buttons"):
|
||||
yield Button("Cancel", id="cancel", variant="default")
|
||||
yield Button("Clear", id="clear", variant="warning")
|
||||
yield Button("Apply", id="apply", variant="primary")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the selection list."""
|
||||
self.query_one("#project-list", SelectionList).focus()
|
||||
|
||||
@on(Button.Pressed, "#apply")
|
||||
def handle_apply(self) -> None:
|
||||
selection_list = self.query_one("#project-list", SelectionList)
|
||||
selected = list(selection_list.selected)
|
||||
if selected:
|
||||
self.dismiss(selected[0]) # Return first selected project
|
||||
else:
|
||||
self.dismiss(None)
|
||||
|
||||
@on(Button.Pressed, "#clear")
|
||||
def handle_clear(self) -> None:
|
||||
self.dismiss(None)
|
||||
|
||||
@on(Button.Pressed, "#cancel")
|
||||
def handle_cancel(self) -> None:
|
||||
self.dismiss(self._current_filter) # Return unchanged
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
self.dismiss(self._current_filter)
|
||||
|
||||
def action_select(self) -> None:
|
||||
self.handle_apply()
|
||||
|
||||
|
||||
class TagFilterScreen(ModalScreen[list[str]]):
|
||||
"""Modal screen for selecting tag filters (multi-select)."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("enter", "select", "Select"),
|
||||
]
|
||||
|
||||
DEFAULT_CSS = """
|
||||
TagFilterScreen {
|
||||
align: center bottom;
|
||||
}
|
||||
|
||||
TagFilterScreen #filter-container {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
max-height: 12;
|
||||
background: $surface;
|
||||
border-top: thick $primary;
|
||||
padding: 1 2;
|
||||
layout: horizontal;
|
||||
}
|
||||
|
||||
TagFilterScreen #filter-title {
|
||||
text-style: bold;
|
||||
width: auto;
|
||||
height: 1;
|
||||
margin-right: 2;
|
||||
padding-top: 1;
|
||||
}
|
||||
|
||||
TagFilterScreen SelectionList {
|
||||
width: 1fr;
|
||||
height: auto;
|
||||
max-height: 8;
|
||||
}
|
||||
|
||||
TagFilterScreen #filter-buttons {
|
||||
width: auto;
|
||||
height: auto;
|
||||
layout: horizontal;
|
||||
align: center middle;
|
||||
margin-left: 2;
|
||||
}
|
||||
|
||||
TagFilterScreen Button {
|
||||
margin: 0 1;
|
||||
}
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
tags: list[str],
|
||||
current_filters: list[str],
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the tag filter screen.
|
||||
|
||||
Args:
|
||||
tags: List of available tags
|
||||
current_filters: Currently selected tag filters
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._tags = tags
|
||||
self._current_filters = current_filters
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Horizontal(id="filter-container"):
|
||||
yield Label("Tags:", id="filter-title")
|
||||
|
||||
selections = [
|
||||
Selection(
|
||||
f"+{tag}",
|
||||
tag,
|
||||
initial_state=tag in self._current_filters,
|
||||
)
|
||||
for tag in self._tags
|
||||
]
|
||||
|
||||
yield SelectionList[str](*selections, id="tag-list")
|
||||
|
||||
with Horizontal(id="filter-buttons"):
|
||||
yield Button("Cancel", id="cancel", variant="default")
|
||||
yield Button("Clear", id="clear", variant="warning")
|
||||
yield Button("Apply", id="apply", variant="primary")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the selection list."""
|
||||
self.query_one("#tag-list", SelectionList).focus()
|
||||
|
||||
@on(Button.Pressed, "#apply")
|
||||
def handle_apply(self) -> None:
|
||||
selection_list = self.query_one("#tag-list", SelectionList)
|
||||
selected = list(selection_list.selected)
|
||||
self.dismiss(selected)
|
||||
|
||||
@on(Button.Pressed, "#clear")
|
||||
def handle_clear(self) -> None:
|
||||
self.dismiss([])
|
||||
|
||||
@on(Button.Pressed, "#cancel")
|
||||
def handle_cancel(self) -> None:
|
||||
self.dismiss(self._current_filters) # Return unchanged
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
self.dismiss(self._current_filters)
|
||||
|
||||
def action_select(self) -> None:
|
||||
self.handle_apply()
|
||||
|
||||
|
||||
class SortConfig:
|
||||
"""Configuration for sort settings."""
|
||||
|
||||
def __init__(self, column: str = "priority", ascending: bool = True):
|
||||
self.column = column
|
||||
self.ascending = ascending
|
||||
|
||||
|
||||
class SortScreen(ModalScreen[Optional[SortConfig]]):
|
||||
"""Modal screen for selecting sort column and direction."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("enter", "select", "Select"),
|
||||
]
|
||||
|
||||
DEFAULT_CSS = """
|
||||
SortScreen {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
SortScreen #sort-container {
|
||||
width: 40;
|
||||
height: auto;
|
||||
background: $surface;
|
||||
border: thick $primary;
|
||||
padding: 1 2;
|
||||
}
|
||||
|
||||
SortScreen #sort-title {
|
||||
text-style: bold;
|
||||
width: 100%;
|
||||
height: 1;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
SortScreen .section-label {
|
||||
margin-top: 1;
|
||||
margin-bottom: 0;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
SortScreen RadioSet {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
background: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
SortScreen RadioButton {
|
||||
width: 100%;
|
||||
background: transparent;
|
||||
padding: 0;
|
||||
height: 1;
|
||||
}
|
||||
|
||||
SortScreen #sort-buttons {
|
||||
width: 100%;
|
||||
height: 3;
|
||||
align: center middle;
|
||||
margin-top: 1;
|
||||
}
|
||||
|
||||
SortScreen Button {
|
||||
margin: 0 1;
|
||||
}
|
||||
"""
|
||||
|
||||
# Available columns for sorting
|
||||
SORT_COLUMNS = [
|
||||
("priority", "Priority"),
|
||||
("project", "Project"),
|
||||
("summary", "Summary"),
|
||||
("due", "Due"),
|
||||
("status", "Status"),
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
current_column: str = "priority",
|
||||
current_ascending: bool = True,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the sort screen.
|
||||
|
||||
Args:
|
||||
current_column: Currently selected sort column
|
||||
current_ascending: Current sort direction (True=ascending)
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._current_column = current_column
|
||||
self._current_ascending = current_ascending
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical(id="sort-container"):
|
||||
yield Label("Sort Tasks", id="sort-title")
|
||||
|
||||
# Column selection
|
||||
yield Label("Sort by:", classes="section-label")
|
||||
with RadioSet(id="column-set"):
|
||||
for key, display in self.SORT_COLUMNS:
|
||||
yield RadioButton(
|
||||
display, value=key == self._current_column, id=f"col-{key}"
|
||||
)
|
||||
|
||||
# Direction selection
|
||||
yield Label("Direction:", classes="section-label")
|
||||
with RadioSet(id="direction-set"):
|
||||
yield RadioButton(
|
||||
"Ascending", value=self._current_ascending, id="dir-asc"
|
||||
)
|
||||
yield RadioButton(
|
||||
"Descending", value=not self._current_ascending, id="dir-desc"
|
||||
)
|
||||
|
||||
with Horizontal(id="sort-buttons"):
|
||||
yield Button("Cancel", id="cancel", variant="default")
|
||||
yield Button("Apply", id="apply", variant="primary")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the column radio set."""
|
||||
self.query_one("#column-set", RadioSet).focus()
|
||||
|
||||
@on(Button.Pressed, "#apply")
|
||||
def handle_apply(self) -> None:
|
||||
column_set = self.query_one("#column-set", RadioSet)
|
||||
direction_set = self.query_one("#direction-set", RadioSet)
|
||||
|
||||
# Get selected column from pressed button id
|
||||
column = self._current_column
|
||||
if column_set.pressed_button and column_set.pressed_button.id:
|
||||
# Extract column key from id like "col-priority"
|
||||
column = column_set.pressed_button.id.replace("col-", "")
|
||||
|
||||
# Get direction
|
||||
ascending = self._current_ascending
|
||||
if direction_set.pressed_button and direction_set.pressed_button.id:
|
||||
ascending = direction_set.pressed_button.id == "dir-asc"
|
||||
|
||||
self.dismiss(SortConfig(column=column, ascending=ascending))
|
||||
|
||||
@on(Button.Pressed, "#cancel")
|
||||
def handle_cancel(self) -> None:
|
||||
self.dismiss(None)
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
self.dismiss(None)
|
||||
|
||||
def action_select(self) -> None:
|
||||
self.handle_apply()
|
||||
131
src/tasks/screens/NotesEditor.py
Normal file
131
src/tasks/screens/NotesEditor.py
Normal file
@@ -0,0 +1,131 @@
|
||||
"""Notes editor screen using built-in TextArea widget."""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Button, Label, TextArea
|
||||
|
||||
|
||||
class NotesEditorScreen(ModalScreen[Optional[str]]):
|
||||
"""Modal screen for editing task notes with built-in TextArea."""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel"),
|
||||
Binding("ctrl+s", "save", "Save"),
|
||||
]
|
||||
|
||||
DEFAULT_CSS = """
|
||||
NotesEditorScreen {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
NotesEditorScreen #editor-container {
|
||||
width: 80%;
|
||||
height: 80%;
|
||||
background: $surface;
|
||||
border: thick $primary;
|
||||
padding: 1;
|
||||
}
|
||||
|
||||
NotesEditorScreen #editor-title {
|
||||
text-style: bold;
|
||||
width: 100%;
|
||||
height: 1;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
NotesEditorScreen #task-summary {
|
||||
width: 100%;
|
||||
height: 1;
|
||||
color: $text-muted;
|
||||
text-align: center;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
NotesEditorScreen TextArea {
|
||||
height: 1fr;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
NotesEditorScreen #editor-buttons {
|
||||
width: 100%;
|
||||
height: 3;
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
NotesEditorScreen Button {
|
||||
margin: 0 1;
|
||||
}
|
||||
|
||||
NotesEditorScreen #help-text {
|
||||
width: 100%;
|
||||
height: 1;
|
||||
color: $text-muted;
|
||||
text-align: center;
|
||||
margin-top: 1;
|
||||
}
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
task_id: int,
|
||||
task_summary: str,
|
||||
current_notes: str,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the notes editor screen.
|
||||
|
||||
Args:
|
||||
task_id: The task ID
|
||||
task_summary: Task summary for display
|
||||
current_notes: Current notes content
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._task_id = task_id
|
||||
self._task_summary = task_summary
|
||||
self._current_notes = current_notes
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical(id="editor-container"):
|
||||
yield Label("Edit Notes", id="editor-title")
|
||||
yield Label(
|
||||
f"Task #{self._task_id}: {self._task_summary[:50]}{'...' if len(self._task_summary) > 50 else ''}",
|
||||
id="task-summary",
|
||||
)
|
||||
|
||||
yield TextArea(
|
||||
self._current_notes,
|
||||
id="notes-textarea",
|
||||
language="markdown",
|
||||
show_line_numbers=True,
|
||||
)
|
||||
|
||||
with Horizontal(id="editor-buttons"):
|
||||
yield Button("Cancel", id="cancel", variant="default")
|
||||
yield Button("Save", id="save", variant="primary")
|
||||
|
||||
yield Label("Ctrl+S to save, Escape to cancel", id="help-text")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the text area."""
|
||||
self.query_one("#notes-textarea", TextArea).focus()
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle button presses."""
|
||||
if event.button.id == "save":
|
||||
self.action_save()
|
||||
elif event.button.id == "cancel":
|
||||
self.action_cancel()
|
||||
|
||||
def action_save(self) -> None:
|
||||
"""Save the notes and dismiss."""
|
||||
textarea = self.query_one("#notes-textarea", TextArea)
|
||||
self.dismiss(textarea.text)
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
"""Cancel editing and dismiss."""
|
||||
self.dismiss(None)
|
||||
14
src/tasks/screens/__init__.py
Normal file
14
src/tasks/screens/__init__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
"""Screen components for Tasks TUI."""
|
||||
|
||||
from .AddTaskScreen import AddTaskScreen
|
||||
from .FilterScreens import ProjectFilterScreen, SortConfig, SortScreen, TagFilterScreen
|
||||
from .NotesEditor import NotesEditorScreen
|
||||
|
||||
__all__ = [
|
||||
"AddTaskScreen",
|
||||
"NotesEditorScreen",
|
||||
"ProjectFilterScreen",
|
||||
"SortConfig",
|
||||
"SortScreen",
|
||||
"TagFilterScreen",
|
||||
]
|
||||
306
src/tasks/widgets/AddTaskForm.py
Normal file
306
src/tasks/widgets/AddTaskForm.py
Normal file
@@ -0,0 +1,306 @@
|
||||
"""Reusable Add Task form widget for Tasks TUI.
|
||||
|
||||
This widget can be used standalone in modals or embedded in other screens
|
||||
(e.g., the mail app for creating tasks from emails).
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Horizontal, Vertical
|
||||
from textual.message import Message
|
||||
from textual.widget import Widget
|
||||
from textual.widgets import Input, Label, RadioButton, RadioSet, Select, TextArea
|
||||
from textual.widgets._select import NoSelection
|
||||
|
||||
from src.tasks.backend import TaskPriority
|
||||
|
||||
|
||||
@dataclass
|
||||
class TaskFormData:
|
||||
"""Data from the add task form."""
|
||||
|
||||
summary: str
|
||||
project: Optional[str] = None
|
||||
tags: list[str] | None = None
|
||||
priority: TaskPriority = TaskPriority.P2
|
||||
due: Optional[datetime] = None
|
||||
notes: Optional[str] = None
|
||||
|
||||
|
||||
class AddTaskForm(Widget):
|
||||
"""A reusable form widget for creating/editing tasks.
|
||||
|
||||
This widget emits a TaskFormData when submitted and can be embedded
|
||||
in various contexts (modal screens, sidebars, etc.)
|
||||
"""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
AddTaskForm {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
padding: 1;
|
||||
}
|
||||
|
||||
AddTaskForm .form-row {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
AddTaskForm .form-label {
|
||||
width: 12;
|
||||
height: 1;
|
||||
padding-right: 1;
|
||||
}
|
||||
|
||||
AddTaskForm .form-input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddTaskForm #summary-input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddTaskForm #project-select {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddTaskForm #tags-input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
AddTaskForm #due-input {
|
||||
width: 20;
|
||||
}
|
||||
|
||||
AddTaskForm #priority-set {
|
||||
width: 1fr;
|
||||
height: auto;
|
||||
background: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
layout: horizontal;
|
||||
}
|
||||
|
||||
AddTaskForm #priority-set RadioButton {
|
||||
width: auto;
|
||||
padding: 0 2;
|
||||
background: transparent;
|
||||
height: 1;
|
||||
}
|
||||
|
||||
AddTaskForm #notes-textarea {
|
||||
width: 1fr;
|
||||
height: 6;
|
||||
}
|
||||
|
||||
AddTaskForm .required {
|
||||
color: $error;
|
||||
}
|
||||
"""
|
||||
|
||||
class Submitted(Message):
|
||||
"""Message emitted when the form is submitted."""
|
||||
|
||||
def __init__(self, data: TaskFormData) -> None:
|
||||
super().__init__()
|
||||
self.data = data
|
||||
|
||||
class Cancelled(Message):
|
||||
"""Message emitted when the form is cancelled."""
|
||||
|
||||
pass
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
projects: list[str] | None = None,
|
||||
initial_data: TaskFormData | None = None,
|
||||
show_notes: bool = True,
|
||||
mail_link: str | None = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize the add task form.
|
||||
|
||||
Args:
|
||||
projects: List of available project names for the dropdown
|
||||
initial_data: Pre-populate form with this data
|
||||
show_notes: Whether to show the notes field
|
||||
mail_link: Optional mail link to prepend to notes (mail://message-id)
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
self._projects = projects or []
|
||||
self._initial_data = initial_data
|
||||
self._show_notes = show_notes
|
||||
self._mail_link = mail_link
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Compose the form layout."""
|
||||
initial = self._initial_data or TaskFormData(summary="")
|
||||
|
||||
# Summary (required)
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Summary", classes="form-label")
|
||||
yield Label("*", classes="required")
|
||||
yield Input(
|
||||
value=initial.summary,
|
||||
placeholder="Task summary...",
|
||||
id="summary-input",
|
||||
classes="form-input",
|
||||
)
|
||||
|
||||
# Project (optional dropdown)
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Project", classes="form-label")
|
||||
# Build options list with empty option for "none"
|
||||
options = [("(none)", "")] + [(p, p) for p in self._projects]
|
||||
yield Select(
|
||||
options=options,
|
||||
value=initial.project or "",
|
||||
id="project-select",
|
||||
allow_blank=True,
|
||||
)
|
||||
|
||||
# Tags (comma-separated input)
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Tags", classes="form-label")
|
||||
tags_str = ", ".join(initial.tags) if initial.tags else ""
|
||||
yield Input(
|
||||
value=tags_str,
|
||||
placeholder="tag1, tag2, tag3...",
|
||||
id="tags-input",
|
||||
classes="form-input",
|
||||
)
|
||||
|
||||
# Priority (radio buttons)
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Priority", classes="form-label")
|
||||
with RadioSet(id="priority-set"):
|
||||
yield RadioButton(
|
||||
"P0", value=initial.priority == TaskPriority.P0, id="priority-p0"
|
||||
)
|
||||
yield RadioButton(
|
||||
"P1", value=initial.priority == TaskPriority.P1, id="priority-p1"
|
||||
)
|
||||
yield RadioButton(
|
||||
"P2", value=initial.priority == TaskPriority.P2, id="priority-p2"
|
||||
)
|
||||
yield RadioButton(
|
||||
"P3", value=initial.priority == TaskPriority.P3, id="priority-p3"
|
||||
)
|
||||
|
||||
# Due date (input with date format)
|
||||
with Horizontal(classes="form-row"):
|
||||
yield Label("Due", classes="form-label")
|
||||
due_str = initial.due.strftime("%Y-%m-%d") if initial.due else ""
|
||||
yield Input(
|
||||
value=due_str,
|
||||
placeholder="YYYY-MM-DD",
|
||||
id="due-input",
|
||||
)
|
||||
|
||||
# Notes (optional textarea)
|
||||
if self._show_notes:
|
||||
with Vertical(classes="form-row"):
|
||||
yield Label("Notes", classes="form-label")
|
||||
# If mail_link is provided, prepend it to notes
|
||||
notes_content = initial.notes or ""
|
||||
if self._mail_link:
|
||||
notes_content = f"<!-- {self._mail_link} -->\n\n{notes_content}"
|
||||
yield TextArea(
|
||||
notes_content,
|
||||
id="notes-textarea",
|
||||
language="markdown",
|
||||
)
|
||||
|
||||
def get_form_data(self) -> TaskFormData:
|
||||
"""Extract current form data.
|
||||
|
||||
Returns:
|
||||
TaskFormData with current form values
|
||||
"""
|
||||
summary = self.query_one("#summary-input", Input).value.strip()
|
||||
|
||||
# Get project (handle NoSelection and empty string)
|
||||
project_select = self.query_one("#project-select", Select)
|
||||
project_value = project_select.value
|
||||
project: str | None = None
|
||||
if isinstance(project_value, str) and project_value:
|
||||
project = project_value
|
||||
|
||||
# Get tags (parse comma-separated)
|
||||
tags_str = self.query_one("#tags-input", Input).value.strip()
|
||||
tags = (
|
||||
[t.strip() for t in tags_str.split(",") if t.strip()] if tags_str else None
|
||||
)
|
||||
|
||||
# Get priority from radio set
|
||||
priority_set = self.query_one("#priority-set", RadioSet)
|
||||
priority = TaskPriority.P2 # default
|
||||
if priority_set.pressed_button and priority_set.pressed_button.id:
|
||||
priority_map = {
|
||||
"priority-p0": TaskPriority.P0,
|
||||
"priority-p1": TaskPriority.P1,
|
||||
"priority-p2": TaskPriority.P2,
|
||||
"priority-p3": TaskPriority.P3,
|
||||
}
|
||||
priority = priority_map.get(priority_set.pressed_button.id, TaskPriority.P2)
|
||||
|
||||
# Get due date
|
||||
due_str = self.query_one("#due-input", Input).value.strip()
|
||||
due = None
|
||||
if due_str:
|
||||
try:
|
||||
due = datetime.strptime(due_str, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
pass # Invalid date format, ignore
|
||||
|
||||
# Get notes
|
||||
notes = None
|
||||
if self._show_notes:
|
||||
try:
|
||||
notes_area = self.query_one("#notes-textarea", TextArea)
|
||||
notes = notes_area.text if notes_area.text.strip() else None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return TaskFormData(
|
||||
summary=summary,
|
||||
project=project,
|
||||
tags=tags,
|
||||
priority=priority,
|
||||
due=due,
|
||||
notes=notes,
|
||||
)
|
||||
|
||||
def validate(self) -> tuple[bool, str]:
|
||||
"""Validate the form data.
|
||||
|
||||
Returns:
|
||||
Tuple of (is_valid, error_message)
|
||||
"""
|
||||
data = self.get_form_data()
|
||||
|
||||
if not data.summary:
|
||||
return False, "Summary is required"
|
||||
|
||||
return True, ""
|
||||
|
||||
def submit(self) -> bool:
|
||||
"""Validate and submit the form.
|
||||
|
||||
Returns:
|
||||
True if form was valid and submitted, False otherwise
|
||||
"""
|
||||
is_valid, error = self.validate()
|
||||
if not is_valid:
|
||||
return False
|
||||
|
||||
self.post_message(self.Submitted(self.get_form_data()))
|
||||
return True
|
||||
|
||||
def cancel(self) -> None:
|
||||
"""Cancel the form."""
|
||||
self.post_message(self.Cancelled())
|
||||
455
src/tasks/widgets/FilterSidebar.py
Normal file
455
src/tasks/widgets/FilterSidebar.py
Normal file
@@ -0,0 +1,455 @@
|
||||
"""Filter sidebar widget for Tasks TUI.
|
||||
|
||||
A collapsible sidebar containing project filter, tag filter, and sort options.
|
||||
Changes are applied immediately when selections change.
|
||||
Uses bordered list containers similar to the mail app sidebar.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from textual import on
|
||||
from textual.app import ComposeResult
|
||||
from textual.containers import Vertical, ScrollableContainer
|
||||
from textual.message import Message
|
||||
from textual.reactive import reactive
|
||||
from textual.widget import Widget
|
||||
from textual.widgets import Label, SelectionList, RadioButton, RadioSet, Static
|
||||
from textual.widgets.selection_list import Selection
|
||||
|
||||
|
||||
class FilterSidebar(Widget):
|
||||
"""Collapsible sidebar with context, project filter, tag filter, and sort options."""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
FilterSidebar {
|
||||
width: 30;
|
||||
height: 100%;
|
||||
background: $surface;
|
||||
}
|
||||
|
||||
FilterSidebar.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
FilterSidebar #sidebar-scroll {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
scrollbar-size: 1 1;
|
||||
}
|
||||
|
||||
/* Bordered list containers like mail app */
|
||||
FilterSidebar .filter-list {
|
||||
height: auto;
|
||||
max-height: 8;
|
||||
min-height: 3;
|
||||
border: round rgb(117, 106, 129);
|
||||
margin: 0 0 1 0;
|
||||
scrollbar-size: 1 1;
|
||||
}
|
||||
|
||||
FilterSidebar .filter-list:focus {
|
||||
border: round $secondary;
|
||||
background: rgb(55, 53, 57);
|
||||
border-title-style: bold;
|
||||
}
|
||||
|
||||
/* Context section - single selection list */
|
||||
FilterSidebar #context-list {
|
||||
height: auto;
|
||||
max-height: 6;
|
||||
min-height: 3;
|
||||
}
|
||||
|
||||
FilterSidebar .sort-section {
|
||||
height: auto;
|
||||
border: round rgb(117, 106, 129);
|
||||
margin: 0 0 1 0;
|
||||
padding: 0 1;
|
||||
}
|
||||
|
||||
FilterSidebar .sort-section:focus-within {
|
||||
border: round $secondary;
|
||||
background: rgb(55, 53, 57);
|
||||
border-title-style: bold;
|
||||
}
|
||||
|
||||
FilterSidebar SelectionList {
|
||||
height: auto;
|
||||
max-height: 8;
|
||||
background: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
FilterSidebar RadioSet {
|
||||
height: auto;
|
||||
background: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
FilterSidebar RadioButton {
|
||||
height: 1;
|
||||
background: transparent;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
FilterSidebar .direction-label {
|
||||
margin-top: 1;
|
||||
color: $text-muted;
|
||||
height: 1;
|
||||
}
|
||||
"""
|
||||
|
||||
# Messages for filter/sort changes
|
||||
class ProjectFilterChanged(Message):
|
||||
"""Sent when project filter selection changes."""
|
||||
|
||||
def __init__(self, project: Optional[str]) -> None:
|
||||
self.project = project
|
||||
super().__init__()
|
||||
|
||||
class TagFilterChanged(Message):
|
||||
"""Sent when tag filter selection changes."""
|
||||
|
||||
def __init__(self, tags: list[str]) -> None:
|
||||
self.tags = tags
|
||||
super().__init__()
|
||||
|
||||
class ContextChanged(Message):
|
||||
"""Sent when context selection changes."""
|
||||
|
||||
def __init__(self, context: Optional[str]) -> None:
|
||||
self.context = context
|
||||
super().__init__()
|
||||
|
||||
class SortChanged(Message):
|
||||
"""Sent when sort settings change."""
|
||||
|
||||
def __init__(self, column: str, ascending: bool) -> None:
|
||||
self.column = column
|
||||
self.ascending = ascending
|
||||
super().__init__()
|
||||
|
||||
# Available sort columns
|
||||
SORT_COLUMNS = [
|
||||
("priority", "Priority"),
|
||||
("project", "Project"),
|
||||
("summary", "Summary"),
|
||||
("due", "Due Date"),
|
||||
("status", "Status"),
|
||||
]
|
||||
|
||||
# Reactive properties - use factory functions for mutable defaults
|
||||
contexts: reactive[list[str]] = reactive(list)
|
||||
projects: reactive[list[tuple[str, int]]] = reactive(list)
|
||||
tags: reactive[list[str]] = reactive(list)
|
||||
current_context: reactive[Optional[str]] = reactive(None)
|
||||
current_project: reactive[Optional[str]] = reactive(None)
|
||||
current_tags: reactive[list[str]] = reactive(list)
|
||||
current_sort_column: reactive[str] = reactive("priority")
|
||||
current_sort_ascending: reactive[bool] = reactive(True)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
contexts: Optional[list[str]] = None,
|
||||
projects: Optional[list[tuple[str, int]]] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
current_context: Optional[str] = None,
|
||||
current_project: Optional[str] = None,
|
||||
current_tags: Optional[list[str]] = None,
|
||||
current_sort_column: str = "priority",
|
||||
current_sort_ascending: bool = True,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__(**kwargs)
|
||||
self.contexts = contexts or []
|
||||
self.projects = projects or []
|
||||
self.tags = tags or []
|
||||
self.current_context = current_context
|
||||
self.current_project = current_project
|
||||
self.current_tags = current_tags or []
|
||||
self.current_sort_column = current_sort_column
|
||||
self.current_sort_ascending = current_sort_ascending
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with ScrollableContainer(id="sidebar-scroll"):
|
||||
# Context filter section - bordered list (at top since it's global)
|
||||
yield SelectionList[str](id="context-list", classes="filter-list")
|
||||
|
||||
# Project filter section - bordered list
|
||||
yield SelectionList[str](id="project-list", classes="filter-list")
|
||||
|
||||
# Tag filter section - bordered list
|
||||
yield SelectionList[str](id="tag-list", classes="filter-list")
|
||||
|
||||
# Sort section - bordered container
|
||||
with Vertical(id="sort-section", classes="sort-section"):
|
||||
with RadioSet(id="sort-column-set"):
|
||||
for key, display in self.SORT_COLUMNS:
|
||||
yield RadioButton(
|
||||
display,
|
||||
value=key == self.current_sort_column,
|
||||
id=f"sort-{key}",
|
||||
)
|
||||
|
||||
yield Label("Direction", classes="direction-label")
|
||||
with RadioSet(id="sort-direction-set"):
|
||||
yield RadioButton(
|
||||
"Ascending",
|
||||
value=self.current_sort_ascending,
|
||||
id="sort-asc",
|
||||
)
|
||||
yield RadioButton(
|
||||
"Descending",
|
||||
value=not self.current_sort_ascending,
|
||||
id="sort-desc",
|
||||
)
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Initialize the sidebar with current filter state and set border titles."""
|
||||
# Set border titles like mail app
|
||||
context_list = self.query_one("#context-list", SelectionList)
|
||||
context_list.border_title = "Context"
|
||||
|
||||
project_list = self.query_one("#project-list", SelectionList)
|
||||
project_list.border_title = "Projects"
|
||||
|
||||
tag_list = self.query_one("#tag-list", SelectionList)
|
||||
tag_list.border_title = "Tags"
|
||||
|
||||
sort_section = self.query_one("#sort-section")
|
||||
sort_section.border_title = "Sort"
|
||||
|
||||
# Update the lists
|
||||
self._update_context_list()
|
||||
self._update_project_list()
|
||||
self._update_tag_list()
|
||||
self._update_subtitles()
|
||||
|
||||
def _update_subtitles(self) -> None:
|
||||
"""Update border subtitles to show selection counts."""
|
||||
context_list = self.query_one("#context-list", SelectionList)
|
||||
if self.current_context:
|
||||
context_list.border_subtitle = f"[b]{self.current_context}[/b]"
|
||||
else:
|
||||
context_list.border_subtitle = "none"
|
||||
|
||||
project_list = self.query_one("#project-list", SelectionList)
|
||||
if self.current_project:
|
||||
project_list.border_subtitle = f"[b]{self.current_project}[/b]"
|
||||
else:
|
||||
project_list.border_subtitle = f"{len(self.projects)} available"
|
||||
|
||||
tag_list = self.query_one("#tag-list", SelectionList)
|
||||
if self.current_tags:
|
||||
tag_list.border_subtitle = f"[b]{len(self.current_tags)} selected[/b]"
|
||||
else:
|
||||
tag_list.border_subtitle = f"{len(self.tags)} available"
|
||||
|
||||
sort_section = self.query_one("#sort-section")
|
||||
direction = "↑" if self.current_sort_ascending else "↓"
|
||||
# Get display name for current column
|
||||
col_display = next(
|
||||
(d for k, d in self.SORT_COLUMNS if k == self.current_sort_column),
|
||||
self.current_sort_column,
|
||||
)
|
||||
sort_section.border_subtitle = f"{col_display} {direction}"
|
||||
|
||||
def _update_context_list(self) -> None:
|
||||
"""Update the context selection list."""
|
||||
context_list = self.query_one("#context-list", SelectionList)
|
||||
context_list.clear_options()
|
||||
|
||||
for ctx in self.contexts:
|
||||
context_list.add_option(
|
||||
Selection(
|
||||
ctx,
|
||||
ctx,
|
||||
initial_state=ctx == self.current_context,
|
||||
)
|
||||
)
|
||||
|
||||
def _update_project_list(self) -> None:
|
||||
"""Update the project selection list."""
|
||||
project_list = self.query_one("#project-list", SelectionList)
|
||||
project_list.clear_options()
|
||||
|
||||
for name, count in self.projects:
|
||||
project_list.add_option(
|
||||
Selection(
|
||||
f"{name} ({count})",
|
||||
name,
|
||||
initial_state=name == self.current_project,
|
||||
)
|
||||
)
|
||||
|
||||
def _update_tag_list(self) -> None:
|
||||
"""Update the tag selection list."""
|
||||
tag_list = self.query_one("#tag-list", SelectionList)
|
||||
tag_list.clear_options()
|
||||
|
||||
for tag in self.tags:
|
||||
tag_list.add_option(
|
||||
Selection(
|
||||
f"+{tag}",
|
||||
tag,
|
||||
initial_state=tag in self.current_tags,
|
||||
)
|
||||
)
|
||||
|
||||
def update_filters(
|
||||
self,
|
||||
contexts: Optional[list[str]] = None,
|
||||
projects: Optional[list[tuple[str, int]]] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
) -> None:
|
||||
"""Update available contexts, projects and tags."""
|
||||
if contexts is not None:
|
||||
self.contexts = contexts
|
||||
self._update_context_list()
|
||||
if projects is not None:
|
||||
self.projects = projects
|
||||
self._update_project_list()
|
||||
if tags is not None:
|
||||
self.tags = tags
|
||||
self._update_tag_list()
|
||||
self._update_subtitles()
|
||||
|
||||
def set_current_project(self, project: Optional[str]) -> None:
|
||||
"""Set the current project filter (updates UI to match)."""
|
||||
self.current_project = project
|
||||
self._update_project_list()
|
||||
self._update_subtitles()
|
||||
|
||||
def set_current_context(self, context: Optional[str]) -> None:
|
||||
"""Set the current context (updates UI to match)."""
|
||||
self.current_context = context
|
||||
self._update_context_list()
|
||||
self._update_subtitles()
|
||||
|
||||
def set_current_tags(self, tags: list[str]) -> None:
|
||||
"""Set the current tag filters (updates UI to match)."""
|
||||
self.current_tags = tags
|
||||
self._update_tag_list()
|
||||
self._update_subtitles()
|
||||
|
||||
def set_sort_settings(self, column: str, ascending: bool) -> None:
|
||||
"""Set the current sort settings (updates UI to match)."""
|
||||
self.current_sort_column = column
|
||||
self.current_sort_ascending = ascending
|
||||
|
||||
# Update radio buttons
|
||||
column_set = self.query_one("#sort-column-set", RadioSet)
|
||||
for button in column_set.query(RadioButton):
|
||||
if button.id == f"sort-{column}":
|
||||
button.value = True
|
||||
|
||||
direction_set = self.query_one("#sort-direction-set", RadioSet)
|
||||
asc_btn = direction_set.query_one("#sort-asc", RadioButton)
|
||||
desc_btn = direction_set.query_one("#sort-desc", RadioButton)
|
||||
asc_btn.value = ascending
|
||||
desc_btn.value = not ascending
|
||||
|
||||
self._update_subtitles()
|
||||
|
||||
@on(SelectionList.SelectedChanged, "#context-list")
|
||||
def _on_context_selection_changed(
|
||||
self, event: SelectionList.SelectedChanged
|
||||
) -> None:
|
||||
"""Handle context selection changes."""
|
||||
selected = list(event.selection_list.selected)
|
||||
# For context, we only allow single selection
|
||||
if selected:
|
||||
new_context = selected[0]
|
||||
# If same context clicked again, deselect it (clear context)
|
||||
if new_context == self.current_context:
|
||||
self.current_context = None
|
||||
event.selection_list.deselect(new_context)
|
||||
else:
|
||||
# Deselect previous if any
|
||||
if self.current_context:
|
||||
event.selection_list.deselect(self.current_context)
|
||||
self.current_context = new_context
|
||||
else:
|
||||
self.current_context = None
|
||||
|
||||
self._update_subtitles()
|
||||
self.post_message(self.ContextChanged(self.current_context))
|
||||
|
||||
@on(SelectionList.SelectedChanged, "#project-list")
|
||||
def _on_project_selection_changed(
|
||||
self, event: SelectionList.SelectedChanged
|
||||
) -> None:
|
||||
"""Handle project selection changes."""
|
||||
selected = list(event.selection_list.selected)
|
||||
# For project, we only allow single selection
|
||||
if selected:
|
||||
new_project = selected[0]
|
||||
# If same project clicked again, deselect it
|
||||
if new_project == self.current_project:
|
||||
self.current_project = None
|
||||
event.selection_list.deselect(new_project)
|
||||
else:
|
||||
# Deselect previous if any
|
||||
if self.current_project:
|
||||
event.selection_list.deselect(self.current_project)
|
||||
self.current_project = new_project
|
||||
else:
|
||||
self.current_project = None
|
||||
|
||||
self._update_subtitles()
|
||||
self.post_message(self.ProjectFilterChanged(self.current_project))
|
||||
|
||||
@on(SelectionList.SelectedChanged, "#tag-list")
|
||||
def _on_tag_selection_changed(self, event: SelectionList.SelectedChanged) -> None:
|
||||
"""Handle tag selection changes."""
|
||||
selected = list(event.selection_list.selected)
|
||||
self.current_tags = selected
|
||||
self._update_subtitles()
|
||||
self.post_message(self.TagFilterChanged(self.current_tags))
|
||||
|
||||
@on(RadioSet.Changed, "#sort-column-set")
|
||||
def _on_sort_column_changed(self, event: RadioSet.Changed) -> None:
|
||||
"""Handle sort column changes."""
|
||||
if event.pressed and event.pressed.id:
|
||||
column = event.pressed.id.replace("sort-", "")
|
||||
if column in [c[0] for c in self.SORT_COLUMNS]:
|
||||
self.current_sort_column = column
|
||||
self._update_subtitles()
|
||||
self.post_message(
|
||||
self.SortChanged(
|
||||
self.current_sort_column, self.current_sort_ascending
|
||||
)
|
||||
)
|
||||
|
||||
@on(RadioSet.Changed, "#sort-direction-set")
|
||||
def _on_sort_direction_changed(self, event: RadioSet.Changed) -> None:
|
||||
"""Handle sort direction changes."""
|
||||
if event.pressed and event.pressed.id:
|
||||
self.current_sort_ascending = event.pressed.id == "sort-asc"
|
||||
self._update_subtitles()
|
||||
self.post_message(
|
||||
self.SortChanged(self.current_sort_column, self.current_sort_ascending)
|
||||
)
|
||||
|
||||
def clear_all_filters(self) -> None:
|
||||
"""Clear all project and tag filters."""
|
||||
# Clear project
|
||||
project_list = self.query_one("#project-list", SelectionList)
|
||||
if self.current_project:
|
||||
project_list.deselect(self.current_project)
|
||||
self.current_project = None
|
||||
|
||||
# Clear tags
|
||||
tag_list = self.query_one("#tag-list", SelectionList)
|
||||
for tag in self.current_tags:
|
||||
tag_list.deselect(tag)
|
||||
self.current_tags = []
|
||||
|
||||
self._update_subtitles()
|
||||
|
||||
# Notify app
|
||||
self.post_message(self.ProjectFilterChanged(None))
|
||||
self.post_message(self.TagFilterChanged([]))
|
||||
6
src/tasks/widgets/__init__.py
Normal file
6
src/tasks/widgets/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
"""Widget components for Tasks TUI."""
|
||||
|
||||
from .AddTaskForm import AddTaskForm, TaskFormData
|
||||
from .FilterSidebar import FilterSidebar
|
||||
|
||||
__all__ = ["AddTaskForm", "TaskFormData", "FilterSidebar"]
|
||||
318
src/utils/ipc.py
Normal file
318
src/utils/ipc.py
Normal file
@@ -0,0 +1,318 @@
|
||||
"""Inter-Process Communication using Unix Domain Sockets.
|
||||
|
||||
This module provides a simple pub/sub mechanism for cross-app notifications.
|
||||
The sync daemon can broadcast messages when data changes, and TUI apps can
|
||||
listen for these messages to refresh their displays.
|
||||
|
||||
Usage:
|
||||
# In sync daemon (publisher):
|
||||
from src.utils.ipc import notify_refresh
|
||||
await notify_refresh("mail") # Notify mail app to refresh
|
||||
await notify_refresh("calendar") # Notify calendar app to refresh
|
||||
await notify_refresh("tasks") # Notify tasks app to refresh
|
||||
|
||||
# In TUI apps (subscriber):
|
||||
from src.utils.ipc import IPCListener
|
||||
|
||||
class MyApp(App):
|
||||
def on_mount(self):
|
||||
self.ipc_listener = IPCListener("mail", self.on_refresh)
|
||||
self.ipc_listener.start()
|
||||
|
||||
def on_unmount(self):
|
||||
self.ipc_listener.stop()
|
||||
|
||||
async def on_refresh(self, message):
|
||||
# Refresh the app's data
|
||||
await self.refresh_data()
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Callable, Optional, Any, Dict
|
||||
|
||||
# Socket paths for each app type
|
||||
SOCKET_DIR = Path("~/.local/share/luk/ipc").expanduser()
|
||||
SOCKET_PATHS = {
|
||||
"mail": SOCKET_DIR / "mail.sock",
|
||||
"calendar": SOCKET_DIR / "calendar.sock",
|
||||
"tasks": SOCKET_DIR / "tasks.sock",
|
||||
}
|
||||
|
||||
|
||||
def ensure_socket_dir():
|
||||
"""Ensure the socket directory exists."""
|
||||
SOCKET_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def get_socket_path(app_type: str) -> Path:
|
||||
"""Get the socket path for a given app type."""
|
||||
if app_type not in SOCKET_PATHS:
|
||||
raise ValueError(
|
||||
f"Unknown app type: {app_type}. Must be one of: {list(SOCKET_PATHS.keys())}"
|
||||
)
|
||||
return SOCKET_PATHS[app_type]
|
||||
|
||||
|
||||
class IPCMessage:
|
||||
"""A message sent via IPC."""
|
||||
|
||||
def __init__(self, event: str, data: Optional[Dict[str, Any]] = None):
|
||||
self.event = event
|
||||
self.data = data or {}
|
||||
|
||||
def to_json(self) -> str:
|
||||
return json.dumps({"event": self.event, "data": self.data})
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> "IPCMessage":
|
||||
parsed = json.loads(json_str)
|
||||
return cls(event=parsed["event"], data=parsed.get("data", {}))
|
||||
|
||||
|
||||
async def notify_refresh(app_type: str, data: Optional[Dict[str, Any]] = None) -> bool:
|
||||
"""Send a refresh notification to a specific app.
|
||||
|
||||
Args:
|
||||
app_type: The type of app to notify ("mail", "calendar", "tasks")
|
||||
data: Optional data to include with the notification
|
||||
|
||||
Returns:
|
||||
True if the notification was sent successfully, False otherwise
|
||||
"""
|
||||
socket_path = get_socket_path(app_type)
|
||||
|
||||
if not socket_path.exists():
|
||||
# No listener, that's okay
|
||||
return False
|
||||
|
||||
try:
|
||||
message = IPCMessage("refresh", data)
|
||||
|
||||
# Connect to the socket and send the message
|
||||
reader, writer = await asyncio.open_unix_connection(str(socket_path))
|
||||
|
||||
writer.write((message.to_json() + "\n").encode())
|
||||
await writer.drain()
|
||||
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
|
||||
return True
|
||||
except (ConnectionRefusedError, FileNotFoundError, OSError):
|
||||
# Socket exists but no one is listening, or other error
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
async def notify_all(data: Optional[Dict[str, Any]] = None) -> Dict[str, bool]:
|
||||
"""Send a refresh notification to all apps.
|
||||
|
||||
Args:
|
||||
data: Optional data to include with the notification
|
||||
|
||||
Returns:
|
||||
Dictionary of app_type -> success status
|
||||
"""
|
||||
results = {}
|
||||
for app_type in SOCKET_PATHS:
|
||||
results[app_type] = await notify_refresh(app_type, data)
|
||||
return results
|
||||
|
||||
|
||||
class IPCListener:
|
||||
"""Listens for IPC messages on a Unix socket.
|
||||
|
||||
Usage:
|
||||
listener = IPCListener("mail", on_message_callback)
|
||||
listener.start()
|
||||
# ... later ...
|
||||
listener.stop()
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app_type: str,
|
||||
callback: Callable[[IPCMessage], Any],
|
||||
):
|
||||
"""Initialize the IPC listener.
|
||||
|
||||
Args:
|
||||
app_type: The type of app ("mail", "calendar", "tasks")
|
||||
callback: Function to call when a message is received.
|
||||
Can be sync or async.
|
||||
"""
|
||||
self.app_type = app_type
|
||||
self.callback = callback
|
||||
self.socket_path = get_socket_path(app_type)
|
||||
self._server: Optional[asyncio.AbstractServer] = None
|
||||
self._running = False
|
||||
self._loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
self._thread: Optional[threading.Thread] = None
|
||||
|
||||
async def _handle_client(
|
||||
self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
|
||||
):
|
||||
"""Handle an incoming client connection."""
|
||||
try:
|
||||
data = await reader.readline()
|
||||
if data:
|
||||
message_str = data.decode().strip()
|
||||
if message_str:
|
||||
message = IPCMessage.from_json(message_str)
|
||||
|
||||
# Call the callback (handle both sync and async)
|
||||
result = self.callback(message)
|
||||
if asyncio.iscoroutine(result):
|
||||
await result
|
||||
except Exception:
|
||||
pass # Ignore errors from malformed messages
|
||||
finally:
|
||||
writer.close()
|
||||
try:
|
||||
await writer.wait_closed()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
async def _run_server(self):
|
||||
"""Run the Unix socket server."""
|
||||
ensure_socket_dir()
|
||||
|
||||
# Remove stale socket file if it exists
|
||||
if self.socket_path.exists():
|
||||
self.socket_path.unlink()
|
||||
|
||||
self._server = await asyncio.start_unix_server(
|
||||
self._handle_client, path=str(self.socket_path)
|
||||
)
|
||||
|
||||
# Set socket permissions (readable/writable by owner only)
|
||||
os.chmod(self.socket_path, 0o600)
|
||||
|
||||
async with self._server:
|
||||
await self._server.serve_forever()
|
||||
|
||||
def _run_in_thread(self):
|
||||
"""Run the event loop in a separate thread."""
|
||||
self._loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self._loop)
|
||||
try:
|
||||
self._loop.run_until_complete(self._run_server())
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
finally:
|
||||
self._loop.close()
|
||||
|
||||
def start(self):
|
||||
"""Start listening for IPC messages in a background thread."""
|
||||
if self._running:
|
||||
return
|
||||
|
||||
self._running = True
|
||||
self._thread = threading.Thread(target=self._run_in_thread, daemon=True)
|
||||
self._thread.start()
|
||||
|
||||
def stop(self):
|
||||
"""Stop listening for IPC messages."""
|
||||
if not self._running:
|
||||
return
|
||||
|
||||
self._running = False
|
||||
|
||||
# Cancel the server
|
||||
if self._server and self._loop:
|
||||
self._loop.call_soon_threadsafe(self._server.close)
|
||||
|
||||
# Stop the event loop
|
||||
if self._loop:
|
||||
self._loop.call_soon_threadsafe(self._loop.stop)
|
||||
|
||||
# Wait for thread to finish
|
||||
if self._thread:
|
||||
self._thread.join(timeout=1.0)
|
||||
|
||||
# Clean up socket file
|
||||
if self.socket_path.exists():
|
||||
try:
|
||||
self.socket_path.unlink()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
class AsyncIPCListener:
|
||||
"""Async version of IPCListener for use within an existing event loop.
|
||||
|
||||
Usage in a Textual app:
|
||||
class MyApp(App):
|
||||
async def on_mount(self):
|
||||
self.ipc_listener = AsyncIPCListener("mail", self.on_refresh)
|
||||
await self.ipc_listener.start()
|
||||
|
||||
async def on_unmount(self):
|
||||
await self.ipc_listener.stop()
|
||||
|
||||
async def on_refresh(self, message):
|
||||
self.refresh_data()
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app_type: str,
|
||||
callback: Callable[[IPCMessage], Any],
|
||||
):
|
||||
self.app_type = app_type
|
||||
self.callback = callback
|
||||
self.socket_path = get_socket_path(app_type)
|
||||
self._server: Optional[asyncio.AbstractServer] = None
|
||||
|
||||
async def _handle_client(
|
||||
self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
|
||||
):
|
||||
"""Handle an incoming client connection."""
|
||||
try:
|
||||
data = await reader.readline()
|
||||
if data:
|
||||
message_str = data.decode().strip()
|
||||
if message_str:
|
||||
message = IPCMessage.from_json(message_str)
|
||||
|
||||
result = self.callback(message)
|
||||
if asyncio.iscoroutine(result):
|
||||
await result
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
writer.close()
|
||||
try:
|
||||
await writer.wait_closed()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
async def start(self):
|
||||
"""Start the Unix socket server."""
|
||||
ensure_socket_dir()
|
||||
|
||||
if self.socket_path.exists():
|
||||
self.socket_path.unlink()
|
||||
|
||||
self._server = await asyncio.start_unix_server(
|
||||
self._handle_client, path=str(self.socket_path)
|
||||
)
|
||||
os.chmod(self.socket_path, 0o600)
|
||||
|
||||
async def stop(self):
|
||||
"""Stop the server and clean up."""
|
||||
if self._server:
|
||||
self._server.close()
|
||||
await self._server.wait_closed()
|
||||
|
||||
if self.socket_path.exists():
|
||||
try:
|
||||
self.socket_path.unlink()
|
||||
except Exception:
|
||||
pass
|
||||
@@ -1,3 +1,35 @@
|
||||
"""
|
||||
Mail utilities module for email operations.
|
||||
"""
|
||||
|
||||
from .helpers import (
|
||||
ensure_directory_exists,
|
||||
format_datetime,
|
||||
format_mail_link,
|
||||
format_mail_link_comment,
|
||||
format_mime_date,
|
||||
has_mail_link,
|
||||
load_last_sync_timestamp,
|
||||
parse_mail_link,
|
||||
parse_maildir_name,
|
||||
remove_mail_link_comment,
|
||||
safe_filename,
|
||||
save_sync_timestamp,
|
||||
truncate_id,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ensure_directory_exists",
|
||||
"format_datetime",
|
||||
"format_mail_link",
|
||||
"format_mail_link_comment",
|
||||
"format_mime_date",
|
||||
"has_mail_link",
|
||||
"load_last_sync_timestamp",
|
||||
"parse_mail_link",
|
||||
"parse_maildir_name",
|
||||
"remove_mail_link_comment",
|
||||
"safe_filename",
|
||||
"save_sync_timestamp",
|
||||
"truncate_id",
|
||||
]
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
"""
|
||||
Mail utility helper functions.
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime
|
||||
import email.utils
|
||||
|
||||
|
||||
def truncate_id(message_id, length=8):
|
||||
"""
|
||||
Truncate a message ID to a reasonable length for display.
|
||||
@@ -24,6 +26,7 @@ def truncate_id(message_id, length=8):
|
||||
return message_id
|
||||
return f"{message_id[:length]}..."
|
||||
|
||||
|
||||
def load_last_sync_timestamp():
|
||||
"""
|
||||
Load the last synchronization timestamp from a file.
|
||||
@@ -32,12 +35,13 @@ def load_last_sync_timestamp():
|
||||
float: The timestamp of the last synchronization, or 0 if not available.
|
||||
"""
|
||||
try:
|
||||
with open('sync_timestamp.json', 'r') as f:
|
||||
with open("sync_timestamp.json", "r") as f:
|
||||
data = json.load(f)
|
||||
return data.get('timestamp', 0)
|
||||
return data.get("timestamp", 0)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return 0
|
||||
|
||||
|
||||
def save_sync_timestamp():
|
||||
"""
|
||||
Save the current timestamp as the last synchronization timestamp.
|
||||
@@ -46,8 +50,9 @@ def save_sync_timestamp():
|
||||
None
|
||||
"""
|
||||
current_time = time.time()
|
||||
with open('sync_timestamp.json', 'w') as f:
|
||||
json.dump({'timestamp': current_time}, f)
|
||||
with open("sync_timestamp.json", "w") as f:
|
||||
json.dump({"timestamp": current_time}, f)
|
||||
|
||||
|
||||
def format_datetime(dt_str, format_string="%m/%d %I:%M %p"):
|
||||
"""
|
||||
@@ -63,11 +68,12 @@ def format_datetime(dt_str, format_string="%m/%d %I:%M %p"):
|
||||
if not dt_str:
|
||||
return ""
|
||||
try:
|
||||
dt = datetime.fromisoformat(dt_str.replace('Z', '+00:00'))
|
||||
dt = datetime.fromisoformat(dt_str.replace("Z", "+00:00"))
|
||||
return dt.strftime(format_string)
|
||||
except (ValueError, AttributeError):
|
||||
return dt_str
|
||||
|
||||
|
||||
def format_mime_date(dt_str):
|
||||
"""
|
||||
Format a datetime string from ISO format to RFC 5322 format for MIME Date headers.
|
||||
@@ -81,11 +87,12 @@ def format_mime_date(dt_str):
|
||||
if not dt_str:
|
||||
return ""
|
||||
try:
|
||||
dt = datetime.fromisoformat(dt_str.replace('Z', '+00:00'))
|
||||
dt = datetime.fromisoformat(dt_str.replace("Z", "+00:00"))
|
||||
return email.utils.format_datetime(dt)
|
||||
except (ValueError, AttributeError):
|
||||
return dt_str
|
||||
|
||||
|
||||
def safe_filename(filename):
|
||||
"""
|
||||
Convert a string to a safe filename.
|
||||
@@ -98,9 +105,10 @@ def safe_filename(filename):
|
||||
"""
|
||||
invalid_chars = '<>:"/\\|?*'
|
||||
for char in invalid_chars:
|
||||
filename = filename.replace(char, '_')
|
||||
filename = filename.replace(char, "_")
|
||||
return filename
|
||||
|
||||
|
||||
def ensure_directory_exists(directory):
|
||||
"""
|
||||
Ensure that a directory exists, creating it if necessary.
|
||||
@@ -114,6 +122,7 @@ def ensure_directory_exists(directory):
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
|
||||
|
||||
def parse_maildir_name(filename):
|
||||
"""
|
||||
Parse a Maildir filename to extract components.
|
||||
@@ -125,9 +134,104 @@ def parse_maildir_name(filename):
|
||||
tuple: (message_id, flags) components of the filename.
|
||||
"""
|
||||
# Maildir filename format: unique-id:flags
|
||||
if ':' in filename:
|
||||
message_id, flags = filename.split(':', 1)
|
||||
if ":" in filename:
|
||||
message_id, flags = filename.split(":", 1)
|
||||
else:
|
||||
message_id = filename
|
||||
flags = ''
|
||||
flags = ""
|
||||
return message_id, flags
|
||||
|
||||
|
||||
# Mail-Task Link Utilities
|
||||
# These functions handle the mail://message-id links that connect tasks to emails
|
||||
|
||||
MAIL_LINK_PREFIX = "mail://"
|
||||
MAIL_LINK_COMMENT_PATTERN = r"<!--\s*mail://([^>\s]+)\s*-->"
|
||||
|
||||
|
||||
def format_mail_link(message_id: str) -> str:
|
||||
"""
|
||||
Format a message ID as a mail link URI.
|
||||
|
||||
Args:
|
||||
message_id: The email message ID (e.g., "abc123@example.com")
|
||||
|
||||
Returns:
|
||||
Mail link URI (e.g., "mail://abc123@example.com")
|
||||
"""
|
||||
# Clean up message ID - remove angle brackets if present
|
||||
message_id = message_id.strip()
|
||||
if message_id.startswith("<"):
|
||||
message_id = message_id[1:]
|
||||
if message_id.endswith(">"):
|
||||
message_id = message_id[:-1]
|
||||
return f"{MAIL_LINK_PREFIX}{message_id}"
|
||||
|
||||
|
||||
def format_mail_link_comment(message_id: str) -> str:
|
||||
"""
|
||||
Format a message ID as an HTML comment for embedding in task notes.
|
||||
|
||||
Args:
|
||||
message_id: The email message ID
|
||||
|
||||
Returns:
|
||||
HTML comment containing the mail link (e.g., "<!-- mail://abc123@example.com -->")
|
||||
"""
|
||||
return f"<!-- {format_mail_link(message_id)} -->"
|
||||
|
||||
|
||||
def parse_mail_link(notes: str) -> str | None:
|
||||
"""
|
||||
Extract a mail link message ID from task notes.
|
||||
|
||||
Looks for an HTML comment in the format: <!-- mail://message-id -->
|
||||
|
||||
Args:
|
||||
notes: The task notes content
|
||||
|
||||
Returns:
|
||||
The message ID if found, None otherwise
|
||||
"""
|
||||
import re
|
||||
|
||||
if not notes:
|
||||
return None
|
||||
|
||||
match = re.search(MAIL_LINK_COMMENT_PATTERN, notes)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
|
||||
def has_mail_link(notes: str) -> bool:
|
||||
"""
|
||||
Check if task notes contain a mail link.
|
||||
|
||||
Args:
|
||||
notes: The task notes content
|
||||
|
||||
Returns:
|
||||
True if a mail link is found, False otherwise
|
||||
"""
|
||||
return parse_mail_link(notes) is not None
|
||||
|
||||
|
||||
def remove_mail_link_comment(notes: str) -> str:
|
||||
"""
|
||||
Remove the mail link comment from task notes.
|
||||
|
||||
Args:
|
||||
notes: The task notes content
|
||||
|
||||
Returns:
|
||||
Notes with the mail link comment removed
|
||||
"""
|
||||
import re
|
||||
|
||||
if not notes:
|
||||
return ""
|
||||
|
||||
# Remove the mail link comment and any trailing newlines
|
||||
cleaned = re.sub(MAIL_LINK_COMMENT_PATTERN + r"\n*", "", notes)
|
||||
return cleaned.strip()
|
||||
|
||||
@@ -27,6 +27,31 @@ from src.utils.mail_utils.helpers import (
|
||||
)
|
||||
|
||||
|
||||
# Module-level session for reuse
|
||||
_shared_session: aiohttp.ClientSession | None = None
|
||||
|
||||
|
||||
async def get_shared_session() -> aiohttp.ClientSession:
|
||||
"""Get or create a shared aiohttp session for connection reuse."""
|
||||
global _shared_session
|
||||
if _shared_session is None or _shared_session.closed:
|
||||
connector = aiohttp.TCPConnector(
|
||||
limit=20, # Max concurrent connections
|
||||
limit_per_host=10, # Max connections per host
|
||||
ttl_dns_cache=300, # Cache DNS for 5 minutes
|
||||
)
|
||||
_shared_session = aiohttp.ClientSession(connector=connector)
|
||||
return _shared_session
|
||||
|
||||
|
||||
async def close_shared_session():
|
||||
"""Close the shared session when done."""
|
||||
global _shared_session
|
||||
if _shared_session and not _shared_session.closed:
|
||||
await _shared_session.close()
|
||||
_shared_session = None
|
||||
|
||||
|
||||
async def save_mime_to_maildir_async(
|
||||
maildir_path,
|
||||
message,
|
||||
@@ -136,63 +161,68 @@ async def create_mime_message_async(
|
||||
|
||||
# First try the direct body content approach
|
||||
message_id = message.get("id", "")
|
||||
|
||||
# Get shared session for connection reuse
|
||||
session = await get_shared_session()
|
||||
|
||||
try:
|
||||
# First get the message with body content
|
||||
body_url = f"https://graph.microsoft.com/v1.0/me/messages/{message_id}?$select=body,bodyPreview"
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(body_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
body_data = await response.json()
|
||||
async with session.get(body_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
body_data = await response.json()
|
||||
|
||||
# Get body content
|
||||
body_content = body_data.get("body", {}).get("content", "")
|
||||
body_type = body_data.get("body", {}).get("contentType", "text")
|
||||
body_preview = body_data.get("bodyPreview", "")
|
||||
# Get body content
|
||||
body_content = body_data.get("body", {}).get("content", "")
|
||||
body_type = body_data.get("body", {}).get("contentType", "text")
|
||||
body_preview = body_data.get("bodyPreview", "")
|
||||
|
||||
# If we have body content, use it
|
||||
if body_content:
|
||||
if body_type.lower() == "html":
|
||||
# Add both HTML and plain text versions
|
||||
# Plain text conversion
|
||||
plain_text = re.sub(r"<br\s*/?>", "\n", body_content)
|
||||
plain_text = re.sub(r"<[^>]*>", "", plain_text)
|
||||
# If we have body content, use it
|
||||
if body_content:
|
||||
if body_type.lower() == "html":
|
||||
# Add both HTML and plain text versions
|
||||
# Plain text conversion
|
||||
plain_text = re.sub(r"<br\s*/?>", "\n", body_content)
|
||||
plain_text = re.sub(r"<[^>]*>", "", plain_text)
|
||||
|
||||
mime_msg.attach(MIMEText(plain_text, "plain"))
|
||||
mime_msg.attach(MIMEText(body_content, "html"))
|
||||
else:
|
||||
# Just plain text
|
||||
mime_msg.attach(MIMEText(body_content, "plain"))
|
||||
elif body_preview:
|
||||
# Use preview if we have it
|
||||
mime_msg.attach(
|
||||
MIMEText(
|
||||
f"{body_preview}\n\n[Message preview only. Full content not available.]",
|
||||
"plain",
|
||||
)
|
||||
)
|
||||
mime_msg.attach(MIMEText(plain_text, "plain"))
|
||||
mime_msg.attach(MIMEText(body_content, "html"))
|
||||
else:
|
||||
# Fallback to MIME content
|
||||
progress.console.print(
|
||||
f"No direct body content for message {truncate_id(message_id)}, trying MIME content..."
|
||||
# Just plain text
|
||||
mime_msg.attach(MIMEText(body_content, "plain"))
|
||||
elif body_preview:
|
||||
# Use preview if we have it
|
||||
mime_msg.attach(
|
||||
MIMEText(
|
||||
f"{body_preview}\n\n[Message preview only. Full content not available.]",
|
||||
"plain",
|
||||
)
|
||||
await fetch_mime_content(
|
||||
mime_msg, message_id, headers, progress
|
||||
)
|
||||
else:
|
||||
progress.console.print(
|
||||
f"Failed to get message body: {response.status}. Trying MIME content..."
|
||||
)
|
||||
await fetch_mime_content(mime_msg, message_id, headers, progress)
|
||||
else:
|
||||
# Fallback to MIME content
|
||||
progress.console.print(
|
||||
f"No direct body content for message {truncate_id(message_id)}, trying MIME content..."
|
||||
)
|
||||
await fetch_mime_content(
|
||||
mime_msg, message_id, headers, progress, session
|
||||
)
|
||||
else:
|
||||
progress.console.print(
|
||||
f"Failed to get message body: {response.status}. Trying MIME content..."
|
||||
)
|
||||
await fetch_mime_content(
|
||||
mime_msg, message_id, headers, progress, session
|
||||
)
|
||||
except Exception as e:
|
||||
progress.console.print(
|
||||
f"Error getting message body: {e}. Trying MIME content..."
|
||||
)
|
||||
await fetch_mime_content(mime_msg, message_id, headers, progress)
|
||||
await fetch_mime_content(mime_msg, message_id, headers, progress, session)
|
||||
|
||||
# Handle attachments only if we want to download them
|
||||
if download_attachments:
|
||||
await add_attachments_async(
|
||||
mime_msg, message, headers, attachments_dir, progress
|
||||
mime_msg, message, headers, attachments_dir, progress, session
|
||||
)
|
||||
else:
|
||||
# Add a header to indicate attachment info was skipped
|
||||
@@ -201,7 +231,7 @@ async def create_mime_message_async(
|
||||
return mime_msg
|
||||
|
||||
|
||||
async def fetch_mime_content(mime_msg, message_id, headers, progress):
|
||||
async def fetch_mime_content(mime_msg, message_id, headers, progress, session=None):
|
||||
"""
|
||||
Fetch and add MIME content to a message when direct body access fails.
|
||||
|
||||
@@ -210,72 +240,78 @@ async def fetch_mime_content(mime_msg, message_id, headers, progress):
|
||||
message_id (str): Message ID.
|
||||
headers (dict): Headers including authentication.
|
||||
progress: Progress instance for updating progress bars.
|
||||
session (aiohttp.ClientSession, optional): Shared session to use.
|
||||
"""
|
||||
# Fallback to getting the MIME content
|
||||
message_content_url = (
|
||||
f"https://graph.microsoft.com/v1.0/me/messages/{message_id}/$value"
|
||||
)
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(message_content_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
full_content = await response.text()
|
||||
# Use provided session or get shared session
|
||||
if session is None:
|
||||
session = await get_shared_session()
|
||||
|
||||
# Check for body tags
|
||||
body_match = re.search(
|
||||
r"<body[^>]*>(.*?)</body>",
|
||||
async with session.get(message_content_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
full_content = await response.text()
|
||||
|
||||
# Check for body tags
|
||||
body_match = re.search(
|
||||
r"<body[^>]*>(.*?)</body>",
|
||||
full_content,
|
||||
re.DOTALL | re.IGNORECASE,
|
||||
)
|
||||
if body_match:
|
||||
body_content = body_match.group(1)
|
||||
# Simple HTML to text conversion
|
||||
body_text = re.sub(r"<br\s*/?>", "\n", body_content)
|
||||
body_text = re.sub(r"<[^>]*>", "", body_text)
|
||||
|
||||
# Add the plain text body
|
||||
mime_msg.attach(MIMEText(body_text, "plain"))
|
||||
|
||||
# Also add the HTML body
|
||||
mime_msg.attach(MIMEText(full_content, "html"))
|
||||
else:
|
||||
# Fallback - try to find content between Content-Type: text/html and next boundary
|
||||
html_parts = re.findall(
|
||||
r"Content-Type: text/html.*?\r?\n\r?\n(.*?)(?:\r?\n\r?\n|$)",
|
||||
full_content,
|
||||
re.DOTALL | re.IGNORECASE,
|
||||
)
|
||||
if body_match:
|
||||
body_content = body_match.group(1)
|
||||
# Simple HTML to text conversion
|
||||
body_text = re.sub(r"<br\s*/?>", "\n", body_content)
|
||||
body_text = re.sub(r"<[^>]*>", "", body_text)
|
||||
if html_parts:
|
||||
html_content = html_parts[0]
|
||||
mime_msg.attach(MIMEText(html_content, "html"))
|
||||
|
||||
# Add the plain text body
|
||||
mime_msg.attach(MIMEText(body_text, "plain"))
|
||||
|
||||
# Also add the HTML body
|
||||
mime_msg.attach(MIMEText(full_content, "html"))
|
||||
# Also make plain text version
|
||||
plain_text = re.sub(r"<br\s*/?>", "\n", html_content)
|
||||
plain_text = re.sub(r"<[^>]*>", "", plain_text)
|
||||
mime_msg.attach(MIMEText(plain_text, "plain"))
|
||||
else:
|
||||
# Fallback - try to find content between Content-Type: text/html and next boundary
|
||||
html_parts = re.findall(
|
||||
r"Content-Type: text/html.*?\r?\n\r?\n(.*?)(?:\r?\n\r?\n|$)",
|
||||
full_content,
|
||||
re.DOTALL | re.IGNORECASE,
|
||||
)
|
||||
if html_parts:
|
||||
html_content = html_parts[0]
|
||||
mime_msg.attach(MIMEText(html_content, "html"))
|
||||
|
||||
# Also make plain text version
|
||||
plain_text = re.sub(r"<br\s*/?>", "\n", html_content)
|
||||
plain_text = re.sub(r"<[^>]*>", "", plain_text)
|
||||
mime_msg.attach(MIMEText(plain_text, "plain"))
|
||||
else:
|
||||
# Just use the raw content as text if nothing else works
|
||||
mime_msg.attach(MIMEText(full_content, "plain"))
|
||||
progress.console.print(
|
||||
f"Using raw content for message {message_id} - no body tags found"
|
||||
)
|
||||
else:
|
||||
error_text = await response.text()
|
||||
progress.console.print(
|
||||
f"Failed to get MIME content: {response.status} {error_text}"
|
||||
)
|
||||
mime_msg.attach(
|
||||
MIMEText(
|
||||
f"Failed to retrieve message body: HTTP {response.status}",
|
||||
"plain",
|
||||
# Just use the raw content as text if nothing else works
|
||||
mime_msg.attach(MIMEText(full_content, "plain"))
|
||||
progress.console.print(
|
||||
f"Using raw content for message {message_id} - no body tags found"
|
||||
)
|
||||
else:
|
||||
error_text = await response.text()
|
||||
progress.console.print(
|
||||
f"Failed to get MIME content: {response.status} {error_text}"
|
||||
)
|
||||
mime_msg.attach(
|
||||
MIMEText(
|
||||
f"Failed to retrieve message body: HTTP {response.status}",
|
||||
"plain",
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
progress.console.print(f"Error retrieving MIME content: {e}")
|
||||
mime_msg.attach(MIMEText(f"Failed to retrieve message body: {str(e)}", "plain"))
|
||||
|
||||
|
||||
async def add_attachments_async(mime_msg, message, headers, attachments_dir, progress):
|
||||
async def add_attachments_async(
|
||||
mime_msg, message, headers, attachments_dir, progress, session=None
|
||||
):
|
||||
"""
|
||||
Add attachments to a MIME message.
|
||||
|
||||
@@ -285,6 +321,7 @@ async def add_attachments_async(mime_msg, message, headers, attachments_dir, pro
|
||||
headers (dict): Headers including authentication.
|
||||
attachments_dir (str): Path to save attachments.
|
||||
progress: Progress instance for updating progress bars.
|
||||
session (aiohttp.ClientSession, optional): Shared session to use.
|
||||
|
||||
Returns:
|
||||
None
|
||||
@@ -296,58 +333,57 @@ async def add_attachments_async(mime_msg, message, headers, attachments_dir, pro
|
||||
f"https://graph.microsoft.com/v1.0/me/messages/{message_id}/attachments"
|
||||
)
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(attachments_url, headers=headers) as response:
|
||||
if response.status != 200:
|
||||
return
|
||||
# Use provided session or get shared session
|
||||
if session is None:
|
||||
session = await get_shared_session()
|
||||
|
||||
attachments_data = await response.json()
|
||||
attachments = attachments_data.get("value", [])
|
||||
async with session.get(attachments_url, headers=headers) as response:
|
||||
if response.status != 200:
|
||||
return
|
||||
|
||||
if not attachments:
|
||||
return
|
||||
attachments_data = await response.json()
|
||||
attachments = attachments_data.get("value", [])
|
||||
|
||||
# Create a directory for this message's attachments
|
||||
message_attachments_dir = os.path.join(attachments_dir, message_id)
|
||||
ensure_directory_exists(message_attachments_dir)
|
||||
if not attachments:
|
||||
return
|
||||
|
||||
# Add a header with attachment count
|
||||
mime_msg["X-Attachment-Count"] = str(len(attachments))
|
||||
# Create a directory for this message's attachments
|
||||
message_attachments_dir = os.path.join(attachments_dir, message_id)
|
||||
ensure_directory_exists(message_attachments_dir)
|
||||
|
||||
for idx, attachment in enumerate(attachments):
|
||||
attachment_name = safe_filename(attachment.get("name", "attachment"))
|
||||
attachment_type = attachment.get(
|
||||
"contentType", "application/octet-stream"
|
||||
# Add a header with attachment count
|
||||
mime_msg["X-Attachment-Count"] = str(len(attachments))
|
||||
|
||||
for idx, attachment in enumerate(attachments):
|
||||
attachment_name = safe_filename(attachment.get("name", "attachment"))
|
||||
attachment_type = attachment.get("contentType", "application/octet-stream")
|
||||
|
||||
# Add attachment info to headers for reference
|
||||
mime_msg[f"X-Attachment-{idx + 1}-Name"] = attachment_name
|
||||
mime_msg[f"X-Attachment-{idx + 1}-Type"] = attachment_type
|
||||
|
||||
attachment_part = MIMEBase(*attachment_type.split("/", 1))
|
||||
|
||||
# Get attachment content
|
||||
if "contentBytes" in attachment:
|
||||
attachment_content = base64.b64decode(attachment["contentBytes"])
|
||||
|
||||
# Save attachment to disk
|
||||
attachment_path = os.path.join(message_attachments_dir, attachment_name)
|
||||
with open(attachment_path, "wb") as f:
|
||||
f.write(attachment_content)
|
||||
|
||||
# Add to MIME message
|
||||
attachment_part.set_payload(attachment_content)
|
||||
encoders.encode_base64(attachment_part)
|
||||
attachment_part.add_header(
|
||||
"Content-Disposition",
|
||||
f'attachment; filename="{attachment_name}"',
|
||||
)
|
||||
mime_msg.attach(attachment_part)
|
||||
|
||||
# Add attachment info to headers for reference
|
||||
mime_msg[f"X-Attachment-{idx + 1}-Name"] = attachment_name
|
||||
mime_msg[f"X-Attachment-{idx + 1}-Type"] = attachment_type
|
||||
|
||||
attachment_part = MIMEBase(*attachment_type.split("/", 1))
|
||||
|
||||
# Get attachment content
|
||||
if "contentBytes" in attachment:
|
||||
attachment_content = base64.b64decode(attachment["contentBytes"])
|
||||
|
||||
# Save attachment to disk
|
||||
attachment_path = os.path.join(
|
||||
message_attachments_dir, attachment_name
|
||||
)
|
||||
with open(attachment_path, "wb") as f:
|
||||
f.write(attachment_content)
|
||||
|
||||
# Add to MIME message
|
||||
attachment_part.set_payload(attachment_content)
|
||||
encoders.encode_base64(attachment_part)
|
||||
attachment_part.add_header(
|
||||
"Content-Disposition",
|
||||
f'attachment; filename="{attachment_name}"',
|
||||
)
|
||||
mime_msg.attach(attachment_part)
|
||||
|
||||
progress.console.print(f"Downloaded attachment: {attachment_name}")
|
||||
else:
|
||||
progress.console.print(
|
||||
f"Skipping attachment with no content: {attachment_name}"
|
||||
)
|
||||
progress.console.print(f"Downloaded attachment: {attachment_name}")
|
||||
else:
|
||||
progress.console.print(
|
||||
f"Skipping attachment with no content: {attachment_name}"
|
||||
)
|
||||
|
||||
179
src/utils/search.py
Normal file
179
src/utils/search.py
Normal file
@@ -0,0 +1,179 @@
|
||||
"""Reusable search input screen for TUI apps.
|
||||
|
||||
A modal input dialog that can be used for search across all apps.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Vertical, Horizontal
|
||||
from textual.screen import ModalScreen
|
||||
from textual.widgets import Input, Static, Label, Button
|
||||
|
||||
|
||||
class SearchScreen(ModalScreen[str | None]):
|
||||
"""A modal screen for search input.
|
||||
|
||||
Returns the search query string on submit, or None on cancel.
|
||||
"""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
SearchScreen {
|
||||
align: center middle;
|
||||
}
|
||||
|
||||
SearchScreen > Vertical {
|
||||
width: 60;
|
||||
height: auto;
|
||||
border: solid $primary;
|
||||
background: $surface;
|
||||
padding: 1 2;
|
||||
}
|
||||
|
||||
SearchScreen > Vertical > Label {
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
SearchScreen > Vertical > Input {
|
||||
margin-bottom: 1;
|
||||
}
|
||||
|
||||
SearchScreen > Vertical > Horizontal {
|
||||
height: auto;
|
||||
align: right middle;
|
||||
}
|
||||
|
||||
SearchScreen > Vertical > Horizontal > Button {
|
||||
margin-left: 1;
|
||||
}
|
||||
"""
|
||||
|
||||
BINDINGS = [
|
||||
Binding("escape", "cancel", "Cancel", show=True),
|
||||
]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
title: str = "Search",
|
||||
placeholder: str = "Enter search query...",
|
||||
initial_value: str = "",
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
classes: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
self._title = title
|
||||
self._placeholder = placeholder
|
||||
self._initial_value = initial_value
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Vertical():
|
||||
yield Label(self._title)
|
||||
yield Input(
|
||||
placeholder=self._placeholder,
|
||||
value=self._initial_value,
|
||||
id="search-input",
|
||||
)
|
||||
with Horizontal():
|
||||
yield Button("Search", variant="primary", id="search-btn")
|
||||
yield Button("Cancel", variant="default", id="cancel-btn")
|
||||
|
||||
def on_mount(self) -> None:
|
||||
"""Focus the input on mount."""
|
||||
self.query_one("#search-input", Input).focus()
|
||||
|
||||
def on_input_submitted(self, event: Input.Submitted) -> None:
|
||||
"""Handle Enter key in input."""
|
||||
self.dismiss(event.value)
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle button presses."""
|
||||
if event.button.id == "search-btn":
|
||||
query = self.query_one("#search-input", Input).value
|
||||
self.dismiss(query)
|
||||
elif event.button.id == "cancel-btn":
|
||||
self.dismiss(None)
|
||||
|
||||
def action_cancel(self) -> None:
|
||||
"""Cancel the search."""
|
||||
self.dismiss(None)
|
||||
|
||||
|
||||
class ClearableSearchInput(Static):
|
||||
"""A search input widget with clear button for use in sidebars/headers.
|
||||
|
||||
Emits SearchInput.Submitted message when user submits a query.
|
||||
Emits SearchInput.Cleared message when user clears the search.
|
||||
"""
|
||||
|
||||
DEFAULT_CSS = """
|
||||
ClearableSearchInput {
|
||||
height: 3;
|
||||
padding: 0 1;
|
||||
}
|
||||
|
||||
ClearableSearchInput > Horizontal {
|
||||
height: auto;
|
||||
}
|
||||
|
||||
ClearableSearchInput > Horizontal > Input {
|
||||
width: 1fr;
|
||||
}
|
||||
|
||||
ClearableSearchInput > Horizontal > Button {
|
||||
width: 3;
|
||||
min-width: 3;
|
||||
}
|
||||
"""
|
||||
|
||||
from textual.message import Message
|
||||
|
||||
class Submitted(Message):
|
||||
"""Search query was submitted."""
|
||||
|
||||
def __init__(self, query: str) -> None:
|
||||
super().__init__()
|
||||
self.query = query
|
||||
|
||||
class Cleared(Message):
|
||||
"""Search was cleared."""
|
||||
|
||||
pass
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
placeholder: str = "Search...",
|
||||
name: Optional[str] = None,
|
||||
id: Optional[str] = None,
|
||||
classes: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
self._placeholder = placeholder
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
with Horizontal():
|
||||
yield Input(placeholder=self._placeholder, id="search-input")
|
||||
yield Button("X", id="clear-btn", variant="error")
|
||||
|
||||
def on_input_submitted(self, event: Input.Submitted) -> None:
|
||||
"""Handle search submission."""
|
||||
self.post_message(self.Submitted(event.value))
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle clear button."""
|
||||
if event.button.id == "clear-btn":
|
||||
input_widget = self.query_one("#search-input", Input)
|
||||
input_widget.value = ""
|
||||
input_widget.focus()
|
||||
self.post_message(self.Cleared())
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
"""Get the current search value."""
|
||||
return self.query_one("#search-input", Input).value
|
||||
|
||||
@value.setter
|
||||
def value(self, new_value: str) -> None:
|
||||
"""Set the search value."""
|
||||
self.query_one("#search-input", Input).value = new_value
|
||||
114
src/utils/shared_config.py
Normal file
114
src/utils/shared_config.py
Normal file
@@ -0,0 +1,114 @@
|
||||
"""Shared configuration for all LUK TUI applications.
|
||||
|
||||
This module provides shared settings that apply across all apps,
|
||||
such as theme configuration.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
try:
|
||||
import toml
|
||||
except ImportError:
|
||||
toml = None # type: ignore
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Available Textual themes
|
||||
AVAILABLE_THEMES = [
|
||||
"textual-dark",
|
||||
"textual-light",
|
||||
"nord",
|
||||
"gruvbox",
|
||||
"catppuccin-mocha",
|
||||
"dracula",
|
||||
"monokai",
|
||||
"solarized-light",
|
||||
"tokyo-night",
|
||||
]
|
||||
|
||||
# Default shared configuration
|
||||
DEFAULT_SHARED_CONFIG = {
|
||||
"theme": {
|
||||
"name": "monokai", # Default Textual theme
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def get_shared_config_path() -> Path:
|
||||
"""Get the shared config file path."""
|
||||
config_home = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
|
||||
return Path(config_home) / "luk" / "shared.toml"
|
||||
|
||||
|
||||
def load_shared_config() -> dict:
|
||||
"""Load shared configuration from TOML file.
|
||||
|
||||
Returns merged config with defaults for any missing values.
|
||||
"""
|
||||
config = DEFAULT_SHARED_CONFIG.copy()
|
||||
|
||||
config_path = get_shared_config_path()
|
||||
if config_path.exists() and toml is not None:
|
||||
try:
|
||||
user_config = toml.load(config_path)
|
||||
# Deep merge user config into defaults
|
||||
for section, values in user_config.items():
|
||||
if section in config and isinstance(config[section], dict):
|
||||
config[section].update(values)
|
||||
else:
|
||||
config[section] = values
|
||||
logger.info(f"Loaded shared config from {config_path}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Error loading shared config: {e}")
|
||||
else:
|
||||
logger.debug(f"No shared config at {config_path}, using defaults")
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def get_theme_name() -> str:
|
||||
"""Get the configured theme name."""
|
||||
config = load_shared_config()
|
||||
return config.get("theme", {}).get("name", "monokai")
|
||||
|
||||
|
||||
def create_default_shared_config() -> None:
|
||||
"""Create a default shared.toml config file if it doesn't exist."""
|
||||
config_path = get_shared_config_path()
|
||||
|
||||
if config_path.exists():
|
||||
return
|
||||
|
||||
if toml is None:
|
||||
logger.warning("toml module not available, cannot create config")
|
||||
return
|
||||
|
||||
# Ensure parent directory exists
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with open(config_path, "w") as f:
|
||||
toml.dump(DEFAULT_SHARED_CONFIG, f)
|
||||
|
||||
logger.info(f"Created default shared config at {config_path}")
|
||||
|
||||
|
||||
# Global cached config
|
||||
_shared_config: Optional[dict] = None
|
||||
|
||||
|
||||
def get_shared_config() -> dict:
|
||||
"""Get the cached shared config, loading if necessary."""
|
||||
global _shared_config
|
||||
if _shared_config is None:
|
||||
_shared_config = load_shared_config()
|
||||
return _shared_config
|
||||
|
||||
|
||||
def reload_shared_config() -> dict:
|
||||
"""Force reload of shared config from disk."""
|
||||
global _shared_config
|
||||
_shared_config = load_shared_config()
|
||||
return _shared_config
|
||||
24
tests/fixtures/himalaya_test_config.toml
vendored
Normal file
24
tests/fixtures/himalaya_test_config.toml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# Himalaya Test Configuration
|
||||
#
|
||||
# This configuration file sets up a local Maildir test account for integration testing.
|
||||
# Copy this file to ~/.config/himalaya/config.toml or merge with existing config.
|
||||
#
|
||||
# Usage:
|
||||
# himalaya -c tests/fixtures/himalaya_test_config.toml envelope list -a test-account
|
||||
# himalaya -c tests/fixtures/himalaya_test_config.toml envelope list -a test-account from edson
|
||||
#
|
||||
# Or set the config path and use the test account:
|
||||
# export HIMALAYA_CONFIG=tests/fixtures/himalaya_test_config.toml
|
||||
# himalaya envelope list -a test-account
|
||||
|
||||
[accounts.test-account]
|
||||
default = true
|
||||
email = "test@example.com"
|
||||
display-name = "Test User"
|
||||
|
||||
# Maildir backend configuration
|
||||
backend.type = "maildir"
|
||||
backend.root-dir = "tests/fixtures/test_mailbox"
|
||||
|
||||
# Message configuration
|
||||
message.send.backend.type = "none"
|
||||
22
tests/fixtures/test_mailbox/INBOX/cur/1702500005.000005.test:2,S
vendored
Normal file
22
tests/fixtures/test_mailbox/INBOX/cur/1702500005.000005.test:2,S
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
From: Edson Martinez <edson.martinez@example.com>
|
||||
To: Test User <test@example.com>
|
||||
Subject: DevOps weekly report
|
||||
Date: Fri, 14 Dec 2025 16:00:00 -0600
|
||||
Message-ID: <msg005@example.com>
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
Hi Team,
|
||||
|
||||
Here's the weekly DevOps report:
|
||||
|
||||
1. Server uptime: 99.9%
|
||||
2. Deployments this week: 12
|
||||
3. Incidents resolved: 3
|
||||
4. Pending tasks: 5
|
||||
|
||||
The CI/CD pipeline improvements are on track for next week.
|
||||
|
||||
Best,
|
||||
Edson Martinez
|
||||
DevOps Lead
|
||||
17
tests/fixtures/test_mailbox/INBOX/cur/1702600004.000004.test:2,S
vendored
Normal file
17
tests/fixtures/test_mailbox/INBOX/cur/1702600004.000004.test:2,S
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
From: Carol Davis <carol.davis@example.com>
|
||||
To: Test User <test@example.com>
|
||||
Subject: Re: Budget spreadsheet
|
||||
Date: Thu, 15 Dec 2025 11:20:00 -0600
|
||||
Message-ID: <msg004@example.com>
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
Hi,
|
||||
|
||||
Thanks for sending over the budget spreadsheet. I've reviewed it and everything looks good.
|
||||
|
||||
One small note: the Q3 numbers need to be updated with the final figures from accounting.
|
||||
|
||||
Let me know once that's done.
|
||||
|
||||
Carol
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user