fast_rich/
nested_progress.rs

1//! Nested progress bars support.
2//!
3//! Simplified version that demonstrates the concept.
4
5/// A simple nested progress structure.
6pub struct NestedProgress {
7    #[allow(dead_code)] // Used for display/debugging
8    description: String,
9    total: u64,
10    current: u64,
11    children: Vec<NestedProgress>,
12}
13
14impl NestedProgress {
15    /// Create a new nested progress.
16    pub fn new(description: impl Into<String>, total: u64) -> Self {
17        NestedProgress {
18            description: description.into(),
19            total,
20            current: 0,
21            children: Vec::new(),
22        }
23    }
24
25    /// Add a child progress.
26    pub fn add_child(&mut self, description: impl Into<String>, total: u64) -> &mut NestedProgress {
27        self.children.push(NestedProgress::new(description, total));
28        self.children.last_mut().unwrap()
29    }
30
31    /// Update progress.
32    pub fn update(&mut self, amount: u64) {
33        self.current = (self.current + amount).min(self.total);
34    }
35
36    /// Get completion percentage.
37    pub fn percent(&self) -> f64 {
38        if self.total == 0 {
39            100.0
40        } else {
41            (self.current as f64 / self.total as f64) * 100.0
42        }
43    }
44
45    /// Get the number of children.
46    pub fn child_count(&self) -> usize {
47        self.children.len()
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_nested_progress_creation() {
57        let nested = NestedProgress::new("Root task", 100);
58        assert_eq!(nested.child_count(), 0);
59    }
60
61    #[test]
62    fn test_add_child() {
63        let mut nested = NestedProgress::new("Parent", 100);
64        nested.add_child("Child task", 50);
65        assert_eq!(nested.child_count(), 1);
66    }
67
68    #[test]
69    fn test_update() {
70        let mut nested = NestedProgress::new("Task", 100);
71        nested.update(50);
72        assert_eq!(nested.percent(), 50.0);
73    }
74}