A rust crate i use for my projects
0
fork

Configure Feed

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

Brr init

MrSnowy 67567b28

+123
+1
.gitignore
··· 1 + /target
+7
Cargo.lock
··· 1 + # This file is automatically @generated by Cargo. 2 + # It is not intended for manual editing. 3 + version = 4 4 + 5 + [[package]] 6 + name = "snowy_libs" 7 + version = "0.1.0"
+6
Cargo.toml
··· 1 + [package] 2 + name = "snowy_libs" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies]
+109
src/lib.rs
··· 1 + use std::{ 2 + sync::{ 3 + Arc, 4 + atomic::{AtomicU64, Ordering}, 5 + }, 6 + thread, 7 + time::{Duration, Instant}, 8 + }; 9 + 10 + pub fn push_progress_bar( 11 + done: &Arc<AtomicU64>, 12 + total: u64, 13 + size: u8, 14 + title: &str, 15 + started_at: Instant, 16 + update_time: Duration, 17 + ) { 18 + let done = done.clone(); 19 + let title = title.to_owned(); 20 + // let total = total.clone(); 21 + // let size = size.clone(); 22 + 23 + thread::spawn(move || { 24 + let sleep = Duration::from_secs(1); 25 + loop { 26 + let done = done.load(Ordering::Relaxed); 27 + 28 + // let total = total.load(Ordering::Relaxed); 29 + // let size = size.load(Ordering::Relaxed); 30 + 31 + // some work here 32 + let procentage_f = done as f32 / total as f32; 33 + let procentage = (procentage_f * 100f32).round(); 34 + // print!("{}") 35 + let done_count = size as f32 * procentage_f; 36 + // let todo_count = size - done_count.round(); 37 + 38 + let mut string = "\r".to_string(); 39 + string.push_str(&title); 40 + string.push_str(" || "); 41 + 42 + if done == total { 43 + string.push_str("\x1b[32m"); 44 + } 45 + 46 + for each in 0..size { 47 + if each <= done_count.round() as u8 { 48 + string.push('■'); 49 + } else { 50 + string.push('·'); 51 + } 52 + } 53 + 54 + if done == total { 55 + string.push_str("\x1b[0m"); 56 + } 57 + 58 + // Guestimate remaining time 59 + let going_for = started_at.elapsed().as_secs_f32(); 60 + let still_todo = 100f32 - procentage; 61 + let time_per_percentage = procentage / going_for; 62 + let remaining_time = (still_todo / time_per_percentage).ceil(); 63 + 64 + if done == total { 65 + string.push_str(&format!(" {}/{} {}% || done! \n", done, total, procentage)); 66 + } else { 67 + string.push_str(&format!( 68 + " {}/{} {}% || {}s ", 69 + done, total, procentage, remaining_time 70 + )); 71 + } 72 + 73 + print!("{string}"); 74 + 75 + if done >= total { 76 + // println!("Stopping!"); 77 + break; 78 + } 79 + print!("sleeping"); 80 + thread::sleep(sleep); 81 + } 82 + }); 83 + } 84 + 85 + #[cfg(test)] 86 + mod tests { 87 + use std::{ 88 + thread, 89 + time::{Duration, Instant}, 90 + }; 91 + 92 + use super::*; 93 + 94 + #[test] 95 + fn test_progress() { 96 + let now = Instant::now(); 97 + 98 + let value = Arc::new(AtomicU64::from(0)); 99 + 100 + push_progress_bar(&value, 100, 50, "Being silly", now, Duration::from_secs(1)); 101 + 102 + for _ in 0..=100 { 103 + value.fetch_add(1, Ordering::Relaxed); 104 + 105 + thread::sleep(Duration::from_millis(100)); 106 + } 107 + // assert_eq!(result, 4); 108 + } 109 + }