Skip to main content

pakem/core/tinker/
formatter.rs

1/// Formatter tabel ANSI ASCII box-drawing dan JSON pretty untuk Lumina Tinker REPL
2pub struct TinkerFormatter;
3
4impl TinkerFormatter {
5    /// Render data kolom dan baris ke dalam format tabel box-drawing yang elegan
6    pub fn render_table(columns: &[String], rows: &[Vec<String>]) -> String {
7        if columns.is_empty() {
8            return "(No columns)\n".to_string();
9        }
10
11        let num_cols = columns.len();
12        let mut col_widths = vec![0usize; num_cols];
13
14        for (i, col) in columns.iter().enumerate() {
15            col_widths[i] = col.len();
16        }
17
18        for row in rows {
19            for (i, cell) in row.iter().enumerate() {
20                if i < num_cols {
21                    col_widths[i] = col_widths[i].max(cell.len());
22                }
23            }
24        }
25
26        // Berikan padding 1 spasi kiri dan kanan
27        let padded_widths: Vec<usize> = col_widths.iter().map(|w| w + 2).collect();
28
29        let mut out = String::new();
30
31        // 1. Garis Atas ┌───┬───┐
32        out.push('┌');
33        for (i, w) in padded_widths.iter().enumerate() {
34            out.push_str(&"─".repeat(*w));
35            if i + 1 < num_cols {
36                out.push('┬');
37            }
38        }
39        out.push_str("┐\n");
40
41        // 2. Baris Header │ col1 │ col2 │
42        out.push('│');
43        for (i, col) in columns.iter().enumerate() {
44            let width = col_widths[i];
45            out.push_str(&format!(" {:<width$} ", col, width = width));
46            out.push('│');
47        }
48        out.push('\n');
49
50        // 3. Garis Pemisah Header ├───┼───┤
51        out.push('├');
52        for (i, w) in padded_widths.iter().enumerate() {
53            out.push_str(&"─".repeat(*w));
54            if i + 1 < num_cols {
55                out.push('┼');
56            }
57        }
58        out.push_str("┤\n");
59
60        // 4. Baris Data
61        if rows.is_empty() {
62            let total_inner_width: usize = padded_widths.iter().sum::<usize>() + (num_cols - 1);
63            let msg = "(no records found)";
64            out.push('│');
65            out.push_str(&format!(" {:<width$} ", msg, width = total_inner_width.saturating_sub(2)));
66            out.push_str("│\n");
67        } else {
68            for row in rows {
69                out.push('│');
70                for (i, cell) in row.iter().enumerate() {
71                    let width = col_widths.get(i).copied().unwrap_or(0);
72                    out.push_str(&format!(" {:<width$} ", cell, width = width));
73                    out.push('│');
74                }
75                out.push('\n');
76            }
77        }
78
79        // 5. Garis Bawah └───┴───┘
80        out.push('└');
81        for (i, w) in padded_widths.iter().enumerate() {
82            out.push_str(&"─".repeat(*w));
83            if i + 1 < num_cols {
84                out.push('┴');
85            }
86        }
87        out.push_str("┘\n");
88
89        out
90    }
91
92    /// Render nilai skalar sederhana dengan durasi
93    pub fn render_scalar(value: &str, duration_ms: f64) -> String {
94        format!("=> {}\n⚡ Executed in {:.2}ms\n", value, duration_ms)
95    }
96
97    /// Render JSON terformat indah
98    pub fn render_json(val: &serde_json::Value, duration_ms: f64) -> String {
99        let pretty = serde_json::to_string_pretty(val).unwrap_or_else(|_| val.to_string());
100        format!("=> {}\n⚡ Executed in {:.2}ms\n", pretty, duration_ms)
101    }
102}