@@ -2,6 +2,7 @@ use chrono::Duration;
2
2
use clap:: { command, Parser } ;
3
3
use edl:: entry:: Entry ;
4
4
use std:: fmt:: Write as FmtWrite ;
5
+ use std:: io;
5
6
use std:: {
6
7
error:: Error ,
7
8
fs,
@@ -13,10 +14,12 @@ use std::{
13
14
#[ command( author, version, about, long_about = None ) ]
14
15
struct Args {
15
16
/// The input EDL file.
17
+ /// When not specified, input will be read from STDIN.
16
18
#[ arg( ) ]
17
- input : String ,
19
+ input : Option < String > ,
18
20
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.
20
23
#[ arg( short, long) ]
21
24
output : Option < String > ,
22
25
@@ -32,7 +35,11 @@ struct Args {
32
35
fn main ( ) -> Result < ( ) , Box < dyn Error > > {
33
36
let args = Args :: parse ( ) ;
34
37
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
+ } ;
36
43
37
44
let mut data = String :: new ( ) ;
38
45
f. read_to_string ( & mut data) ?;
@@ -53,21 +60,28 @@ fn main() -> Result<(), Box<dyn Error>> {
53
60
} )
54
61
. collect ( ) ;
55
62
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
+ }
58
70
71
+ Ok ( ( ) )
72
+ }
73
+
74
+ fn wtite_entries ( entries : & Vec < & Entry > , w : & mut impl Write ) -> io:: Result < ( ) > {
59
75
let with_hours =
60
76
entries. last ( ) . is_some ( ) && entries. last ( ) . unwrap ( ) . timestamp >= Duration :: hours ( 1 ) ;
61
77
62
78
let def_name = String :: from ( "-" ) ;
63
79
64
80
for e in entries {
65
81
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) ?;
67
83
}
68
84
69
- println ! ( "{output_file} has been generated." ) ;
70
-
71
85
Ok ( ( ) )
72
86
}
73
87
0 commit comments