| 1 | //! Gallery: data displays fully drawn, mid draw-in, empty and from agent JSON. |
| 2 | |
| 3 | use ratatui::{buffer::Buffer, layout::Rect}; |
| 4 | |
| 5 | use super::Entry; |
| 6 | use crate::{ |
| 7 | DataBarItem, DataBars, DataDisplay, DataDisplayView, DataSparkline, DataTrend, Paint, Theme, |
| 8 | }; |
| 9 | |
| 10 | fn series() -> Vec<f64> { |
| 11 | (0..64) |
| 12 | .map(|i| 120.0 + 60.0 * (f64::from(i) / 6.0).sin() + f64::from((i * 7) % 11) * 3.0) |
| 13 | .collect() |
| 14 | } |
| 15 | |
| 16 | fn items() -> Vec<DataBarItem> { |
| 17 | vec![ |
| 18 | DataBarItem::new("engine", 412.0), |
| 19 | DataBarItem::new("tui", 268.0), |
| 20 | DataBarItem::new("runtime", 131.0), |
| 21 | DataBarItem::new("protocol", 44.0), |
| 22 | ] |
| 23 | } |
| 24 | |
| 25 | fn row(area: Rect, y: u16, h: u16) -> Rect { |
| 26 | Rect::new(area.x, area.y.saturating_add(y), area.width, h).intersection(area) |
| 27 | } |
| 28 | |
| 29 | fn sparklines(area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 30 | let values = series(); |
| 31 | DataSparkline::new("Requests", &values) |
| 32 | .unit("rpm") |
| 33 | .paint(row(area, 0, 1), buf, theme); |
| 34 | DataSparkline::new("Requests", &values) |
| 35 | .unit("rpm") |
| 36 | .drawn(0.45) |
| 37 | .paint(row(area, 1, 1), buf, theme); |
| 38 | DataSparkline::new("Requests", &[]).paint(row(area, 2, 1), buf, theme); |
| 39 | } |
| 40 | |
| 41 | fn bars(area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 42 | let items = items(); |
| 43 | DataBars::new("Tests by crate", &items).paint(row(area, 0, 5), buf, theme); |
| 44 | } |
| 45 | |
| 46 | fn bars_growing(area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 47 | let items = items(); |
| 48 | DataBars::new("Tests by crate", &items) |
| 49 | .previous(&[300.0, 300.0, 300.0, 300.0]) |
| 50 | .drawn(0.5) |
| 51 | .paint(row(area, 0, 5), buf, theme); |
| 52 | } |
| 53 | |
| 54 | fn trend(area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 55 | let values = series(); |
| 56 | DataTrend::new("Latency", &values) |
| 57 | .unit("ms") |
| 58 | .rows(7) |
| 59 | .paint(area, buf, theme); |
| 60 | } |
| 61 | |
| 62 | fn trend_drawing(area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 63 | let values = series(); |
| 64 | DataTrend::new("Latency", &values) |
| 65 | .unit("ms") |
| 66 | .rows(7) |
| 67 | .drawn(0.6) |
| 68 | .paint(area, buf, theme); |
| 69 | } |
| 70 | |
| 71 | fn from_agent(area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 72 | let shown = DataDisplay::from_json( |
| 73 | r#"{"kind":"bars","label":"Tokens this turn","unit":"tokens", |
| 74 | "items":[{"label":"read","value":18200},{"label":"wrote","value":2400}, |
| 75 | {"label":"cached","value":61000}]}"#, |
| 76 | ) |
| 77 | .expect("gallery fixture is a valid data display"); |
| 78 | let view: DataDisplayView<'_> = shown.view(); |
| 79 | view.paint(area, buf, theme); |
| 80 | } |
| 81 | |
| 82 | pub(crate) fn entries() -> Vec<Entry> { |
| 83 | vec![ |
| 84 | Entry { |
| 85 | name: "data-sparkline", |
| 86 | width: 80, |
| 87 | height: 3, |
| 88 | draw: sparklines, |
| 89 | }, |
| 90 | Entry { |
| 91 | name: "data-bars", |
| 92 | width: 60, |
| 93 | height: 5, |
| 94 | draw: bars, |
| 95 | }, |
| 96 | Entry { |
| 97 | name: "data-bars-growing", |
| 98 | width: 60, |
| 99 | height: 5, |
| 100 | draw: bars_growing, |
| 101 | }, |
| 102 | Entry { |
| 103 | name: "data-trend", |
| 104 | width: 72, |
| 105 | height: 7, |
| 106 | draw: trend, |
| 107 | }, |
| 108 | Entry { |
| 109 | name: "data-trend-drawing", |
| 110 | width: 72, |
| 111 | height: 7, |
| 112 | draw: trend_drawing, |
| 113 | }, |
| 114 | Entry { |
| 115 | name: "data-from-agent", |
| 116 | width: 60, |
| 117 | height: 4, |
| 118 | draw: from_agent, |
| 119 | }, |
| 120 | ] |
| 121 | } |
| 122 |