From 69439f56f46460998db48841d3c8794dd7c733ee Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sun, 23 Jun 2024 19:41:31 +0200 Subject: [PATCH] Rought and working builder --- src/main.rs | 2 + src/makepdf.rs | 73 ++++++++++-------- src/makepdf_functions.rs | 160 +++++++++++++++++++++++++++++++++++++++ src/makepdf_misc.rs | 104 +++++++++++++++++++++++++ src/makepdf_structs.rs | 46 ++++------- 5 files changed, 321 insertions(+), 64 deletions(-) create mode 100644 src/makepdf_functions.rs create mode 100644 src/makepdf_misc.rs diff --git a/src/main.rs b/src/main.rs index 8871d9a..60e4e89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ #![warn(missing_docs)] #![warn(clippy::missing_docs_in_private_items)] mod makepdf; +mod makepdf_functions; +mod makepdf_misc; mod makepdf_structs; mod stat_algo; mod stat_io; diff --git a/src/makepdf.rs b/src/makepdf.rs index 5be971b..49edf44 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -1,9 +1,10 @@ //! Make a pdf using pdflatex -// use std::str::pattern::Pattern; use std::time::{Duration, Instant}; +// use std::str::pattern::Pattern; use std::{cmp::min, fmt::Write}; +use dynfmt::FormatArgs; use indicatif::{ProgressBar, ProgressState, ProgressStyle}; use logos::Logos; use std::str; @@ -14,23 +15,33 @@ use anyhow::{anyhow, Result}; use clap::Args; use std::process::Stdio; +use crate::makepdf_functions::{make_clean, make_file, make_glossaries, make_list}; +use crate::makepdf_misc::{BLOCK_PROGRESSION, HELP_BLOCK_SELECTION, HELP_VERBOSITY_LEVEL}; use crate::makepdf_structs::{ - BuildInfo, LineType, Status, VerboseLevel, COL_NORMAL, MAKE_VERBOSITY_LEVEL, TXT_BOXX, - TXT_CNTX, TXT_CRIT, TXT_ERRR, TXT_INFO, TXT_MISC, TXT_UKNW, TXT_WARN, + BuildInfo, LineType, MakeTargets, 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)] -#[command(after_help = format!("{} {}","VERBOSITY:",MAKE_VERBOSITY_LEVEL.as_str()))] +// #[command(after_)] /// The configuration struct for the PdfMaker pub(crate) struct MakeConfig { /// The filename to compile - pub(crate) target: String, + pub(crate) filename: String, + + /// Special target + #[arg(short, long)] + pub(crate) target: Option, /// The verbosity level - #[arg(short, long, default_value = "warning")] + #[arg(short, long, default_value = "warning", help = format!("The verbosity level. {}",HELP_VERBOSITY_LEVEL.as_str()))] pub(crate) verbosity: VerboseLevel, + /// The progress bar style + #[arg(short, long, default_value = "class", help = format!("The progress bar style. {}",HELP_BLOCK_SELECTION.as_str()))] + pub(crate) pbar: String, + /// switch to non interactive mode #[arg(short = 'N', long, default_value_t = false)] pub(crate) non_interactive: bool, @@ -45,39 +56,37 @@ pub(crate) struct MakeConfig { } /// Make a pdf spawning a subprocess -pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> { - let _ = config; +pub(crate) fn make_pdf(args: &MakeConfig, _config: &Config) -> Result<()> { + let Ok(Some(_)) = BLOCK_PROGRESSION.get_key(&args.pbar) else { + panic!( + "Progressbar {} doesn't exists. Use one of the following {:?}", + args.pbar, HELP_BLOCK_SELECTION + ); + }; + + // Make the target to build + let file = make_file(args); // Identify special targets: Clean, Glossaries, All, List etc. - match args.target.as_str() { - "all" => make_all(args), - "clean" => make_clean(args), - "list" => make_list(args), - "glossaries" => make_glossaries(args), - _ => (), + match args.target { + Some(MakeTargets::All) => make_all(args, file), + Some(MakeTargets::List) => make_list(args), + Some(MakeTargets::Glossaries) => make_glossaries(args), + Some(MakeTargets::Clean) => make_clean(args), + None => { + launch_subprocess(args, file).unwrap(); + } } - launch_subprocess(args).unwrap(); Ok(()) } -fn make_list(args: &MakeConfig) { - todo!() -} - -fn make_glossaries(args: &MakeConfig) { - todo!() -} - -fn make_all(args: &MakeConfig) { - todo!() -} - -fn make_clean(args: &MakeConfig) { - todo!() +/// Make all targets +fn make_all(args: &MakeConfig, rebuild_target: String) { + launch_subprocess(args, rebuild_target).unwrap(); } #[tokio::main] /// Pdfreading and curating -async fn launch_subprocess(args: &MakeConfig) -> Result { +async fn launch_subprocess(args: &MakeConfig, rebuild_target: String) -> Result { let mut cmd = Command::new("pdflatex"); let mut status = Status::zero(); @@ -91,7 +100,7 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { if args.non_interactive { cmd.arg("--interaction=nonstopmode"); } - cmd.arg(&args.target); + cmd.arg(&rebuild_target); cmd.env("max_print_line", "3000"); // Specify that we want the command's standard output piped back to us. cmd.stdout(Stdio::piped()); @@ -128,7 +137,7 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { .with_key("per_sec",|state:&ProgressState,w:&mut dyn Write|{ write!(w,"{:.1}/lps",state.per_sec()).unwrap() }) - .progress_chars("#>-"), + .progress_chars(&BLOCK_PROGRESSION[&args.pbar]), ); pb.enable_steady_tick(Duration::from_millis(100)); diff --git a/src/makepdf_functions.rs b/src/makepdf_functions.rs new file mode 100644 index 0000000..2082c1b --- /dev/null +++ b/src/makepdf_functions.rs @@ -0,0 +1,160 @@ +//! Helper functions + +use std::{ + collections::HashMap, + fs::File, + io::{Read, Write}, + path::{Path, PathBuf}, + sync::LazyLock, +}; + +use anyhow::Result; +use regex::Regex; + +use crate::makepdf::MakeConfig; + +/// Regex detecting wether an input line is present +static INPUT_REGEX: LazyLock = + LazyLock::new(|| Regex::new(r#" *\\input\{"?([A-z/-_]+)\}"?"#).unwrap()); + +/// Make the (new/purged) target file and return it's name +pub(crate) fn make_file(args: &MakeConfig) -> String { + let path = target_name(&args.filename); + let m = make_ignore_targets(args); + let mut f = File::open(&args.filename).expect("The file doesn't exist"); + let mut of = File::create(&path).unwrap(); + let mut contents = String::new(); + + // Warn about wrong keys + // clear_keys(&mut args); + + f.read_to_string(&mut contents).unwrap(); + for line in contents.lines() { + if let Some(c) = INPUT_REGEX.captures(line) { + // Check wether it should be added + if add_checker(&c[1], &m, args) { + println!("[PREP] Including {}", &c[1]); + of.write_all(line.as_bytes()).unwrap(); + of.write_all("\n".as_bytes()).unwrap(); + } else { + println!("[PREP] Excluding {}", &c[1]); + } + } else { + of.write_all(line.as_bytes()).unwrap(); + of.write_all("\n".as_bytes()).unwrap(); + } + } + format!("{:?}", &path) +} + +// fn clear_keys(args: &mut &MakeConfig) -> _ { +// for k in args.only{ +// if +// } +// } + +/// Algorithm to check wether to add or not the entry +fn add_checker(check: &str, dict_entries: &HashMap, args: &MakeConfig) -> bool { + if args.only.is_empty() && args.no.is_empty() { + return true; + } + for k in args.only.iter() { + if dict_entries.contains_key(k) && dict_entries[k] == check { + return true; + } + } + if !args.no.is_empty() { + for k in args.no.iter() { + if dict_entries.contains_key(k) && dict_entries[k] == check { + return false; + } + } + return true; + } + false +} +/// List all chapters targetable by ignore and only +pub(crate) fn make_list(args: &MakeConfig) { + let m = make_ignore_targets(args); + println!("The ignore targets are the following:"); + println!("{:#?}", m); +} + +/// Make the dict of targets +fn make_ignore_targets(args: &MakeConfig) -> HashMap { + let mut map: HashMap = HashMap::new(); + let mut f = File::open(&args.filename).unwrap(); + let mut contents = String::new(); + f.read_to_string(&mut contents).unwrap(); + for line in contents.lines() { + if let Some(c) = INPUT_REGEX.captures(line) { + // println!("[TRGT] {:?}", &c[1]); + simplify_add(c[1].to_string(), &mut map).unwrap(); + } + } + + map +} + +/// Create a target name, by appending _build to it +pub(crate) fn target_name(target: &str) -> PathBuf { + let target = Path::new(target); + let mut target_oss = target.file_stem().unwrap().to_owned(); + target_oss.push("_build.tex"); + target_oss.into() +} + +/// Try to add a simplified entry to a list +pub(crate) fn simplify_add( + entry: String, + dict_entries: &mut HashMap, +) -> Result<()> { + // If the value was already inside, no need to add it + if dict_entries.values().any(|e| e == &entry) { + return Ok(()); + } + // Strategy one: use the last part after / + if let Some(e) = entry.split("/").nth(1) { + let e = e.to_lowercase(); + if e.len() >= 3 { + for i in 3..e.len() { + if !dict_entries.contains_key(&e[..i]) { + dict_entries.insert(e[..i].to_owned(), entry); + return Ok(()); + } + } + } + { + if !dict_entries.contains_key(&e) { + dict_entries.insert(e.to_owned(), entry); + return Ok(()); + } + } + } + + // Strategy two use first part before / and add _ to last + let mut spl = entry.split("/"); + if let (Some(p), Some(s)) = (spl.next(), spl.next()) { + let (p, s) = (p.to_lowercase(), s.to_lowercase()); + let key = format!("{:1}_{}", p, s); + if !dict_entries.contains_key(&key) { + dict_entries.insert(key.to_owned(), entry); + return Ok(()); + } + } + + // Last ditch strategy + let e = entry.replace("/", "_").to_lowercase(); + dict_entries.insert(e, entry); + Ok(()) +} + +/// Make the glossaries +pub(crate) fn make_glossaries(args: &MakeConfig) { + todo!() +} + +/// Make the working directory clean, by eliminating all temporary files +pub(crate) fn make_clean(args: &MakeConfig) { + todo!() +} diff --git a/src/makepdf_misc.rs b/src/makepdf_misc.rs new file mode 100644 index 0000000..bdc27aa --- /dev/null +++ b/src/makepdf_misc.rs @@ -0,0 +1,104 @@ +//! Printing and misc utilities + +use crate::makepdf_structs::{MakeTargets, VerboseLevel}; +use phf::phf_map; +use std::{collections::HashMap, sync::LazyLock}; + +/// Convert str to verbosity level +static STR_TO_VERBOSITY: 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, +}; + +/// Convert str to build target +static STR_TO_BUILD_TARGET: phf::Map<&'static str, MakeTargets> = phf_map! { + "all" => MakeTargets::All, + "list" => MakeTargets::List, + "glossaries" => MakeTargets::Glossaries, + "clean" => MakeTargets::Clean, +}; + +/// List all blocktype +pub(crate) static HELP_BLOCK_SELECTION: LazyLock = LazyLock::new(|| { + let mut targets = BLOCK_PROGRESSION.keys().collect::>(); + targets.sort(); + format!("{:?}", targets) +}); + +/// List all targets +pub(crate) static HELP_VERBOSITY_LEVEL: LazyLock = LazyLock::new(|| { + let mut targets = STR_TO_VERBOSITY.values().collect::>(); + targets.sort(); + format!("{:?}", targets) +}); + +/// List all targets +pub(crate) static HELP_BUILD_TARGET_LIST: LazyLock = LazyLock::new(|| { + let targets = STR_TO_BUILD_TARGET.values().collect::>(); + format!("{:?}", targets) +}); + +impl From<&str> for VerboseLevel { + fn from(value: &str) -> Self { + if let Some(target) = STR_TO_VERBOSITY.get(&value.to_lowercase()).cloned() { + target + } else { + panic!( + "Invalid debug level {}. The debug level available are (from highest verbosity to lowest): {:?}", + value, HELP_VERBOSITY_LEVEL + ) + } + } +} + +impl From<&str> for MakeTargets { + fn from(value: &str) -> Self { + if let Some(target) = STR_TO_BUILD_TARGET.get(&value.to_lowercase()).cloned() { + target + } else { + panic!( + "Invalid build target {}. The build target available are {:?}", + value, HELP_BUILD_TARGET_LIST + ) + } + } +} + +/// All progress bar styles +pub(crate) static BLOCK_PROGRESSION: LazyLock> = LazyLock::new(|| { + let mut m = HashMap::new(); + m.insert("class".to_string(), "#>-".to_string()); + m.insert("cargo".to_string(), "#> ".to_string()); + m.insert("rough".to_string(), "█ ".to_string()); + m.insert("fine".to_string(), "█▉▊▋▌▍▎▏ ".to_string()); + m.insert("vert".to_string(), "█▇▆▅▄▃▂▁ ".to_string()); + m.insert("fade".to_string(), "█▓▒░ ".to_string()); + m.insert("block".to_string(), "█▛▌▖ ".to_string()); + m.insert("curse".to_string(), "▓▙▚▐▔ ▛▘▖▄▟▞▂█".to_string()); + m.insert("load".to_string(), " ⱠØ₳Đł₦₲ ".to_string()); + m.insert("cool".to_string(), "▰▱".to_string()); + m.insert("neo".to_string(), "▰═".to_string()); + m +}); +// Spinners +// ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ ▇ ▆ ▅ ▄ ▃ ▁ +// ▉▊▋▌▍▎▏▎▍▌▋▊▉ +// ▖ ▘ ▝ ▗ +// ┤ ┘ ┴ └ ├ ┌ ┬ ┐ +// ◢ ◣ ◤ ◥ +// ◡◡ ⊙⊙ ◠◠ +// ⣾⣽⣻⢿⡿⣟⣯⣷ ⠁⠂⠄⡀⢀⠠⠐⠈ +// .tick_strings(&[ +// "▹▹▹▹▹", +// "▸▹▹▹▹", +// "▹▸▹▹▹", +// "▹▹▸▹▹", +// "▹▹▹▸▹", +// "▹▹▹▹▸", +// "▪▪▪▪▪", +// ]), diff --git a/src/makepdf_structs.rs b/src/makepdf_structs.rs index 8d206b5..2cd6d4d 100644 --- a/src/makepdf_structs.rs +++ b/src/makepdf_structs.rs @@ -9,7 +9,6 @@ use std::{ }; use console::Style; -use phf::phf_map; use crate::makepdf::MakeConfig; @@ -82,34 +81,17 @@ pub enum VerboseLevel { 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, -}; - -/// List all targets -pub(crate) static MAKE_VERBOSITY_LEVEL: LazyLock = LazyLock::new(|| { - let mut targets = STR_TO_TARGET.values().collect::>(); - targets.sort(); - format!("{:?}", targets) -}); - -impl From<&str> for VerboseLevel { - fn from(value: &str) -> Self { - if let Some(target) = STR_TO_TARGET.get(&value.to_lowercase()).cloned() { - target - } else { - panic!( - "Invalid debug level {}. The debug level available are (from highest verbosity to lowest): {:?}", - value, MAKE_VERBOSITY_LEVEL - ) - } - } +/// List of all alternative targets +#[derive(Debug, Clone)] +pub(crate) enum MakeTargets { + /// Make all targets + All, + /// Make a list of all optionals + List, + /// Make glossaries + Glossaries, + /// Make the working directory clean + Clean, } /// Keeps tracks of the stats of the last(s) run to offer accurate estimation @@ -133,7 +115,7 @@ impl BuildInfo { /// Create a new empty run pub fn new(args: &MakeConfig) -> Self { Self { - filename: args.target.clone(), + filename: args.filename.clone(), // options: args.update_from_arg_matches, options: Vec::new(), lines_parsed: 0, @@ -283,7 +265,7 @@ pub(crate) enum LineType { /// The warning context generated by a package #[regex(r"\([A-z_-]+\) [^\n]+", |lex| split_by(")",&lex.slice()[1..]))] - PackageContextWarning((String, String)), + PackageContext((String, String)), /// Loading (Load something) #[regex(r"(\)+ )?\(/usr[^)]*", |lex| lex.slice().split("/").last().unwrap_or("").to_string())] @@ -345,7 +327,7 @@ impl LineType { LineType::DocumentClass => VerboseLevel::Misc, LineType::EnteringDocument(_) => VerboseLevel::Info, LineType::Library(_) => VerboseLevel::Context, - LineType::PackageContextWarning(_) => VerboseLevel::Warning, + LineType::PackageContext(_) => VerboseLevel::Warning, LineType::Loading(_) => VerboseLevel::Context, LineType::Unknown(_) => VerboseLevel::Uncatchable, LineType::PageGen => VerboseLevel::None, -- 2.45.1