diff --git a/Cargo.toml b/Cargo.toml index 8a35d35..3bcdb22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,10 @@ authors = ["Smart SangGe "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +<<<<<<< HEAD clap = "2.33.2" indicatif = "0.15" console = "0.14" +======= +clap = "2.26.0" +>>>>>>> parent of 4ca1b62 (feat: finish all func) diff --git a/src/main.rs b/src/main.rs index 373205a..f90f860 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ extern crate clap; +<<<<<<< HEAD extern crate indicatif; extern crate console; @@ -9,6 +10,10 @@ use std::fs::File; use std::io::{self, Write}; +======= + +use clap::{Arg, App}; +>>>>>>> parent of 4ca1b62 (feat: finish all func) fn main() { let matches = App::new("Rget") @@ -21,12 +26,8 @@ fn main() { .index(1) .help("url to download")) .get_matches(); - let url = matches.value_of("URL").unwrap(); - - if let Err(e) = download(url, false) { - eprintln!("Download error: {}", e); - } + println!("{}", url); } fn create_progress_bar(quiet_mode: bool, msg: &str, length: Option) -> ProgressBar { @@ -52,56 +53,41 @@ fn create_progress_bar(quiet_mode: bool, msg: &str, length: Option) -> Prog bar } -fn download(target: &str, quiet_mode: bool) -> Result<(), Box> { +fn download(target: &str, quiet_mode: bool) -> Result<(), Box<::std::error::Error>> { // parse url - let url = reqwest::Url::parse(target)?; - let client = reqwest::blocking::Client::new(); - let mut resp = client.get(url).send()?; - - - - if !quiet_mode { - println!("HTTP request sent... {}", style(resp.status()).green()); - } - + let url = parse_url(target)?; + let client = Client::new().unwrap(); + let mut resp = client.get(url)? + .send() + .unwrap(); + print(format!("HTTP request sent... {}", + style(format!("{}", resp.status())).green()), + quiet_mode); if resp.status().is_success() { let headers = resp.headers().clone(); - let ct_len = headers.get("content-length") - .and_then(|ct_len| ct_len.to_str().ok()) - .and_then(|ct_len_str| ct_len_str.parse::().ok()); - - let ct_type = headers.get("content-type") - .and_then(|ct_type| ct_type.to_str().ok()) - .unwrap(); // Be cautious with unwrap; it's better to handle None case + let ct_len = headers.get::().map(|ct_len| **ct_len); + let ct_type = headers.get::().unwrap(); match ct_len { Some(len) => { - if !quiet_mode { - println!("Length: {} ({})", - style(len).green(), - style(HumanBytes(len)).red()); - } + print(format!("Length: {} ({})", + style(len).green(), + style(format!("{}", HumanBytes(len))).red()), + quiet_mode); }, None => { - if !quiet_mode { - println!("Length: {}", style("unknown").red()); - } + print(format!("Length: {}", style("unknown").red()), quiet_mode); }, } + print(format!("Type: {}", style(ct_type).green()), quiet_mode); - // Print content type - custom_print(format!("Type: {}", style(ct_type).green()), quiet_mode); - - // Get the filename from the URL let fname = target.split("/").last().unwrap(); - // Print saving message - custom_print(format!("Saving to: {}", style(fname).green()), quiet_mode); - + print(format!("Saving to: {}", style(fname).green()), quiet_mode); let chunk_size = match ct_len { Some(x) => x as usize / 99, @@ -134,17 +120,4 @@ fn download(target: &str, quiet_mode: bool) -> Result<(), Box io::Result<()> { - let mut file = File::create(filename)?; - file.write_all(data)?; - Ok(()) -} - -fn custom_print(message: String, quiet_mode: bool) { - if !quiet_mode { - println!("{}", message); - } -} - +} \ No newline at end of file