summaryrefslogtreecommitdiff
path: root/src/progress.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/progress.rs')
-rw-r--r--src/progress.rs48
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);
}