working good state
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import re
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime # Add this import at the top of the file
|
||||
|
||||
from actions.newest import action_newest
|
||||
from actions.oldest import action_oldest
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
import logging
|
||||
@@ -9,8 +14,8 @@ from textual.widget import Widget
|
||||
from textual.app import App, ComposeResult, SystemCommand, RenderResult
|
||||
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.widgets import Header, Footer, Static, Label, Input, Button, Markdown
|
||||
from textual.reactive import reactive, Reactive
|
||||
from textual.binding import Binding
|
||||
from textual.timer import Timer
|
||||
from textual.containers import ScrollableContainer, Horizontal, Vertical, Grid
|
||||
@@ -22,25 +27,24 @@ from maildir_gtd.actions.show_message import show_message
|
||||
from maildir_gtd.actions.next import action_next
|
||||
from maildir_gtd.actions.previous import action_previous
|
||||
from maildir_gtd.actions.task import action_create_task
|
||||
from maildir_gtd.widgets.EnvelopeHeader import EnvelopeHeader
|
||||
|
||||
logging.basicConfig(
|
||||
level="NOTSET",
|
||||
handlers=[TextualHandler()],
|
||||
)
|
||||
|
||||
class Hello(Widget):
|
||||
"""Display a greeting."""
|
||||
|
||||
def render(self) -> RenderResult:
|
||||
return "Hello, [b]World[/b]!"
|
||||
|
||||
class StatusTitle(Static):
|
||||
total_messages: Reactive[int] = Reactive(0)
|
||||
current_message_index: Reactive[int] = Reactive(0)
|
||||
current_message_id: Reactive[int] = Reactive(1)
|
||||
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"Inbox Message ID: {self.current_message_id} | [b]{self.current_message_index}[/b]/{self.total_messages}"
|
||||
return f"{self.folder} | ID: {self.current_message_id} | [b]{self.current_message_index}[/b]/{self.total_messages}"
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -48,9 +52,10 @@ class StatusTitle(Static):
|
||||
class EmailViewerApp(App):
|
||||
"""A simple email viewer app using the Himalaya CLI."""
|
||||
title = "Maildir GTD Reader"
|
||||
current_message_id: Reactive[int] = Reactive(1)
|
||||
current_message_id: Reactive[int] = reactive(1)
|
||||
CSS_PATH = "email_viewer.tcss"
|
||||
|
||||
folder = reactive("INBOX")
|
||||
markdown: Reactive[str] = reactive("Loading...")
|
||||
|
||||
def get_system_commands(self, screen: Screen) -> Iterable[SystemCommand]:
|
||||
yield from super().get_system_commands(screen)
|
||||
@@ -60,6 +65,8 @@ class EmailViewerApp(App):
|
||||
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)
|
||||
|
||||
BINDINGS = [
|
||||
Binding("j", "next", "Next message"),
|
||||
@@ -80,20 +87,36 @@ class EmailViewerApp(App):
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Create child widgets for the app."""
|
||||
yield Header(show_clock=True)
|
||||
# yield Header(show_clock=True)
|
||||
yield StatusTitle().data_bind(EmailViewerApp.current_message_id)
|
||||
yield ScrollableContainer(Static(Label("Loading Email Viewer App"), id="main_content"))
|
||||
yield EnvelopeHeader()
|
||||
yield Markdown(id="main_content", markdown=self.markdown)
|
||||
yield Footer()
|
||||
|
||||
def watch_current_message_id(self, message_id: int, old_message_id: int) -> None:
|
||||
def watch_current_message_id(self, old_message_id: int, new_message_id: int) -> None:
|
||||
"""Called when the current message ID changes."""
|
||||
if (message_id == old_message_id):
|
||||
logging.info(f"Current message ID changed from {old_message_id} to {new_message_id}")
|
||||
self.query_one("#main_content").loading = True
|
||||
self.markdown = ""
|
||||
if (new_message_id == old_message_id):
|
||||
return
|
||||
# self.notify("Current message ID changed", title="Status", severity="information")
|
||||
logging.info(f"Current message ID changed to {message_id}")
|
||||
|
||||
try:
|
||||
rawText = subprocess.run(
|
||||
["himalaya", "message", "read", str(new_message_id)],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
if rawText.returncode == 0:
|
||||
# Render the email content as Markdown
|
||||
fixedText = rawText.stdout.replace("(https://urldefense.com/v3/", "(")
|
||||
fixedText = re.sub(r"atlOrigin.+?\)", ")", fixedText)
|
||||
self.query_one("#main_content").loading = False
|
||||
self.query_one("#main_content").update(markdown = str(fixedText))
|
||||
logging.info(fixedText)
|
||||
|
||||
result = subprocess.run(
|
||||
["himalaya", "envelope", "list", "-o", "json"],
|
||||
["himalaya", "envelope", "list", "-o", "json", "-s", "9999"],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
@@ -102,40 +125,35 @@ class EmailViewerApp(App):
|
||||
envelopes = json.loads(result.stdout)
|
||||
if envelopes:
|
||||
status = self.query_one(StatusTitle)
|
||||
status.total_messages = len(envelopes) # Get the first envelope's ID
|
||||
status.current_message_index = next(
|
||||
(index + 1 for index, envelope in enumerate(envelopes) if int(envelope['id']) == message_id),
|
||||
0 # Default to 0 if no match is found
|
||||
)
|
||||
status.total_messages = len(envelopes)
|
||||
|
||||
headers = self.query_one(EnvelopeHeader)
|
||||
# Find the index of the envelope that matches the current_message_id
|
||||
for index, envelope in enumerate(sorted(envelopes, key=lambda x: int(x['id']))):
|
||||
if int(envelope['id']) == new_message_id:
|
||||
headers.subject = envelope['subject']
|
||||
headers.from_ = envelope['from']['addr']
|
||||
headers.to = envelope['to']['addr']
|
||||
headers.date = datetime.strptime(envelope['date'].replace("+00:00", ""), "%Y-%m-%d %H:%M").strftime("%a %b %d %H:%M")
|
||||
status.current_message_index = index + 1 # 1-based index
|
||||
break
|
||||
status.update()
|
||||
headers.update()
|
||||
|
||||
else:
|
||||
self.query_one("#main_content", Static).update("Failed to fetch the most recent message ID.")
|
||||
self.query_one("#main_content").update("Failed to fetch the most recent message ID.")
|
||||
except Exception as e:
|
||||
self.query_one("#main_content", Static).update(f"Error: {e}")
|
||||
self.query_one("#main_content").update(f"Error: {e}")
|
||||
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.alert_timer: Timer | None = None # Timer to throttle alerts
|
||||
self.theme = "monokai"
|
||||
self.title = "MaildirGTD"
|
||||
|
||||
# self.watch(self.query_one(StatusTitle), "current_message_id", update_progress)
|
||||
# Fetch the ID of the most recent message using the Himalaya CLI
|
||||
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)
|
||||
if envelopes:
|
||||
self.current_message_id = int(envelopes[0]['id'])
|
||||
else:
|
||||
self.query_one("#main_content", Static).update("Failed to fetch the most recent message ID.")
|
||||
except Exception as e:
|
||||
self.query_one("#main_content", Static).update(f"Error: {e}")
|
||||
|
||||
self.show_message(self.current_message_id)
|
||||
self.action_oldest()
|
||||
|
||||
def show_message(self, message_id: int) -> None:
|
||||
show_message(self, message_id)
|
||||
@@ -165,24 +183,30 @@ class EmailViewerApp(App):
|
||||
|
||||
def action_scroll_down(self) -> None:
|
||||
"""Scroll the main content down."""
|
||||
self.query_one("#main_content", Static).scroll_down()
|
||||
self.query_one("#main_content").scroll_down()
|
||||
|
||||
def action_scroll_up(self) -> None:
|
||||
"""Scroll the main content up."""
|
||||
self.query_one("#main_content", Static).scroll_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", Static).scroll_page_down()
|
||||
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", Static).scroll_page_up()
|
||||
self.query_one("#main_content").scroll_page_up()
|
||||
|
||||
def action_quit(self) -> None:
|
||||
"""Quit the application."""
|
||||
self.exit()
|
||||
|
||||
def action_oldest(self) -> None:
|
||||
action_oldest(self)
|
||||
|
||||
def action_newest(self) -> None:
|
||||
action_newest(self)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = EmailViewerApp()
|
||||
app.run()
|
||||
|
||||
Reference in New Issue
Block a user