Skip to content

Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.

refactor: implement RAII terminal cleanup and ensure subtitle temporary file deletion after playback

Samir Hossains83428ca49f54
diff --git a/src/main.rs b/src/main.rs
index 2788195..1a6b22c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,18 @@
use moviebox_tui::tui::app::App;
+struct TerminalGuard;
+
+impl Drop for TerminalGuard {
+ fn drop(&mut self) {
+ let _ = crossterm::terminal::disable_raw_mode();
+ let _ = crossterm::execute!(
+ std::io::stdout(),
+ crossterm::terminal::LeaveAlternateScreen,
+ crossterm::event::DisableFocusChange
+ );
+ }
+}
+
#[tokio::main]
async fn main() -> std::io::Result<()> {
let stdout = std::io::stdout();
@@ -13,15 +26,8 @@ async fn main() -> std::io::Result<()> {
crossterm::event::EnableFocusChange
)?;
- let mut app = App::new();
- let result = app.run(&mut terminal).await;
+ let _guard = TerminalGuard;
- crossterm::terminal::disable_raw_mode()?;
- crossterm::execute!(
- std::io::stdout(),
- crossterm::terminal::LeaveAlternateScreen,
- crossterm::event::DisableFocusChange
- )?;
-
- result
+ let mut app = App::new();
+ app.run(&mut terminal).await
}
diff --git a/src/tui/app.rs b/src/tui/app.rs
index 1664469..62ed54e 100644
--- a/src/tui/app.rs
+++ b/src/tui/app.rs
@@ -770,7 +770,7 @@ impl App {
if key.code == KeyCode::F(1) {
self.action_sender.send(Action::ToggleHelp).ok();
- return Some(());
+ return None;
}
match self.state.input_mode {
@@ -4072,6 +4072,7 @@ impl App {
self.state.player_picker_popup = false;
tokio::spawn(async move {
let mut local_sub = sub.clone();
+ let mut sub_temp_path = None;
if kind == crate::tui::state::PlayerKind::Vlc
|| kind == crate::tui::state::PlayerKind::Iina
{
@@ -4087,6 +4088,7 @@ impl App {
));
if tokio::fs::write(&temp_path, bytes).await.is_ok() {
local_sub = Some(temp_path.to_string_lossy().to_string());
+ sub_temp_path = Some(temp_path);
}
}
}
@@ -4105,6 +4107,10 @@ impl App {
}
let _ = cmd.spawn();
+
+ if let Some(path) = sub_temp_path {
+ let _ = tokio::fs::remove_file(path).await;
+ }
});
}
Action::LaunchPlayback(kind, source) => {