minimo 0.5.42

terminal ui library combining alot of things from here and there and making it slightly easier to play with
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use std::fmt::{Display, Formatter};
use std::io::{self, Write};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::time::{Duration, Instant};

use crossterm::{
    cursor,
    style::{self, Color, SetForegroundColor, Stylize},
    terminal::{Clear, ClearType},
    ExecutableCommand, QueueableCommand,
};

use super::*;

// Types for context storage
#[derive(Clone, Debug)]
pub struct ProgressContextData {
    bars: Vec<ProgressBarData>,
    spinners: Vec<SpinnerData>,
    active: bool,
}

#[derive(Clone, Debug)]
struct ProgressBarData {
    id: String,
    current: usize,
    total: usize,
    message: String,
}

#[derive(Clone, Debug)]
struct SpinnerData {
    id: String,
    message: String,
    active: bool,
}

// Progress state management
pub struct ProgressManager {
    data: Arc<RwLock<ProgressContextData>>,
}

impl ProgressManager {
    pub fn new() -> Self {
        let data = Arc::new(RwLock::new(ProgressContextData {
            bars: Vec::new(),
            spinners: Vec::new(),
            active: true,
        }));

        set!(progress_data => data.clone());

        Self { data }
    }

    pub fn create_bar(&self, id: impl Into<String>, total: usize) -> ProgressBar {
        let id = id.into();
        let mut data = self.data.write().unwrap();
        
        data.bars.push(ProgressBarData {
            id: id.clone(),
            current: 0,
            total,
            message: String::new(),
        });

        ProgressBar::new(id, total, self.data.clone())
    }

    pub fn create_spinner(&self, id: impl Into<String>, message: impl Into<String>) -> Spinner {
        let id = id.into();
        let message = message.into();
        let mut data = self.data.write().unwrap();
        
        data.spinners.push(SpinnerData {
            id: id.clone(),
            message: message.clone(),
            active: true,
        });

        Spinner::new(id, message, self.data.clone())
    }
}

// Style configurations
#[derive(Clone)]
pub struct ProgressStyle {
    pub bar_chars: String,
    pub empty_chars: String,
    pub prefix: String,
    pub suffix: String,
    pub colors: Vec<Color>,
}

impl Default for ProgressStyle {
    fn default() -> Self {
        Self {
            bar_chars: "".to_string(),
            empty_chars: "".to_string(),
            prefix: "[".to_string(),
            suffix: "]".to_string(),
            colors: vec![
                Color::Cyan,
                Color::Magenta,
                Color::Blue,
                Color::Green,
            ],
        }
    }
}

#[derive(Clone)]
pub struct SpinnerStyle {
    pub frames: Vec<String>,
    pub interval: Duration,
    pub colors: Vec<Color>,
}

impl Default for SpinnerStyle {
    fn default() -> Self {
        Self {
            frames: vec![
                "", "", "", "", "", "", "", "", "", "",
            ].into_iter().map(String::from).collect(),
            interval: Duration::from_millis(80),
            colors: vec![
                Color::Cyan,
                Color::Magenta,
                Color::Blue,
                Color::Green,
            ],
        }
    }
}

// Progress bar implementation
pub struct ProgressBar {
    id: String,
    total: usize,
    style: ProgressStyle,
    data: Arc<RwLock<ProgressContextData>>,
}

impl ProgressBar {
    fn new(id: String, total: usize, data: Arc<RwLock<ProgressContextData>>) -> Self {
        Self {
            id,
            total,
            style: ProgressStyle::default(),
            data,
        }
    }

    pub fn with_style(mut self, style: ProgressStyle) -> Self {
        self.style = style;
        self
    }

    pub fn increment(&self, amount: usize) {
        let mut data = self.data.write().unwrap();
        if let Some(bar) = data.bars.iter_mut().find(|b| b.id == self.id) {
            bar.current = (bar.current + amount).min(bar.total);
            drop(data);
            self.draw();
        }
    }

    pub fn set_message(&self, msg: impl Into<String>) {
        let mut data = self.data.write().unwrap();
        if let Some(bar) = data.bars.iter_mut().find(|b| b.id == self.id) {
            bar.message = msg.into();
            drop(data);
            self.draw();
        }
    }

    pub fn finish(&self) {
        let mut data = self.data.write().unwrap();
        if let Some(bar) = data.bars.iter_mut().find(|b| b.id == self.id) {
            bar.current = bar.total;
            drop(data);
            self.draw();
            println!();
        }
    }

    fn draw(&self) {
        let data = self.data.read().unwrap();
        if !data.active {
            return;
        }

        if let Some(bar) = data.bars.iter().find(|b| b.id == self.id) {
            let width = 40;
            let progress = bar.current as f64 / bar.total as f64;
            let filled = (width as f64 * progress) as usize;
            let empty = width - filled;

            let mut stdout = io::stdout();
            stdout.queue(Clear(ClearType::CurrentLine)).unwrap()
                  .queue(cursor::MoveToColumn(0)).unwrap();
            let colors = self.style.colors.clone();
            //fill remaining colors as white
            let remaining_colors = vec![Color::White; colors.len() - filled];
            let all_colors = [colors, remaining_colors].concat();

            // Draw progress bar
            print!("{}", self.style.prefix);
            print!("{}", self.style.bar_chars.repeat(filled));
            print!("{}", self.style.empty_chars.repeat(empty));
            print!("{} ", self.style.suffix);

            // Draw progress information
            print!(
                "{:.1}% [{}/{}] {}",
                progress * 100.0,
                bar.current,
                bar.total,
                bar.message
            );

            stdout.flush().unwrap();
        }
    }
}

// Spinner implementation
pub struct Spinner {
    id: String,
    style: SpinnerStyle,
    data: Arc<RwLock<ProgressContextData>>,
    handle: Option<thread::JoinHandle<()>>,
}

impl Spinner {
    fn new(id: String, message: String, data: Arc<RwLock<ProgressContextData>>) -> Self {
        let style = SpinnerStyle::default();
        let style_clone = style.clone();
        let spinner_data = data.clone();
        let spinner_id = id.clone();

        let handle = thread::spawn(move || {
            let mut frame_idx = 0;
            let mut color_idx = 0;

            while {
                let data = spinner_data.read().unwrap();
                data.active && data.spinners.iter().any(|s| s.id == spinner_id && s.active)
            } {
                let data = spinner_data.read().unwrap();
                if let Some(spinner) = data.spinners.iter().find(|s| s.id == spinner_id) {
                    let frame = &style.frames[frame_idx];
                    let color = style.colors[color_idx];

                    let mut stdout = io::stdout();
                    stdout.queue(Clear(ClearType::CurrentLine)).unwrap()
                          .queue(cursor::MoveToColumn(0)).unwrap()
                          .queue(SetForegroundColor(color)).unwrap();

                    print!("{} {}", frame, spinner.message);
                    stdout.flush().unwrap();

                    frame_idx = (frame_idx + 1) % style.frames.len();
                    color_idx = (color_idx + 1) % style.colors.len();
                }

                drop(data);
                thread::sleep(style.interval);
            }
        });

        Self {
            id,
            style: style_clone,
            data,
            handle: Some(handle),
        }
    }

    pub fn with_style(mut self, style: SpinnerStyle) -> Self {
        self.style = style;
        self
    }

    pub fn set_message(&self, msg: impl Into<String>) {
        let mut data = self.data.write().unwrap();
        if let Some(spinner) = data.spinners.iter_mut().find(|s| s.id == self.id) {
            spinner.message = msg.into();
        }
    }

    pub fn finish(&mut self) {
        let mut data = self.data.write().unwrap();
        if let Some(spinner) = data.spinners.iter_mut().find(|s| s.id == self.id) {
            spinner.active = false;
        }
        drop(data);

        if let Some(handle) = self.handle.take() {
            handle.join().unwrap();
        }
        println!();
    }
}

impl Drop for Spinner {
    fn drop(&mut self) {
        self.finish();
    }
}

// Multi-progress implementation
pub struct MultiProgress {
    bars: Vec<ProgressBar>,
    current_step: usize,
    style: ProgressStyle,
    data: Arc<RwLock<ProgressContextData>>,
}

impl MultiProgress {
    pub fn new(steps: Vec<(String, usize)>, data: Arc<RwLock<ProgressContextData>>) -> Self {
        let bars = steps
            .into_iter()
            .map(|(name, total)| {
                ProgressBar::new(name, total, data.clone())
                    .with_style(ProgressStyle::default())
            })
            .collect();

        Self {
            bars,
            current_step: 0,
            style: ProgressStyle::default(),
            data,
        }
    }

    pub fn increment(&mut self, amount: usize) {
        if self.current_step < self.bars.len() {
            self.bars[self.current_step].increment(amount);
            
            let data = self.data.read().unwrap();
            if let Some(bar) = data.bars.iter().find(|b| b.id == self.bars[self.current_step].id) {
                if bar.current >= bar.total {
                    self.current_step += 1;
                }
            }
        }
    }

    pub fn set_message(&mut self, msg: impl Into<String>) {
        if self.current_step < self.bars.len() {
            self.bars[self.current_step].set_message(msg);
        }
    }

    pub fn finish(&mut self) {
        for bar in &self.bars {
            bar.finish();
        }
    }
}

// Helper functions for common progress patterns
pub fn download_progress(total_bytes: usize) -> ProgressBar {
    let manager = ProgressManager::new();
    let style = ProgressStyle {
        bar_chars: "".to_string(),
        empty_chars: "".to_string(),
        prefix: "".to_string(),
        suffix: "".to_string(),
        colors: vec![Color::Cyan],
    };

    manager.create_bar("download", total_bytes)
        .with_style(style)
}

pub fn processing_spinner(message: impl Into<String>) -> Spinner {
    let manager = ProgressManager::new();
    let style = SpinnerStyle {
        frames: vec!["", "", "", ""]
            .into_iter()
            .map(String::from)
            .collect(),
        interval: Duration::from_millis(100),
        colors: vec![Color::Cyan, Color::Blue],
    };

    manager.create_spinner("processing", message)
        .with_style(style)
}

pub fn installation_progress() -> MultiProgress {
    let manager = ProgressManager::new();
    let steps = vec![
        ("download".to_string(), 100),
        ("extract".to_string(), 100),
        ("install".to_string(), 100),
        ("configure".to_string(), 100),
    ];

    MultiProgress::new(steps, manager.data)
}

// Example usage
#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_progress_bar() {
        let manager = ProgressManager::new();
        let bar = manager.create_bar("test", 100);
        
        for i in 0..=100 {
            bar.increment(1);
            bar.set_message(format!("Processing {}", i));
            thread::sleep(Duration::from_millis(10));
        }
        
        bar.finish();
    }

    #[test]
    fn test_spinner() {
        let manager = ProgressManager::new();
        let mut spinner = manager.create_spinner("test", "Processing...");
        
        thread::sleep(Duration::from_secs(2));
        spinner.set_message("Almost done...");
        thread::sleep(Duration::from_secs(2));
        spinner.finish();
    }

    #[test]
    fn test_multi_progress() {
        let manager = ProgressManager::new();
        let steps = vec![
            ("Step 1".to_string(), 100),
            ("Step 2".to_string(), 100),
            ("Step 3".to_string(), 100),
        ];
        
        let mut multi = MultiProgress::new(steps, manager.data);
        
        for _ in 0..3 {
            for i in 0..=100 {
                multi.increment(1);
                multi.set_message(format!("Processing step {}", i));
                thread::sleep(Duration::from_millis(10));
            }
        }
        
        multi.finish();
    }
}

// Macros for easier progress creation
#[macro_export]
macro_rules! progress_bar {
    ($total:expr) => {{
        let manager = $crate::ProgressManager::new();
        manager.create_bar("default", $total)
    }};
    ($id:expr, $total:expr) => {{
        let manager = $crate::ProgressManager::new();
        manager.create_bar($id, $total)
    }};
}

#[macro_export]
macro_rules! spinner {
    ($message:expr) => {{
        let manager = $crate::ProgressManager::new();
        manager.create_spinner("default", $message)
    }};
    ($id:expr, $message:expr) => {{
        let manager = $crate::ProgressManager::new();
        manager.create_spinner($id, $message)
    }};
}