dbtui 0.3.4

Terminal database client with Vim-style navigation
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};

use crate::ui::state::{AppState, ExportField, ImportField};
use crate::ui::theme::Theme;

pub(super) fn render_confirm_delete_connection(
    frame: &mut Frame,
    theme: &Theme,
    area: Rect,
    conn_name: &str,
) {
    let width = 50_u16;
    let height = 5_u16;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(" Delete Connection ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.error_fg))
        .style(Style::default().bg(theme.dialog_bg));

    let text = vec![
        Line::from(""),
        Line::from(vec![
            Span::raw("  Delete "),
            Span::styled(
                conn_name,
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("? "),
            Span::styled(
                "y",
                Style::default()
                    .fg(theme.error_fg)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("/"),
            Span::styled(
                "n",
                Style::default()
                    .fg(theme.conn_connected)
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
    ];

    frame.render_widget(ratatui::widgets::Clear, popup);
    let content = Paragraph::new(text).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_confirm_close(frame: &mut Frame, theme: &Theme, area: Rect) {
    let width = 44_u16;
    let height = 5_u16;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(" Unsaved Changes ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.conn_connecting))
        .style(Style::default().bg(theme.dialog_bg));

    let text = vec![
        Line::from(""),
        Line::from(vec![
            Span::raw("  Save before closing? "),
            Span::styled(
                "y",
                Style::default()
                    .fg(theme.conn_connected)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("/"),
            Span::styled(
                "n",
                Style::default()
                    .fg(theme.error_fg)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("/"),
            Span::styled("Esc", Style::default().fg(theme.dim)),
        ]),
    ];

    // Clear area behind popup
    frame.render_widget(ratatui::widgets::Clear, popup);

    let content = Paragraph::new(text).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_confirm_quit(frame: &mut Frame, state: &AppState, theme: &Theme, area: Rect) {
    // Collect unsaved tab names
    let unsaved: Vec<String> = state
        .tabs
        .iter()
        .filter(|t| {
            t.editor.as_ref().is_some_and(|e| e.modified)
                || t.body_editor.as_ref().is_some_and(|e| e.modified)
                || t.decl_editor.as_ref().is_some_and(|e| e.modified)
        })
        .map(|t| {
            format!(
                "  {} {} ({})",
                t.kind.icon(),
                t.kind.display_name(),
                t.kind.kind_label()
            )
        })
        .collect();

    let list_height = unsaved.len() as u16;
    let width = 50_u16;
    // 3 = border top + empty line before list; 2 = empty line + hint line; 1 = border bottom
    let height = 3 + list_height + 2 + 1;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(" Unsaved Changes ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.conn_connecting))
        .style(Style::default().bg(theme.dialog_bg));

    let mut lines = vec![Line::from("")];

    for name in &unsaved {
        lines.push(Line::from(Span::styled(
            name.as_str(),
            Style::default().fg(theme.error_fg),
        )));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::raw("  Quit anyway? "),
        Span::styled(
            "y",
            Style::default()
                .fg(theme.conn_connected)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw("/"),
        Span::styled(
            "n",
            Style::default()
                .fg(theme.error_fg)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw("/"),
        Span::styled("Esc", Style::default().fg(theme.dim)),
    ]));

    frame.render_widget(ratatui::widgets::Clear, popup);

    let content = Paragraph::new(lines).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_save_grid_confirm(
    frame: &mut Frame,
    state: &AppState,
    theme: &Theme,
    area: Rect,
) {
    use crate::ui::tabs::RowChange;

    let tab = match state.active_tab() {
        Some(t) => t,
        None => return,
    };

    let modified = tab
        .grid_changes
        .values()
        .filter(|c| matches!(c, RowChange::Modified { .. }))
        .count();
    let new = tab
        .grid_changes
        .values()
        .filter(|c| matches!(c, RowChange::New { .. }))
        .count();
    let deleted = tab
        .grid_changes
        .values()
        .filter(|c| matches!(c, RowChange::Deleted))
        .count();

    let width = 44_u16;
    let height = 9_u16;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(" Save Changes ")
        .borders(Borders::ALL)
        .border_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .style(Style::default().bg(theme.dialog_bg));

    let mut lines = vec![Line::from("")];
    if modified > 0 {
        lines.push(Line::from(Span::styled(
            format!("  {modified} modified row(s)"),
            Style::default().fg(Color::Yellow),
        )));
    }
    if new > 0 {
        lines.push(Line::from(Span::styled(
            format!("  {new} new row(s)"),
            Style::default().fg(Color::Green),
        )));
    }
    if deleted > 0 {
        lines.push(Line::from(Span::styled(
            format!("  {deleted} deleted row(s)"),
            Style::default().fg(Color::Red),
        )));
    }
    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::raw("  Save to database? "),
        Span::styled(
            "y",
            Style::default()
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw("/"),
        Span::styled(
            "n",
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        ),
    ]));

    frame.render_widget(ratatui::widgets::Clear, popup);

    let content = Paragraph::new(lines).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_confirm_drop(frame: &mut Frame, state: &AppState, theme: &Theme, area: Rect) {
    let action = match &state.sidebar.pending_action {
        Some(a) => a,
        None => return,
    };

    let width = 48_u16;
    let height = 7_u16;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(" Drop Object ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Red).add_modifier(Modifier::BOLD))
        .style(Style::default().bg(theme.dialog_bg));

    let obj_label = format!("  {} {}.{}", action.obj_type, action.schema, action.name);
    let lines = vec![
        Line::from(""),
        Line::from(Span::styled(
            obj_label,
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
        Line::from(vec![
            Span::raw("  Drop? "),
            Span::styled(
                "y",
                Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
            ),
            Span::raw("/"),
            Span::styled(
                "n",
                Style::default()
                    .fg(Color::Green)
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
    ];

    frame.render_widget(ratatui::widgets::Clear, popup);

    let content = Paragraph::new(lines).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_rename_object(frame: &mut Frame, state: &AppState, theme: &Theme, area: Rect) {
    let action = match &state.sidebar.pending_action {
        Some(a) => a,
        None => return,
    };

    let width = 50_u16;
    let height = 7_u16;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(format!(" Rename {} ", action.obj_type))
        .borders(Borders::ALL)
        .border_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .style(Style::default().bg(theme.dialog_bg));

    let old_label = format!("  {}.{} \u{2192}", action.schema, action.name);
    let input_text = format!("  {}\u{2588}", state.sidebar.rename_buf);

    let lines = vec![
        Line::from(""),
        Line::from(Span::styled(old_label, Style::default().fg(theme.dim))),
        Line::from(Span::styled(
            input_text,
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
    ];

    frame.render_widget(ratatui::widgets::Clear, popup);

    let content = Paragraph::new(lines).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_confirm_compile(
    frame: &mut Frame,
    state: &AppState,
    theme: &Theme,
    area: Rect,
) {
    let tab = match state.active_tab() {
        Some(t) => t,
        None => return,
    };

    let obj_label = match &tab.kind {
        crate::ui::tabs::TabKind::Package { schema, name, .. } => {
            format!("  PACKAGE {schema}.{name}")
        }
        crate::ui::tabs::TabKind::Function { schema, name, .. } => {
            format!("  FUNCTION {schema}.{name}")
        }
        crate::ui::tabs::TabKind::Procedure { schema, name, .. } => {
            format!("  PROCEDURE {schema}.{name}")
        }
        _ => return,
    };

    let has_decl = tab.decl_editor.as_ref().is_some_and(|e| e.modified);
    let has_body = tab.body_editor.as_ref().is_some_and(|e| e.modified);
    let has_source = tab.editor.as_ref().is_some_and(|e| e.modified);

    let width = 48_u16;
    let height = 9_u16;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(" Compile to Database ")
        .borders(Borders::ALL)
        .border_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .style(Style::default().bg(theme.dialog_bg));

    let mut lines = vec![Line::from("")];
    lines.push(Line::from(Span::styled(
        obj_label,
        Style::default()
            .fg(theme.accent)
            .add_modifier(Modifier::BOLD),
    )));
    lines.push(Line::from(""));

    if has_decl {
        lines.push(Line::from(Span::styled(
            "  \u{270e} Declaration (modified)",
            Style::default().fg(Color::Yellow),
        )));
    }
    if has_body {
        lines.push(Line::from(Span::styled(
            "  \u{270e} Body (modified)",
            Style::default().fg(Color::Yellow),
        )));
    }
    if has_source {
        lines.push(Line::from(Span::styled(
            "  \u{270e} Source (modified)",
            Style::default().fg(Color::Yellow),
        )));
    }
    if !has_decl && !has_body && !has_source {
        lines.push(Line::from(Span::styled(
            "  (no changes detected)",
            Style::default().fg(theme.dim),
        )));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::raw("  Compile? "),
        Span::styled(
            "y",
            Style::default()
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw("/"),
        Span::styled(
            "n",
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        ),
    ]));

    frame.render_widget(ratatui::widgets::Clear, popup);
    let content = Paragraph::new(lines).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_save_script_name(
    frame: &mut Frame,
    state: &AppState,
    theme: &Theme,
    area: Rect,
) {
    let width = 44_u16;
    let height = 5_u16;
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width.min(area.width), height.min(area.height));

    let block = Block::default()
        .title(" Save Script As ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.conn_connecting))
        .style(Style::default().bg(theme.dialog_bg));

    let name_buf = state.scripts.save_name.as_deref().unwrap_or("");

    let text = vec![
        Line::from(""),
        Line::from(vec![
            Span::raw("  Name: "),
            Span::styled(
                format!("{name_buf}\u{2588}"),
                Style::default()
                    .fg(theme.conn_connecting)
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
    ];

    frame.render_widget(ratatui::widgets::Clear, popup);

    let content = Paragraph::new(text).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_bind_variables(
    frame: &mut Frame,
    state: &AppState,
    theme: &Theme,
    area: Rect,
) {
    let bv = match &state.dialogs.bind_variables {
        Some(b) => b,
        None => return,
    };

    let var_count = bv.variables.len();
    let width = 50_u16.min(area.width.saturating_sub(4));
    // 3 = border top + title line + border bottom, +1 per var, +1 for hint line
    let height = (3 + var_count as u16 + 2).min(area.height.saturating_sub(2));
    let x = (area.width.saturating_sub(width)) / 2;
    let y = (area.height.saturating_sub(height)) / 2;
    let popup = Rect::new(x, y, width, height);

    let block = Block::default()
        .title(" Bind Variables ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.conn_connecting))
        .style(Style::default().bg(theme.dialog_bg));

    frame.render_widget(ratatui::widgets::Clear, popup);

    let inner = block.inner(popup);
    frame.render_widget(block, popup);

    // Render each variable
    for (i, (name, value)) in bv.variables.iter().enumerate() {
        if i as u16 >= inner.height.saturating_sub(1) {
            break;
        }
        let row_y = inner.y + i as u16;
        let is_selected = i == bv.selected_idx;

        let label = format!(":{name}");
        let display_val = if is_selected {
            format!("{value}\u{2588}") // block cursor
        } else {
            value.clone()
        };

        let label_style = if is_selected {
            Style::default()
                .fg(theme.conn_connecting)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(theme.dim)
        };

        let val_style = if is_selected {
            Style::default()
                .fg(theme.accent)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(theme.status_fg)
        };

        let line = Line::from(vec![
            Span::styled(format!("  {label:<16}"), label_style),
            Span::styled(display_val, val_style),
        ]);

        let row_rect = Rect::new(inner.x, row_y, inner.width, 1);
        frame.render_widget(Paragraph::new(line), row_rect);
    }

    // Hint line at bottom
    let hint_y = inner.y + inner.height.saturating_sub(1);
    let hint = Line::from(vec![
        Span::styled("  Tab", Style::default().fg(theme.accent)),
        Span::styled(" next  ", Style::default().fg(theme.dim)),
        Span::styled("Enter", Style::default().fg(theme.conn_connected)),
        Span::styled(" execute  ", Style::default().fg(theme.dim)),
        Span::styled("Esc", Style::default().fg(theme.error_fg)),
        Span::styled(" cancel", Style::default().fg(theme.dim)),
    ]);
    let hint_rect = Rect::new(inner.x, hint_y, inner.width, 1);
    frame.render_widget(Paragraph::new(hint), hint_rect);
}

pub(super) fn render_theme_picker(frame: &mut Frame, state: &AppState, theme: &Theme, area: Rect) {
    use crate::ui::theme::THEME_NAMES;

    let count = THEME_NAMES.len();
    let height = (count as u16 + 2).min(area.height);
    let width = 30_u16.min(area.width);
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width, height);

    frame.render_widget(ratatui::widgets::Clear, popup);

    let block = Block::default()
        .title(" Theme ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.accent))
        .style(Style::default().bg(theme.dialog_bg));

    let inner = block.inner(popup);
    frame.render_widget(block, popup);

    let items: Vec<ratatui::widgets::ListItem> = THEME_NAMES
        .iter()
        .map(|name| {
            let is_current = theme.name == *name;
            let icon = if is_current { "\u{25cf} " } else { "  " };
            ratatui::widgets::ListItem::new(Line::from(vec![
                Span::styled(
                    icon,
                    Style::default().fg(if is_current {
                        theme.conn_connected
                    } else {
                        theme.dim
                    }),
                ),
                Span::styled(*name, Style::default().fg(theme.topbar_fg)),
            ]))
        })
        .collect();

    let mut list_state = ratatui::widgets::ListState::default();
    list_state.select(Some(state.dialogs.theme_picker.cursor));

    let list = ratatui::widgets::List::new(items)
        .highlight_style(
            Style::default()
                .bg(theme.tree_selected_bg)
                .fg(theme.tree_selected_fg)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("\u{25b8} ");

    frame.render_stateful_widget(list, inner, &mut list_state);
}

pub(super) fn render_export_dialog(frame: &mut Frame, state: &AppState, theme: &Theme, area: Rect) {
    let dialog = match &state.dialogs.export_dialog {
        Some(d) => d,
        None => return,
    };

    let width = 56u16.min(area.width.saturating_sub(4));
    let height = 13u16.min(area.height.saturating_sub(2));
    let x = area.x + (area.width.saturating_sub(width)) / 2;
    let y = area.y + (area.height.saturating_sub(height)) / 2;
    let dialog_area = Rect::new(x, y, width, height);

    let border_color = if dialog.error.is_some() {
        theme.error_fg
    } else {
        theme.border_focused
    };
    // Clear the area behind the dialog and paint an opaque background so
    // the editor/grid underneath doesn't bleed through.
    frame.render_widget(ratatui::widgets::Clear, dialog_area);
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .title(" Export ")
        .style(Style::default().bg(theme.dialog_bg));
    let inner_block = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);

    let inner = inner_block;
    let mut lines = Vec::new();
    let focused_fg = theme.border_focused;
    let normal_fg = theme.status_fg;

    // Error line
    if let Some(ref err) = dialog.error {
        lines.push(Line::from(Span::styled(
            err.as_str(),
            Style::default().fg(theme.error_fg),
        )));
    }

    // Path
    let path_fg = if dialog.focused == ExportField::Path {
        focused_fg
    } else {
        normal_fg
    };
    lines.push(Line::from(vec![
        Span::styled("Path: ", Style::default().fg(path_fg)),
        Span::styled(dialog.path.as_str(), Style::default().fg(path_fg)),
        if dialog.focused == ExportField::Path {
            Span::styled("\u{2588}", Style::default().fg(path_fg))
        } else {
            Span::raw("")
        },
    ]));

    // Include credentials
    let cred_fg = if dialog.focused == ExportField::IncludeCredentials {
        focused_fg
    } else {
        normal_fg
    };
    let cred_val = if dialog.include_credentials {
        "[Y] Yes"
    } else {
        "[N] No "
    };
    lines.push(Line::from(vec![
        Span::styled("Include credentials? ", Style::default().fg(cred_fg)),
        Span::styled(
            cred_val,
            Style::default().fg(cred_fg).add_modifier(Modifier::BOLD),
        ),
    ]));

    // Show password toggle
    let sp_fg = if dialog.focused == ExportField::ShowPassword {
        focused_fg
    } else {
        normal_fg
    };
    let sp_val = if dialog.show_password {
        "[Y] Yes"
    } else {
        "[N] No "
    };
    lines.push(Line::from(vec![
        Span::styled("Show password?      ", Style::default().fg(sp_fg)),
        Span::styled(
            sp_val,
            Style::default().fg(sp_fg).add_modifier(Modifier::BOLD),
        ),
    ]));

    // Password
    let pw_fg = if dialog.focused == ExportField::Password {
        focused_fg
    } else {
        normal_fg
    };
    let pw_display = if dialog.show_password {
        dialog.password.clone()
    } else {
        "*".repeat(dialog.password.len())
    };
    lines.push(Line::from(vec![
        Span::styled("Password: ", Style::default().fg(pw_fg)),
        Span::styled(pw_display, Style::default().fg(pw_fg)),
        if dialog.focused == ExportField::Password {
            Span::styled("\u{2588}", Style::default().fg(pw_fg))
        } else {
            Span::raw("")
        },
    ]));

    // Confirm
    let cf_fg = if dialog.focused == ExportField::Confirm {
        focused_fg
    } else {
        normal_fg
    };
    let cf_display = if dialog.show_password {
        dialog.confirm.clone()
    } else {
        "*".repeat(dialog.confirm.len())
    };
    lines.push(Line::from(vec![
        Span::styled("Confirm:  ", Style::default().fg(cf_fg)),
        Span::styled(cf_display, Style::default().fg(cf_fg)),
        if dialog.focused == ExportField::Confirm {
            Span::styled("\u{2588}", Style::default().fg(cf_fg))
        } else {
            Span::raw("")
        },
    ]));

    // Footer
    lines.push(Line::raw(""));
    lines.push(Line::from(vec![
        Span::styled("[Enter] Export  ", Style::default().fg(normal_fg)),
        Span::styled("[Esc] Cancel  ", Style::default().fg(normal_fg)),
        Span::styled("[Tab] Next field", Style::default().fg(normal_fg)),
    ]));

    let content = Paragraph::new(lines);
    frame.render_widget(content, inner);
}

pub(super) fn render_import_dialog(frame: &mut Frame, state: &AppState, theme: &Theme, area: Rect) {
    let dialog = match &state.dialogs.import_dialog {
        Some(d) => d,
        None => return,
    };

    let width = 56u16.min(area.width.saturating_sub(4));
    let height = 10u16.min(area.height.saturating_sub(2));
    let x = area.x + (area.width.saturating_sub(width)) / 2;
    let y = area.y + (area.height.saturating_sub(height)) / 2;
    let dialog_area = Rect::new(x, y, width, height);

    let border_color = if dialog.error.is_some() {
        theme.error_fg
    } else {
        theme.border_focused
    };
    // Clear + opaque background so the editor underneath doesn't bleed
    // through the dialog.
    frame.render_widget(ratatui::widgets::Clear, dialog_area);
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .title(" Import ")
        .style(Style::default().bg(theme.dialog_bg));
    let inner = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);
    let mut lines = Vec::new();
    let focused_fg = theme.border_focused;
    let normal_fg = theme.status_fg;

    // Error line
    if let Some(ref err) = dialog.error {
        lines.push(Line::from(Span::styled(
            err.as_str(),
            Style::default().fg(theme.error_fg),
        )));
    }

    // File path
    let path_fg = if dialog.focused == ImportField::Path {
        focused_fg
    } else {
        normal_fg
    };
    lines.push(Line::from(vec![
        Span::styled("File: ", Style::default().fg(path_fg)),
        Span::styled(dialog.path.as_str(), Style::default().fg(path_fg)),
        if dialog.focused == ImportField::Path {
            Span::styled("\u{2588}", Style::default().fg(path_fg))
        } else {
            Span::raw("")
        },
    ]));

    // Show password toggle
    let sp_fg = if dialog.focused == ImportField::ShowPassword {
        focused_fg
    } else {
        normal_fg
    };
    let sp_val = if dialog.show_password {
        "[Y] Yes"
    } else {
        "[N] No "
    };
    lines.push(Line::from(vec![
        Span::styled("Show password?  ", Style::default().fg(sp_fg)),
        Span::styled(
            sp_val,
            Style::default().fg(sp_fg).add_modifier(Modifier::BOLD),
        ),
    ]));

    // Password
    let pw_fg = if dialog.focused == ImportField::Password {
        focused_fg
    } else {
        normal_fg
    };
    let pw_display = if dialog.show_password {
        dialog.password.clone()
    } else {
        "*".repeat(dialog.password.len())
    };
    lines.push(Line::from(vec![
        Span::styled("Password: ", Style::default().fg(pw_fg)),
        Span::styled(pw_display, Style::default().fg(pw_fg)),
        if dialog.focused == ImportField::Password {
            Span::styled("\u{2588}", Style::default().fg(pw_fg))
        } else {
            Span::raw("")
        },
    ]));

    // Footer
    lines.push(Line::raw(""));
    lines.push(Line::from(vec![
        Span::styled("[Enter] Import  ", Style::default().fg(normal_fg)),
        Span::styled("[Esc] Cancel  ", Style::default().fg(normal_fg)),
        Span::styled("[Tab] Next field", Style::default().fg(normal_fg)),
    ]));

    let content = Paragraph::new(lines);
    frame.render_widget(content, inner);
}

pub(super) fn render_leader_help(
    frame: &mut Frame,
    state: &AppState,
    theme: &Theme,
    area: Rect,
    level: usize,
) {
    use crate::keybindings::Context;
    use ratatui::style::Color;

    let key_style = Style::default()
        .fg(Color::Yellow)
        .add_modifier(Modifier::BOLD);
    let desc_style = Style::default().fg(theme.status_fg);
    let header_style = Style::default()
        .fg(theme.accent)
        .add_modifier(Modifier::BOLD);

    // Read the configured key for (context, action) at render time so the
    // popup always reflects the user's current keybindings.toml.
    let pk = |ctx: Context, action: &str| state.bindings.primary_key(ctx, action);
    let owned: Vec<(String, &str)>;
    let title: &str;
    match level {
        2 => {
            title = "Leader > b";
            owned = vec![(pk(Context::LeaderBuffer, "close_tab"), "close buffer")];
        }
        3 => {
            title = "Leader > Leader";
            // <leader><leader>s is hardcoded (compile_to_db isn't in any context map).
            owned = vec![("s".to_string(), "compile to DB")];
        }
        4 => {
            title = "Leader > w";
            owned = vec![(pk(Context::LeaderWindow, "close_group"), "close group")];
        }
        5 => {
            title = "Leader > s";
            owned = vec![
                (pk(Context::LeaderSnippet, "snippet_select"), "SELECT"),
                (pk(Context::LeaderSnippet, "snippet_update"), "UPDATE"),
                (pk(Context::LeaderSnippet, "snippet_delete"), "DELETE"),
                (
                    pk(Context::LeaderSnippet, "snippet_call_proc"),
                    "CALL/EXEC proc",
                ),
                (
                    pk(Context::LeaderSnippet, "snippet_select_func"),
                    "SELECT func",
                ),
                (
                    pk(Context::LeaderSnippet, "snippet_create_table"),
                    "CREATE TABLE",
                ),
            ];
        }
        6 => {
            title = "Leader > f";
            owned = vec![
                (
                    pk(Context::LeaderFile, "export_connections"),
                    "export connections",
                ),
                (
                    pk(Context::LeaderFile, "import_connections"),
                    "import connections",
                ),
            ];
        }
        7 => {
            title = "Leader > q";
            owned = vec![(pk(Context::LeaderQuit, "quit_app"), "quit app")];
        }
        _ => {
            title = "Leader (Space)";
            owned = vec![
                (pk(Context::Leader, "execute_query"), "execute query"),
                (
                    pk(Context::Leader, "execute_query_new_tab"),
                    "execute \u{2192} new tab",
                ),
                (pk(Context::Leader, "toggle_sidebar"), "toggle sidebar"),
                (pk(Context::Leader, "vertical_split"), "vertical split"),
                (
                    pk(Context::Leader, "move_tab_to_other_group"),
                    "move tab to other group",
                ),
                (
                    pk(Context::Leader, "open_script_connection_picker"),
                    "connection",
                ),
                (pk(Context::Leader, "open_theme_picker"), "theme"),
                (pk(Context::Leader, "toggle_diagnostic_list"), "diagnostics"),
                (
                    pk(Context::Leader, "open_file_submenu"),
                    "+file (export/import)",
                ),
                (pk(Context::Leader, "open_quit_submenu"), "+quit..."),
                (pk(Context::Leader, "open_snippet_submenu"), "+snippets..."),
                (pk(Context::Leader, "open_buffer_submenu"), "+buffer..."),
                (
                    pk(Context::Leader, "open_window_submenu"),
                    "+close group...",
                ),
                ("Spc".to_string(), "+compile..."),
            ];
        }
    };
    let entries: Vec<(&str, &str)> = owned.iter().map(|(k, d)| (k.as_str(), *d)).collect();

    let mut lines = vec![
        Line::from(Span::styled(format!(" {title}"), header_style)),
        Line::from(""),
    ];
    for (key, desc) in &entries {
        lines.push(Line::from(vec![
            Span::styled(format!("  {key:<8}  "), key_style),
            Span::styled(*desc, desc_style),
        ]));
    }

    let height = (lines.len() as u16 + 2).min(area.height);
    let width = 28_u16.min(area.width);
    let x = area.width.saturating_sub(width + 1);
    let y = area.height.saturating_sub(height + 2);
    let popup = Rect::new(x, y, width, height);

    frame.render_widget(ratatui::widgets::Clear, popup);

    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.border_focused))
        .style(Style::default().bg(Color::Reset));

    let content = Paragraph::new(lines).block(block);
    frame.render_widget(content, popup);
}

pub(super) fn render_script_conn_picker(
    frame: &mut Frame,
    state: &AppState,
    theme: &Theme,
    area: Rect,
) {
    use crate::ui::state::PickerItem;

    let picker = match &state.dialogs.script_conn_picker {
        Some(p) => p,
        None => return,
    };

    let visible = picker.visible_items();
    let count = visible.len();
    let height = (count as u16 + 2).min(14).min(area.height);
    let width = 38_u16.min(area.width);
    let x = area.width.saturating_sub(width) / 2;
    let y = area.height.saturating_sub(height) / 2;
    let popup = Rect::new(x, y, width, height);

    frame.render_widget(ratatui::widgets::Clear, popup);

    let block = Block::default()
        .title(" Select Connection ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme.accent))
        .style(Style::default().bg(theme.dialog_bg));

    let inner = block.inner(popup);
    frame.render_widget(block, popup);

    let items: Vec<ratatui::widgets::ListItem> = visible
        .iter()
        .map(|item| match item {
            PickerItem::Active(name) => ratatui::widgets::ListItem::new(Line::from(vec![
                Span::raw("  "),
                Span::styled("\u{25cf} ", Style::default().fg(theme.conn_connected)),
                Span::styled(name.as_str(), Style::default().fg(theme.topbar_fg)),
            ])),
            PickerItem::OthersHeader => {
                let arrow = if picker.others_expanded {
                    "\u{25bc}"
                } else {
                    "\u{25b6}"
                };
                ratatui::widgets::ListItem::new(Line::from(vec![
                    Span::raw("  "),
                    Span::styled(
                        format!("{arrow} Others"),
                        Style::default()
                            .fg(theme.dim)
                            .add_modifier(Modifier::ITALIC),
                    ),
                ]))
            }
            PickerItem::Other(name) => ratatui::widgets::ListItem::new(Line::from(vec![
                Span::raw("    "),
                Span::styled("\u{25cb} ", Style::default().fg(theme.dim)),
                Span::styled(name.as_str(), Style::default().fg(theme.dim)),
            ])),
        })
        .collect();

    let mut list_state = ratatui::widgets::ListState::default();
    list_state.select(Some(picker.cursor));

    let list = ratatui::widgets::List::new(items)
        .highlight_style(
            Style::default()
                .bg(theme.tree_selected_bg)
                .fg(theme.tree_selected_fg)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("\u{25b8} ");

    frame.render_stateful_widget(list, inner, &mut list_state);
}