refactor email viewer app

This commit is contained in:
Tim Bendt
2025-04-30 13:11:00 -04:00
parent 3f48ef8e11
commit a158fad485
27 changed files with 362 additions and 303 deletions

View File

@@ -0,0 +1 @@
# Initialize the actions subpackage

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,21 @@
from textual.widgets import Static
import subprocess
from maildir_gtd.actions.next import action_next
def action_archive(app) -> None:
"""Archive the current email message."""
app.show_status(f"Archiving message {app.current_message_id}...")
try:
result = subprocess.run(
["himalaya", "message", "move", "Archives", str(app.current_message_id)],
capture_output=True,
text=True
)
if result.returncode == 0:
app.query_one("#main_content", Static).update(f"Message {app.current_message_id} archived.")
app.show_status(f"Message {app.current_message_id} archived.")
action_next(app) # Automatically show the next message
else:
app.query_one("#main_content", Static).update(f"Failed to archive message {app.current_message_id}.")
except Exception as e:
app.query_one("#main_content", Static).update(f"Error: {e}")

View File

@@ -0,0 +1,23 @@
import subprocess
from textual.widgets import Static
from maildir_gtd.actions.next import action_next
def action_delete(app) -> None:
"""Delete the current email message."""
app.show_status(f"Deleting message {app.current_message_id}...")
app.query_one("#main_content", Static).loading = True
try:
result = subprocess.run(
["himalaya", "message", "delete", str(app.current_message_id)],
capture_output=True,
text=True
)
if result.returncode == 0:
app.query_one("#main_content", Static).loading = False
app.query_one("#main_content", Static).update(f"Message {app.current_message_id} deleted.")
action_next(app) # Automatically show the next message
else:
app.query_one("#main_content", Static).update(f"Failed to delete message {app.current_message_id}.")
except Exception as e:
app.query_one("#main_content", Static).update(f"Error: {e}")

View File

@@ -0,0 +1,36 @@
import logging
from typing import Iterable
from textual import on
from textual.app import App, ComposeResult, SystemCommand
from textual.logging import TextualHandler
from textual.screen import Screen
from textual.widgets import Header, Footer, Static, Label, Input, Button
from textual.reactive import Reactive
from textual.binding import Binding
from textual.timer import Timer
from textual.containers import ScrollableContainer, Horizontal, Vertical, Grid
import subprocess
def action_next(app) -> None:
"""Show the next email message by finding the next higher ID from the list of envelope IDs."""
try:
result = subprocess.run(
["himalaya", "envelope", "list", "-o", "json"],
capture_output=True,
text=True
)
if result.returncode == 0:
import json
envelopes = json.loads(result.stdout)
ids = sorted(int(envelope['id']) for envelope in envelopes)
for envelope_id in ids:
if envelope_id > app.current_message_id:
app.current_message_id = envelope_id
app.show_message(app.current_message_id)
app.show_status(f"Showing next message: {app.current_message_id}")
return
app.show_status("No newer messages found.", severity="warning")
else:
app.show_status("Failed to fetch envelope list.", severity="error")
except Exception as e:
app.show_status(f"Error: {e}", severity="error")

View File

@@ -0,0 +1,16 @@
from maildir_gtd.screens.OpenMessage import OpenMessageScreen
def action_open(app) -> None:
"""Show the input modal for opening a specific message by ID."""
def check_id(message_id: str) -> bool:
try:
int(message_id)
app.show_status(f"Opening message {message_id}...")
app.current_message_id = message_id
app.show_message(app.current_message_id)
except ValueError:
app.show_status("Invalid message ID. Please enter an integer.", severity="error")
return True
return False
app.push_screen(OpenMessageScreen(), check_id)

View File

@@ -0,0 +1,26 @@
from textual.widgets import Static
import subprocess
def action_previous(app) -> None:
"""Show the previous email message by finding the next lower ID from the list of envelope IDs."""
try:
result = subprocess.run(
["himalaya", "envelope", "list", "-o", "json"],
capture_output=True,
text=True
)
if result.returncode == 0:
import json
envelopes = json.loads(result.stdout)
ids = sorted((int(envelope['id']) for envelope in envelopes), reverse=True)
for envelope_id in ids:
if envelope_id < app.current_message_id:
app.current_message_id = envelope_id
app.show_message(app.current_message_id)
app.show_status(f"Showing previous message: {app.current_message_id}")
return
app.show_status("No older messages found.", severity="warning")
else:
app.show_status("Failed to fetch envelope list.", severity="error")
except Exception as e:
app.show_status(f"Error: {e}", severity="error")

View File

@@ -0,0 +1,22 @@
from textual.widgets import Static
import subprocess
def show_message(app, message_id: int) -> None:
"""Fetch and display the email message by ID."""
app.query_one("#main_content", Static).loading = True
try:
result = subprocess.run(
["himalaya", "message", "read", str(message_id)],
capture_output=True,
text=True
)
if result.returncode == 0:
# Render the email content as Markdown
from rich.markdown import Markdown
markdown_content = Markdown(result.stdout, justify=True)
app.query_one("#main_content", Static).loading = False
app.query_one("#main_content", Static).update(markdown_content)
else:
app.query_one("#main_content", Static).update(f"Failed to fetch message {message_id}.")
except Exception as e:
app.query_one("#main_content", Static).update(f"Error: {e}")

View File

@@ -0,0 +1,22 @@
import subprocess
from maildir_gtd.screens.CreateTask import CreateTaskScreen
def action_create_task(app) -> None:
"""Show the input modal for creating a task."""
def check_task(task_args: str) -> bool:
try:
result = subprocess.run(
["task", "add"] + task_args.split(" "),
capture_output=True,
text=True
)
if result.returncode == 0:
app.show_status("Task created successfully.")
else:
app.show_status(f"Failed to create task: {result.stderr}")
except Exception as e:
app.show_status(f"Error: {e}", severity="error")
return True
return False
app.push_screen(CreateTaskScreen(), check_task)