diff --git a/src/lib.rs b/src/lib.rs index 130f74b..6cf0b33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ mod m3u8; mod parser; use std::{ fs, - io::{stdin, stdout, Write}, + io::{stdin, stdout, Stdin, StdoutLock, Write}, process, }; @@ -21,22 +21,14 @@ pub fn setup() -> String { return fs::read_to_string(&ilovetv_config_file).expect("Failed to read iptv_url"); } - let mut stdout = stdout().lock(); - let stdin = stdin(); + let mut readline = Readline::new(); println!("Hello, I would need an url to your iptv/m3u/m3u8 stream"); let url = loop { - print!("enter url: "); - stdout.flush().unwrap(); - let mut url = String::new(); - let _ = stdin.read_line(&mut url); + let url = readline.input("enter url: "); + let yn = readline.input("Are you sure? (Y/n) "); - print!("Are you sure? (Y/n) "); - stdout.flush().unwrap(); - let mut yn = String::new(); - let _ = stdin.read_line(&mut yn); - - if yn.to_lowercase() != "n" { + if yn.trim().to_lowercase() != "n" { break url.trim().to_string(); } }; @@ -49,3 +41,25 @@ pub fn setup() -> String { url } + +pub struct Readline<'a> { + stdout: StdoutLock<'a>, + stdin: Stdin, +} + +impl<'a> Readline<'a> { + pub fn new() -> Self { + Self { + stdout: stdout().lock(), + stdin: stdin(), + } + } + + pub fn input(&mut self, toprint: &str) -> String { + print!("{}", toprint); + self.stdout.flush().unwrap(); + let mut buffer = String::new(); + self.stdin.read_line(&mut buffer).unwrap(); + buffer + } +} diff --git a/src/main.rs b/src/main.rs index 672f747..835a9a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,39 +1,32 @@ -use std::io::{self, stdout, Write}; +use std::io::{self, stdin, stdout, Stdin, StdoutLock, Write}; use std::num::ParseIntError; use std::process::Command; use std::rc::Rc; -use iptvnator_rs::{download_with_progress, setup, M3u8, Parser}; +use iptvnator_rs::{download_with_progress, setup, M3u8, Parser, Readline}; #[tokio::main] async fn main() { println!("Welcome to iptvnator_rs, the port of my iptvprogram written in python, now in rust BLAZINGLY FAST\n"); let parser = Parser::new("iptv.m3u8".to_owned(), setup(), "watched.txt".to_owned()).await; - let stdin = io::stdin(); - let mut stdout = stdout().lock(); let mut search_result: Option>> = None; + let mut readline = Readline::new(); loop { - let mut buf = String::new(); - // Dont't perform a search if user has just watched, instead present the previous search if search_result.is_none() { - print!("Search by name: "); - stdout.flush().unwrap(); - stdin.read_line(&mut buf).unwrap(); - buf = buf.trim().to_owned(); + let search = readline.input("Search by name: "); // If they want to quit, let them- - if buf.trim() == "q" { + if search.trim() == "q" { break; } - search_result = Some(Rc::new(parser.find(&buf))); + search_result = Some(Rc::new(parser.find(&search))); if search_result.as_ref().unwrap().len() == 0 { println!("Nothing found"); - stdout.flush().unwrap(); continue; } } @@ -42,12 +35,10 @@ async fn main() { for (idx, m3u8_item) in search_result.as_ref().unwrap().iter().enumerate().rev() { println!(" {}: {}", idx + 1, m3u8_item); } - print!("Which one do you wish to stream? [ q/s/r/d ]: "); - stdout.flush().unwrap(); - buf = String::new(); - stdin.read_line(&mut buf).unwrap(); - let user_wish = buf.trim(); + let user_wish = readline.input("Which one do you wish to stream? [ q/s/r/d ]: "); + let user_wish = user_wish.trim(); + // If they want to quit, let them- if user_wish == "q" { break; @@ -67,13 +58,9 @@ async fn main() { } continue; } else if user_wish == "d" { - print!("Download all or select in comma separated [A]: "); - stdout.flush().unwrap(); - - let mut selection = String::new(); - stdin.read_line(&mut selection).unwrap(); - + let selection = readline.input("Download all or select in comma separated [A]: "); let selection = selection.trim(); + let to_download = loop { break if selection.to_lowercase() == "a" { println!("Downloading all");