@ -25,15 +25,24 @@ pub struct Cli {
save : Option < PathBuf > ,
/// Load the chain from the provided output file name before appending to it.
#[ arg(short, long) ]
load : Option < PathBuf > ,
load : Option < PathBuf > , // TODO: Group with `save` & add `-F/--file` opt for save & load from same file.
/// Force over-writes of files, and force output of binary data to TTY.
#[ arg(short, long) ]
force : bool ,
/// The number of lines to output from the chain.
lines : Option < NonZeroUsize > , // TODO: Allow 0 for only save + load operations.
/// Do not read into chain from `stdin` if there is a loaded chain that is not empty.
#[ arg(short, long) ]
no_consume : bool ,
/// Do not read into chain from `stdin` ever.
#[ arg(short, long= " write " ) ]
write_only : bool , // XXX: Should we group this with `no_consume`?
//TODO: Num of lines, etc.
/// The number of lines to output from the chain. Default is 1.
///
/// If 0, generation from chain is skipped.
#[ arg(default_value_t = 1) ]
lines : usize ,
}
fn buffered_read_all_lines < T : BufRead + ? Sized , F : FnMut ( & str ) -> io ::Result < ( ) > > ( input : & mut T , mut then : F ) -> io ::Result < usize >
@ -106,24 +115,26 @@ fn main() {
let mut stdin = stdin . lock ( ) ;
let mut chain = create_chain ( & cli ) ;
//TODO: When chain is not empty (i.e. loaded,) it is okay for the input to be empty. (XXX: Should we *ignore* stdin for this? Empty stdin (CTRL+D on TTY) works. Do we actually need to change this behaviour? I don't think so.) (TODO: Add option to skip reading when loading file, maybe?)
buffered_read_all_lines ( & mut stdin , | string | {
chain . feed ( & string . split_whitespace ( )
. filter ( | word | ! word . is_empty ( ) )
. map ( | s | s . to_owned ( ) ) . collect ::< Vec < _ > > ( ) ) ;
Ok ( ( ) )
} ) . expect ( "Failed to read from stdin" ) ;
if ! ( cli . write_only | | ( cli . no_consume & & ! chain . is_empty ( ) ) ) {
buffered_read_all_lines ( & mut stdin , | string | {
chain . feed ( & string . split_whitespace ( )
. filter ( | word | ! word . is_empty ( ) )
. map ( | s | s . to_owned ( ) ) . collect ::< Vec < _ > > ( ) ) ;
Ok ( ( ) )
} ) . expect ( "Failed to read from stdin" ) ;
}
if ! chain . is_empty ( ) {
let lines = cli . lines . map ( NonZeroUsize ::get ) . unwrap_or ( 1 ) ;
if lines > 1 {
for string in chain . str_iter_for ( lines ) {
println! ( "{}" , string ) ;
}
} else {
println! ( "{}" , chain . generate_str ( ) ) ;
}
match cli . lines {
0 = > ( ) ,
1 = > println! ( "{}" , chain . generate_str ( ) ) ,
lines = > {
for string in chain . str_iter_for ( lines ) {
println! ( "{}" , string ) ;
}
} ,
} ;
}
complete_chain ( & cli , chain ) . unwrap ( ) ;