Jelajahi Sumber

added a progress bar using indicatif

Hvrnbi 1 bulan lalu
induk
melakukan
9f5b84b957
3 mengubah file dengan 118 tambahan dan 3 penghapusan
  1. 108 1
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 8 1
      src/lib.rs

+ 108 - 1
Cargo.lock

@@ -278,6 +278,18 @@ version = "1.0.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
 
+[[package]]
+name = "console"
+version = "0.16.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fe5f465a4f6fee88fad41b85d990f84c835335e85b5d9e6e63e0d06d28cba7c"
+dependencies = [
+ "encode_unicode",
+ "libc",
+ "unicode-width",
+ "windows-sys",
+]
+
 [[package]]
 name = "crc32fast"
 version = "1.5.0"
@@ -334,6 +346,12 @@ version = "1.16.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
 
+[[package]]
+name = "encode_unicode"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
+
 [[package]]
 name = "equator"
 version = "0.4.2"
@@ -406,6 +424,30 @@ dependencies = [
  "miniz_oxide",
 ]
 
+[[package]]
+name = "futures-core"
+version = "0.3.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e"
+
+[[package]]
+name = "futures-task"
+version = "0.3.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd"
+
+[[package]]
+name = "futures-util"
+version = "0.3.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc"
+dependencies = [
+ "futures-core",
+ "futures-task",
+ "pin-project-lite",
+ "slab",
+]
+
 [[package]]
 name = "getrandom"
 version = "0.3.4"
@@ -485,6 +527,19 @@ version = "1.12.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "89194689a993ab15268672e99e7b0e19da2da3268ac682e8f02d29d4d1434cd7"
 
+[[package]]
+name = "indicatif"
+version = "0.18.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9433806cd6b4ec1aba79c021c7e4c58fb4c3b9977c085062e611ac929998fb0c"
+dependencies = [
+ "console",
+ "portable-atomic",
+ "unicode-width",
+ "unit-prefix",
+ "web-time",
+]
+
 [[package]]
 name = "interpolate_name"
 version = "0.2.4"
@@ -521,6 +576,17 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "js-sys"
+version = "0.3.103"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102"
+dependencies = [
+ "cfg-if",
+ "futures-util",
+ "wasm-bindgen",
+]
+
 [[package]]
 name = "lebe"
 version = "0.5.3"
@@ -688,12 +754,13 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
 
 [[package]]
 name = "p2sa"
-version = "0.1.1"
+version = "0.1.2"
 dependencies = [
  "clap",
  "edge-detection",
  "fastrand",
  "image",
+ "indicatif",
  "svg",
 ]
 
@@ -709,6 +776,12 @@ version = "0.1.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec"
 
+[[package]]
+name = "pin-project-lite"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
+
 [[package]]
 name = "png"
 version = "0.18.1"
@@ -722,6 +795,12 @@ dependencies = [
  "miniz_oxide",
 ]
 
+[[package]]
+name = "portable-atomic"
+version = "1.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
+
 [[package]]
 name = "ppv-lite86"
 version = "0.2.21"
@@ -927,6 +1006,12 @@ dependencies = [
  "quote",
 ]
 
+[[package]]
+name = "slab"
+version = "0.4.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
+
 [[package]]
 name = "smallvec"
 version = "1.15.2"
@@ -1002,6 +1087,18 @@ version = "1.0.24"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
 
+[[package]]
+name = "unicode-width"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
+
+[[package]]
+name = "unit-prefix"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "81e544489bf3d8ef66c953931f56617f423cd4b5494be343d9b9d3dda037b9a3"
+
 [[package]]
 name = "utf8parse"
 version = "0.2.2"
@@ -1073,6 +1170,16 @@ dependencies = [
  "unicode-ident",
 ]
 
+[[package]]
+name = "web-time"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
+dependencies = [
+ "js-sys",
+ "wasm-bindgen",
+]
+
 [[package]]
 name = "weezl"
 version = "0.1.12"

+ 2 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "p2sa"
-version = "0.1.1"
+version = "0.1.2"
 edition = "2024"
 description = "A Rust library and a CLI tool to transform your pictures into String Art 🧵"
 repository = "https://github.com/Hvrnbi/p2sa"
@@ -15,4 +15,5 @@ clap = { version = "4.6.1", features = ["derive"] }
 edge-detection = "0.3.0"
 fastrand = "2.4.1"
 image = "0.25.10"
+indicatif = "0.18.6"
 svg = "0.18.0"

+ 8 - 1
src/lib.rs

@@ -1,5 +1,6 @@
 use image::{GrayImage, Luma, imageops::FilterType::Nearest};
 use svg::{Document, node::element::Path, node::element::path::Data, node::element::Rectangle};
+use indicatif::ProgressBar;
 
 /** Import a picture, resize it, and convert it to Greyscale */
 fn import_picture(path: &String, new_size: [u32; 2], edge_detection: bool) -> GrayImage {
@@ -255,6 +256,8 @@ pub fn p2sa(src_path: String, output_path: String, new_size: [u32; 2], nails_cou
         is_svg = false;
     }
 
+    let bar = ProgressBar::new(100);
+
     while err_rising_cnt < 3 {
         let best_nail_and_err_red: ([u32; 2], f32) = find_best_line(&src_img, &new_img, start, nails.clone());
         let best_nail: [u32; 2] = best_nail_and_err_red.0;
@@ -263,7 +266,9 @@ pub fn p2sa(src_path: String, output_path: String, new_size: [u32; 2], nails_cou
             first_err_red = best_nail_and_err_red.1;
         }
         
-        println!("Progress estimation : {} %", (best_nail_and_err_red.1 - first_err_red) * 100. / - first_err_red);
+        if (best_nail_and_err_red.1 - first_err_red) * 100. / - first_err_red > bar.position() as f32 && (best_nail_and_err_red.1 - first_err_red) * 100. / - first_err_red < 100. {
+            bar.inc(1);
+        }
 
         if best_nail_and_err_red.1 >= 0. {
             err_rising_cnt += 1;
@@ -281,6 +286,8 @@ pub fn p2sa(src_path: String, output_path: String, new_size: [u32; 2], nails_cou
         lines_cnt += 1;
     }
 
+    bar.finish();
+
     svg_data = svg_data.close();
 
     if is_svg {