From 35bb1edfc338993c2bcd898a8fbbbba7384c6dd8 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Mon, 27 Feb 2023 16:24:16 +0100 Subject: [PATCH] Save watched right after something is watched, and use match --- Cargo.lock | 29 ------------ Cargo.toml | 1 - README.md | 1 - src/main.rs | 122 +++++++++++++++++++++++++++----------------------- src/parser.rs | 9 +--- 5 files changed, 69 insertions(+), 93 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9624c8c..c695a43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -123,16 +123,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "ctrlc" -version = "3.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1631ca6e3c59112501a9d87fd86f21591ff77acd31331e8a73f8d80a65bbdd71" -dependencies = [ - "nix", - "windows-sys", -] - [[package]] name = "directories" version = "4.0.1" @@ -470,7 +460,6 @@ version = "0.1.0" dependencies = [ "bytes", "colored", - "ctrlc", "directories", "futures-util", "indicatif", @@ -576,18 +565,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "nix" -version = "0.26.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" -dependencies = [ - "bitflags", - "cfg-if", - "libc", - "static_assertions", -] - [[package]] name = "num_cpus" version = "1.15.0" @@ -959,12 +936,6 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "syn" version = "1.0.107" diff --git a/Cargo.toml b/Cargo.toml index d7c4c2c..bfb13f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" [dependencies] bytes = "1.3.0" colored = "2.0.0" -ctrlc = "3.2.4" directories = "4.0.1" futures-util = "0.3.25" indicatif = { version = "0.17.3", features = ["tokio"] } diff --git a/README.md b/README.md index 5f444a9..8204ac1 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,5 @@ You will need to install mpv, and have it in your path, otherwise it wont work ## Left to do -- Implement the ctrlc handler so that the program saves watched links before exiting. - Create a GUI - Would be nice to bundle mpv in some form diff --git a/src/main.rs b/src/main.rs index d737aff..811a340 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,22 +29,27 @@ async fn main() { let search = readline.input("Search by name [ r/q/f ]: ").to_lowercase(); let search = search.trim(); - // If they want to quit, let them- - if search == "q" { - break; - } else if search == "r" { - search_result = None; - refresh(&parser).await; - continue; - } else if search == "f" { - mpv_fs = !mpv_fs; - println!( - "Toggled mpv to {}launch in fullscreen", - if mpv_fs { "" } else { "not " } - ); - continue; + // Special commands + match search { + // Quit + "q" => break, + // Refresh playlist + "r" => { + search_result = None; + refresh(&parser).await; + continue; + } + // Toggle fullscreen for mpv + "f" => { + mpv_fs = !mpv_fs; + println!( + "Toggled mpv to {}launch in fullscreen", + if mpv_fs { "" } else { "not " } + ); + continue; + } + _ => {} } - search_result = Some(Rc::new(parser.find(search))); if search_result.as_ref().unwrap().len() == 0 { @@ -64,61 +69,68 @@ async fn main() { let user_wish = user_wish.trim(); // If they want to quit, let them- - if user_wish == "q" { - break; - } else if user_wish == "s" { - search_result = None; - continue; - } else if user_wish == "r" { - println!("Refreshing local m3u8-file"); - search_result = None; - refresh(&parser).await; - continue; - } else if user_wish == "d" { - let selection = readline - .input("Download all or select in comma separated [a | 1,2,3,4]: ") - .to_lowercase(); - let selection = selection.trim(); + match user_wish { + // Quit + "q" => break, + // Go inte search-mode + "s" => { + search_result = None; + continue; + } + // Refresh playlist + "r" => { + println!("Refreshing local m3u8-file"); + search_result = None; + refresh(&parser).await; + continue; + } + // Downloadmode + "d" => { + let selection = readline + .input("Download all or select in comma separated [a | 1,2,3,4]: ") + .to_lowercase(); + let selection = selection.trim(); - let to_download = loop { - break if selection == "a" { - println!("Downloading all"); - search_result.as_ref().unwrap().clone() - } else { - let selections = selection - .split(",") - .map(|x| x.trim().parse::()) - .collect::>>(); + let to_download = loop { + break if selection == "a" { + println!("Downloading all"); + search_result.as_ref().unwrap().clone() + } else { + let selections = selection + .split(",") + .map(|x| x.trim().parse::()) + .collect::>>(); - for selection in selections.iter() { - if selection.is_err() { - println!("Not a valid number or the option {}", "a".bold()); - continue; + for selection in selections.iter() { + if selection.is_err() { + println!("Not a valid number or the option {}", "a".bold()); + continue; + } + } + let selections = selections.into_iter().map(|x| x.unwrap() - 1); + let mut final_selections = Vec::new(); + for selection in selections { + final_selections.push((search_result.as_ref().unwrap())[selection]); } - } - let selections = selections.into_iter().map(|x| x.unwrap() - 1); - let mut final_selections = Vec::new(); - for selection in selections { - final_selections.push((search_result.as_ref().unwrap())[selection]); - } - Rc::new(final_selections) + Rc::new(final_selections) + }; }; - }; - download_m3u8(to_download).await; + download_m3u8(to_download).await; + } + _ => {} } let choosen = user_wish.parse::(); match choosen { Ok(k) => { let search_result = search_result.as_ref().unwrap().clone(); - stream(&(search_result[k - 1]), mpv_fs) + stream(&(search_result[k - 1]), mpv_fs); + parser.save_watched(); } Err(e) => println!("Have to be a valid number! {:?}", e), } } - - parser.save_watched(); } /* diff --git a/src/parser.rs b/src/parser.rs index 6f82618..d2e6825 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -90,13 +90,8 @@ impl Parser { let _ = fs::create_dir_all(&*self.watched_name.parent().unwrap()); - match fs::write(&*self.watched_name, watched_items.join("\n")) { - Ok(_) => { - println!("Saved watched") - } - Err(e) => { - eprintln!("Failed to write downloaded m3u8file {:?}", e); - } + if let Err(e) = fs::write(&*self.watched_name, watched_items.join("\n")) { + eprintln!("Failed to write watched links {:?}", e); } }