sabiql 1.11.0

A fast, driver-less TUI for browsing and editing PostgreSQL databases
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::time::Instant;

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Wrap};

use crate::app::model::app_state::AppState;
use crate::app::model::explain_context::CompareSlot;
use crate::app::model::shared::flash_timer::FlashId;
use crate::domain::explain_plan::{self, ComparisonVerdict};
use crate::ui::theme::ThemePalette;

pub fn render(
    frame: &mut Frame,
    area: Rect,
    state: &AppState,
    now: Instant,
    theme: &ThemePalette,
) -> u16 {
    let can_yank = state.explain.left.is_some() && state.explain.right.is_some();
    let left = state.explain.left.as_ref();
    let right = state.explain.right.as_ref();
    let scroll_offset = state.explain.compare_scroll_offset;

    let mut lines: Vec<Line> = Vec::new();
    let mut flash_mask: Vec<bool> = Vec::new();

    if let (Some(l), Some(r)) = (left, right) {
        render_verdict_section(&mut lines, &mut flash_mask, l, r, area.width, theme);
    }

    if area.width >= 60 {
        render_slot_columns(&mut lines, &mut flash_mask, left, right, area.width, theme);
    } else {
        render_slot_stacked(&mut lines, &mut flash_mask, left, right, theme);
    }

    if lines.is_empty() {
        lines.push(Line::from(Span::styled(
            " Run EXPLAIN (Ctrl+E) to start comparing.",
            Style::default().fg(theme.semantic.text.placeholder),
        )));
        flash_mask.push(false);
    }

    let max_scroll = lines.len().saturating_sub(area.height as usize);
    let clamped = scroll_offset.min(max_scroll);
    let mut visible: Vec<Line> = lines.into_iter().skip(clamped).collect();
    let visible_mask: Vec<bool> = flash_mask.into_iter().skip(clamped).collect();

    let flash_active = can_yank && state.flash_timers.is_active(FlashId::SqlModal, now);
    crate::ui::primitives::atoms::apply_yank_flash_masked(
        &mut visible,
        flash_active,
        &visible_mask,
        theme,
    );

    frame.render_widget(
        Paragraph::new(visible)
            .style(Style::default().fg(theme.semantic.text.primary))
            .wrap(Wrap { trim: false }),
        area,
    );
    area.height
}

fn push_empty(lines: &mut Vec<Line>, flash_mask: &mut Vec<bool>) {
    lines.push(Line::raw(""));
    flash_mask.push(false);
}

// Copied to clipboard — flash on yank
fn push_content(lines: &mut Vec<Line>, flash_mask: &mut Vec<bool>, line: Line<'static>) {
    lines.push(line);
    flash_mask.push(true);
}

// UI chrome — never flash
fn push_chrome(lines: &mut Vec<Line>, flash_mask: &mut Vec<bool>, line: Line<'static>) {
    lines.push(line);
    flash_mask.push(false);
}

// ── Verdict (only when both slots are populated) ─────────────────────────────

fn render_verdict_section(
    lines: &mut Vec<Line>,
    flash_mask: &mut Vec<bool>,
    left: &CompareSlot,
    right: &CompareSlot,
    width: u16,
    theme: &ThemePalette,
) {
    let result = explain_plan::compare_plans(&left.plan, &right.plan);

    let (verdict_label, verdict_style) = match result.verdict {
        ComparisonVerdict::Improved => (
            "\u{2193} Improved",
            Style::default()
                .fg(theme.semantic.status.success)
                .add_modifier(Modifier::BOLD),
        ),
        ComparisonVerdict::Worsened => (
            "\u{2191} Worsened",
            Style::default()
                .fg(theme.semantic.status.error)
                .add_modifier(Modifier::BOLD),
        ),
        ComparisonVerdict::Similar => (
            "\u{2248} Similar",
            Style::default()
                .fg(theme.semantic.text.accent)
                .add_modifier(Modifier::BOLD),
        ),
        ComparisonVerdict::Unavailable => (
            "Comparison unavailable",
            Style::default()
                .fg(theme.semantic.text.muted)
                .add_modifier(Modifier::BOLD),
        ),
    };

    push_empty(lines, flash_mask);
    push_content(
        lines,
        flash_mask,
        Line::from(Span::styled(format!(" {verdict_label}"), verdict_style)),
    );
    push_empty(lines, flash_mask);

    for reason in &result.reasons {
        push_content(
            lines,
            flash_mask,
            Line::from(vec![
                Span::styled(
                    "  \u{2022} ",
                    Style::default().fg(theme.semantic.text.muted),
                ),
                Span::styled(
                    reason.clone(),
                    Style::default().fg(theme.semantic.text.primary),
                ),
            ]),
        );
    }
    if !result.reasons.is_empty() {
        push_empty(lines, flash_mask);
    }

    let sep = "\u{2500}".repeat(width.saturating_sub(2) as usize);
    push_chrome(
        lines,
        flash_mask,
        Line::styled(format!(" {sep}"), theme.modal_border_style()),
    );
    push_empty(lines, flash_mask);
}

// ── Side-by-side slot columns (shared across all states) ─────────────────────

fn render_slot_columns(
    lines: &mut Vec<Line>,
    flash_mask: &mut Vec<bool>,
    left: Option<&CompareSlot>,
    right: Option<&CompareSlot>,
    total_width: u16,
    theme: &ThemePalette,
) {
    let half = (total_width.saturating_sub(3) / 2) as usize;
    let sep = Span::styled(" \u{2502} ", theme.modal_border_style());

    let active_header = Style::default()
        .fg(theme.semantic.text.accent)
        .add_modifier(Modifier::BOLD);
    let empty_header = Style::default()
        .fg(theme.semantic.text.dim)
        .add_modifier(Modifier::BOLD);

    let left_label = match left {
        Some(s) => format!(" {}", s.source.label()),
        None => " Previous".to_string(),
    };
    let right_label = match right {
        Some(s) => format!(" {}", s.source.label()),
        None => " Latest".to_string(),
    };

    push_chrome(
        lines,
        flash_mask,
        Line::from(vec![
            Span::styled(
                pad_or_truncate(&left_label, half),
                if left.is_some() {
                    active_header
                } else {
                    empty_header
                },
            ),
            sep.clone(),
            Span::styled(
                pad_or_truncate(&right_label, half),
                if right.is_some() {
                    active_header
                } else {
                    empty_header
                },
            ),
        ]),
    );

    let detail_style = Style::default().fg(theme.semantic.text.muted);
    let placeholder_style = Style::default().fg(theme.semantic.text.placeholder);

    let left_detail = slot_detail_text(left);
    let right_detail = slot_detail_text(right);

    push_chrome(
        lines,
        flash_mask,
        Line::from(vec![
            Span::styled(
                pad_or_truncate(&left_detail, half),
                if left.is_some() {
                    detail_style
                } else {
                    placeholder_style
                },
            ),
            sep.clone(),
            Span::styled(
                pad_or_truncate(&right_detail, half),
                if right.is_some() {
                    detail_style
                } else {
                    placeholder_style
                },
            ),
        ]),
    );

    let thin_sep = "\u{2500}".repeat(half.saturating_sub(1));
    push_chrome(
        lines,
        flash_mask,
        Line::from(vec![
            Span::styled(
                format!(" {thin_sep}"),
                Style::default().fg(theme.semantic.text.dim),
            ),
            sep.clone(),
            Span::styled(
                format!(" {thin_sep}"),
                Style::default().fg(theme.semantic.text.dim),
            ),
        ]),
    );

    let dim_style = Style::default().fg(theme.semantic.text.dim);

    let l_plan: Vec<&str> = left
        .map(|s| s.plan.raw_text.lines().collect())
        .unwrap_or_default();
    let r_plan: Vec<&str> = right
        .map(|s| s.plan.raw_text.lines().collect())
        .unwrap_or_default();
    let max = l_plan.len().max(r_plan.len());

    for i in 0..max {
        let l = l_plan.get(i).unwrap_or(&"");
        let r = r_plan.get(i).unwrap_or(&"");

        let mut row_spans = vec![Span::styled(" ".to_string(), dim_style)];
        row_spans.extend(super::plan_highlight::highlight_truncated(
            l,
            half.saturating_sub(1),
            theme,
        ));
        row_spans.push(sep.clone());
        row_spans.push(Span::styled(" ".to_string(), dim_style));
        row_spans.extend(super::plan_highlight::highlight_truncated(
            r,
            half.saturating_sub(1),
            theme,
        ));
        push_content(lines, flash_mask, Line::from(row_spans));
    }
}

// ── Stacked layout (narrow terminals) ────────────────────────────────────────

fn render_slot_stacked(
    lines: &mut Vec<Line>,
    flash_mask: &mut Vec<bool>,
    left: Option<&CompareSlot>,
    right: Option<&CompareSlot>,
    theme: &ThemePalette,
) {
    let header_style = Style::default()
        .fg(theme.semantic.text.accent)
        .add_modifier(Modifier::BOLD);
    let badge_style = Style::default().fg(theme.semantic.text.muted);

    render_stacked_slot(
        lines,
        flash_mask,
        left,
        " Previous",
        header_style,
        badge_style,
        theme,
    );
    push_empty(lines, flash_mask);
    render_stacked_slot(
        lines,
        flash_mask,
        right,
        " Latest",
        header_style,
        badge_style,
        theme,
    );
}

fn render_stacked_slot(
    lines: &mut Vec<Line>,
    flash_mask: &mut Vec<bool>,
    slot: Option<&CompareSlot>,
    empty_label: &str,
    active_style: Style,
    badge_style: Style,
    theme: &ThemePalette,
) {
    if let Some(s) = slot {
        push_chrome(
            lines,
            flash_mask,
            Line::from(Span::styled(format!(" {}", s.source.label()), active_style)),
        );
        let time_secs = s.plan.execution_secs();
        push_chrome(
            lines,
            flash_mask,
            Line::from(Span::styled(
                format!("  {}  ({:.2}s)", mode_label(s.plan.is_analyze), time_secs),
                badge_style,
            )),
        );
        for line in s.plan.raw_text.lines() {
            push_content(
                lines,
                flash_mask,
                super::plan_highlight::highlight_plan_line(line, theme),
            );
        }
    } else {
        push_chrome(
            lines,
            flash_mask,
            Line::from(Span::styled(
                empty_label.to_string(),
                Style::default()
                    .fg(theme.semantic.text.dim)
                    .add_modifier(Modifier::BOLD),
            )),
        );
        push_chrome(
            lines,
            flash_mask,
            Line::from(Span::styled(
                "  Run EXPLAIN again to compare",
                Style::default().fg(theme.semantic.text.placeholder),
            )),
        );
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────────

fn slot_detail_text(slot: Option<&CompareSlot>) -> String {
    match slot {
        Some(s) => {
            let time_secs = s.plan.execution_secs();
            format!(" {}  ({:.2}s)", mode_label(s.plan.is_analyze), time_secs)
        }
        None => " Run EXPLAIN again".to_string(),
    }
}

fn mode_label(is_analyze: bool) -> &'static str {
    if is_analyze { "ANALYZE" } else { "EXPLAIN" }
}

pub(super) fn pad_or_truncate(s: &str, width: usize) -> String {
    let char_count = s.chars().count();
    if char_count > width {
        s.chars().take(width.saturating_sub(1)).collect::<String>() + "\u{2026}"
    } else {
        format!("{s:<width$}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::model::explain_context::SlotSource;
    use crate::domain::explain_plan::ExplainPlan;
    use crate::ui::theme::DEFAULT_THEME;

    fn sample_slot(label: SlotSource, plan: &str) -> CompareSlot {
        CompareSlot {
            plan: ExplainPlan {
                raw_text: plan.to_string(),
                top_node_type: Some("Seq Scan".to_string()),
                total_cost: Some(10.0),
                estimated_rows: Some(1),
                is_analyze: false,
                execution_time_ms: 250,
            },
            query_snippet: "SELECT 1".to_string(),
            full_query: "SELECT 1".to_string(),
            source: label,
        }
    }

    #[test]
    fn stacked_compare_flashes_only_plan_content_rows() {
        let left = sample_slot(
            SlotSource::AutoPrevious,
            "Seq Scan on users  (cost=0.00..10.00 rows=1 width=32)\n  Filter: (id > 1)",
        );
        let right = sample_slot(
            SlotSource::AutoLatest,
            "Index Scan using users_pkey on users  (cost=0.00..5.00 rows=1 width=32)",
        );

        let mut lines = Vec::new();
        let mut flash_mask = Vec::new();
        render_slot_stacked(
            &mut lines,
            &mut flash_mask,
            Some(&left),
            Some(&right),
            &DEFAULT_THEME,
        );

        let rendered: Vec<String> = lines
            .iter()
            .map(|line| {
                line.spans
                    .iter()
                    .map(|span| span.content.as_ref())
                    .collect()
            })
            .collect();

        assert_eq!(lines.len(), flash_mask.len());
        assert_eq!(
            flash_mask,
            vec![false, false, true, true, false, false, false, true]
        );
        assert!(rendered[0].contains("Previous"));
        assert!(rendered[1].contains("EXPLAIN"));
        assert!(rendered[2].contains("Seq Scan on users"));
        assert!(rendered[3].contains("Filter:"));
        assert!(rendered[4].is_empty());
        assert!(rendered[5].contains("Latest"));
        assert!(rendered[6].contains("EXPLAIN"));
        assert!(rendered[7].contains("Index Scan using users_pkey"));
    }

    #[test]
    fn stacked_compare_empty_slot_never_marks_flashable_rows() {
        let mut lines = Vec::new();
        let mut flash_mask = Vec::new();
        render_slot_stacked(&mut lines, &mut flash_mask, None, None, &DEFAULT_THEME);

        assert_eq!(lines.len(), flash_mask.len());
        assert!(flash_mask.iter().all(|&flash| !flash));
    }
}