diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-04 10:14:38 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-04 15:25:43 +0800 |
| commit | f65b6a9eb6bd9d295b9adb5b95e0973f6e43954c (patch) | |
| tree | 5f3b40271ee0dd259aff29488f65803028376939 /src/progress.rs | |
| parent | 05b7b483056902a07f83bc14f443ab59ce1471d8 (diff) | |
Bump version to 0.1.2
- Add increase/reduce functions (#1)
Diffstat (limited to 'src/progress.rs')
| -rw-r--r-- | src/progress.rs | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/src/progress.rs b/src/progress.rs index a5509ef..977ffc1 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -17,10 +17,8 @@ pub struct ProgressCenter { /// ``` rust /// use just_progress::progress; /// -/// fn main() { -/// // Initialize -/// let progress_center = progress::init(); -/// } +/// // Initialize +/// let progress_center = progress::init(); /// ``` pub fn init() -> &'static ProgressCenter { let (tx, rx) = watch::channel::<HashMap<String, ProgressState>>(HashMap::new()); @@ -55,11 +53,11 @@ where let state = rx.borrow().clone(); // If it's a close flag, break - if let Some(_) = state.get(SPECIAL_FLAG_CLOSE) { + if state.contains_key(SPECIAL_FLAG_CLOSE) { break; } // Clear flag - if let Some(_) = state.get(SPECIAL_FLAG_CLEAR) { + if state.contains_key(SPECIAL_FLAG_CLEAR) { // Send all known names + progress 0 + EmptyState for (name, _) in state.iter().filter(|(k, _)| *k != SPECIAL_FLAG_CLEAR) { callback( @@ -137,6 +135,42 @@ pub fn complete(name: &str) { update_progress(name, 1.); } +/// Increase +/// +/// Increase a progress item's value by a specified amount (clamped to 1.0) +pub fn increase(name: &str, amount: f32) { + let Some(center) = PROGRESS_CENTER.get() else { + return; + }; + let mut state = center.tx.borrow().clone(); + let entry = state + .entry(name.to_string()) + .or_insert_with(|| ProgressState { + progress: 0.0, + info: ProgressInfo::default(), + }); + entry.progress = (entry.progress + amount).clamp(0.0, 1.0); + let _ = center.tx.send(state); +} + +/// Reduce +/// +/// Reduce a progress item's value by a specified amount (clamped to 0.0) +pub fn reduce(name: &str, amount: f32) { + let Some(center) = PROGRESS_CENTER.get() else { + return; + }; + let mut state = center.tx.borrow().clone(); + let entry = state + .entry(name.to_string()) + .or_insert_with(|| ProgressState { + progress: 0.0, + info: ProgressInfo::default(), + }); + entry.progress = (entry.progress - amount).clamp(0.0, 1.0); + let _ = center.tx.send(state); +} + /// Clear all /// /// Send a message to clear all items, ensuring no progress remains @@ -177,7 +211,7 @@ pub fn update_progress(name: &str, progress: f32) { progress: 0.0, info: ProgressInfo::default(), }); - entry.progress = progress; + entry.progress = progress.clamp(0.0, 1.0); let _ = center.tx.send(state); } |
