This commit is contained in:
Tim Bendt
2025-05-13 08:16:23 -06:00
parent 7123ff1f43
commit 5c9ad69309
4 changed files with 257 additions and 177 deletions

View File

@@ -75,8 +75,10 @@ class ContentContainer(ScrollableContainer):
async def get_message_body(self, message_id: int) -> str:
"""Fetch the message body from Himalaya CLI."""
try:
# Store the ID of the message we're currently loading
loading_id = message_id
process = await asyncio.create_subprocess_shell(
f"himalaya message read {str(message_id)} -p",
stdout=asyncio.subprocess.PIPE,
@@ -85,15 +87,46 @@ class ContentContainer(ScrollableContainer):
stdout, stderr = await process.communicate()
logging.info(f"stdout: {stdout.decode()[0:50]}...")
# Check if we're still loading the same message or if navigation has moved on
if loading_id != self.current_id:
logging.info(f"Message ID changed during loading. Abandoning load of {loading_id}")
return ""
if process.returncode == 0:
# Process the email content
fixed_text = stdout.decode().replace("https://urldefense.com/v3/", "")
content = stdout.decode()
# Remove header lines from the beginning of the message
# Headers typically end with a blank line before the message body
lines = content.split('\n')
body_start = 0
# Find the first blank line which typically separates headers from body
for i, line in enumerate(lines):
if line.strip() == '' and i > 0:
# Check if we're past the headers section
# Headers are typically in "Key: Value" format
has_headers = any(': ' in l for l in lines[:i])
if has_headers:
body_start = i + 1
break
# Join the body lines back together
content = '\n'.join(lines[body_start:])
# Apply existing cleanup logic
fixed_text = content.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
# Check again if we're still on the same message before updating UI
if loading_id != self.current_id:
logging.info(f"Message ID changed after loading. Abandoning update for {loading_id}")
return fixed_text
# Update the plaintext content
plaintext = self.query_one("#plaintext_content", Label)
plaintext.update(fixed_text)
@@ -107,12 +140,20 @@ class ContentContainer(ScrollableContainer):
self.query_one("#markdown_content").add_class("hidden")
self.loading = False
return fixed_text
else:
logging.error(f"Error fetching message: {stderr.decode()}")
self.loading = False
return f"Error fetching message content: {stderr.decode()}"
except Exception as e:
logging.error(f"Error fetching message content: {e}")
self.loading = False
return f"Error fetching message content: {e}"
finally:
# Ensure loading state is always reset if this worker completes
# This prevents the loading indicator from getting stuck
if loading_id == self.current_id:
self.loading = False
async def render_markdown(self) -> None:
"""Render the markdown content asynchronously."""