diff --git a/Cargo.toml b/Cargo.toml index 2e83686..e196ff6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,11 +13,13 @@ csv = "1.3.0" debug_print = "1.0.0" dynfmt = { version = "0.1.5", features = ["curly"] } enum_dispatch = "0.3.13" +futures = "0.3.30" glob = "0.3.1" phf = { version = "0.11.2", features = ["macros"] } regex = "1.10.4" regex_static = "0.1.1" serde = { version = "1.0.198", features = ["derive"] } +tokio = { version = "1.38.0", features = ["full"] } toml = "0.8.12" [profile.dev] diff --git a/src/main.rs b/src/main.rs index 1b5deb9..4618302 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ // #![allow(dead_code)] #![warn(missing_docs)] #![warn(clippy::missing_docs_in_private_items)] +mod makepdf; mod stat_algo; mod stat_io; mod stat_structs; @@ -21,6 +22,7 @@ use std::{ use anyhow::{Context, Result}; use clap::{Args, Parser, Subcommand}; +use makepdf::{make_pdf, MakeConfig}; use serde::Deserialize; use stats::{stat_solver, StatConfig}; use table_structs::Craft; @@ -45,6 +47,8 @@ enum Commands { Config(ConfigArgs), /// Handle stat repartition Stats(StatConfig), + /// Make a pdf via pdflatex + Make(MakeConfig), } #[derive(Debug, Args)] @@ -90,6 +94,7 @@ fn main() -> Result<()> { Commands::Tablegen(args) => table_gen(&args, &config), Commands::Config(args) => write_config(&args, &config, config_str), Commands::Stats(args) => stat_solver(&args, &config), + Commands::Make(args) => make_pdf(&args, &config), } } diff --git a/src/makepdf.rs b/src/makepdf.rs new file mode 100644 index 0000000..c336b89 --- /dev/null +++ b/src/makepdf.rs @@ -0,0 +1,92 @@ +//! Make a pdf using pdflatex + +use anyhow::Result; +use clap::Args; + +use crate::Config; + +#[derive(Args, Debug, Clone)] +/// The configuration struct for the PdfMaker +pub(crate) struct MakeConfig { + /// The filename to compile + pub(crate) filename: String, + /// The verbosity level + #[arg(short, long, default_value_t = false)] + pub(crate) verbosity: bool, +} + +/// Make a pdf spawning a subprocess +pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> { + let _ = config; + launch_subprocess(args).unwrap(); + Ok(()) +} + +use tokio::io::{AsyncBufReadExt, BufReader}; +use tokio::process::Command; + +use std::process::Stdio; + +#[tokio::main] +/// Pdfreading and curating +async fn launch_subprocess(args: &MakeConfig) -> Result<(), Box> { + let mut cmd = Command::new("pdflatex"); + // cmd.arg("-8bit"); + cmd.arg(&args.filename); + + // Specify that we want the command's standard output piped back to us. + // By default, standard input/output/error will be inherited from the + // current process (for example, this means that standard input will + // come from the keyboard and standard output/error will go directly to + // the terminal if this process is invoked from the command line). + cmd.stdout(Stdio::piped()); + + let mut child = cmd.spawn().expect("failed to spawn command"); + + let stdout = child + .stdout + .take() + .expect("child did not have a handle to stdout"); + + let mut reader = BufReader::new(stdout).split(0xA); + + // Ensure the child process is spawned in the runtime so it can + // make progress on its own while we await for any output. + tokio::spawn(async move { + let status = child + .wait() + .await + .expect("child process encountered an error"); + + println!("child status was: {}", status); + }); + + // while let Some(line) = reader.next_line().await? { + // // let vec: Vec = line.into(); + // println!("Line: {:?}", line); + // } + loop { + let res = reader.next_segment().await; + if let Err(e) = res { + println!("{}", e); + break; + } else if let Ok(line) = res { + if let Some(line) = line { + use std::str; + let s = match str::from_utf8(&line) { + Ok(v) => v, + Err(e) => { + println!("Invalid UTF-8 sequence: {}", e); + continue; + } + }; + println!("Line: {:?}", s); + } else { + break; + } + } + // let vec: Vec = line.into(); + } + + Ok(()) +}