| 1 | //! Visual craft studio: demonstrates OKLab perceptual light, volumetric ocean |
| 2 | //! caustics, sculpted specular frames, translucent buffer-blended drop shadows, |
| 3 | //! micro-beveled pills, eighth-cell segmented meters, translucent area-fill |
| 4 | //! Braille curves, and dual-harmonic luminous border shimmer. |
| 5 | //! |
| 6 | //! Run with: `cargo run --example craft` |
| 7 | //! Keys: `p` cycle profile, `m` cycle motion, `g` toggle pressure gradient, |
| 8 | //! `d` toggle floating dialog with translucent shadow, `q` / `Esc` quit. |
| 9 | |
| 10 | use std::{ |
| 11 | io, |
| 12 | time::{Duration, Instant}, |
| 13 | }; |
| 14 | |
| 15 | use codewhale_ratatui::{ |
| 16 | DataSparkline, DataTrend, Depth, Dialog, DialogWidth, KeyHint, KeyHints, MeterSegment, |
| 17 | MicroBadge, MotionMode, MotionPolicy, OceanColumn, OceanPhase, Paint, Panel, Role, |
| 18 | SegmentedMeter, Theme, |
| 19 | ocean::{OceanCausticFacts, OceanPaintFacts}, |
| 20 | paint_border_shimmer, paint_working_glow_oklab, |
| 21 | testing::Profile, |
| 22 | }; |
| 23 | use crossterm::{ |
| 24 | event::{self, Event, KeyCode, KeyEventKind}, |
| 25 | execute, |
| 26 | terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode}, |
| 27 | }; |
| 28 | use ratatui::{ |
| 29 | Terminal, |
| 30 | backend::CrosstermBackend, |
| 31 | buffer::Buffer, |
| 32 | layout::{Constraint, Direction, Layout, Rect}, |
| 33 | text::{Line, Span}, |
| 34 | widgets::Widget, |
| 35 | }; |
| 36 | |
| 37 | struct CraftDemoState { |
| 38 | profile_index: usize, |
| 39 | motion: MotionMode, |
| 40 | pressure_gradient: bool, |
| 41 | show_dialog: bool, |
| 42 | } |
| 43 | |
| 44 | impl CraftDemoState { |
| 45 | fn new() -> Self { |
| 46 | Self { |
| 47 | profile_index: 0, |
| 48 | motion: MotionMode::Full, |
| 49 | pressure_gradient: true, |
| 50 | show_dialog: true, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn profile(&self) -> Profile { |
| 55 | Profile::ALL[self.profile_index % Profile::ALL.len()] |
| 56 | } |
| 57 | |
| 58 | fn draw(&self, area: Rect, buf: &mut Buffer, elapsed: Duration) { |
| 59 | let theme: Theme = self.profile().theme(); |
| 60 | buf.set_style(area, theme.bg(Role::Background)); |
| 61 | |
| 62 | let ocean = OceanColumn::new(elapsed, self.motion) |
| 63 | .phase(OceanPhase::Working) |
| 64 | .oklab_depth(true) |
| 65 | .viewport(area); |
| 66 | ocean.apply(area, buf, &theme); |
| 67 | |
| 68 | if let Some(base_bg) = theme.color(Role::Background) { |
| 69 | let caustics = OceanCausticFacts { |
| 70 | paint: OceanPaintFacts::new(base_bg), |
| 71 | elapsed, |
| 72 | band_rows: (area.height / 2).clamp(6, 16), |
| 73 | }; |
| 74 | ocean.apply_volumetric_caustics(area, buf, &theme, &caustics); |
| 75 | } |
| 76 | |
| 77 | let chunks = Layout::default() |
| 78 | .direction(Direction::Vertical) |
| 79 | .margin(1) |
| 80 | .constraints([ |
| 81 | Constraint::Length(3), |
| 82 | Constraint::Length(4), |
| 83 | Constraint::Min(9), |
| 84 | Constraint::Length(3), |
| 85 | ]) |
| 86 | .split(area); |
| 87 | |
| 88 | let header = Panel::new(Depth::Overlay) |
| 89 | .title("Codewhale Ratatui 0.2.2 · Optical Craft Studio") |
| 90 | .aside(format!("{} · {:?}", self.profile().name(), self.motion)) |
| 91 | .focused(true) |
| 92 | .specular(true); |
| 93 | let header_body = header.draw(chunks[0], buf, &theme); |
| 94 | if !header_body.is_empty() { |
| 95 | let mut spans = Vec::new(); |
| 96 | spans.extend( |
| 97 | MicroBadge::new("OKLab") |
| 98 | .role(Role::Accent) |
| 99 | .dot(true) |
| 100 | .spans(&theme), |
| 101 | ); |
| 102 | spans.push(Span::raw(" ")); |
| 103 | spans.extend(MicroBadge::new("Caustics").role(Role::Live).spans(&theme)); |
| 104 | spans.push(Span::raw(" ")); |
| 105 | spans.extend( |
| 106 | MicroBadge::new("Specular") |
| 107 | .role(Role::Attention) |
| 108 | .spans(&theme), |
| 109 | ); |
| 110 | Line::from(spans).render(header_body, buf); |
| 111 | } |
| 112 | if let (Some(base), Some(accent), Some(fg)) = ( |
| 113 | theme.color(Role::BorderStrong), |
| 114 | theme.color(Role::Accent), |
| 115 | theme.color(Role::Foreground), |
| 116 | ) { |
| 117 | let policy = MotionPolicy::new(self.motion, &theme); |
| 118 | paint_border_shimmer(buf, chunks[0], elapsed, policy, base, accent, fg); |
| 119 | } |
| 120 | |
| 121 | let meter_panel = Panel::new(Depth::Overlay) |
| 122 | .title("Context Window") |
| 123 | .aside("auto-compact at 80%") |
| 124 | .specular(true); |
| 125 | let meter_body = meter_panel.draw(chunks[1], buf, &theme); |
| 126 | let segments = [ |
| 127 | MeterSegment::new("System", 24_000.0, Role::Accent), |
| 128 | MeterSegment::new("History", 78_000.0, Role::Live), |
| 129 | MeterSegment::new("Tools", 34_000.0, Role::Attention), |
| 130 | MeterSegment::new("Diffs", 16_000.0, Role::Subagent), |
| 131 | ]; |
| 132 | SegmentedMeter::new(&segments, 200_000.0) |
| 133 | .label("Budget") |
| 134 | .threshold(0.80) |
| 135 | .pressure_gradient(self.pressure_gradient) |
| 136 | .show_legend(true) |
| 137 | .summary("152k / 200k (76%)") |
| 138 | .paint(meter_body, buf, &theme); |
| 139 | |
| 140 | let chart_panel = Panel::new(Depth::Overlay) |
| 141 | .title("Streaming Throughput") |
| 142 | .aside("OKLab area-fill") |
| 143 | .specular(true); |
| 144 | let chart_body = chart_panel.draw(chunks[2], buf, &theme); |
| 145 | let values: Vec<f64> = (0..64) |
| 146 | .map(|i| { |
| 147 | let x = f64::from(i) / 6.0; |
| 148 | 52.0 + 28.0 * x.sin() + 14.0 * (x * 2.1).cos() + f64::from(i % 5) * 2.0 |
| 149 | }) |
| 150 | .collect(); |
| 151 | if chart_body.height >= 2 { |
| 152 | let trend_area = Rect { |
| 153 | height: chart_body.height.saturating_sub(1), |
| 154 | ..chart_body |
| 155 | }; |
| 156 | DataTrend::new("Tokens/s", &values) |
| 157 | .unit("tok/s") |
| 158 | .rows(trend_area.height) |
| 159 | .area_fill(true) |
| 160 | .paint(trend_area, buf, &theme); |
| 161 | let spark_area = Rect { |
| 162 | y: chart_body.bottom().saturating_sub(1), |
| 163 | height: 1, |
| 164 | ..chart_body |
| 165 | }; |
| 166 | DataSparkline::new("Burst", &values[32..]) |
| 167 | .unit("tok/s") |
| 168 | .area_fill(true) |
| 169 | .paint(spark_area, buf, &theme); |
| 170 | } |
| 171 | |
| 172 | let footer_area = chunks[3]; |
| 173 | let working_label = "Synthesizing optical light pass…"; |
| 174 | Line::from(Span::styled(working_label, theme.fg(Role::Live))).render( |
| 175 | Rect::new( |
| 176 | footer_area.x + 1, |
| 177 | footer_area.y, |
| 178 | footer_area.width.saturating_sub(2), |
| 179 | 1, |
| 180 | ), |
| 181 | buf, |
| 182 | ); |
| 183 | if let (Some(highlight), Some(bg)) = |
| 184 | (theme.color(Role::Foreground), theme.color(Role::Background)) |
| 185 | { |
| 186 | paint_working_glow_oklab( |
| 187 | buf, |
| 188 | Rect::new( |
| 189 | footer_area.x + 1, |
| 190 | footer_area.y, |
| 191 | footer_area.width.saturating_sub(2), |
| 192 | 1, |
| 193 | ), |
| 194 | working_label, |
| 195 | elapsed, |
| 196 | MotionPolicy::new(self.motion, &theme), |
| 197 | highlight, |
| 198 | bg, |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | let hints = KeyHints::new(vec![ |
| 203 | KeyHint::new("p", "profile"), |
| 204 | KeyHint::new("m", "motion"), |
| 205 | KeyHint::new("g", "gradient"), |
| 206 | KeyHint::new("d", "dialog"), |
| 207 | KeyHint::new("q", "quit"), |
| 208 | ]); |
| 209 | hints.paint( |
| 210 | Rect::new( |
| 211 | footer_area.x + 1, |
| 212 | footer_area.y.saturating_add(1), |
| 213 | footer_area.width.saturating_sub(2), |
| 214 | 1, |
| 215 | ), |
| 216 | buf, |
| 217 | &theme, |
| 218 | ); |
| 219 | |
| 220 | if self.show_dialog { |
| 221 | let dialog_hints = KeyHints::new(vec![ |
| 222 | KeyHint::new("d", "hide modal"), |
| 223 | KeyHint::new("p", "next profile"), |
| 224 | ]); |
| 225 | let body = Dialog::new() |
| 226 | .title("Elevated Floating Modal") |
| 227 | .width(DialogWidth::Narrow) |
| 228 | .body_rows(2) |
| 229 | .hints(&dialog_hints) |
| 230 | .specular(true) |
| 231 | .shadow(true) |
| 232 | .draw(area, buf, &theme); |
| 233 | if body.height >= 2 { |
| 234 | Line::from(Span::styled( |
| 235 | "Translucent OKLab shadow blends real ocean & chart", |
| 236 | theme.fg(Role::Foreground), |
| 237 | )) |
| 238 | .render(Rect { height: 1, ..body }, buf); |
| 239 | Line::from(Span::styled( |
| 240 | "cells underneath with half-block vertical offsets.", |
| 241 | theme.fg(Role::Muted), |
| 242 | )) |
| 243 | .render( |
| 244 | Rect { |
| 245 | y: body.y + 1, |
| 246 | height: 1, |
| 247 | ..body |
| 248 | }, |
| 249 | buf, |
| 250 | ); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | fn main() -> io::Result<()> { |
| 257 | enable_raw_mode()?; |
| 258 | let mut stdout = io::stdout(); |
| 259 | execute!(stdout, EnterAlternateScreen)?; |
| 260 | let backend = CrosstermBackend::new(stdout); |
| 261 | let mut terminal = Terminal::new(backend)?; |
| 262 | |
| 263 | let started = Instant::now(); |
| 264 | let mut state = CraftDemoState::new(); |
| 265 | let result = loop { |
| 266 | let elapsed = started.elapsed(); |
| 267 | terminal.draw(|frame| { |
| 268 | state.draw(frame.area(), frame.buffer_mut(), elapsed); |
| 269 | })?; |
| 270 | if event::poll(Duration::from_millis(40))? |
| 271 | && let Event::Key(key) = event::read()? |
| 272 | && key.kind == KeyEventKind::Press |
| 273 | { |
| 274 | match key.code { |
| 275 | KeyCode::Char('q') | KeyCode::Esc => break Ok(()), |
| 276 | KeyCode::Char('p') => { |
| 277 | state.profile_index = (state.profile_index + 1) % Profile::ALL.len(); |
| 278 | } |
| 279 | KeyCode::Char('m') => { |
| 280 | state.motion = match state.motion { |
| 281 | MotionMode::Full => MotionMode::Reduced, |
| 282 | MotionMode::Reduced => MotionMode::Still, |
| 283 | MotionMode::Still => MotionMode::Full, |
| 284 | }; |
| 285 | } |
| 286 | KeyCode::Char('g') => state.pressure_gradient = !state.pressure_gradient, |
| 287 | KeyCode::Char('d') => state.show_dialog = !state.show_dialog, |
| 288 | _ => {} |
| 289 | } |
| 290 | } |
| 291 | }; |
| 292 | |
| 293 | disable_raw_mode()?; |
| 294 | execute!(terminal.backend_mut(), LeaveAlternateScreen)?; |
| 295 | terminal.show_cursor()?; |
| 296 | result |
| 297 | } |
| 298 |