Added `--write-only` & `--no-consume` for when loading a chain is all that"s desired. Added 0 line output (default is still 1) for if saving/modifying a chain is all that"s desired.

Bumped version.

Fortune for genmarkov's current commit: Middle blessing − 中吉
io-uring-async-support
Avril 2 weeks ago
parent 0a7d530068
commit 5293810b79
Signed by: flanchan
GPG Key ID: 284488987C31F630

2
Cargo.lock generated

@ -388,7 +388,7 @@ checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "markov"
version = "0.2.1+1"
version = "0.2.2"
dependencies = [
"async-compression",
"bytes",

@ -1,6 +1,6 @@
[package]
name = "markov"
version = "0.2.1+1"
version = "0.2.2"
description = "Generate string of text from Markov chain fed by stdin or file(s)"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
@ -20,10 +20,10 @@ lto = "fat"
strip = false
[features]
default = ["threads", "io_uring"]
default = ["threads"]
threads = ["zstd/zstdmt", "dep:num_cpus"]
io_uring = ["dep:tokio-uring", "dep:async-compression", "dep:futures", "dep:tokio"]
#io_uring = ["dep:tokio-uring", "dep:async-compression", "dep:futures", "dep:tokio"]
[dependencies]
async-compression = { version = "0.4.18", features = ["tokio", "zstd", "zstdmt"], optional = true }

@ -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();

Loading…
Cancel
Save