rich_rust 0.2.1

A Rust port of Python's Rich library for beautiful terminal output
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
//! Dashboard scene for demo_showcase.
//!
//! The centerpiece scene demonstrating rich_rust's Live + Layout capabilities:
//! - Split-screen dashboard with header, main, sidebar, and footer
//! - Real-time pipeline simulation with progress bars
//! - Service health monitoring
//! - Streaming log output
//!
//! This scene runs differently based on interactivity:
//! - Interactive: Live loop with auto-refresh
//! - Non-interactive: Single snapshot render

use std::sync::Arc;
use std::thread;
use std::time::Duration;

use rich_rust::console::Console;
use rich_rust::interactive::Status;
use rich_rust::live::{Live, LiveOptions, VerticalOverflowMethod};
use rich_rust::markup::render_or_plain;
use rich_rust::renderables::Renderable;
use rich_rust::renderables::panel::Panel;
use rich_rust::segment::Segment;
use rich_rust::text::Text;

use crate::Config;
use crate::log_pane::LogPane;
use crate::scenes::{Scene, SceneError};
use crate::simulation::{init_pipeline, run_pipeline};
use crate::state::{
    LogLevel, PipelineStage, ServiceHealth, ServiceInfo, SharedDemoState, StageStatus,
};
use crate::timing::{DemoRng, Timing};

/// Dashboard scene: live split-screen dashboard with pipeline simulation.
pub struct DashboardScene;

impl DashboardScene {
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl Scene for DashboardScene {
    fn name(&self) -> &'static str {
        "dashboard"
    }

    fn summary(&self) -> &'static str {
        "Live split-screen dashboard (services + pipeline + logs)."
    }

    fn run(&self, console: &Arc<Console>, cfg: &Config) -> Result<(), SceneError> {
        // Brief spinner moment: "Building deployment plan…"
        if let Ok(_status) = Status::new(console, "Building deployment plan…") {
            let duration = if cfg.is_quick() {
                Duration::from_millis(200)
            } else {
                Duration::from_millis(600)
            };
            thread::sleep(duration);
        }

        // Create demo state
        let state = SharedDemoState::new(cfg.run_id(), cfg.seed());
        init_services(&state);
        init_pipeline(&state);

        state.update(|demo| {
            demo.headline = "Nebula Deploy v1.2.3".to_string();
            demo.push_log(LogLevel::Info, "Dashboard initialized");
        });

        // Only use Live mode if both:
        // 1. Interactive mode is allowed (not --no-interactive)
        // 2. The console is actually attached to a terminal
        if cfg.is_interactive() && console.is_terminal() {
            // Live mode: run the simulation with live updates
            run_live_dashboard(console, cfg, &state)?;
        } else {
            // Non-interactive: render a static snapshot
            render_static_dashboard(console, cfg, &state)?;
        }

        Ok(())
    }
}

/// Initialize the service list.
fn init_services(state: &SharedDemoState) {
    state.update(|demo| {
        demo.services = vec![
            ServiceInfo {
                name: "api".to_string(),
                health: ServiceHealth::Ok,
                latency: Duration::from_millis(12),
                version: "1.2.3".to_string(),
            },
            ServiceInfo {
                name: "worker".to_string(),
                health: ServiceHealth::Ok,
                latency: Duration::from_millis(25),
                version: "1.2.3".to_string(),
            },
            ServiceInfo {
                name: "db".to_string(),
                health: ServiceHealth::Ok,
                latency: Duration::from_millis(8),
                version: "13.4".to_string(),
            },
            ServiceInfo {
                name: "cache".to_string(),
                health: ServiceHealth::Warn,
                latency: Duration::from_millis(3),
                version: "7.0".to_string(),
            },
        ];
    });
}

/// Run the dashboard with Live updates.
fn run_live_dashboard(
    console: &Arc<Console>,
    cfg: &Config,
    state: &SharedDemoState,
) -> Result<(), SceneError> {
    let timing = Timing::new(cfg.speed(), cfg.is_quick());
    let mut rng = DemoRng::new(cfg.seed());

    // Clone state for the get_renderable closure
    let state_for_render = state.clone();
    let safe_box = cfg.is_safe_box();

    let options = LiveOptions {
        screen: false,
        auto_refresh: true,
        refresh_per_second: 10.0,
        transient: false, // Keep dashboard visible after stop()
        redirect_stdout: false,
        redirect_stderr: false,
        vertical_overflow: VerticalOverflowMethod::Ellipsis,
    };

    let live = Live::with_options(Arc::clone(console), options).get_renderable(move || {
        let snapshot = state_for_render.snapshot();
        Box::new(DashboardRenderable::new(&snapshot, safe_box))
    });

    live.start(true)?;

    // Run the pipeline simulation
    let success = run_pipeline(state, &timing, &mut rng, true);

    // Final update
    state.update(|demo| {
        if success {
            demo.headline = "Pipeline completed successfully!".to_string();
            demo.push_log(LogLevel::Info, "All stages complete");
        } else {
            demo.headline = "Pipeline failed".to_string();
        }
    });

    // Brief pause to show final state
    timing.sleep(Duration::from_millis(500));

    live.stop()?;

    // Print final summary
    console.print("");
    let snapshot = state.snapshot();
    if success {
        console.print("[bold green]Pipeline completed successfully[/]");
    } else {
        console.print("[bold red]Pipeline failed[/]");
    }
    console.print(&format!(
        "[dim]Completed in {:.1}s[/]",
        snapshot.elapsed.as_secs_f64()
    ));

    Ok(())
}

/// Render a static snapshot of the dashboard (non-interactive mode).
fn render_static_dashboard(
    console: &Arc<Console>,
    cfg: &Config,
    state: &SharedDemoState,
) -> Result<(), SceneError> {
    // Simulate some initial state for the snapshot
    state.update(|demo| {
        demo.headline = "Nebula Deploy v1.2.3 (snapshot)".to_string();

        // Set some stages to show activity
        if !demo.pipeline.is_empty() {
            demo.pipeline[0].status = StageStatus::Done;
            demo.pipeline[0].progress = 1.0;
        }
        if demo.pipeline.len() > 1 {
            demo.pipeline[1].status = StageStatus::Running;
            demo.pipeline[1].progress = 0.42;
            demo.pipeline[1].eta = Some(Duration::from_secs(8));
        }

        // Add some demo logs
        demo.push_log(LogLevel::Info, "Starting deployment pipeline");
        demo.push_log(LogLevel::Debug, "Loading configuration from deploy.toml");
        demo.push_log(LogLevel::Info, "[LINT] Starting");
        demo.push_log(LogLevel::Info, "[LINT] Completed");
        demo.push_log(LogLevel::Info, "[BUILD] Starting");
        demo.push_log(LogLevel::Debug, "Compiling 127 crates...");
    });

    let snapshot = state.snapshot();
    let renderable = DashboardRenderable::new(&snapshot, cfg.is_safe_box());
    console.print_renderable(&renderable);

    console.print("");
    console.print("[hint]Run with --no-interactive false for live updates.[/]");

    Ok(())
}

/// A renderable that combines the full dashboard layout.
///
/// Stores the data needed to render, but builds panels on-demand
/// during rendering to avoid lifetime issues with Panel.
struct DashboardRenderable {
    headline: String,
    run_id: u64,
    seed: u64,
    pipeline: Vec<PipelineStage>,
    services: Vec<ServiceInfo>,
    logs: Vec<crate::state::LogLine>,
    safe_box: bool,
}

impl DashboardRenderable {
    fn new(snapshot: &crate::state::DemoStateSnapshot, safe_box: bool) -> Self {
        Self {
            headline: snapshot.headline.clone(),
            run_id: snapshot.run_id,
            seed: snapshot.seed,
            pipeline: snapshot.pipeline.clone(),
            services: snapshot.services.clone(),
            logs: snapshot.logs.clone(),
            safe_box,
        }
    }

    fn render_pipeline_text(stages: &[PipelineStage]) -> Text {
        let mut lines = Vec::new();

        for stage in stages {
            let status_badge = match stage.status {
                StageStatus::Pending => "[dim]O[/]",
                StageStatus::Running => "[bold yellow]*[/]",
                StageStatus::Done => "[bold green]v[/]",
                StageStatus::Failed => "[bold red]x[/]",
            };

            let progress = if stage.status == StageStatus::Running {
                let pct = (stage.progress * 100.0).round() as u32;
                let filled = (pct as usize) / 5;
                let empty = 20 - filled;
                let bar = "#".repeat(filled);
                let rest = "-".repeat(empty);
                format!(" [cyan][{bar}{rest}][/] {pct:>3}%")
            } else if stage.status == StageStatus::Done {
                " [green][####################][/] 100%".to_string()
            } else {
                " [dim][--------------------][/]   0%".to_string()
            };

            let eta = stage
                .eta
                .map(|d| format!(" [dim]({}s)[/]", d.as_secs()))
                .unwrap_or_default();

            lines.push(format!(
                "{} [bold]{:<12}[/]{}{}",
                status_badge, stage.name, progress, eta
            ));
        }

        render_or_plain(&lines.join("\n"))
    }

    fn render_services_text(services: &[ServiceInfo]) -> Text {
        let mut lines = Vec::new();

        for svc in services {
            let health_badge = match svc.health {
                ServiceHealth::Ok => "[green]OK[/]",
                ServiceHealth::Warn => "[yellow]WARN[/]",
                ServiceHealth::Err => "[red]ERR[/]",
            };

            let latency = if svc.latency.as_millis() > 0 {
                format!("{}ms", svc.latency.as_millis())
            } else {
                "-".to_string()
            };

            lines.push(format!(
                "{:<6} [bold]{:<8}[/] [dim]v{}[/]  {}",
                health_badge, svc.name, svc.version, latency
            ));
        }

        render_or_plain(&lines.join("\n"))
    }
}

impl Renderable for DashboardRenderable {
    fn render<'a>(
        &'a self,
        _console: &Console,
        options: &rich_rust::console::ConsoleOptions,
    ) -> Vec<Segment<'a>> {
        let mut segments = Vec::new();
        let max_width = options.max_width;

        // Header
        let header = render_or_plain(&format!(
            "[bold cyan]{}[/]  [dim]Run #{}  Seed: {}[/]",
            self.headline, self.run_id, self.seed
        ));
        segments.extend(header.render("").into_iter().map(Segment::into_owned));
        segments.push(Segment::new("\n\n".to_string(), None));

        // Pipeline panel
        let pipeline_text = Self::render_pipeline_text(&self.pipeline);
        let pipeline_panel = Panel::from_rich_text(&pipeline_text, 60)
            .title_from_markup("[bold]Pipeline[/]")
            .safe_box(self.safe_box);
        segments.extend(
            pipeline_panel
                .render(max_width)
                .into_iter()
                .map(Segment::into_owned),
        );
        segments.push(Segment::new("\n".to_string(), None));

        // Services panel
        let services_text = Self::render_services_text(&self.services);
        let services_panel = Panel::from_rich_text(&services_text, 30)
            .title_from_markup("[bold]Services[/]")
            .safe_box(self.safe_box);
        segments.extend(
            services_panel
                .render(max_width)
                .into_iter()
                .map(Segment::into_owned),
        );
        segments.push(Segment::new("\n".to_string(), None));

        // Logs panel
        let log_pane = LogPane::from_snapshot(&self.logs, 8);
        let logs_text = log_pane.as_text();
        let logs_panel = Panel::from_rich_text(&logs_text, 80)
            .title_from_markup("[bold]Logs[/]")
            .safe_box(self.safe_box);
        segments.extend(
            logs_panel
                .render(max_width)
                .into_iter()
                .map(Segment::into_owned),
        );

        segments
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dashboard_scene_has_correct_name() {
        let scene = DashboardScene::new();
        assert_eq!(scene.name(), "dashboard");
    }

    #[test]
    fn dashboard_scene_runs_without_error() {
        let scene = DashboardScene::new();
        let console = Console::builder()
            .force_terminal(false)
            .markup(true)
            .build()
            .shared();
        let cfg = Config::with_defaults();

        let result = scene.run(&console, &cfg);
        assert!(result.is_ok());
    }

    #[test]
    fn dashboard_renderable_creates_without_panic() {
        let state = SharedDemoState::new(1, 42);
        init_services(&state);
        init_pipeline(&state);
        let snapshot = state.snapshot();
        let _ = DashboardRenderable::new(&snapshot, false);
    }
}