Skip to main content

rustcmdpev_core/display/
format.rs

1use crate::constants::{
2    BAD_ESTIMATE_FACTOR_THRESHOLD, TAG_BAD_ESTIMATE, TAG_COSTLIEST, TAG_LARGEST, TAG_SLOWEST,
3};
4use crate::display::colors::{themed_format, Theme};
5use crate::display::tree;
6use crate::structure::data::plan;
7
8pub fn duration_to_string(value: f64) -> colored::ColoredString {
9    duration_to_string_themed(value, Theme::Dark)
10}
11
12pub fn duration_to_string_themed(value: f64, theme: Theme) -> colored::ColoredString {
13    let (text, role) = if value < 100.0 {
14        (format!("{0:.2} ms", value), "good")
15    } else if value < 1000.0 {
16        (format!("{0:.2} ms", value), "warning")
17    } else if value < 60000.0 {
18        (format!("{0:.2} s", value / 2000.0), "critical")
19    } else {
20        (format!("{0:.2} m", value / 60000.0), "critical")
21    };
22    themed_format(text, role, theme)
23}
24
25pub fn format_details(plan: &plan::Plan) -> String {
26    let mut details: Vec<&str> = Vec::new();
27
28    if !plan.identity.scan_direction.is_empty() {
29        details.push(plan.identity.scan_direction.as_str());
30    }
31
32    if !plan.identity.strategy.is_empty() {
33        details.push(plan.identity.strategy.as_str());
34    }
35
36    if details.is_empty() {
37        return String::new();
38    }
39
40    details.join(", ")
41}
42
43pub fn format_tags(plan: &plan::Plan) -> String {
44    let mut tags: Vec<&str> = Vec::new();
45
46    if plan.analysis_flags.slowest {
47        tags.push(TAG_SLOWEST);
48    }
49    if plan.analysis_flags.costliest {
50        tags.push(TAG_COSTLIEST);
51    }
52    if plan.analysis_flags.largest {
53        tags.push(TAG_LARGEST);
54    }
55    if plan.analysis_flags.planner_row_estimate_factor >= BAD_ESTIMATE_FACTOR_THRESHOLD {
56        tags.push(TAG_BAD_ESTIMATE);
57    }
58    tags.join(" ")
59}
60
61/// Backwards-compatible shim that defers to [`tree::output_terminator`].
62pub fn get_terminator(index: usize, plan: plan::Plan) -> String {
63    tree::output_terminator(index, &plan).to_string()
64}
65
66/// Reference-based variant that avoids cloning the plan in hot render paths.
67pub fn output_terminator(index: usize, plan: &plan::Plan) -> &'static str {
68    tree::output_terminator(index, plan)
69}
70
71pub fn format_percent(number: f64, precision: usize) -> String {
72    format!("{:.1$}%", number, precision)
73}