fixes to email display
This commit is contained in:
@@ -164,7 +164,7 @@ class EmailViewerApp(App):
|
||||
return (envelope for envelope in self.all_envelopes if envelope.get("id"))
|
||||
|
||||
def watch_status_title(self, old_status_title: str, new_status_title: str) -> None:
|
||||
self.query_one("#main_content").border_title = new_status_title
|
||||
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."""
|
||||
@@ -223,7 +223,7 @@ class EmailViewerApp(App):
|
||||
logging.info(f"new_message_id: {new_message_id}, type: {type(new_message_id)}")
|
||||
logging.info(f"message_metadata keys: {list(self.message_metadata.keys())}")
|
||||
|
||||
content_container = self.query_one("#main_content")
|
||||
content_container = self.query_one(ContentContainer)
|
||||
content_container.display_content(new_message_id)
|
||||
|
||||
if new_message_id in self.message_metadata:
|
||||
|
||||
@@ -19,8 +19,9 @@ class ContentContainer(ScrollableContainer):
|
||||
self.markup_worker = None
|
||||
self.current_text = ""
|
||||
self.current_id = None
|
||||
self.message_cache = dict()
|
||||
# LRU cache with a max size of 100 messages
|
||||
self.get_message_body = lru_cache(maxsize=100)(self._get_message_body)
|
||||
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
"""Compose the container with a label for plaintext and markdown for rich content."""
|
||||
@@ -29,7 +30,6 @@ class ContentContainer(ScrollableContainer):
|
||||
yield Markdown(id="markdown_content", classes="hidden")
|
||||
|
||||
def update_header(self, subject: str = "", date: str = "", from_: str = "", to: str = "", cc: str = "", bcc: str = "") -> None:
|
||||
"""Update the header with the given email details."""
|
||||
header = self.query_one(EnvelopeHeader)
|
||||
header.subject = subject
|
||||
header.date = date
|
||||
@@ -44,34 +44,37 @@ class ContentContainer(ScrollableContainer):
|
||||
header.styles.height = "1" if self.header_expanded else "auto"
|
||||
self.header_expanded = not self.header_expanded
|
||||
|
||||
async def display_content(self, message_id: int) -> None:
|
||||
def display_content(self, message_id: int) -> None:
|
||||
"""Display content for the given message ID."""
|
||||
self.current_id = message_id
|
||||
|
||||
# Show loading state
|
||||
self.loading = True
|
||||
|
||||
# Get message body (from cache or fetch new)
|
||||
message_text = await self.get_message_body(message_id)
|
||||
self.current_text = message_text
|
||||
|
||||
# Update the plaintext content
|
||||
plaintext = self.query_one("#plaintext_content", Label)
|
||||
await plaintext.update(message_text)
|
||||
|
||||
if not self.plaintext_mode:
|
||||
# Check if the message is already cached
|
||||
if message_id in self.message_cache:
|
||||
self.current_text = self.message_cache[message_id]
|
||||
plaintext = self.query_one("#plaintext_content", Label)
|
||||
plaintext.update(self.current_text)
|
||||
if not self.plaintext_mode:
|
||||
# We're in markdown mode, so render the markdown
|
||||
await self.render_markdown()
|
||||
else:
|
||||
self.render_markdown()
|
||||
else:
|
||||
# Hide markdown, show plaintext
|
||||
plaintext.remove_class("hidden")
|
||||
self.query_one("#markdown_content").add_class("hidden")
|
||||
|
||||
self.loading = False
|
||||
self.loading = False
|
||||
return self.current_text
|
||||
else:
|
||||
# Get message body (from cache or fetch new)
|
||||
self.get_message_body(message_id)
|
||||
|
||||
|
||||
@work(exclusive=True)
|
||||
async def _get_message_body(self, message_id: int) -> str:
|
||||
async def get_message_body(self, message_id: int) -> str:
|
||||
"""Fetch the message body from Himalaya CLI."""
|
||||
|
||||
|
||||
try:
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
f"himalaya message read {str(message_id)} -p",
|
||||
@@ -83,10 +86,26 @@ class ContentContainer(ScrollableContainer):
|
||||
|
||||
if process.returncode == 0:
|
||||
# Process the email content
|
||||
fixedText = stdout.decode().replace("https://urldefense.com/v3/", "")
|
||||
fixedText = re.sub(r"atlOrigin.+?\w", "", fixedText)
|
||||
logging.info(f"rendering fixedText: {fixedText[0:50]}")
|
||||
return fixedText
|
||||
fixed_text = stdout.decode().replace("https://urldefense.com/v3/", "")
|
||||
fixed_text = re.sub(r"atlOrigin.+?\w", "", fixed_text)
|
||||
logging.info(f"rendering fixedText: {fixed_text[0:50]}")
|
||||
|
||||
self.current_text = fixed_text
|
||||
self.message_cache[message_id] = fixed_text
|
||||
|
||||
# Update the plaintext content
|
||||
plaintext = self.query_one("#plaintext_content", Label)
|
||||
plaintext.update(fixed_text)
|
||||
|
||||
if not self.plaintext_mode:
|
||||
# We're in markdown mode, so render the markdown
|
||||
self.render_markdown()
|
||||
else:
|
||||
# Hide markdown, show plaintext
|
||||
plaintext.remove_class("hidden")
|
||||
self.query_one("#markdown_content").add_class("hidden")
|
||||
|
||||
self.loading = False
|
||||
else:
|
||||
logging.error(f"Error fetching message: {stderr.decode()}")
|
||||
return f"Error fetching message content: {stderr.decode()}"
|
||||
@@ -122,6 +141,4 @@ class ContentContainer(ScrollableContainer):
|
||||
|
||||
return self.plaintext_mode
|
||||
|
||||
def clear_cache(self) -> None:
|
||||
"""Clear the message body cache."""
|
||||
self.get_message_body.cache_clear()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user