basically refactored the email viewer
This commit is contained in:
@@ -2,47 +2,41 @@ import asyncio
|
||||
import logging
|
||||
|
||||
from textual import work
|
||||
from textual.logging import TextualHandler
|
||||
from textual.widgets import ListView
|
||||
|
||||
logging.basicConfig(
|
||||
level="NOTSET",
|
||||
handlers=[TextualHandler()],
|
||||
)
|
||||
from apis.himalaya import client as himalaya_client
|
||||
|
||||
|
||||
@work(exclusive=False)
|
||||
async def archive_current(app) -> None:
|
||||
"""Archive the current email message."""
|
||||
try:
|
||||
index = app.current_message_index
|
||||
logging.info("Archiving message ID: " + str(app.current_message_id))
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
f"himalaya message move Archives {app.current_message_id}",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
app.show_status(f"{stdout.decode()}", "info")
|
||||
logging.info(stdout.decode())
|
||||
if process.returncode == 0:
|
||||
# Remove the item from the ListView
|
||||
app.query_one(ListView).pop(index)
|
||||
@work(exclusive=True)
|
||||
async def archive_current(app):
|
||||
"""Archive the current message."""
|
||||
if not app.current_message_id:
|
||||
app.show_status("No message selected to archive.", "error")
|
||||
return
|
||||
|
||||
# Find the next message to display using the MessageStore
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(index)
|
||||
# Store the current message ID and index
|
||||
current_message_id = app.current_message_id
|
||||
current_index = app.current_message_index
|
||||
|
||||
# Show the next available message
|
||||
if next_id is not None and next_idx is not None:
|
||||
# Set ListView index first to ensure UI is synchronized
|
||||
app.query_one(ListView).index = next_idx
|
||||
# Now update the current_message_id to trigger content update
|
||||
app.current_message_id = next_id
|
||||
else:
|
||||
# No messages left, just update ListView
|
||||
app.query_one(ListView).index = 0
|
||||
app.reload_needed = True
|
||||
# Find the next message to display after archiving
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(current_index)
|
||||
if next_id is None or next_idx is None:
|
||||
# If there's no next message, try to find a previous one
|
||||
next_id, next_idx = app.message_store.find_prev_valid_id(current_index)
|
||||
|
||||
# Archive the message using our Himalaya client module
|
||||
success = await himalaya_client.archive_message(current_message_id)
|
||||
|
||||
if success:
|
||||
app.show_status(f"Message {current_message_id} archived.", "success")
|
||||
app.message_store.remove_envelope(current_message_id)
|
||||
app.refresh_list_view()
|
||||
|
||||
# Select the next available message if it exists
|
||||
if next_id is not None and next_idx is not None:
|
||||
app.current_message_id = next_id
|
||||
app.current_message_index = next_idx
|
||||
else:
|
||||
app.show_status(f"Error archiving message: {stderr.decode()}", "error")
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", "error")
|
||||
# If there are no other messages, reset the UI
|
||||
app.current_message_id = 0
|
||||
app.show_status("No more messages available.", "warning")
|
||||
else:
|
||||
app.show_status(f"Failed to archive message {current_message_id}.", "error")
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from textual import work
|
||||
from textual.widgets import ListView
|
||||
from apis.himalaya import client as himalaya_client
|
||||
|
||||
|
||||
@work(exclusive=False)
|
||||
async def delete_current(app) -> None:
|
||||
app.show_status(f"Deleting message {app.current_message_id}...")
|
||||
try:
|
||||
index = app.current_message_index
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
f"himalaya message delete {app.current_message_id}",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
app.show_status(f"{stdout.decode()}", "info")
|
||||
if process.returncode == 0:
|
||||
# Remove the item from the ListView
|
||||
await app.query_one(ListView).pop(index)
|
||||
@work(exclusive=True)
|
||||
async def delete_current(app):
|
||||
"""Delete the current message."""
|
||||
if not app.current_message_id:
|
||||
app.show_status("No message selected to delete.", "error")
|
||||
return
|
||||
|
||||
# Find the next message to display using the MessageStore
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(index)
|
||||
# Store the current message ID and index
|
||||
current_message_id = app.current_message_id
|
||||
current_index = app.current_message_index
|
||||
|
||||
# Show the next available message
|
||||
if next_id is not None and next_idx is not None:
|
||||
# Set ListView index first to ensure UI is synchronized
|
||||
app.query_one(ListView).index = next_idx
|
||||
# Now update the current_message_id to trigger content update
|
||||
app.current_message_id = next_id
|
||||
else:
|
||||
# No messages left, just update ListView
|
||||
app.query_one(ListView).index = 0
|
||||
app.reload_needed = True
|
||||
# Find the next message to display after deletion
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(current_index)
|
||||
if next_id is None or next_idx is None:
|
||||
# If there's no next message, try to find a previous one
|
||||
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)
|
||||
|
||||
if success:
|
||||
app.show_status(f"Message {current_message_id} deleted.", "success")
|
||||
app.message_store.remove_envelope(current_message_id)
|
||||
app.refresh_list_view()
|
||||
|
||||
# Select the next available message if it exists
|
||||
if next_id is not None and next_idx is not None:
|
||||
app.current_message_id = next_id
|
||||
app.current_message_index = next_idx
|
||||
else:
|
||||
app.show_status(
|
||||
f"Failed to delete message {app.current_message_id}. {stderr.decode()}",
|
||||
"error",
|
||||
)
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", "error")
|
||||
# If there are no other messages, reset the UI
|
||||
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")
|
||||
|
||||
@@ -1,28 +1,49 @@
|
||||
import asyncio
|
||||
|
||||
import logging
|
||||
from textual import work
|
||||
from textual.screen import ModalScreen
|
||||
from apis.taskwarrior import client as taskwarrior_client
|
||||
from maildir_gtd.screens.CreateTask import CreateTaskScreen
|
||||
|
||||
class TaskAction:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
def action_create_task(app) -> None:
|
||||
"""Show the input modal for creating a task."""
|
||||
def action_create_task(app):
|
||||
"""Show the create task screen."""
|
||||
|
||||
async def check_task(task_args: str) -> bool:
|
||||
try:
|
||||
result = await asyncio.create_subprocess_shell(
|
||||
f"task add {task_args}",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await result.communicate()
|
||||
if result.returncode == 0:
|
||||
app.show_status(f"Task created: {stdout.decode()}")
|
||||
else:
|
||||
app.show_status(
|
||||
f"Failed to create task: {stderr.decode()}", severity="error"
|
||||
)
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", severity="error")
|
||||
return True
|
||||
return False
|
||||
current_message_id = app.current_message_id
|
||||
if not current_message_id:
|
||||
app.show_status("No message selected to create task from.", "error")
|
||||
return
|
||||
|
||||
app.push_screen(CreateTaskScreen(), check_task)
|
||||
# Prepare data for the create task screen
|
||||
metadata = app.message_store.get_metadata(current_message_id)
|
||||
subject = metadata.get("subject", "No subject") if metadata else "No subject"
|
||||
from_addr = metadata["from"].get("addr", "Unknown") if metadata else "Unknown"
|
||||
|
||||
# Show the create task screen with the current message data
|
||||
app.push_screen(CreateTaskScreen(subject=subject, from_addr=from_addr))
|
||||
|
||||
@work(exclusive=True)
|
||||
async def create_task(subject, description=None, tags=None, project=None, due=None, priority=None):
|
||||
"""
|
||||
Create a task with the Taskwarrior API client.
|
||||
"""
|
||||
try:
|
||||
success, result = await taskwarrior_client.create_task(
|
||||
task_description=subject,
|
||||
tags=tags or [],
|
||||
project=project,
|
||||
due=due,
|
||||
priority=priority
|
||||
)
|
||||
|
||||
if success:
|
||||
return True, result
|
||||
else:
|
||||
logging.error(f"Failed to create task: {result}")
|
||||
return False, result
|
||||
except Exception as e:
|
||||
logging.error(f"Exception creating task: {e}")
|
||||
return False, str(e)
|
||||
|
||||
Reference in New Issue
Block a user