diff --git a/Cargo.toml b/Cargo.toml index f3b76e7..45cb137 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,14 @@ edition = "2021" [dependencies] anyhow = "1.0.86" clap = { version = "4.5.7", features = ["derive"] } +console = "0.15.8" 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" +indicatif = "0.17.8" logos = "0.14.0" phf = { version = "0.11.2", features = ["macros"] } regex = "1.10.5" diff --git a/src/main.rs b/src/main.rs index 4618302..8871d9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ #![warn(missing_docs)] #![warn(clippy::missing_docs_in_private_items)] mod makepdf; +mod makepdf_structs; mod stat_algo; mod stat_io; mod stat_structs; diff --git a/src/makepdf.rs b/src/makepdf.rs index 7354a75..572d38b 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -1,8 +1,11 @@ //! Make a pdf using pdflatex -// use std::fmt::Display; -use logos::{Lexer, Logos}; -use phf::phf_map; +// use std::str::pattern::Pattern; +use std::time::{Duration, Instant}; +use std::{cmp::min, fmt::Write}; + +use indicatif::{ProgressBar, ProgressState, ProgressStyle}; +use logos::Logos; use std::str; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; @@ -11,6 +14,10 @@ use anyhow::{anyhow, Result}; use clap::Args; use std::process::Stdio; +use crate::makepdf_structs::{ + BuildInfo, LineType, Status, VerboseLevel, COL_NORMAL, TXT_BOXX, TXT_CNTX, TXT_CRIT, TXT_ERRR, + TXT_INFO, TXT_MISC, TXT_UKNW, TXT_WARN, +}; use crate::Config; #[derive(Args, Debug, Clone)] @@ -27,53 +34,6 @@ pub(crate) struct MakeConfig { // pub(crate) blacklist: Vec, } -/// The verbosity level -#[derive(Clone, Debug, PartialEq, PartialOrd)] -pub enum VerboseLevel { - /// Shouldn't be printed no matter what - None, - /// Garbage - Misc, - /// Boxness, barely above Misc - Box, - /// Context info (Debug info) - Context, - /// Usefull info - Info, - /// Important warnings - Warning, - /// Errors encountered requiring user input - Error, - /// Uncatachable errors - Critical, - /// Literrally uncatchable things - Uncatchable, -} - -static STR_TO_TARGET: phf::Map<&'static str, VerboseLevel> = phf_map! { - "misc" =>VerboseLevel::Misc, - "box" => VerboseLevel::Box, - "context" => VerboseLevel::Context, - "info" => VerboseLevel::Info, - "warning" => VerboseLevel::Warning, - "error" => VerboseLevel::Error, - "critical" => VerboseLevel::Critical, -}; - -impl From<&str> for VerboseLevel { - fn from(value: &str) -> Self { - if let Some(target) = STR_TO_TARGET.get(value).cloned() { - target - } else { - panic!( - "Invalid debug level {}. The debug level available are: {:?}", - value, - STR_TO_TARGET.keys() - ) - } - } -} - /// Make a pdf spawning a subprocess pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> { let _ = config; @@ -81,54 +41,21 @@ pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> { Ok(()) } -#[derive(Debug)] -/// The current status of a run, holding all final info -struct Status { - /// Number of encountered info (mainly o/ufull) - info: usize, - /// Number of warnings, mainly label/reference rerun - warnings: usize, - /// Only makes sense with non-interactie builds - errors: usize, - /// List of all recoverable - v_errors: Vec, -} - -impl Status { - /// Add a recoverable error to the list of errors - fn add_error(&mut self, error_msg: String) { - self.errors += 1; - self.v_errors.push(error_msg); - } -} - -// impl Display for Status { -// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -// todo!() -// } -// } - -impl Status { - /// Initialize a new status - fn zero() -> Self { - Self { - info: 0, - warnings: 0, - errors: 0, - v_errors: Vec::new(), - } - } -} - #[tokio::main] /// Pdfreading and curating async fn launch_subprocess(args: &MakeConfig) -> Result { let mut cmd = Command::new("pdflatex"); let mut status = Status::zero(); - let mut context = Vec::new(); + + let started = Instant::now(); + let mut current_run = BuildInfo::new(args); + let last_run = BuildInfo::load(args); + + let mut built = 0; + let to_build = last_run.lines_parsed; + cmd.arg(&args.filename); cmd.env("max_print_line", "3000"); - // Specify that we want the command's standard output piped back to us. cmd.stdout(Stdio::piped()); @@ -144,16 +71,36 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { // 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 + let _status = child .wait() .await .expect("Child process encountered an error"); - - println!("Child status was: {}", status); + // sleep(Duration::from_millis(100)).await; + // println!("Child status was: {}", status); }); + let pb = ProgressBar::new(to_build); + pb.set_style( + ProgressStyle::with_template( + "{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] (lines: {pos}/{len}) (speed: {per_sec}) (eta: {eta})", + ) + .unwrap() + .with_key("eta", |state: &ProgressState, w: &mut dyn Write| { + write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap() + }) + .with_key("per_sec",|state:&ProgressState,w:&mut dyn Write|{ + write!(w,"{:.1}/lps",state.per_sec()).unwrap() + }) + .progress_chars("#>-"), + ); + pb.enable_steady_tick(Duration::from_millis(100)); + loop { let res = reader.next_segment().await; + let new = min(built + 1, to_build); + built = new; + pb.set_position(new); + current_run.lines_parsed += 1; if let Err(e) = res { return Err(anyhow!("[CORE] Runtime error : {}", e)); } else if let Ok(line) = res { @@ -166,207 +113,28 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { continue; } }; - // println!("Line: {:?}", s); - checkline(args, s, &mut status, &mut context).expect("Error while checking line"); + checkline(args, s, &mut status, &mut current_run, &started, &pb) + .expect("Error while checking line"); } else { break; } } } - // println!("{:?}", status); + current_run.time_ellapsed = started.elapsed(); + pb.finish_and_clear(); + status.print(¤t_run); + current_run.save(); Ok(status) } -/// The line encountered -// #[allow(unused)] -#[derive(Debug, Logos, PartialEq)] -#[logos(skip r"[) ]+")] -enum LineType { - /// The tex building mode : entering extended mode - #[regex("entering[_ ]extended[_ ]mode")] - Mode, - /// Info level - #[token("L3 programming layer", |lex| lex.slice().to_string())] - #[token("For additional information", |lex| lex.slice().to_string())] - #[token("Pictures in Paragraphs", |lex| lex.slice().to_string())] - #[token("2021/01/20 v 2.00 tkz-graph.sty", |lex| lex.slice().to_string())] - Info(String), - - /// Strange starting sequence - #[token("? ) [")] - #[token("? [1{")] - StartingSequence, - - /// Strange end sequence - #[token(" ){/")] - EndSequence, - - /// Converter [Loading MPS to PDF converter (version 2006.09.02).] - #[token("[Loading MPS to PDF converter")] - MPSConverter, - - /// A chapter was processed - #[regex(r"Chapitre [0-9]+", |lex| lex.slice()[9..].to_string())] - Chapter(String), - - /// Missing reference - #[token(r"LaTeX Warning: Reference")] - MissingReferencePrs, - - /// Missed reference, completed with all info - MissingReference(String), - - /// Generic Latex Warning - #[regex(r"(LaTex |Warning:){2}[^\n]+", |lex| lex.slice()[14..].to_string())] - Warning(String), - - // /// Runaway argument - // #[token("Runaway argument?")] - // RunAway, - /// PdfTex VersionThis is pdfTeX, Version 3.141592653-2.6-1.40.22 - #[token("This is pdfTeX")] - PdfTexVersion, - - /// Context, mosty for boxes - // TODO: False positives / Reparsing Context("10") )) [107] (./Ecoles/Protecteur.tex - #[regex(r"\[\d[^\]]+", |lex| lex.slice()[1..lex.slice().len()-1].to_string())] - Context(String), - - /// Overfull and underfull boxes - #[regex("(Over|Under)full")] - BoxFullness, - /// The Latex Version - #[regex("LaTeX2e")] - LaTexVersion, - /// The write18 mode, whatever that is - #[token(" restricted \\write18 enabled.")] - WriteMode, - - /// The class of document (book, article, etc) - // s if s.starts_with("Document Class") => LineType::DocumentClass(s.to_string()), - #[token("Document Class:")] - DocumentClass, - - /// Signals that the parser entered a document - //TODO: Fix name collection - // #[regex(r"(\)+ )?\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").next().unwrap_or("").to_string())] - // #[regex(r"(\)+ )?(\[\w+\])?\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").next().unwrap_or("").to_string())] - - // #[regex(r"[\) ]*\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] - // #[regex(r"[\) ]*(\[[0-9]+\] )?\(\./[A-z/-_]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] - // #[regex(r"[\) 0-9\[\]]*\(\./[A-z/-_]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] - #[regex(r"\(\./[A-z/_-]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] - EnteringDocument(String), - - /// Info on the loading of a library - #[regex(r"Library \(\w+\)[^\n]+", |lex| split_by(":",&lex.slice()[8..]))] - Library((String, String)), - - /// The warning generated by a package - #[regex(r"Package [A-z_-]+ Warning: [^\n]+", |lex| split_by(":",lex.slice()))] - PackageWarning((String, String)), - - /// The warning context generated by a package - #[regex(r"\([A-z_-]+\) [^\n]+", |lex| split_by(")",&lex.slice()[1..]))] - PackageContextWarning((String, String)), - - /// Loading (Load something) - #[regex(r"(\)+ )?\(/usr[^)]*", |lex| lex.slice().split("/").last().unwrap_or("").to_string())] - Loading(String), - - /// Unknown type - Unknown(String), - - /// Building blocks, signals pages being rendered ? - #[token(" []")] - #[token("[]")] - PageGen, - - /// Output - // #[regex(r"Output written on", |lex| lex.slice()[18..].to_string())] - #[token(r"Output written on")] - Output, - - /// Log - // #[regex("Transcript written on [^\n]+", |lex| lex.slice()[18..].to_string())] - #[token("Transcript written on")] - Log, - - // /// Barely parsable data - // #[regex(r"\)+")] - // Garbage, - /// Empty line, to discard - Empty, - - /// Geometry package - #[token("*geometry* driver: auto-detecting")] - GeometryResolution, - - /// Geometry found - #[token("*geometry* detected driver: pdftex")] - GeometryFound, - - /// Transcript - #[token("(see the transcript file for additional information)")] - Transcript, -} - -impl LineType { - /// Decides all verbose levels, for output - fn vlvl(&self) -> VerboseLevel { - match &self { - LineType::Mode => VerboseLevel::Misc, - LineType::Info(_) => VerboseLevel::Misc, - LineType::StartingSequence => VerboseLevel::Misc, - LineType::EndSequence => VerboseLevel::Misc, - LineType::MPSConverter => VerboseLevel::Misc, - LineType::Chapter(_) => VerboseLevel::Context, - LineType::MissingReferencePrs => VerboseLevel::Warning, - LineType::Warning(_) => VerboseLevel::Warning, - LineType::MissingReference(_) => VerboseLevel::Warning, - LineType::PdfTexVersion => VerboseLevel::Misc, - LineType::Context(_) => VerboseLevel::Misc, - LineType::BoxFullness => VerboseLevel::Box, - LineType::LaTexVersion => VerboseLevel::Misc, - LineType::WriteMode => VerboseLevel::Misc, - LineType::DocumentClass => VerboseLevel::Misc, - LineType::EnteringDocument(_) => VerboseLevel::Info, - LineType::Library(_) => VerboseLevel::Context, - LineType::PackageWarning(_) => VerboseLevel::Box, - LineType::PackageContextWarning(_) => VerboseLevel::Warning, - LineType::Loading(_) => VerboseLevel::Context, - LineType::Unknown(_) => VerboseLevel::Uncatchable, - LineType::PageGen => VerboseLevel::None, - LineType::Output => VerboseLevel::Misc, - LineType::Log => VerboseLevel::Misc, - LineType::Empty => VerboseLevel::None, - LineType::GeometryResolution => VerboseLevel::Misc, - LineType::GeometryFound => VerboseLevel::Misc, - LineType::Transcript => VerboseLevel::Misc, - } - } -} - -/// Tries to split the token and return two string -fn split_by(delim: &str, text: &str) -> Option<(String, String)> { - let mut slice = text.split(delim); - // println!("{:?}", text); - if let Some(one) = slice.next() { - // println!("{:?}", one); - if let Some(two) = slice.next() { - // println!("{} {:?}", one, slice.next()); - return Some((one.to_string(), two.trim_start().to_string())); - } - } - None -} - /// Check each line and changes the status and/or display according warnings fn checkline( args: &MakeConfig, orginal_line: &str, status: &mut Status, - context: &mut [String], + run: &mut BuildInfo, + started: &Instant, + pb: &ProgressBar, ) -> Result<()> { // Match the line to guess what is happening let mut lex = LineType::lexer(orginal_line); @@ -376,11 +144,6 @@ fn checkline( _ => LineType::Unknown(orginal_line.to_string()), }; - // Early exit if the verbosity level doenst match - if parsed_line.vlvl() < args.verbosity { - return Ok(()); - } - // Post processing for some lines that can't be properly handlend by the lexer let final_line = match parsed_line { // LineType::Mode => todo!(), @@ -388,19 +151,32 @@ fn checkline( // LineType::StartingSequence => todo!(), // LineType::EndSequence => todo!(), // LineType::MPSConverter => todo!(), - // LineType::Chapter(_) => todo!(), + ref a @ LineType::Chapter(ref chap) => { + run.add_lap(started, chap); + a + } LineType::MissingReferencePrs => &LineType::MissingReference(orginal_line.to_string()), // LineType::Warning(_) => todo!(), - // LineType::PdfTexVersion => todo!(), + LineType::PdfTexVersion(_) => &LineType::PdfTexVersion(orginal_line.to_string()), // LineType::Context(_) => todo!(), - // TODO: Add - // LineType::BoxFullness => todo!(), + LineType::BoxFullnessPrs => &LineType::BoxFullness(orginal_line.to_string()), // LineType::LaTexVersion => todo!(), // LineType::WriteMode => todo!(), // LineType::DocumentClass => todo!(), // LineType::EnteringDocument(_) => todo!(), // LineType::Library(_) => todo!(), - // LineType::PackageWarning(_) => todo!(), + LineType::PackageWarningPrs((ref pkg, ref wrn)) => match pkg.as_str() { + "Package tabularray Warning" => { + &LineType::LowPackageWarning(("tabulararray".to_string(), wrn.clone())) + } + _ => { + let mut name = pkg.split(" "); + &LineType::PackageWarning(( + name.nth(1).unwrap_or("unknown").to_string(), + wrn.clone(), + )) + } + }, // LineType::PackageContextWarning(_) => todo!(), // LineType::Loading(_) => todo!(), // LineType::Unknown(_) => todo!(), @@ -414,16 +190,68 @@ fn checkline( _ => &parsed_line, }; - match &parsed_line.vlvl() { + // Early exit if the verbosity level doenst match + if parsed_line.vlvl() < args.verbosity { + return Ok(()); + } + + match &final_line.vlvl() { VerboseLevel::None => (), - VerboseLevel::Misc => println!("[MISC] {:?}", final_line), - VerboseLevel::Box => println!("[BOXX] {:?}", final_line), - VerboseLevel::Context => println!("[CNTX] {:?}", final_line), // Context tends to be shit ? - VerboseLevel::Info => println!("[INFO] {:?}", final_line), - VerboseLevel::Warning => println!("[WARN] {:?}", final_line), - VerboseLevel::Error => println!("[CRIT] {:?}", final_line), - VerboseLevel::Critical => println!("[CRIT] {:?}", final_line), - VerboseLevel::Uncatchable => println!("[UKNW] {}", orginal_line), + VerboseLevel::Misc => pb.println(format!( + "{} {:?}", + *TXT_MISC, + COL_NORMAL.apply_to(final_line) + )), + VerboseLevel::Box => pb.println(format!( + "{} {:?}", + *TXT_BOXX, + COL_NORMAL.apply_to(final_line) + )), + VerboseLevel::Context => pb.println(format!( + "{} {:?}", + *TXT_CNTX, + COL_NORMAL.apply_to(final_line) + )), // Context tends to be shit ))? + VerboseLevel::Info => { + status.info += 1; + pb.println(format!( + "{} {:?}", + *TXT_INFO, + COL_NORMAL.apply_to(final_line) + )) + } + VerboseLevel::Warning => { + status.warnings += 1; + pb.println(format!( + "{} {:?}", + *TXT_WARN, + COL_NORMAL.apply_to(final_line) + )) + } + VerboseLevel::Error => { + status.errors += 1; + pb.println(format!( + "{} {:?}", + *TXT_ERRR, + COL_NORMAL.apply_to(final_line) + )) + } + VerboseLevel::Critical => { + status.errors += 1; + pb.println(format!( + "{} {:?}", + *TXT_CRIT, + COL_NORMAL.apply_to(final_line) + )) + } + VerboseLevel::Uncatchable => { + // status.errors += 1; + pb.println(format!( + "{} {}", + *TXT_UKNW, + COL_NORMAL.apply_to(orginal_line) + )) + } } Ok(()) } diff --git a/src/makepdf_structs.rs b/src/makepdf_structs.rs new file mode 100644 index 0000000..382f67c --- /dev/null +++ b/src/makepdf_structs.rs @@ -0,0 +1,446 @@ +//! Holds all the structs for the makepdf module + +use logos::Logos; +use serde::{Deserialize, Serialize}; +use std::{ + io::{Read, Write}, + sync::LazyLock, + time::{Duration, Instant}, +}; + +use console::Style; +use phf::phf_map; + +use crate::makepdf::MakeConfig; + +/// Color Scheme for the NORMAL level +pub(crate) static COL_NORMAL: LazyLock