Extract readline
This commit is contained in:
		
							
								
								
									
										40
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								src/lib.rs
									
									
									
									
									
								
							@@ -2,7 +2,7 @@ mod m3u8;
 | 
				
			|||||||
mod parser;
 | 
					mod parser;
 | 
				
			||||||
use std::{
 | 
					use std::{
 | 
				
			||||||
    fs,
 | 
					    fs,
 | 
				
			||||||
    io::{stdin, stdout, Write},
 | 
					    io::{stdin, stdout, Stdin, StdoutLock, Write},
 | 
				
			||||||
    process,
 | 
					    process,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -21,22 +21,14 @@ pub fn setup() -> String {
 | 
				
			|||||||
        return fs::read_to_string(&ilovetv_config_file).expect("Failed to read iptv_url");
 | 
					        return fs::read_to_string(&ilovetv_config_file).expect("Failed to read iptv_url");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut stdout = stdout().lock();
 | 
					    let mut readline = Readline::new();
 | 
				
			||||||
    let stdin = stdin();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    println!("Hello, I would need an url to your iptv/m3u/m3u8 stream");
 | 
					    println!("Hello, I would need an url to your iptv/m3u/m3u8 stream");
 | 
				
			||||||
    let url = loop {
 | 
					    let url = loop {
 | 
				
			||||||
        print!("enter url: ");
 | 
					        let url = readline.input("enter url: ");
 | 
				
			||||||
        stdout.flush().unwrap();
 | 
					        let yn = readline.input("Are you sure? (Y/n) ");
 | 
				
			||||||
        let mut url = String::new();
 | 
					 | 
				
			||||||
        let _ = stdin.read_line(&mut url);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        print!("Are you sure? (Y/n) ");
 | 
					        if yn.trim().to_lowercase() != "n" {
 | 
				
			||||||
        stdout.flush().unwrap();
 | 
					 | 
				
			||||||
        let mut yn = String::new();
 | 
					 | 
				
			||||||
        let _ = stdin.read_line(&mut yn);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if yn.to_lowercase() != "n" {
 | 
					 | 
				
			||||||
            break url.trim().to_string();
 | 
					            break url.trim().to_string();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
@@ -49,3 +41,25 @@ pub fn setup() -> String {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    url
 | 
					    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
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										35
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								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::num::ParseIntError;
 | 
				
			||||||
use std::process::Command;
 | 
					use std::process::Command;
 | 
				
			||||||
use std::rc::Rc;
 | 
					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]
 | 
					#[tokio::main]
 | 
				
			||||||
async fn main() {
 | 
					async fn main() {
 | 
				
			||||||
    println!("Welcome to iptvnator_rs, the port of my iptvprogram written in python, now in rust BLAZINGLY FAST\n");
 | 
					    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 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<Rc<Vec<&M3u8>>> = None;
 | 
					    let mut search_result: Option<Rc<Vec<&M3u8>>> = None;
 | 
				
			||||||
 | 
					    let mut readline = Readline::new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    loop {
 | 
					    loop {
 | 
				
			||||||
        let mut buf = String::new();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Dont't perform a search if user has just watched, instead present the previous search
 | 
					        // Dont't perform a search if user has just watched, instead present the previous search
 | 
				
			||||||
        if search_result.is_none() {
 | 
					        if search_result.is_none() {
 | 
				
			||||||
            print!("Search by name: ");
 | 
					            let search = readline.input("Search by name: ");
 | 
				
			||||||
            stdout.flush().unwrap();
 | 
					 | 
				
			||||||
            stdin.read_line(&mut buf).unwrap();
 | 
					 | 
				
			||||||
            buf = buf.trim().to_owned();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // If they want to quit, let them-
 | 
					            // If they want to quit, let them-
 | 
				
			||||||
            if buf.trim() == "q" {
 | 
					            if search.trim() == "q" {
 | 
				
			||||||
                break;
 | 
					                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 {
 | 
					            if search_result.as_ref().unwrap().len() == 0 {
 | 
				
			||||||
                println!("Nothing found");
 | 
					                println!("Nothing found");
 | 
				
			||||||
                stdout.flush().unwrap();
 | 
					 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -42,12 +35,10 @@ async fn main() {
 | 
				
			|||||||
        for (idx, m3u8_item) in search_result.as_ref().unwrap().iter().enumerate().rev() {
 | 
					        for (idx, m3u8_item) in search_result.as_ref().unwrap().iter().enumerate().rev() {
 | 
				
			||||||
            println!("  {}: {}", idx + 1, m3u8_item);
 | 
					            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 they want to quit, let them-
 | 
				
			||||||
        if user_wish == "q" {
 | 
					        if user_wish == "q" {
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
@@ -67,13 +58,9 @@ async fn main() {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            continue;
 | 
					            continue;
 | 
				
			||||||
        } else if user_wish == "d" {
 | 
					        } else if user_wish == "d" {
 | 
				
			||||||
            print!("Download all or select in comma separated [A]: ");
 | 
					            let selection = readline.input("Download all or select in comma separated [A]: ");
 | 
				
			||||||
            stdout.flush().unwrap();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            let mut selection = String::new();
 | 
					 | 
				
			||||||
            stdin.read_line(&mut selection).unwrap();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            let selection = selection.trim();
 | 
					            let selection = selection.trim();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            let to_download = loop {
 | 
					            let to_download = loop {
 | 
				
			||||||
                break if selection.to_lowercase() == "a" {
 | 
					                break if selection.to_lowercase() == "a" {
 | 
				
			||||||
                    println!("Downloading all");
 | 
					                    println!("Downloading all");
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user