42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
import asyncio
|
|
from textual import work
|
|
from textual.widgets import ListView
|
|
|
|
|
|
@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)
|
|
|
|
# Find the next message to display using the MessageStore
|
|
next_id, next_idx = app.message_store.find_next_valid_id(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
|
|
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")
|