Skip to content

Commit 0a998ec

Browse files
committed
use STDIN and STDOUT
1 parent 929ae6d commit 0a998ec

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapterize/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chapterize"
3-
version = "1.1.0"
3+
version = "1.2.0"
44
edition = "2021"
55
description = "Parse EDLs to generate YouTube timestamps."
66

chapterize/src/main.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use chrono::Duration;
22
use clap::{command, Parser};
33
use edl::entry::Entry;
44
use std::fmt::Write as FmtWrite;
5+
use std::io;
56
use std::{
67
error::Error,
78
fs,
@@ -13,10 +14,12 @@ use std::{
1314
#[command(author, version, about, long_about = None)]
1415
struct Args {
1516
/// The input EDL file.
17+
/// When not specified, input will be read from STDIN.
1618
#[arg()]
17-
input: String,
19+
input: Option<String>,
1820

19-
/// The output TXT file (defaults to input file name + .txt).
21+
/// The output TXT file.
22+
/// When not specified, output will be printed to STDOUT.
2023
#[arg(short, long)]
2124
output: Option<String>,
2225

@@ -32,7 +35,11 @@ struct Args {
3235
fn main() -> Result<(), Box<dyn Error>> {
3336
let args = Args::parse();
3437

35-
let mut f = fs::File::open(Path::new(&args.input))?;
38+
let mut f: Box<dyn Read> = if let Some(input_file) = args.input {
39+
Box::new(fs::File::open(Path::new(&input_file))?)
40+
} else {
41+
Box::new(io::stdin())
42+
};
3643

3744
let mut data = String::new();
3845
f.read_to_string(&mut data)?;
@@ -53,21 +60,28 @@ fn main() -> Result<(), Box<dyn Error>> {
5360
})
5461
.collect();
5562

56-
let output_file = args.output.unwrap_or_else(|| format!("{}.txt", args.input));
57-
let mut out = fs::File::create(Path::new(&output_file))?;
63+
if let Some(output_file) = args.output {
64+
let mut out = fs::File::create(Path::new(&output_file))?;
65+
wtite_entries(&entries, &mut out)?;
66+
println!("{output_file} has been generated.");
67+
} else {
68+
wtite_entries(&entries, &mut io::stdout())?;
69+
}
5870

71+
Ok(())
72+
}
73+
74+
fn wtite_entries(entries: &Vec<&Entry>, w: &mut impl Write) -> io::Result<()> {
5975
let with_hours =
6076
entries.last().is_some() && entries.last().unwrap().timestamp >= Duration::hours(1);
6177

6278
let def_name = String::from("-");
6379

6480
for e in entries {
6581
let name = e.name.as_ref().unwrap_or(&def_name);
66-
writeln!(out, "{} {}", to_timestamp(e.timestamp, with_hours), name)?;
82+
writeln!(w, "{} {}", to_timestamp(e.timestamp, with_hours), name)?;
6783
}
6884

69-
println!("{output_file} has been generated.");
70-
7185
Ok(())
7286
}
7387

0 commit comments

Comments
 (0)