diff options
| author | Nathan Jaremko | 2019-01-15 23:48:47 -0500 | 
|---|---|---|
| committer | Nathan Jaremko | 2019-01-15 23:48:47 -0500 | 
| commit | a8f00062a2365e8933c572ed0366e4d4c950e06a (patch) | |
| tree | cc1179eb97ded84eee1863ec2d1b29fbd8173910 /src/structs.rs | |
| parent | a30116498585badecb7bb13c4a1b0a48394cf03e (diff) | |
| download | podcast-a8f00062a2365e8933c572ed0366e4d4c950e06a.tar.bz2 | |
Improve handling of piping
Diffstat (limited to 'src/structs.rs')
| -rw-r--r-- | src/structs.rs | 15 | 
1 files changed, 9 insertions, 6 deletions
| diff --git a/src/structs.rs b/src/structs.rs index 4e9ef43..0fde2b3 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -276,7 +276,7 @@ impl Podcast {              if let Some(ep_title) = episodes[episodes.len() - ep_num].title() {                  if !downloaded.contains(&ep_title) {                      if let Err(err) = episodes[episodes.len() - ep_num].download(self.title()) { -                        println!("{}", err); +                        eprintln!("{}", err);                      }                  }              } @@ -311,6 +311,8 @@ impl Episode {      }      pub fn download(&self, podcast_name: &str) -> Result<()> { +        let stdout = io::stdout(); +          let mut path = get_podcast_dir()?;          path.push(podcast_name);          DirBuilder::new() @@ -327,7 +329,10 @@ impl Episode {                  );                  path.push(filename);                  if !path.exists() { -                    println!("Downloading: {}", path.to_str().unwrap()); +                    { +                        let mut handle = stdout.lock(); +                        writeln!(&mut handle, "Downloading: {}", path.to_str().unwrap()).ok(); +                    }                      let mut file = File::create(&path).chain_err(|| UNABLE_TO_CREATE_FILE)?;                      let mut resp = reqwest::get(url).chain_err(|| UNABLE_TO_GET_HTTP_RESPONSE)?;                      let mut content: Vec<u8> = Vec::new(); @@ -336,10 +341,8 @@ impl Episode {                      file.write_all(&content)                          .chain_err(|| UNABLE_TO_WRITE_FILE)?;                  } else { -                    println!( -                        "File already exists: {}", -                        path.to_str().chain_err(|| UNABLE_TO_CONVERT_TO_STR)? -                    ); +                    let mut handle = stdout.lock(); +                    writeln!(&mut handle, "File already exists: {}", path.to_str().chain_err(|| UNABLE_TO_CONVERT_TO_STR)?).ok();                  }              }          } | 
