Skip to content

Commit

Permalink
Make --pipe-input more flexible by enable specification of key
Browse files Browse the repository at this point in the history
  • Loading branch information
C0ffeeCode committed Mar 6, 2024
1 parent d4866a5 commit d9d720d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
28 changes: 21 additions & 7 deletions crates/typst-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ pub struct SharedArgs {
)]
pub inputs: Vec<(String, String)>,

/// Read input from stdin/pipe, visible through sys.inputs.piped
/// Read input from stdin/pipe and make it available at the specified key.
#[clap(
long = "pipe-input",
action = ArgAction::SetTrue,
action = ArgAction::Set,
value_parser = ValueParser::new(parse_input_key),
)]
pub pipe_input: bool,
pub pipe_input: Option<String>,

/// Adds additional directories to search for fonts
#[clap(
Expand Down Expand Up @@ -192,14 +193,27 @@ fn parse_input_pair(raw: &str) -> Result<(String, String), String> {
let (key, val) = raw
.split_once('=')
.ok_or("input must be a key and a value separated by an equal sign")?;
let key = key.trim().to_owned();
if key.is_empty() {
return Err("the key was missing or empty".to_owned());
}
let key = match parse_input_key(key) {
Ok(k) => k,
Err(e) => return Err(e),
};
let val = val.trim().to_owned();
Ok((key, val))
}

/// Parses and validates a key for input
///
/// # Errors
///
/// This function will return an error if the key is blank
fn parse_input_key(key: &str) -> Result<String, String> {
let trim = &key.trim();
if trim.is_empty() {
return Err("the key was missing or empty".to_owned());
}
Ok(trim.to_string())
}

/// Lists all discovered fonts in system and custom font paths
#[derive(Debug, Clone, Parser)]
pub struct FontsCommand {
Expand Down
12 changes: 7 additions & 5 deletions crates/typst-cli/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ impl SystemWorld {

let mut inputs = command.inputs.to_owned();
// Read data from pipe if enstructed to do so.
if command.pipe_input {
let stdin = stdin();
let stdin = stdin.lock();
let piped_input = read_to_string(stdin).expect("failed to read form pipe");
inputs.push(("piped".to_owned(), piped_input.trim().to_string()));
match &command.pipe_input {
Some(key) => {
let piped_input =
read_to_string(stdin().lock()).expect("failed to read form stdin");
inputs.push((key.to_owned(), piped_input.trim().to_string()));
}
None => {}
}

// Convert the Vec<(String, String)> to a Typst Dictionary.
Expand Down

0 comments on commit d9d720d

Please sign in to comment.