|
| 1 | +//! Task state breakdown bar component. |
| 2 | +
|
| 3 | +use ratatui::widgets::Paragraph; |
| 4 | +use ratatui::layout::Rect; |
| 5 | +use ratatui::style::Color; |
| 6 | +use ratatui::style::Style; |
| 7 | +use ratatui::text::Line; |
| 8 | +use ratatui::text::Span; |
| 9 | +use ratatui::Frame; |
| 10 | + |
| 11 | +use crate::scheduler::SchedulerSnapshot; |
| 12 | + |
| 13 | +use super::super::support::state::count_states; |
| 14 | +use super::super::support::state::BarSegments; |
| 15 | +use super::super::support::state::StateCounts; |
| 16 | + |
| 17 | +/* IMPLEMENTATIONS */ |
| 18 | + |
| 19 | +pub fn render(frame: &mut Frame, area: Rect, snapshot: &SchedulerSnapshot) { |
| 20 | + let counts = count_states(snapshot); |
| 21 | + let width = area.width as usize; |
| 22 | + |
| 23 | + let lines = vec![ |
| 24 | + status_line(&counts), |
| 25 | + render_state_bar(&counts, width), |
| 26 | + ]; |
| 27 | + |
| 28 | + frame.render_widget(Paragraph::new(lines), area); |
| 29 | +} |
| 30 | + |
| 31 | +fn styled_span(label: &'static str, color: Color) -> Span<'static> { |
| 32 | + Span::styled(label, Style::default().fg(color)) |
| 33 | +} |
| 34 | + |
| 35 | +fn status_line(counts: &StateCounts) -> Line<'static> { |
| 36 | + let dot = styled_span("• ", Color::White); |
| 37 | + |
| 38 | + Line::from(vec![ |
| 39 | + styled_span(" Running: ", Color::Green), |
| 40 | + Span::raw(format!("{} ", counts.running)), |
| 41 | + dot.clone(), |
| 42 | + styled_span("Ready: ", Color::Blue), |
| 43 | + Span::raw(format!("{} ", counts.ready)), |
| 44 | + dot.clone(), |
| 45 | + styled_span("Waiting: ", Color::Cyan), |
| 46 | + Span::raw(format!("{} ", counts.waiting)), |
| 47 | + dot.clone(), |
| 48 | + styled_span("Suspended: ", Color::Gray), |
| 49 | + Span::raw(format!("{} ", counts.suspended)), |
| 50 | + dot, |
| 51 | + styled_span("Errors: ", Color::Red), |
| 52 | + Span::raw(format!("{}", counts.errors)), |
| 53 | + ]) |
| 54 | +} |
| 55 | + |
| 56 | +fn render_state_bar(counts: &StateCounts, width: usize) -> Line<'static> { |
| 57 | + Line::from(bar_spans(&bar_segments(counts, width))) |
| 58 | +} |
| 59 | + |
| 60 | +fn compute_ratio(count: usize, total: usize, width: usize) -> f64 { |
| 61 | + ((count as f64 / total as f64) * width as f64).round() |
| 62 | +} |
| 63 | + |
| 64 | +fn bar_segments(counts: &StateCounts, width: usize) -> BarSegments { |
| 65 | + let total = counts.suspended |
| 66 | + + counts.running |
| 67 | + + counts.waiting |
| 68 | + + counts.errors |
| 69 | + + counts.ready; |
| 70 | + |
| 71 | + if total == 0 { |
| 72 | + return BarSegments { |
| 73 | + errors: width, |
| 74 | + suspended: 0, |
| 75 | + running: 0, |
| 76 | + waiting: 0, |
| 77 | + ready: 0, |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + let suspended = compute_ratio(counts.suspended, total, width) as usize; |
| 82 | + let running = compute_ratio(counts.running, total, width) as usize; |
| 83 | + let waiting = compute_ratio(counts.waiting, total, width) as usize; |
| 84 | + let errors = compute_ratio(counts.errors, total, width) as usize; |
| 85 | + let ready = compute_ratio(counts.ready, total, width) as usize; |
| 86 | + |
| 87 | + let used = suspended + running + waiting + errors + ready; |
| 88 | + let adjust = width.saturating_sub(used); |
| 89 | + |
| 90 | + BarSegments { |
| 91 | + errors: errors + adjust, |
| 92 | + suspended, |
| 93 | + running, |
| 94 | + waiting, |
| 95 | + ready, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +fn bar_span(count: usize, color: Color) -> Span<'static> { |
| 100 | + Span::styled("▃".repeat(count), Style::default().fg(color)) |
| 101 | +} |
| 102 | + |
| 103 | +fn bar_spans(seg: &BarSegments) -> Vec<Span<'static>> { |
| 104 | + vec![ |
| 105 | + bar_span(seg.running, Color::Green), |
| 106 | + bar_span(seg.ready, Color::Blue), |
| 107 | + bar_span(seg.waiting, Color::Cyan), |
| 108 | + bar_span(seg.suspended, Color::Gray), |
| 109 | + bar_span(seg.errors, Color::Red), |
| 110 | + ] |
| 111 | +} |
0 commit comments