aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Jaremko2019-01-15 23:48:47 -0500
committerNathan Jaremko2019-01-15 23:48:47 -0500
commita8f00062a2365e8933c572ed0366e4d4c950e06a (patch)
treecc1179eb97ded84eee1863ec2d1b29fbd8173910
parenta30116498585badecb7bb13c4a1b0a48394cf03e (diff)
downloadpodcast-a8f00062a2365e8933c572ed0366e4d4c950e06a.tar.bz2
Improve handling of piping
-rw-r--r--src/actions.rs27
-rw-r--r--src/structs.rs15
2 files changed, 26 insertions, 16 deletions
diff --git a/src/actions.rs b/src/actions.rs
index e4ff9f7..ca4f3c0 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -15,9 +15,6 @@ use std::path::PathBuf;
use toml;
pub fn list_episodes(search: &str) -> Result<()> {
- let stdout = io::stdout();
- let mut handle = stdout.lock();
-
let re = Regex::new(&format!("(?i){}", &search)).chain_err(|| UNABLE_TO_PARSE_REGEX)?;
let mut path = get_podcast_dir()?;
path.push(".rss");
@@ -33,6 +30,8 @@ pub fn list_episodes(search: &str) -> Result<()> {
.chain_err(|| UNABLE_TO_CREATE_CHANNEL_FROM_FILE)?;
let podcast = Podcast::from(channel);
let episodes = podcast.episodes();
+ let stdout = io::stdout();
+ let mut handle = stdout.lock();
for (num, ep) in episodes.iter().enumerate() {
writeln!(
&mut handle,
@@ -41,7 +40,7 @@ pub fn list_episodes(search: &str) -> Result<()> {
ep.title()
.chain_err(|| "unable to retrieve episode title")?
)
- .chain_err(|| "unable to write to stdout")?
+ .ok();
}
return Ok(());
}
@@ -129,7 +128,7 @@ pub fn list_subscriptions(state: &State) -> Result<()> {
let stdout = io::stdout();
let mut handle = stdout.lock();
for podcast in &state.subscriptions() {
- writeln!(&mut handle, "{}", &podcast.title).chain_err(|| "unable to write to stdout")?;
+ writeln!(&mut handle, "{}", &podcast.title).ok();
}
Ok(())
}
@@ -166,8 +165,12 @@ pub fn download_episode_by_num(state: &State, p_search: &str, e_search: &str) ->
}
}
} else {
- println!("Failed to parse episode number...");
- println!("Attempting to find episode by name...");
+ {
+ let stdout = io::stdout();
+ let mut handle = stdout.lock();
+ writeln!(&mut handle, "Failed to parse episode number...").ok();
+ writeln!(&mut handle, "Attempting to find episode by name...").ok();
+ }
download_episode_by_name(state, p_search, e_search, false)
.chain_err(|| "Failed to download episode.")?;
}
@@ -198,7 +201,7 @@ pub fn download_episode_by_name(
})
.for_each(|ep| {
ep.download(podcast.title()).unwrap_or_else(|_| {
- println!("Error downloading episode: {}", podcast.title())
+ eprintln!("Error downloading episode: {}", podcast.title())
});
})
} else {
@@ -337,8 +340,12 @@ pub fn play_episode_by_num(state: &State, p_search: &str, ep_num_string: &str) -
}
}
} else {
- println!("Failed to parse episode number...");
- println!("Attempting to find episode by name...");
+ {
+ let stdout = io::stdout();
+ let mut handle = stdout.lock();
+ writeln!(&mut handle, "Failed to parse episode number...").ok();
+ writeln!(&mut handle, "Attempting to find episode by name...").ok();
+ }
play_episode_by_name(state, p_search, ep_num_string)
.chain_err(|| "Failed to play episode by name.")?;
}
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();
}
}
}