Save watched right after something is watched, and use match

This commit is contained in:
Love 2023-02-27 16:24:16 +01:00
parent d580ca8956
commit 35bb1edfc3
5 changed files with 69 additions and 93 deletions

29
Cargo.lock generated
View File

@ -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"

View File

@ -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"] }

View File

@ -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

View File

@ -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::<usize>())
.collect::<Vec<Result<usize, ParseIntError>>>();
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::<usize>())
.collect::<Vec<Result<usize, ParseIntError>>>();
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::<usize>();
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();
}
/*

View File

@ -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);
}
}