diff --git a/src/instance/launch.rs b/src/instance/launch.rs index 8d6fb9c..0bd984d 100644 --- a/src/instance/launch.rs +++ b/src/instance/launch.rs @@ -252,6 +252,8 @@ pub async fn launch( "legacy".to_string(), ]; + let (kill_tx, kill_rx) = tokio::sync::oneshot::channel::<()>(); + crate::running::register_kill(&name, kill_tx); crate::running::set_state(&name, crate::running::RunState::Starting); tracing::info!( "[{}] Starting Minecraft ({} {})", @@ -272,6 +274,7 @@ pub async fn launch( let mut child = match cmd.spawn() { Ok(c) => c, Err(e) => { + crate::running::cleanup_kill_sender(&name); crate::running::remove(&name); return Err(LaunchError::Io(e)); } @@ -308,7 +311,17 @@ pub async fn launch( }); } - let code = child.wait().await.ok().and_then(|s| s.code()); + let code = tokio::select! { + _ = kill_rx => { + tracing::info!("[{}] Kill requested, terminating process", name_for_task); + let _ = child.kill().await; + let _ = child.wait().await; + None + } + result = child.wait() => { + result.ok().and_then(|s| s.code()) + } + }; tracing::info!("[{}] Exited with code {:?}", name_for_task, code); if code == Some(0) { @@ -325,6 +338,7 @@ pub async fn launch( tracing::warn!("Failed to update last_played for '{}': {}", name_for_task, e); } crate::running::push_last_played(&name_for_task, chrono::Utc::now()); + crate::running::cleanup_kill_sender(&name_for_task); }); Ok(()) diff --git a/src/running.rs b/src/running.rs index b2d876f..698c20c 100644 --- a/src/running.rs +++ b/src/running.rs @@ -16,6 +16,9 @@ pub static RUNNING: Lazy>>> = pub static PENDING_LAST_PLAYED: Lazy)>>>> = Lazy::new(|| Arc::new(Mutex::new(Vec::new()))); +pub static KILL_SENDERS: Lazy>>>> = + Lazy::new(|| Arc::new(Mutex::new(HashMap::new()))); + pub fn set_state(name: &str, state: RunState) { if let Ok(mut map) = RUNNING.lock() { map.insert(name.to_string(), state); @@ -53,3 +56,25 @@ pub fn drain_last_played() -> Vec<(String, DateTime)> { .map(|mut q| q.drain(..).collect()) .unwrap_or_default() } + +pub fn register_kill(name: &str, tx: tokio::sync::oneshot::Sender<()>) { + if let Ok(mut map) = KILL_SENDERS.lock() { + map.insert(name.to_string(), tx); + } +} + +pub fn send_kill(name: &str) -> bool { + if let Ok(mut map) = KILL_SENDERS.lock() { + if let Some(tx) = map.remove(name) { + let _ = tx.send(()); + return true; + } + } + false +} + +pub fn cleanup_kill_sender(name: &str) { + if let Ok(mut map) = KILL_SENDERS.lock() { + map.remove(name); + } +} diff --git a/src/tui/layout.rs b/src/tui/layout.rs index 06e74fd..8b5ab42 100644 --- a/src/tui/layout.rs +++ b/src/tui/layout.rs @@ -316,6 +316,14 @@ impl App { } } } + KeyCode::Esc + if self.focused == FocusedArea::Profiles + && !self.profiles_state.search.active => + { + if let Some(instance) = self.profiles_state.selected_instance() { + crate::running::send_kill(&instance.name); + } + } _ => {} } @@ -370,7 +378,8 @@ impl App { fn spawn_launch(&self, instance: crate::instance::InstanceConfig) { use crate::instance::launch; - use crate::{running, tui::error_buffer}; + use crate::running; + use crate::tui::error_buffer; let instances_dir = self.instance_manager.instances_dir.clone(); let meta_dir = self.instance_manager.meta_dir.clone(); diff --git a/src/tui/widgets/content.rs b/src/tui/widgets/content.rs index 3940bcb..7a4f78c 100644 --- a/src/tui/widgets/content.rs +++ b/src/tui/widgets/content.rs @@ -59,15 +59,6 @@ impl ContentTab { } } -fn spinner_char() -> char { - const FRAMES: &[char] = &['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; - let ms = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_millis(); - FRAMES[(ms / 100) as usize % FRAMES.len()] -} - pub fn render( frame: &mut Frame, area: Rect, @@ -182,7 +173,7 @@ pub fn title( let run_state = crate::running::get(&inst.name); let (prefix, prefix_style) = match run_state { Some(RunState::Running) | Some(RunState::Starting) => ( - format!("{} ", spinner_char()), + String::from("\u{25b6} "), Style::default() .fg(THEME.colors.success) .add_modifier(Modifier::BOLD), diff --git a/src/tui/widgets/popups/new_instance.rs b/src/tui/widgets/popups/new_instance.rs index 8cd37ce..54c4641 100644 --- a/src/tui/widgets/popups/new_instance.rs +++ b/src/tui/widgets/popups/new_instance.rs @@ -448,12 +448,8 @@ fn close_popup(state: &mut WizardState, profiles_state: &mut profiles::State) { } fn wizard_title(_state: &WizardState) -> Line<'static> { - Line::styled( - "New Instance", - Style::default() - .fg(THEME.colors.foreground) - .add_modifier(Modifier::BOLD), - ) + use crate::tui::widgets::styled_title; + styled_title("New Instance", true) } fn step_keybinds(state: &WizardState) -> ratatui::text::Line<'static> { @@ -528,6 +524,7 @@ fn render_version_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe .collect(); let list = List::new(items) + .style(Style::default().fg(THEME.colors.foreground)) .highlight_style( Style::default() .fg(THEME.colors.row_highlight) @@ -556,6 +553,7 @@ fn render_loader_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffer .collect(); let list = List::new(items) + .style(Style::default().fg(THEME.colors.foreground)) .highlight_style( Style::default() .fg(THEME.colors.row_highlight) @@ -598,6 +596,7 @@ fn render_loader_version_step( .collect(); let list = List::new(items) + .style(Style::default().fg(THEME.colors.foreground)) .highlight_style( Style::default() .fg(THEME.colors.row_highlight) diff --git a/src/tui/widgets/profiles.rs b/src/tui/widgets/profiles.rs index eadec63..b046fa4 100644 --- a/src/tui/widgets/profiles.rs +++ b/src/tui/widgets/profiles.rs @@ -242,7 +242,7 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S let run_state = get_run_state(&instance.name); let indicator = match &run_state { Some(RunState::Running) | Some(RunState::Starting) => { - Span::styled("\u{25c9} ", Style::default().fg(THEME.colors.success)) + Span::styled("\u{25b6} ", Style::default().fg(THEME.colors.success)) } Some(RunState::Crashed(_)) => { Span::styled("\u{2717} ", Style::default().fg(THEME.colors.error))