feat: harmony terminal ui can now browse scores and (almost) launch them

This commit is contained in:
2025-01-28 16:51:58 -05:00
parent 3410751463
commit 6628e193e0
8 changed files with 107 additions and 39 deletions

View File

@@ -1,16 +1,18 @@
use std::sync::{Arc, RwLock};
use std::sync::{ Arc, RwLock};
use log::warn;
use crossterm::event::{Event, KeyCode, KeyEventKind};
use log::{debug, error, info, trace, warn};
use ratatui::{
layout::{Constraint, Rect},
style::{Style, Stylize},
widgets::{Block, Clear, List, ListState, Paragraph, StatefulWidget, Widget},
Frame,
};
use tokio::sync::mpsc;
use crate::ScoreItem;
use crate::{HarmonyTuiEvent, ScoreItem};
use super::ratatui_utils::center;
use crate::ratatui_utils::center;
#[derive(Debug)]
enum ExecutionState {
@@ -33,10 +35,11 @@ pub(crate) struct ScoreListWidget {
scores: Vec<ScoreItem>,
execution: Option<Execution>,
execution_history: Vec<Execution>,
sender: mpsc::Sender<HarmonyTuiEvent>,
}
impl ScoreListWidget {
pub(crate) fn new(scores: Vec<ScoreItem>) -> Self {
pub(crate) fn new(scores: Vec<ScoreItem>, sender : mpsc::Sender<HarmonyTuiEvent>) -> Self {
let mut list_state = ListState::default();
list_state.select_first();
let list_state = Arc::new(RwLock::new(list_state));
@@ -45,6 +48,7 @@ impl ScoreListWidget {
list_state,
execution: None,
execution_history: vec![],
sender,
}
}
@@ -60,6 +64,9 @@ impl ScoreListWidget {
state: ExecutionState::INITIATED,
score: score.clone(),
});
info!("{:#?}\n\nConfirm Execution (Press y/n)", score.0);
} else {
warn!("No Score selected, nothing to launch");
}
}
@@ -82,25 +89,6 @@ impl ScoreListWidget {
}
let execution = self.execution.as_ref().unwrap();
// let confirm = Paragraph::new(format!("{:#?}", execution.score.0)).block(
// Block::default()
// .borders(Borders::ALL)
// .title("Confirm Execution")
// .border_type(BorderType::Rounded),
// );
// let rect = frame.area().inner(Margin::new(5, 5));
// frame.render_widget(confirm, rect);
let area = center(
frame.area(),
Constraint::Percentage(80),
Constraint::Percentage(80), // top and bottom border + content
);
let popup = Paragraph::new(format!("{:#?}", execution.score.0))
.block(Block::bordered().title("Confirm Execution (Press y/n)"));
frame.render_widget(Clear, area);
frame.render_widget(popup, area);
}
fn clear_execution(&mut self) {
@@ -112,20 +100,41 @@ impl ScoreListWidget {
}
}
pub(crate) fn confirm(&mut self, confirm: bool) {
pub(crate) async fn confirm(&mut self, confirm: bool) {
if let Some(execution) = &mut self.execution {
match confirm {
true => {
execution.state = ExecutionState::CONFIRMED;
todo!("launch execution {:#?}", execution);
},
info!("Launch execution {:?}", execution);
warn!("Launch execution");
error!("Launch execution");
trace!("Launch execution");
debug!("Launch execution");
self.sender.send(HarmonyTuiEvent::LaunchScore(execution.score.clone())).await.expect("Should be able to send message");
}
false => {
execution.state = ExecutionState::CANCELED;
info!("Execution cancelled");
self.clear_execution();
}
}
}
}
pub(crate) async fn handle_event(&mut self, event: &Event) {
if let Event::Key(key) = event {
if key.kind == KeyEventKind::Press {
match key.code {
KeyCode::Char('j') | KeyCode::Down => self.scroll_down(),
KeyCode::Char('k') | KeyCode::Up => self.scroll_up(),
KeyCode::Enter => self.launch_execution(),
KeyCode::Char('y') => self.confirm(true).await,
KeyCode::Char('n') => self.confirm(false).await,
_ => {}
}
}
}
}
}
impl Widget for &ScoreListWidget {