Skip to content

Commit

Permalink
support writing to stdout for piping
Browse files Browse the repository at this point in the history
  • Loading branch information
C0ffeeCode committed Jan 11, 2024
1 parent e75e43a commit 66729f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions crates/typst-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ pub struct CompileCommand {
/// Path to output file (PDF, PNG, or SVG)
pub output: Option<PathBuf>,

#[clap(
long = "stdout",
default_value_t = false,
)]
pub output_to_stdout: bool,

/// The format of the output file, inferred from the extension by default
#[arg(long = "format", short = 'f')]
pub format: Option<OutputFormat>,
Expand Down
17 changes: 14 additions & 3 deletions crates/typst-cli/src/compile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

use chrono::{Datelike, Timelike};
Expand Down Expand Up @@ -166,9 +167,19 @@ fn export_pdf(
) -> StrResult<()> {
let ident = world.input().to_string_lossy();
let buffer = typst_pdf::pdf(document, Some(&ident), now());
let output = command.output();
fs::write(output, buffer)
.map_err(|err| eco_format!("failed to write PDF file ({err})"))?;

// If to stdout
if command.output_to_stdout {
let mut stdout = std::io::stdout().lock();
stdout.write_all(&buffer)
.map_err(|err| eco_format!("failed to write PDF to stdout ({err})"))?;
}
// If not to stdout or output file is explicitly set
if !command.output_to_stdout | command.output.is_some() {
let output = command.output();
fs::write(output, buffer)
.map_err(|err| eco_format!("failed to write PDF file ({err})"))?;
}
Ok(())
}

Expand Down

0 comments on commit 66729f9

Please sign in to comment.