Weirdness

This commit is contained in:
Dandaedre 2024-06-21 15:42:45 +02:00
parent 47c7735a67
commit 2cc32f41ce
1 changed files with 82 additions and 26 deletions

View File

@ -1,7 +1,13 @@
//! Make a pdf using pdflatex
use anyhow::Result;
// use std::fmt::Display;
use std::str;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
use anyhow::{anyhow, Result};
use clap::Args;
use std::process::Stdio;
use crate::Config;
@ -22,31 +28,63 @@ pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> {
Ok(())
}
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
#[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<String>,
}
use std::process::Stdio;
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<(), Box<dyn std::error::Error>> {
async fn launch_subprocess(args: &MakeConfig) -> Result<Status> {
let mut cmd = Command::new("pdflatex");
// cmd.arg("-8bit");
let mut status = Status::zero();
let mut context = Vec::new();
cmd.arg(&args.filename);
cmd.env("max_print_line", "1000");
// 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 mut child = cmd.spawn().expect("Failed to spawn command");
let stdout = child
.stdout
.take()
.expect("child did not have a handle to stdout");
.expect("Child did not have a handle to stdout");
let mut reader = BufReader::new(stdout).split(0xA);
@ -56,37 +94,55 @@ async fn launch_subprocess(args: &MakeConfig) -> Result<(), Box<dyn std::error::
let status = child
.wait()
.await
.expect("child process encountered an error");
.expect("Child process encountered an error");
println!("child status was: {}", status);
println!("Child status was: {}", status);
});
// while let Some(line) = reader.next_line().await? {
// // let vec: Vec<u8> = line.into();
// println!("Line: {:?}", line);
// }
loop {
let res = reader.next_segment().await;
if let Err(e) = res {
println!("{}", e);
break;
return Err(anyhow!("[CORE] Runtime error : {}", e));
} 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);
// status.errors += 1;
status.add_error(format!("[CORE] Invalid UTF-8 sequence: {}", e));
continue;
}
};
println!("Line: {:?}", s);
// println!("Line: {:?}", s);
checkline(s, &mut status, &mut context).expect("Error while checking line");
} else {
break;
}
}
// let vec: Vec<u8> = line.into();
}
Ok(())
// println!("{:?}", status);
Ok(status)
}
// /// The line encountered
// enum LineType {
// /// The tex building mode
// Mode,
// Info,
// BoxFullness,
// /// Unknown type
// Unknown(String),
// }
// /// Check each line and changes the status and/or display according warnings
// fn checkline(s: &str, status: &mut Status, context: &mut Vec<String>) -> Result<()> {
// // Match the line to guess what is happening
// // match s {
// // s if s.starts_with("entering_extended_mode") => LineType::Mode,
// // _ => LineType::Unknown(s.to_string()),
// // }
// // status.warnings += 1;
// // if status.warnings % 3 == 0 {
// // println!("{}", s);
// // }
// Ok(())
// }