feat(tui): add panic logging and improve event handling

- Integrate `log_panics` for better error tracking in TUI.
- Enhance score interpretation result handling with async task management.
- Improve layout consistency in the UI rendering process.
This commit is contained in:
2025-02-04 14:44:03 -05:00
parent 0ade6209bb
commit 134f2b78d6
11 changed files with 432 additions and 45 deletions

View File

@@ -15,3 +15,4 @@ crossterm = { version = "0.28.1", features = [ "event-stream" ] }
color-eyre = "0.6.3"
tokio-stream = "0.1.17"
tui-logger = "0.14.1"
log-panics = "2.1.0"

View File

@@ -1,22 +1,21 @@
mod ratatui_utils;
mod widget;
use log::{debug, info};
use log::{debug, error, info};
use tokio::sync::mpsc;
use tokio_stream::StreamExt;
use tui_logger::{TuiWidgetEvent, TuiWidgetState};
use widget::{help::HelpWidget, score::ScoreListWidget};
use std::{sync::Arc, time::Duration};
use std::{panic, sync::Arc, time::Duration};
use crossterm::event::{Event, EventStream, KeyCode, KeyEventKind};
use harmony::{maestro::Maestro, score::Score};
use ratatui::{
self,
self, Frame,
layout::{Constraint, Layout, Position},
style::{Color, Style},
widgets::{Block, Borders, ListItem},
Frame,
};
pub mod tui {
@@ -51,7 +50,6 @@ pub async fn init(maestro: Maestro) -> Result<(), Box<dyn std::error::Error>> {
pub struct HarmonyTUI {
score: ScoreListWidget,
should_quit: bool,
channel_handle: tokio::task::JoinHandle<()>,
tui_state: TuiWidgetState,
}
@@ -69,7 +67,6 @@ impl HarmonyTUI {
HarmonyTUI {
should_quit: false,
score,
channel_handle: handle,
tui_state: TuiWidgetState::new(),
}
}
@@ -84,15 +81,24 @@ impl HarmonyTUI {
info!("Received event {event:#?}");
match event {
HarmonyTuiEvent::LaunchScore(score_item) => {
info!(
"Interpretation result {:#?}",
maestro.interpret(score_item.0).await
)
let maestro = maestro.clone();
let interpretation_result =
tokio::spawn(async move { maestro.interpret(score_item.0).await })
.await;
match interpretation_result {
Ok(success) => info!("Score execution successful {success:?}"),
Err(e) => {
error!("Score execution failed {:#?}", e);
}
}
}
}
}
info!("STOPPING message channel receiver loop");
});
(handle, sender)
}
@@ -105,6 +111,7 @@ impl HarmonyTUI {
color_eyre::install()?;
let mut terminal = ratatui::init();
log_panics::init();
terminal.hide_cursor()?;
// TODO improve performance here
@@ -130,17 +137,14 @@ impl HarmonyTUI {
frame.set_cursor_position(Position::new(size.x / 2, size.y / 2));
let [app_area, help_area] =
Layout::vertical([Constraint::Percentage(100), Constraint::Min(4)])
.areas(frame.area());
Layout::vertical([Constraint::Percentage(100), Constraint::Min(4)]).areas(frame.area());
let help_block = Block::default().borders(Borders::TOP);
frame.render_widget(&help_block, help_area);
frame.render_widget(HelpWidget::new(), help_block.inner(help_area));
let [list_area, output_area] =
Layout::horizontal([Constraint::Min(30), Constraint::Percentage(100)])
.areas(app_area);
Layout::horizontal([Constraint::Min(30), Constraint::Percentage(100)]).areas(app_area);
let block = Block::default().borders(Borders::RIGHT);
frame.render_widget(&block, list_area);
@@ -172,7 +176,9 @@ impl HarmonyTUI {
KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
KeyCode::PageUp => self.tui_state.transition(TuiWidgetEvent::PrevPageKey),
KeyCode::PageDown => self.tui_state.transition(TuiWidgetEvent::NextPageKey),
KeyCode::Char('G') | KeyCode::End => self.tui_state.transition(TuiWidgetEvent::EscapeKey),
KeyCode::Char('G') | KeyCode::End => {
self.tui_state.transition(TuiWidgetEvent::EscapeKey)
}
_ => self.score.handle_event(event).await,
}
}