Skip to content

Commit

Permalink
fix: avoid appending an unnecessary newline to input files
Browse files Browse the repository at this point in the history
  • Loading branch information
tranzystorekk committed Dec 6, 2022
1 parent 72c57c8 commit a776bf8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ fn main() -> Result<()> {
Ok(())
}

fn print_content(content: &str) -> Result<()> {
print!("{}", content);
std::io::stdout().flush()?;

Ok(())
}

fn do_input_cmd(state: &State, args: InputArgs) -> Result<()> {
let (year, day) = match (args.year, args.day) {
(Some(y), Some(d)) => (y, d),
Expand All @@ -61,7 +68,7 @@ fn do_input_cmd(state: &State, args: InputArgs) -> Result<()> {

if args.force {
let contents = web::fetch_input(state, year, day)?;
println!("{}", contents);
print_content(&contents)?;
cache::force_write(year, day, contents.as_bytes())?;
return Ok(());
}
Expand All @@ -71,11 +78,11 @@ fn do_input_cmd(state: &State, args: InputArgs) -> Result<()> {
let mut buf = String::new();
file.read_to_string(&mut buf)
.context("Failed to read cached input file")?;
println!("{}", buf);
print_content(&buf)?;
}
Entry::Missing(mut file) => {
let contents = web::fetch_input(state, year, day)?;
println!("{}", contents);
print_content(&contents)?;
file.write_all(contents.as_bytes())
.context("Failed to write input file to cache")?;
}
Expand Down

0 comments on commit a776bf8

Please sign in to comment.