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
use ratatui::style::{Color, Modifier, Style};

use crate::app::model::shared::theme_id::ThemeId;
use crate::app::policy::write::write_guardrails::RiskLevel;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatusTone {
    Success,
    Error,
    Warning,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ThemePalette {
    pub semantic: SemanticTokens,
    pub component: ComponentTokens,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SemanticTokens {
    pub surface: SurfaceTokens,
    pub text: TextTokens,
    pub status: StatusTokens,
    pub cursor: CursorTokens,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SurfaceTokens {
    pub focus_border: Color,
    pub unfocus_border: Color,
    pub highlight_border: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextTokens {
    pub primary: Color,
    pub secondary: Color,
    pub muted: Color,
    pub dim: Color,
    pub accent: Color,
    pub placeholder: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StatusTokens {
    pub success: Color,
    pub error: Color,
    pub warning: Color,
    pub pending: Color,
    pub medium_risk: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CursorTokens {
    pub fg: Color,
    pub bg: Color,
    pub text_fg: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ComponentTokens {
    pub modal: ModalTokens,
    pub navigation: NavigationTokens,
    pub editor: EditorTokens,
    pub table: TableTokens,
    pub feedback: FeedbackTokens,
    pub syntax: SyntaxTokens,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ModalTokens {
    pub title: Color,
    pub hint: Color,
    pub border: Color,
    pub border_highlight: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NavigationTokens {
    pub key_chip_bg: Color,
    pub key_chip_fg: Color,
    pub section_header: Color,
    pub scrollbar_active: Color,
    pub scrollbar_inactive: Color,
    pub tab_active: Color,
    pub tab_inactive: Color,
    pub active_indicator: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EditorTokens {
    pub current_line_bg: Color,
    pub completion_selected_bg: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TableTokens {
    pub result_row_active_bg: Color,
    pub result_cell_active_bg: Color,
    pub cell_edit_fg: Color,
    pub staged_delete_bg: Color,
    pub staged_delete_fg: Color,
    pub striped_row_bg: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FeedbackTokens {
    pub yank_flash_bg: Color,
    pub yank_flash_fg: Color,
    pub note_text: Color,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SyntaxTokens {
    pub sql_keyword: Color,
    pub sql_string: Color,
    pub sql_number: Color,
    pub sql_comment: Color,
    pub sql_operator: Color,
    pub sql_text: Color,
}

impl ThemePalette {
    pub fn risk_color(&self, level: RiskLevel) -> Color {
        match level {
            RiskLevel::Low => self.semantic.status.warning,
            RiskLevel::Medium => self.semantic.status.medium_risk,
            RiskLevel::High => self.semantic.status.error,
        }
    }

    pub fn modal_title_style(&self) -> Style {
        Style::default()
            .fg(self.component.modal.title)
            .add_modifier(Modifier::BOLD)
    }

    pub fn modal_hint_style(&self) -> Style {
        Style::default().fg(self.component.modal.hint)
    }

    pub fn panel_border_style(&self, focused: bool, highlight: bool) -> Style {
        let color = if focused {
            self.semantic.surface.focus_border
        } else if highlight {
            self.semantic.surface.highlight_border
        } else {
            self.semantic.surface.unfocus_border
        };
        Style::default().fg(color)
    }

    pub fn picker_selected_style(&self) -> Style {
        Style::default()
            .bg(self.component.editor.completion_selected_bg)
            .fg(self.semantic.text.primary)
            .add_modifier(Modifier::BOLD)
    }

    pub fn modal_input_border_style(&self, focused: bool, has_error: bool) -> Style {
        let color = if has_error {
            self.semantic.status.error
        } else if focused {
            self.component.modal.border_highlight
        } else {
            self.component.modal.border
        };
        Style::default().fg(color)
    }

    pub fn modal_border_style(&self) -> Style {
        Style::default().fg(self.component.modal.border)
    }

    pub fn status_style(&self, tone: StatusTone) -> Style {
        let color = match tone {
            StatusTone::Success => self.semantic.status.success,
            StatusTone::Error => self.semantic.status.error,
            StatusTone::Warning => self.semantic.status.warning,
        };
        Style::default().fg(color)
    }

    pub fn block_cursor_style(&self) -> Style {
        Style::default()
            .bg(self.semantic.cursor.bg)
            .fg(self.semantic.cursor.text_fg)
    }

    pub fn insert_cursor_style(&self) -> Style {
        Style::default().fg(self.semantic.cursor.fg)
    }
}

pub const DEFAULT_THEME: ThemePalette = ThemePalette {
    semantic: SemanticTokens {
        surface: SurfaceTokens {
            focus_border: Color::Rgb(0x97, 0xc9, 0xc3),
            unfocus_border: Color::Rgb(0x45, 0x47, 0x55),
            highlight_border: Color::Rgb(0xb0, 0xdd, 0xd8),
        },
        text: TextTokens {
            primary: Color::Rgb(0xe9, 0xdb, 0xdb),
            secondary: Color::Rgb(0xc0, 0xb8, 0xb8),
            muted: Color::Rgb(0x5c, 0x63, 0x70),
            dim: Color::Rgb(0x6a, 0x6e, 0x7a),
            accent: Color::Rgb(0xd4, 0xa4, 0x85),
            placeholder: Color::Rgb(0x5b, 0x5f, 0x6e),
        },
        status: StatusTokens {
            success: Color::Rgb(0x97, 0xc9, 0xc3),
            error: Color::Rgb(0xc4, 0x74, 0x6e),
            warning: Color::Rgb(0xe0, 0xaf, 0x68),
            pending: Color::Rgb(0xd4, 0xa0, 0x60),
            medium_risk: Color::Rgb(0xd4, 0x70, 0x50),
        },
        cursor: CursorTokens {
            fg: Color::White,
            bg: Color::White,
            text_fg: Color::Black,
        },
    },
    component: ComponentTokens {
        modal: ModalTokens {
            title: Color::Rgb(0xe9, 0xdb, 0xdb),
            hint: Color::Rgb(0xc0, 0xb8, 0xb0),
            border: Color::Rgb(0x70, 0x68, 0x60),
            border_highlight: Color::Rgb(0xc0, 0xb8, 0xb8),
        },
        navigation: NavigationTokens {
            key_chip_bg: Color::Rgb(0x3a, 0x3a, 0x4a),
            key_chip_fg: Color::Rgb(0xd4, 0xa4, 0x85),
            section_header: Color::Rgb(0x6a, 0xb8, 0x9a),
            scrollbar_active: Color::Rgb(0xc0, 0xb8, 0xb0),
            scrollbar_inactive: Color::Rgb(0x50, 0x52, 0x5e),
            tab_active: Color::Rgb(0xd0, 0xc0, 0xa0),
            tab_inactive: Color::Rgb(0x5b, 0x5f, 0x6e),
            active_indicator: Color::Rgb(0xff, 0xff, 0xff),
        },
        editor: EditorTokens {
            current_line_bg: Color::Rgb(0x22, 0x26, 0x33),
            completion_selected_bg: Color::Rgb(0x45, 0x47, 0x5a),
        },
        table: TableTokens {
            result_row_active_bg: Color::Rgb(0x2e, 0x2e, 0x44),
            result_cell_active_bg: Color::Rgb(0x3a, 0x3a, 0x5a),
            cell_edit_fg: Color::Rgb(0xa8, 0xb8, 0xb5),
            staged_delete_bg: Color::Rgb(0x3d, 0x22, 0x22),
            staged_delete_fg: Color::Rgb(0xee, 0x77, 0x77),
            striped_row_bg: Color::Rgb(0x1e, 0x1e, 0x23),
        },
        feedback: FeedbackTokens {
            yank_flash_bg: Color::Rgb(0xF4, 0x9E, 0x4C),
            yank_flash_fg: Color::Rgb(0x11, 0x14, 0x19),
            note_text: Color::Rgb(0x66, 0x66, 0x77),
        },
        syntax: SyntaxTokens {
            sql_keyword: Color::Rgb(0x80, 0x90, 0xa8),
            sql_string: Color::Rgb(0xcd, 0xc8, 0xdb),
            sql_number: Color::Rgb(0xd4, 0xa4, 0x85),
            sql_comment: Color::Rgb(0x62, 0x72, 0xa4),
            sql_operator: Color::Rgb(0x8a, 0x91, 0xa5),
            sql_text: Color::Rgb(0xe9, 0xdb, 0xdb),
        },
    },
};

#[cfg(any(test, feature = "test-support"))]
pub const TEST_CONTRAST_THEME: ThemePalette = ThemePalette {
    semantic: SemanticTokens {
        surface: SurfaceTokens {
            focus_border: Color::Rgb(0x2f, 0xc4, 0xb2),
            unfocus_border: Color::Rgb(0x5d, 0x62, 0x74),
            highlight_border: Color::Rgb(0xff, 0xc8, 0x57),
        },
        text: TextTokens {
            primary: Color::Rgb(0xf6, 0xf0, 0xe8),
            secondary: Color::Rgb(0xc9, 0xd6, 0xdf),
            muted: Color::Rgb(0x92, 0xb3, 0xc2),
            dim: Color::Rgb(0x6a, 0x85, 0x95),
            accent: Color::Rgb(0xff, 0xc8, 0x57),
            placeholder: Color::Rgb(0x92, 0xb3, 0xc2),
        },
        status: StatusTokens {
            success: Color::Rgb(0x7b, 0xe0, 0x73),
            error: Color::Rgb(0xff, 0x7a, 0x59),
            warning: Color::Rgb(0xff, 0xc8, 0x57),
            pending: Color::Rgb(0xff, 0x9f, 0x1c),
            medium_risk: Color::Rgb(0xff, 0x9f, 0x1c),
        },
        cursor: CursorTokens {
            fg: Color::Rgb(0xff, 0xf4, 0xe0),
            bg: Color::Rgb(0xff, 0xf4, 0xe0),
            text_fg: Color::Rgb(0x0d, 0x11, 0x18),
        },
    },
    component: ComponentTokens {
        modal: ModalTokens {
            title: Color::Rgb(0xf6, 0xf0, 0xe8),
            hint: Color::Rgb(0x7b, 0xe0, 0x73),
            border: Color::Rgb(0xd8, 0x2a, 0x1f),
            border_highlight: Color::Rgb(0xff, 0xe0, 0x66),
        },
        navigation: NavigationTokens {
            key_chip_bg: Color::Rgb(0x1a, 0x45, 0x5e),
            key_chip_fg: Color::Rgb(0xff, 0xe0, 0x66),
            section_header: Color::Rgb(0x2f, 0xc4, 0xb2),
            scrollbar_active: Color::Rgb(0x2f, 0xc4, 0xb2),
            scrollbar_inactive: Color::Rgb(0x5d, 0x62, 0x74),
            tab_active: Color::Rgb(0x2f, 0xc4, 0xb2),
            tab_inactive: Color::Rgb(0x92, 0xb3, 0xc2),
            active_indicator: Color::Rgb(0x2f, 0xc4, 0xb2),
        },
        editor: EditorTokens {
            current_line_bg: Color::Rgb(0x1d, 0x2d, 0x3f),
            completion_selected_bg: Color::Rgb(0x2d, 0x5d, 0x46),
        },
        table: TableTokens {
            result_row_active_bg: Color::Rgb(0x2b, 0x32, 0x54),
            result_cell_active_bg: Color::Rgb(0x3a, 0x44, 0x6e),
            cell_edit_fg: Color::Rgb(0xff, 0xe0, 0x66),
            staged_delete_bg: Color::Rgb(0x4a, 0x1f, 0x1f),
            staged_delete_fg: Color::Rgb(0xff, 0x7a, 0x59),
            striped_row_bg: Color::Rgb(0x1d, 0x21, 0x2b),
        },
        feedback: FeedbackTokens {
            yank_flash_bg: Color::Rgb(0xff, 0xc8, 0x57),
            yank_flash_fg: Color::Rgb(0x14, 0x17, 0x21),
            note_text: Color::Rgb(0x92, 0xb3, 0xc2),
        },
        syntax: SyntaxTokens {
            sql_keyword: Color::Rgb(0x7d, 0xc4, 0xff),
            sql_string: Color::Rgb(0x9b, 0xf0, 0x8f),
            sql_number: Color::Rgb(0xff, 0xb8, 0x6b),
            sql_comment: Color::Rgb(0x7c, 0x8a, 0xa5),
            sql_operator: Color::Rgb(0x5e, 0xe0, 0xd5),
            sql_text: Color::Rgb(0xf6, 0xf0, 0xe8),
        },
    },
};

pub fn palette_for(theme_id: ThemeId) -> &'static ThemePalette {
    match theme_id {
        ThemeId::Default => &DEFAULT_THEME,
        #[cfg(any(test, feature = "test-support"))]
        ThemeId::TestContrast => &TEST_CONTRAST_THEME,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn palette_for_default_returns_default_theme() {
        assert_eq!(palette_for(ThemeId::Default), &DEFAULT_THEME);
    }

    #[test]
    fn palette_for_test_contrast_returns_test_theme() {
        assert_eq!(palette_for(ThemeId::TestContrast), &TEST_CONTRAST_THEME);
    }

    #[test]
    fn panel_border_style_prefers_focus_over_highlight() {
        let style = DEFAULT_THEME.panel_border_style(true, true);

        assert_eq!(style.fg, Some(DEFAULT_THEME.semantic.surface.focus_border));
    }

    #[test]
    fn picker_selected_style_uses_selected_colors() {
        let style = DEFAULT_THEME.picker_selected_style();

        assert_eq!(
            style.bg,
            Some(DEFAULT_THEME.component.editor.completion_selected_bg)
        );
        assert_eq!(style.fg, Some(DEFAULT_THEME.semantic.text.primary));
        assert!(style.add_modifier.contains(Modifier::BOLD));
    }

    #[test]
    fn modal_input_border_style_prefers_error_over_focus() {
        let style = DEFAULT_THEME.modal_input_border_style(true, true);

        assert_eq!(style.fg, Some(DEFAULT_THEME.semantic.status.error));
    }

    #[test]
    fn status_style_uses_requested_tone() {
        let style = DEFAULT_THEME.status_style(StatusTone::Warning);

        assert_eq!(style.fg, Some(DEFAULT_THEME.semantic.status.warning));
    }

    #[test]
    fn modal_hint_style_uses_hint_token_without_bold() {
        let style = DEFAULT_THEME.modal_hint_style();

        assert_eq!(style.fg, Some(DEFAULT_THEME.component.modal.hint));
        assert!(!style.add_modifier.contains(Modifier::BOLD));
    }

    #[test]
    fn modal_border_style_uses_component_modal_border() {
        let style = DEFAULT_THEME.modal_border_style();

        assert_eq!(style.fg, Some(DEFAULT_THEME.component.modal.border));
    }

    #[test]
    fn block_cursor_style_uses_semantic_cursor_colors() {
        let style = DEFAULT_THEME.block_cursor_style();

        assert_eq!(style.bg, Some(DEFAULT_THEME.semantic.cursor.bg));
        assert_eq!(style.fg, Some(DEFAULT_THEME.semantic.cursor.text_fg));
    }
}