dbtui 0.1.0

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
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use ratatui::Frame;
use unicode_width::UnicodeWidthStr;

use super::buffer::VimEditor;
use super::{VimMode, VisualKind};
use crate::ui::theme::Theme;

/// Visual selection range: ((start_row, start_col), (end_row, end_col))
type VisualRange = Option<((usize, usize), (usize, usize))>;

const SQL_KEYWORDS: &[&str] = &[
    "SELECT", "FROM", "WHERE", "INSERT", "INTO", "UPDATE", "DELETE", "SET",
    "JOIN", "LEFT", "RIGHT", "INNER", "OUTER", "FULL", "CROSS", "ON",
    "AND", "OR", "NOT", "IN", "IS", "NULL", "LIKE", "BETWEEN", "EXISTS",
    "AS", "ORDER", "BY", "GROUP", "HAVING", "LIMIT", "OFFSET", "DISTINCT",
    "UNION", "ALL", "CREATE", "ALTER", "DROP", "TABLE", "INDEX", "VIEW",
    "BEGIN", "END", "COMMIT", "ROLLBACK", "DECLARE", "CURSOR", "FETCH",
    "CASE", "WHEN", "THEN", "ELSE", "ASC", "DESC", "COUNT", "SUM", "AVG",
    "MAX", "MIN", "PROCEDURE", "FUNCTION", "PACKAGE", "BODY", "REPLACE",
    "VALUES", "WITH", "RECURSIVE", "TRIGGER", "GRANT", "REVOKE",
    "TYPE", "RETURN", "IF", "ELSIF", "LOOP", "FOR", "WHILE", "EXIT",
    "EXCEPTION", "RAISE", "PRAGMA", "EXECUTE", "IMMEDIATE", "BULK",
    "COLLECT", "FORALL", "OPEN", "CLOSE", "DBMS_OUTPUT", "PUT_LINE",
];

pub fn render(
    frame: &mut Frame,
    editor: &mut VimEditor,
    focused: bool,
    theme: &Theme,
    area: Rect,
    title: &str,
) {
    render_with_options(frame, editor, focused, theme, area, title, None);
}

pub fn render_with_options(
    frame: &mut Frame,
    editor: &mut VimEditor,
    focused: bool,
    theme: &Theme,
    area: Rect,
    title: &str,
    border_override: Option<ratatui::style::Color>,
) {
    // Update visible height based on area (minus borders and command line)
    editor.visible_height = area.height.saturating_sub(3) as usize;

    let default_border = if !focused {
        theme.border_unfocused
    } else {
        match editor.mode {
            VimMode::Insert => theme.border_insert,
            _ => theme.border_focused,
        }
    };
    let border_color = border_override.unwrap_or(default_border);

    let block = Block::default()
        .title(format!(" {} ", title))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .style(Style::default().bg(theme.editor_bg));

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

    if inner.height < 2 {
        return;
    }

    // Content area and command line area
    let content_height = inner.height.saturating_sub(1) as usize;
    let full_width = inner.width as usize;
    let cmd_area = Rect {
        x: inner.x,
        y: inner.y + inner.height - 1,
        width: inner.width,
        height: 1,
    };

    // Compute visual selection range
    let visual_range = if let VimMode::Visual(_) = &editor.mode {
        editor.visual_range()
    } else {
        None
    };
    let visual_kind = match &editor.mode {
        VimMode::Visual(k) => Some(k.clone()),
        _ => None,
    };

    // Render lines — each line is padded to full widget width to prevent ghosting
    let line_count_width = format!("{}", editor.lines.len()).len().max(3);
    let bg_style = Style::default().bg(theme.editor_bg);
    let num_col_width = line_count_width + 2; // digits + 2 spaces
    let available_text_width = full_width.saturating_sub(num_col_width);

    // Pre-truncate lines that exceed available width so their storage
    // outlives the spans that borrow from them.
    let mut truncated_cache: Vec<Option<String>> = Vec::with_capacity(content_height);
    for screen_row in 0..content_height {
        let line_idx = editor.scroll_offset + screen_row;
        if line_idx < editor.lines.len() {
            let line_text = &editor.lines[line_idx];
            let tw = UnicodeWidthStr::width(line_text.as_str());
            if tw > available_text_width {
                truncated_cache.push(Some(truncate_to_width(line_text, available_text_width)));
            } else {
                truncated_cache.push(None);
            }
        } else {
            truncated_cache.push(None);
        }
    }

    let mut rendered_lines: Vec<Line> = Vec::with_capacity(content_height);

    for (screen_row, cached) in truncated_cache.iter().enumerate() {
        let line_idx = editor.scroll_offset + screen_row;
        if line_idx >= editor.lines.len() {
            // Tilde for empty lines past end of file
            let prefix = format!("{:>width$}  ", "~", width = line_count_width);
            let used = prefix.len();
            let mut spans = vec![
                Span::styled(prefix, Style::default().fg(theme.dim)),
            ];
            // Pad to fill entire width
            if used < full_width {
                spans.push(Span::styled(" ".repeat(full_width - used), bg_style));
            }
            rendered_lines.push(Line::from(spans));
            continue;
        }

        let is_cursor_line = line_idx == editor.cursor_row && focused;

        // Use truncated text if the line exceeds viewport width
        let render_text: &str = match cached {
            Some(t) => t.as_str(),
            None => editor.lines[line_idx].as_str(),
        };

        // Relative line numbers (like nvim set relativenumber + number)
        let line_num = if is_cursor_line {
            format!("{:>width$}  ", line_idx + 1, width = line_count_width)
        } else {
            let distance = line_idx.abs_diff(editor.cursor_row);
            format!("{:>width$}  ", distance, width = line_count_width)
        };
        let num_style = if is_cursor_line {
            Style::default()
                .fg(theme.editor_line_nr_active)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(theme.editor_line_nr)
        };

        let num_len = line_num.len();
        let mut spans: Vec<Span> = vec![Span::styled(line_num, num_style)];

        // Check if this line has visual selection
        let line_visual = compute_line_visual(
            line_idx,
            render_text.len(),
            &visual_range,
            &visual_kind,
        );

        if let Some((vis_start, vis_end)) = line_visual {
            render_line_with_visual(render_text, vis_start, vis_end, theme, &mut spans);
        } else {
            highlight_sql_line(render_text, theme, &mut spans);
        }

        // Pad to fill entire width — every line MUST cover full_width
        let used = num_len + UnicodeWidthStr::width(render_text);
        if used < full_width {
            spans.push(Span::styled(" ".repeat(full_width - used), bg_style));
        }

        // Cursor rendering (if on this line and focused)
        if is_cursor_line && focused {
            let cursor_screen_col =
                (line_count_width + 2 + editor.cursor_col) as u16;
            let cursor_screen_row = screen_row as u16;
            frame.set_cursor(
                inner.x + cursor_screen_col,
                inner.y + cursor_screen_row,
            );
        }

        rendered_lines.push(Line::from(spans));
    }

    let content = Paragraph::new(rendered_lines)
        .style(bg_style);
    let content_area = Rect {
        x: inner.x,
        y: inner.y,
        width: inner.width,
        height: inner.height.saturating_sub(1),
    };
    frame.render_widget(Clear, content_area);
    frame.render_widget(content, content_area);

    // Command line (padded to full width)
    let cmd_text = if !editor.command_line.is_empty() {
        editor.command_line.clone()
    } else {
        format!(
            " {}:{} ",
            editor.cursor_row + 1,
            editor.cursor_col + 1
        )
    };
    let cmd_style = if !editor.command_line.is_empty() {
        Style::default().fg(theme.accent)
    } else {
        Style::default().fg(theme.dim)
    };
    let cmd_used = UnicodeWidthStr::width(cmd_text.as_str());
    let mut cmd_spans = vec![Span::styled(cmd_text, cmd_style)];
    if cmd_used < full_width {
        cmd_spans.push(Span::styled(" ".repeat(full_width - cmd_used), bg_style));
    }
    let cmd_line = Paragraph::new(Line::from(cmd_spans))
        .style(bg_style);
    frame.render_widget(Clear, cmd_area);
    frame.render_widget(cmd_line, cmd_area);
}

fn compute_line_visual(
    line_idx: usize,
    line_len: usize,
    visual_range: &VisualRange,
    visual_kind: &Option<VisualKind>,
) -> Option<(usize, usize)> {
    let ((sr, sc), (er, ec)) = (*visual_range)?;
    let kind = visual_kind.as_ref()?;

    if line_idx < sr || line_idx > er {
        return None;
    }

    match kind {
        VisualKind::Line => Some((0, line_len)),
        VisualKind::Char => {
            if sr == er {
                Some((sc, (ec + 1).min(line_len)))
            } else if line_idx == sr {
                Some((sc, line_len))
            } else if line_idx == er {
                Some((0, (ec + 1).min(line_len)))
            } else {
                Some((0, line_len))
            }
        }
        VisualKind::Block => {
            let left = sc.min(ec);
            let right = (sc.max(ec) + 1).min(line_len);
            if left < right {
                Some((left, right))
            } else {
                None
            }
        }
    }
}

fn render_line_with_visual<'a>(
    line: &'a str,
    vis_start: usize,
    vis_end: usize,
    theme: &Theme,
    spans: &mut Vec<Span<'a>>,
) {
    let visual_style = Style::default()
        .bg(theme.tree_selected_bg)
        .fg(theme.tree_selected_fg);

    let len = line.len();
    let vs = vis_start.min(len);
    let ve = vis_end.min(len);

    if vs > 0 {
        highlight_sql_segment(&line[..vs], theme, spans);
    }
    if vs < ve {
        spans.push(Span::styled(&line[vs..ve], visual_style));
    }
    if ve < len {
        highlight_sql_segment(&line[ve..], theme, spans);
    }
    if line.is_empty() {
        // Show at least a highlighted space for empty selected lines
        spans.push(Span::styled(" ", visual_style));
    }
}

fn highlight_sql_segment<'a>(text: &'a str, theme: &Theme, spans: &mut Vec<Span<'a>>) {
    highlight_sql_line(text, theme, spans);
}

pub fn highlight_sql_line<'a>(line: &'a str, theme: &Theme, spans: &mut Vec<Span<'a>>) {
    if line.is_empty() {
        return;
    }

    // Check for line comment
    if let Some(comment_pos) = line.find("--") {
        if comment_pos > 0 {
            highlight_tokens(&line[..comment_pos], theme, spans);
        }
        spans.push(Span::styled(
            &line[comment_pos..],
            Style::default()
                .fg(theme.sql_comment)
                .add_modifier(Modifier::ITALIC),
        ));
        return;
    }

    highlight_tokens(line, theme, spans);
}

fn highlight_tokens<'a>(text: &'a str, theme: &Theme, spans: &mut Vec<Span<'a>>) {
    let mut remaining = text;

    while !remaining.is_empty() {
        // Skip leading whitespace
        if remaining.starts_with(|c: char| c.is_whitespace()) {
            let ws_end = remaining
                .find(|c: char| !c.is_whitespace())
                .unwrap_or(remaining.len());
            spans.push(Span::raw(&remaining[..ws_end]));
            remaining = &remaining[ws_end..];
            continue;
        }

        // String literal
        if remaining.starts_with('\'') {
            let end = remaining[1..]
                .find('\'')
                .map(|p| p + 2)
                .unwrap_or(remaining.len());
            spans.push(Span::styled(
                &remaining[..end],
                Style::default().fg(theme.sql_string),
            ));
            remaining = &remaining[end..];
            continue;
        }

        // Number
        if remaining.starts_with(|c: char| c.is_ascii_digit()) {
            let end = remaining
                .find(|c: char| !c.is_ascii_digit() && c != '.')
                .unwrap_or(remaining.len());
            spans.push(Span::styled(
                &remaining[..end],
                Style::default().fg(theme.sql_number),
            ));
            remaining = &remaining[end..];
            continue;
        }

        // Word (potential keyword or identifier)
        if remaining.starts_with(|c: char| c.is_alphanumeric() || c == '_') {
            let end = remaining
                .find(|c: char| !c.is_alphanumeric() && c != '_')
                .unwrap_or(remaining.len());
            let word = &remaining[..end];
            let upper = word.to_uppercase();

            if SQL_KEYWORDS.contains(&upper.as_str()) {
                spans.push(Span::styled(
                    word,
                    Style::default()
                        .fg(theme.sql_keyword)
                        .add_modifier(Modifier::BOLD),
                ));
            } else {
                spans.push(Span::raw(word));
            }
            remaining = &remaining[end..];
            continue;
        }

        // Operators and punctuation
        let end = remaining
            .find(|c: char| c.is_alphanumeric() || c == '_' || c == '\'' || c.is_whitespace())
            .unwrap_or(remaining.len())
            .max(1);
        spans.push(Span::styled(
            &remaining[..end],
            Style::default().fg(theme.sql_operator),
        ));
        remaining = &remaining[end..];
    }
}

/// Truncate a string to fit within `max_width` display cells.
/// Respects multi-byte character boundaries.
fn truncate_to_width(s: &str, max_width: usize) -> String {
    let mut width = 0;
    let mut end = 0;
    for (i, c) in s.char_indices() {
        let cw = unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
        if width + cw > max_width {
            break;
        }
        width += cw;
        end = i + c.len_utf8();
    }
    s[..end].to_string()
}