euv-cli 0.50.0

Cross-platform CLI tool for the euv native UI framework supporting run, build, check, fmt, doctor, new, and Hyperlane SSE dynamic updates across all supported platforms.
Documentation
use super::*;

/// Label helpers on `StatusBadge`.
impl StatusBadge {
    /// Returns the short uppercase badge label (`PASS`, `SKIP`, `FAIL`, `WARN`, `INFO`).
    ///
    /// # Returns
    ///
    /// - `&'static str` - Fixed-width status token.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Pass => BADGE_PASS,
            Self::Skip => BADGE_SKIP,
            Self::Fail => BADGE_FAIL,
            Self::Warn => BADGE_WARN,
            Self::Info => BADGE_INFO,
        }
    }
}

/// Builder and rendering methods on `TerminalTable`.
impl TerminalTable {
    /// Creates a new `TerminalTable` initialized with `headers`.
    ///
    /// # Arguments
    ///
    /// - `Vec<String>` - Ordered column header labels.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty table ready for row insertion.
    #[must_use]
    pub fn with_headers(headers: Vec<String>) -> Self {
        Self {
            headers,
            rows: Vec::new(),
        }
    }

    /// Appends a single row of cell values to this table.
    ///
    /// # Arguments
    ///
    /// - `Vec<String>` - Cell texts matching column order.
    pub fn push_row(&mut self, row: Vec<String>) {
        let Self { rows, .. } = self;
        rows.push(row);
    }

    /// Renders this table into aligned plain-text lines separated by two spaces.
    ///
    /// # Returns
    ///
    /// - `String` - Rendered multi-line terminal table.
    #[must_use]
    pub fn render(&self) -> String {
        let headers: &[String] = self.get_headers();
        let rows: &[Vec<String>] = self.get_rows();
        let col_count: usize = headers.len().max(
            rows.iter()
                .map(|row: &Vec<String>| row.len())
                .max()
                .unwrap_or(0),
        );
        if col_count == 0 {
            return String::new();
        }
        let mut widths: Vec<usize> = vec![0; col_count];
        for (idx, cell) in headers.iter().enumerate() {
            widths[idx] = widths[idx].max(cell.chars().count());
        }
        for row in rows.iter() {
            for (idx, cell) in row.iter().enumerate() {
                widths[idx] = widths[idx].max(cell.chars().count());
            }
        }
        let mut out: String = String::new();
        if !headers.is_empty() {
            write_table_row(&mut out, headers, &widths);
            let divider: Vec<String> = widths
                .iter()
                .map(|width: &usize| "-".repeat((*width).max(3)))
                .collect();
            write_table_row(&mut out, &divider, &widths);
        }
        for row in rows.iter() {
            write_table_row(&mut out, row, &widths);
        }
        out
    }
}