sorting-race 0.2.0

Terminal-based sorting algorithm race visualization tool with real-time metrics
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
//! Progress bar widgets for visualizing algorithm completion

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Style},
    widgets::{Block, Widget},
};

/// A single progress bar
#[derive(Debug, Clone)]
pub struct ProgressBar {
    progress: f32,
    label: String,
    style: Style,
    filled_style: Style,
    empty_style: Style,
    block: Option<Block<'static>>,
    show_percentage: bool,
    show_label: bool,
}

impl ProgressBar {
    /// Create a new progress bar
    pub fn new() -> Self {
        Self {
            progress: 0.0,
            label: String::new(),
            style: Style::default(),
            filled_style: Style::default().fg(Color::Green),
            empty_style: Style::default().fg(Color::Gray),
            block: None,
            show_percentage: true,
            show_label: true,
        }
    }

    /// Set the progress (0.0 to 1.0)
    pub fn progress(mut self, progress: f32) -> Self {
        self.progress = progress.clamp(0.0, 1.0);
        self
    }

    /// Set the label
    pub fn label<T>(mut self, label: T) -> Self 
    where
        T: Into<String>,
    {
        self.label = label.into();
        self
    }

    /// Set the base style
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set the style for filled portion
    pub fn filled_style(mut self, style: Style) -> Self {
        self.filled_style = style;
        self
    }

    /// Set the style for empty portion
    pub fn empty_style(mut self, style: Style) -> Self {
        self.empty_style = style;
        self
    }

    /// Set the block
    pub fn block(mut self, block: Block<'static>) -> Self {
        self.block = Some(block);
        self
    }

    /// Set whether to show percentage
    pub fn show_percentage(mut self, show: bool) -> Self {
        self.show_percentage = show;
        self
    }

    /// Set whether to show label
    pub fn show_label(mut self, show: bool) -> Self {
        self.show_label = show;
        self
    }

    /// Render the progress bar to a buffer
    pub fn render_widget(&self, area: Rect, buf: &mut Buffer) {
        if area.width < 3 || area.height < 1 {
            return;
        }

        let inner_area = if let Some(ref block) = self.block {
            let inner = block.inner(area);
            block.render(area, buf);
            inner
        } else {
            area
        };

        if inner_area.width == 0 || inner_area.height == 0 {
            return;
        }

        // Calculate space for progress bar
        let mut bar_area = inner_area;
        let mut info_area = None;

        // Reserve space for label and percentage if needed
        if ((self.show_label && !self.label.is_empty()) || self.show_percentage)
            && inner_area.height >= 2 {
                bar_area.height = inner_area.height - 1;
                info_area = Some(Rect {
                    x: inner_area.x,
                    y: inner_area.y + bar_area.height,
                    width: inner_area.width,
                    height: 1,
                });
            }

        // Render the progress bar
        let filled_width = (self.progress * bar_area.width as f32) as u16;
        let _empty_width = bar_area.width - filled_width;

        // Fill the filled portion
        for y in bar_area.top()..bar_area.bottom() {
            for x in bar_area.left()..(bar_area.left() + filled_width) {
                if x < bar_area.right() {
                    buf[(x, y)]
                        .set_symbol("â–ˆ")
                        .set_style(self.filled_style);
                }
            }
            
            // Fill the empty portion
            for x in (bar_area.left() + filled_width)..bar_area.right() {
                buf[(x, y)]
                    .set_symbol("â–‘")
                    .set_style(self.empty_style);
            }
        }

        // Render label and percentage
        if let Some(info_area) = info_area {
            let mut info_text = String::new();
            
            if self.show_label && !self.label.is_empty() {
                info_text.push_str(&self.label);
            }
            
            if self.show_percentage {
                if !info_text.is_empty() {
                    info_text.push(' ');
                }
                info_text.push_str(&format!("{:.1}%", self.progress * 100.0));
            }

            // Center the info text
            let info_chars: Vec<char> = info_text.chars().collect();
            let start_x = if info_area.width > info_chars.len() as u16 {
                info_area.left() + (info_area.width - info_chars.len() as u16) / 2
            } else {
                info_area.left()
            };

            for (i, ch) in info_chars.iter().enumerate() {
                let x = start_x + i as u16;
                if x < info_area.right() {
                    buf[(x, info_area.top())]
                        .set_symbol(&ch.to_string())
                        .set_style(self.style);
                }
            }
        }
    }
}

impl Default for ProgressBar {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget for ProgressBar {
    fn render(self, area: Rect, buf: &mut Buffer) {
        self.render_widget(area, buf);
    }
}

impl Widget for &ProgressBar {
    fn render(self, area: Rect, buf: &mut Buffer) {
        self.render_widget(area, buf);
    }
}

/// Collection of progress bars for multiple algorithms
#[derive(Debug, Clone)]
pub struct ProgressBars {
    bars: Vec<(String, ProgressBar)>,
    style: Style,
    block: Option<Block<'static>>,
}

impl ProgressBars {
    /// Create a new progress bars collection
    pub fn new() -> Self {
        Self {
            bars: Vec::new(),
            style: Style::default(),
            block: None,
        }
    }

    /// Add or update a progress bar
    pub fn add_bar<T>(&mut self, name: T, progress: f32) 
    where
        T: Into<String>,
    {
        let name_string = name.into();
        
        // Check if bar already exists
        for (existing_name, bar) in &mut self.bars {
            if *existing_name == name_string {
                *bar = ProgressBar::new()
                    .label(&name_string)
                    .progress(progress);
                return;
            }
        }
        
        // Add new bar
        let bar = ProgressBar::new()
            .label(&name_string)
            .progress(progress);
        self.bars.push((name_string, bar));
    }

    /// Set the base style
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set the block
    pub fn block(mut self, block: Block<'static>) -> Self {
        self.block = Some(block);
        self
    }

    /// Clear all bars
    pub fn clear(&mut self) {
        self.bars.clear();
    }

    /// Get number of bars
    pub fn len(&self) -> usize {
        self.bars.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.bars.is_empty()
    }

    /// Render the progress bars to a buffer
    pub fn render_widget(&self, area: Rect, buf: &mut Buffer) {
        if area.width < 3 || area.height < 3 {
            return;
        }

        let inner_area = if let Some(ref block) = self.block {
            let inner = block.inner(area);
            block.render(area, buf);
            inner
        } else {
            area
        };

        if self.bars.is_empty() || inner_area.height == 0 {
            return;
        }

        let bar_height = if inner_area.height >= self.bars.len() as u16 * 2 {
            2 // Space for bar and info
        } else {
            1 // Just the bar
        };

        let total_height_needed = self.bars.len() as u16 * bar_height;
        let start_y = if total_height_needed <= inner_area.height {
            inner_area.top()
        } else {
            inner_area.top()
        };

        for (i, (_name, bar)) in self.bars.iter().enumerate() {
            let y = start_y + (i as u16 * bar_height);
            
            if y >= inner_area.bottom() {
                break;
            }

            let bar_area = Rect {
                x: inner_area.x,
                y,
                width: inner_area.width,
                height: bar_height.min(inner_area.bottom() - y),
            };

            bar.render_widget(bar_area, buf);
        }
    }
}

impl Default for ProgressBars {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget for ProgressBars {
    fn render(self, area: Rect, buf: &mut Buffer) {
        self.render_widget(area, buf);
    }
}

impl Widget for &ProgressBars {
    fn render(self, area: Rect, buf: &mut Buffer) {
        self.render_widget(area, buf);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::{
        buffer::Buffer,
        layout::Rect,
    };

    #[test]
    fn test_progress_bar_creation() {
        let bar = ProgressBar::new()
            .progress(0.5)
            .label("Test")
            .show_percentage(true);
        
        assert!((bar.progress - 0.5).abs() < f32::EPSILON);
        assert_eq!(bar.label, "Test");
        assert!(bar.show_percentage);
    }

    #[test]
    fn test_progress_clamping() {
        let bar1 = ProgressBar::new().progress(-0.5);
        assert!((bar1.progress - 0.0).abs() < f32::EPSILON);

        let bar2 = ProgressBar::new().progress(1.5);
        assert!((bar2.progress - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_progress_bars_collection() {
        let mut bars = ProgressBars::new();
        bars.add_bar("Algorithm1", 0.3);
        bars.add_bar("Algorithm2", 0.7);
        
        assert_eq!(bars.len(), 2);
        assert!(!bars.is_empty());

        // Update existing bar
        bars.add_bar("Algorithm1", 0.5);
        assert_eq!(bars.len(), 2); // Should still be 2
    }

    #[test]
    fn test_render_widget() {
        let bar = ProgressBar::new()
            .progress(0.5)
            .label("Test Bar")
            .show_percentage(true);

        let area = Rect::new(0, 0, 20, 3);
        let mut buffer = Buffer::empty(area);

        bar.render_widget(area, &mut buffer);
        
        // Should not panic and should have some content
        let content = buffer.content();
        assert!(!content.is_empty());
    }

    #[test]
    fn test_render_progress_bars_collection() {
        let mut bars = ProgressBars::new();
        bars.add_bar("Quick Sort", 0.8);
        bars.add_bar("Merge Sort", 0.4);
        bars.add_bar("Bubble Sort", 0.2);

        let area = Rect::new(0, 0, 30, 10);
        let mut buffer = Buffer::empty(area);

        bars.render_widget(area, &mut buffer);
        
        // Should not panic and should have some content
        let content = buffer.content();
        assert!(!content.is_empty());
    }

    #[test]
    fn test_clear_bars() {
        let mut bars = ProgressBars::new();
        bars.add_bar("Test", 0.5);
        assert_eq!(bars.len(), 1);

        bars.clear();
        assert_eq!(bars.len(), 0);
        assert!(bars.is_empty());
    }

    #[test]
    fn test_small_area_handling() {
        let bar = ProgressBar::new().progress(0.5);

        // Test very small area
        let tiny_area = Rect::new(0, 0, 1, 1);
        let mut buffer = Buffer::empty(tiny_area);
        bar.render_widget(tiny_area, &mut buffer);
        
        // Should handle gracefully without panicking
    }
}