tikz-gallery-generator

Custum build of stapix for tikz.pablopie.xyz

Commit
f6ea799c636d7efc47209979bf67e5d27e4b0031
Parent
67ba2c9015ac86ec8d2503287d1300b48d2c570b
Author
Pablo <pablo-pie@riseup.net>
Date

Improved logging

Made it so that an entry's page name is logged with the ".html" extension at the end

Diffstats

2 files changed, 27 insertions, 17 deletions

Status Name Changes Insertions Deletions
Modified src/log.rs 2 files changed 5 4
Modified src/main.rs 2 files changed 22 13
diff --git a/src/log.rs b/src/log.rs
@@ -5,7 +5,7 @@
 //! require synchronization because they happen right before the entire
 //! application is killed.
 
-use std::{io::{self, Write}, fmt::Arguments, time::Duration};
+use std::{io::{self, Write}, fmt::{Display, Arguments}, time::Duration};
 
 const BOLD_RED:    &str = "\u{001b}[1;31m";
 const BOLD_GREEN:  &str = "\u{001b}[1;32m";
@@ -103,7 +103,7 @@ impl JobListLogger {
   }
 
   /// Adds a job to the queue and logs the fact this job has started
-  pub fn job_started(&mut self, job_name: &str) {
+  pub fn job_started<T: Display + ?Sized>(&mut self, job_name: &T) {
     self.inc_counter(job_name);
 
     let mut stdout = io::stdout();
@@ -130,10 +130,11 @@ impl JobListLogger {
     self.job_finished_impl();
   }
 
-  fn inc_counter(&mut self, job_name: &str) {
+  fn inc_counter<T: Display + ?Sized>(&mut self, job_name: &T) {
+    use std::fmt::Write;
     self.count += 1;
     self.current_job_name.clear();
-    self.current_job_name.push_str(job_name);
+    let _ = write!(&mut self.current_job_name, "{}", job_name);
   }
 
   fn job_finished_impl(&self) {
diff --git a/src/main.rs b/src/main.rs
@@ -24,6 +24,9 @@ mod gallery_entry;
 /// A wrapper for displaying the path for the thumbnail of a given path
 pub struct ThumbPath<'a>(pub &'a GalleryEntry);
 
+/// A wrapper for displaying file names with ".html" appended at the end
+pub struct HtmlFileName<'a>(pub &'a str);
+
 const FULL_BUILD_OPT: &str = "-B";
 const N_THREADS_OPT:  &str = "-j";
 const BOTH_OPTS:      &str = "-Bj";
@@ -54,8 +57,8 @@ fn main() -> ExitCode {
   };
 
   let mut full_build = false;
-  let total_threads = num_cpus::get();
-  let mut num_threads = total_threads - 1;
+  let total_cores = num_cpus::get();
+  let mut num_cores = total_cores - 1;
   while let Some(arg) = args.next() {
     let mut is_valid_arg = false;
 
@@ -77,7 +80,7 @@ fn main() -> ExitCode {
       };
 
       match val.parse() {
-        Ok(val) => num_threads = val,
+        Ok(val) => num_cores = val,
         Err(_)  => {
           errorln!("Expected a number, got {val:?}");
           log::usage(&program);
@@ -109,7 +112,7 @@ fn main() -> ExitCode {
       log::usage_config();
       return ExitCode::FAILURE;
     }
-    Ok(Ok(pics)) => if render_gallery(pics, full_build, num_threads, total_threads).is_err() {
+    Ok(Ok(pics)) => if render_gallery(pics, full_build, num_cores, total_cores).is_err() {
       return ExitCode::FAILURE;
     },
   }
@@ -121,8 +124,8 @@ fn main() -> ExitCode {
 fn render_gallery(
   pics: Vec<GalleryEntry>,
   full_build: bool,
-  num_threads: usize,
-  total_threads: usize,
+  num_cores: usize,
+  total_cores: usize,
 ) -> Result<(), ()> {
   struct Job {
     pic_id:     usize,
@@ -144,7 +147,7 @@ fn render_gallery(
 
     let mut page_path = PathBuf::from(TARGET_PATH);
     page_path.push(PAGES_PATH);
-    page_path.push(pic.file_name.clone() + ".html");
+    page_path.push(format!("{}", HtmlFileName(&pic.file_name)));
 
     if pic.alt.is_empty() {
       warnln!(
@@ -183,7 +186,7 @@ fn render_gallery(
 
   for Job { pic_id, page_path, .. } in &jobs {
     let pic = &pics[*pic_id];
-    pages_logger.job_started(&pic.file_name);
+    pages_logger.job_started(&HtmlFileName(&pic.file_name));
       render_pic_page(pic, page_path).map_err(|_| ())?;
     pages_logger.job_finished();
   }
@@ -197,15 +200,15 @@ fn render_gallery(
   infoln!("Copied image files to the target directory");
 
   // ========================================================================
-  let num_threads = cmp::min(num_threads, jobs.len());
+  let num_cores = cmp::min(num_cores, jobs.len());
   let mut thumbs_logger = JobListLogger::new(jobs.len());
 
   // NOTE: only spawn the threads if necessary
-  if num_threads > 1 {
-    infoln!("Rendering thumbnails... (using {num_threads}/{total_threads} threads)");
+  if num_cores > 1 {
+    infoln!("Rendering thumbnails... (using {num_cores}/{total_cores} cores)");
     let rendering_pool = ThreadPool::with_name(
       String::from("thumbnails renderer"),
-      num_threads,
+      num_cores,
     );
     let (sender, reciever) = mpsc::channel();
 
@@ -235,7 +238,7 @@ fn render_gallery(
       thumbs_logger.job_started_and_finished(&pic.file_name);
     }
   } else {
-    infoln!("Rendering thumbnails... (using 1/{total_threads} thread)");
+    infoln!("Rendering thumbnails... (using 1/{total_cores} core)");
     for Job { pic_id, thumb_path, .. } in &jobs {
       let pic = &pics[*pic_id];
 
@@ -596,3 +599,9 @@ impl From> for PathBuf {
     result
   }
 }
+
+impl Display for HtmlFileName<'_> {
+  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
+    write!(f, "{}.html", self.0)
+  }
+}