A rust crate i use for my projects
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Woah, more improvements

MrSnowy d46e9f12 b5b4267f

+138 -55
+42 -1
Cargo.lock
··· 3 3 version = 4 4 4 5 5 [[package]] 6 + name = "libc" 7 + version = "0.2.174" 8 + source = "registry+https://github.com/rust-lang/crates.io-index" 9 + checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" 10 + 11 + [[package]] 6 12 name = "snowy_libs" 7 - version = "0.1.2" 13 + version = "0.1.3" 14 + dependencies = [ 15 + "termsize", 16 + ] 17 + 18 + [[package]] 19 + name = "termsize" 20 + version = "0.1.9" 21 + source = "registry+https://github.com/rust-lang/crates.io-index" 22 + checksum = "6f11ff5c25c172608d5b85e2fb43ee9a6d683a7f4ab7f96ae07b3d8b590368fd" 23 + dependencies = [ 24 + "libc", 25 + "winapi", 26 + ] 27 + 28 + [[package]] 29 + name = "winapi" 30 + version = "0.3.9" 31 + source = "registry+https://github.com/rust-lang/crates.io-index" 32 + checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 33 + dependencies = [ 34 + "winapi-i686-pc-windows-gnu", 35 + "winapi-x86_64-pc-windows-gnu", 36 + ] 37 + 38 + [[package]] 39 + name = "winapi-i686-pc-windows-gnu" 40 + version = "0.4.0" 41 + source = "registry+https://github.com/rust-lang/crates.io-index" 42 + checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 43 + 44 + [[package]] 45 + name = "winapi-x86_64-pc-windows-gnu" 46 + version = "0.4.0" 47 + source = "registry+https://github.com/rust-lang/crates.io-index" 48 + checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+2 -1
Cargo.toml
··· 1 1 [package] 2 2 name = "snowy_libs" 3 - version = "0.1.2" 3 + version = "0.1.3" 4 4 edition = "2024" 5 5 6 6 [dependencies] 7 + termsize = "0.1.9"
+94 -53
src/lib.rs
··· 1 + use std::io::Write; 2 + use std::thread::JoinHandle; 1 3 use std::{ 4 + io, 2 5 sync::{ 3 6 Arc, 4 7 atomic::{AtomicU64, Ordering}, ··· 7 10 time::{Duration, Instant}, 8 11 }; 9 12 13 + /// This function creates a thread where it will print a progress bar based on the done count that you will update. 14 + /// 15 + /// Stops when done >= total 16 + /// 17 + /// Size is a relative value that is between 0-10 10 18 pub fn start_progress_bar( 11 19 done: &Arc<AtomicU64>, 12 20 total: u64, 13 21 size: u8, 14 22 title: &str, 15 - started_at: Instant, 16 23 update_time: Duration, 17 - ) { 24 + ) -> JoinHandle<()> { 25 + assert!(size <= 10, "Size must be between 0 and 10"); 18 26 let done = done.clone(); 19 27 let title = title.to_owned(); 20 - // let total = total.clone(); 21 - // let size = size.clone(); 28 + 29 + // let mut cycles = vec![]; 22 30 23 31 thread::spawn(move || { 32 + let mut string = String::with_capacity(256); 33 + let started_at = Instant::now(); 34 + 24 35 loop { 36 + let mod_size = match termsize::get() { 37 + Some(val) => val.cols - ((val.cols / 10) * (size as u16)), 38 + None => size as u16, 39 + }; 40 + 25 41 let done = done.load(Ordering::Relaxed); 26 42 27 43 // some work here 28 - let procentage_f = done as f32 / total as f32; 29 - let procentage = (procentage_f * 100f32).round(); 30 - // print!("{}") 31 - let done_count = (size as f32 * procentage_f) as u64; 44 + let percentage_f = done as f32 / total as f32; 45 + let percentage = (percentage_f * 100f32).round() as u8; 46 + let done_bars = (mod_size as f32 * percentage_f) as u16; 32 47 33 - let mut string = "\r".to_string(); 34 - string.push_str(&title); 35 - string.push_str(" [[ "); 48 + string.clear(); 49 + string.push_str(&format!("\r\x1b[2K{} [[ ", title)); 36 50 37 - if done == total { 51 + // Make the cubes green when its finished 52 + if done >= total { 38 53 string.push_str("\x1b[32m"); 39 54 } 40 55 41 - for each in 0..size { 42 - if each as u64 <= done_count { 43 - string.push('■'); 56 + // Print the progress bar 57 + string.push_str(&"■".repeat(done_bars as usize)); 58 + 59 + // Make the dots darker 60 + string.push_str("\x1b[2m"); 61 + string.push_str(&"·".repeat((mod_size - done_bars) as usize)); 62 + 63 + // Reset any styling 64 + string.push_str("\x1b[0m"); 65 + 66 + let going_for = started_at.elapsed().as_secs_f32(); 67 + 68 + if done == 0 { 69 + string.push_str(&format!(" {}/{} {}% ]] ..", done, total, percentage)); 70 + } else { 71 + let time = { 72 + if done >= total { 73 + let time = going_for.round() as u64; 74 + 75 + string.push_str(&format!(" {}/{} 100% ]] in ", total, total,)); 76 + time 77 + } else { 78 + // Guesstimate remaining time 79 + let time_per_num = going_for / done as f32; 80 + let remaining_time = (time_per_num * (total - done) as f32).ceil() as u64; 81 + 82 + string.push_str(&format!(" {}/{} {}% ]] ", done, total, percentage)); 83 + remaining_time 84 + } 85 + }; 86 + 87 + let hours = time / 3600; 88 + let minutes = (time % 3600) / 60; 89 + let seconds = time % 60; 90 + 91 + if hours > 0 { 92 + string.push_str(&format!("{hours}h {minutes}m {seconds}s")); 93 + } else if minutes > 0 { 94 + string.push_str(&format!("{minutes}m {seconds}s")); 44 95 } else { 45 - string.push('·'); 96 + string.push_str(&format!("{seconds}s")); 46 97 } 47 98 } 48 99 49 - if done == total { 50 - string.push_str("\x1b[0m"); 51 - } 52 - 53 - // Guestimate remaining time 54 - let going_for = started_at.elapsed().as_secs_f32(); 55 - let time_per_num = (done as f32 / going_for) / 100f32; 56 - let remaining_time = (time_per_num * (total - done) as f32).ceil() as u64; 57 - 58 - if done == total { 59 - string.push_str(&format!( 60 - " {}/{} {}% ]] in {}s! \n", 61 - done, 62 - total, 63 - procentage, 64 - going_for.round() 65 - )); 100 + if done >= total { 101 + string.push_str("!\n"); 66 102 } else { 67 - string.push_str(&format!( 68 - " {}/{} {}% ]] {}s ", 69 - done, total, procentage, remaining_time 70 - )); 103 + string.push(' '); 71 104 } 72 105 73 106 print!("{string}"); 107 + let _ = io::stdout().flush(); // Try to flush 108 + 109 + // cycles.push(started_at.elapsed().as_micros()); 74 110 75 111 if done >= total { 76 112 break; 77 113 } 78 114 thread::sleep(update_time); 79 115 } 80 - }); 116 + 117 + // let mut avg = 0; 118 + // for x in &cycles { 119 + // avg += x; 120 + // } 121 + 122 + // avg /= cycles.len() as u128; 123 + // println!("Took on average {}μs ", avg); 124 + }) 81 125 } 82 126 83 127 #[cfg(test)] ··· 91 135 92 136 #[test] 93 137 fn test_progress() { 94 - let now = Instant::now(); 95 - 96 138 let value = Arc::new(AtomicU64::from(0)); 97 139 98 - start_progress_bar( 99 - &value, 100 - 1811, 101 - 50, 102 - "Being silly", 103 - now, 104 - Duration::from_millis(30), 105 - ); 140 + let thread = start_progress_bar(&value, 511, 6, "Writing", Duration::from_millis(100)); 141 + thread::spawn(move || { 142 + thread::sleep(Duration::from_secs(1)); 106 143 107 - for _ in 0..=12811 { 108 - value.fetch_add(1, Ordering::Relaxed); 144 + for _ in 0..511 { 145 + value.fetch_add(1, Ordering::Relaxed); 109 146 110 - thread::sleep(Duration::from_millis(100)); 111 - } 112 - // assert_eq!(result, 4); 147 + thread::sleep(Duration::from_millis(100)); 148 + } 149 + }); 150 + 151 + thread.join().unwrap(); 152 + 153 + println!("finished!"); 113 154 } 114 155 }