shellql 0.1.7-beta

A Vim- and tmux-inspired terminal database manager for developers
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
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
/// Add-connection form state — supports both URL and field-by-field Config modes.
use crate::connection::{
    ConnectionSource, DatabaseConnection, DatabaseString, Engine, MysqlConnection,
    PostgresConnection, SqliteConnection, SslOptions, SslVerifyMode,
};

// ── Text mode ────────────────────────────────────────────────────────────────

/// Vim-inspired input mode for text fields.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TextMode {
    /// Navigate with `h/l`, `0/$`, `x`; enter Insert with `i/a/I/A`.
    Normal,
    /// Type freely; `Esc` returns to Normal.
    Insert,
}

// ── Field identifiers ─────────────────────────────────────────────────────────

/// Every possible form field across all engines and input modes.
/// `visible_fields` returns the ordered subset that applies to the current state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldId {
    // Always visible
    Name,
    Engine,
    InputMode,
    // URL mode (all engines)
    Url,
    // Config mode — Postgres / MySQL
    Hostname,
    Port,
    Username,
    Password,
    Database,
    PoolSize,
    Ssl,
    // Config mode — SQLite
    SqlitePath,
    CreateIfMissing,
}

impl FieldId {
    pub fn label(&self) -> &'static str {
        match self {
            FieldId::Name => "Name",
            FieldId::Engine => "Engine",
            FieldId::InputMode => "Input",
            FieldId::Url => "URL",
            FieldId::Hostname => "Hostname",
            FieldId::Port => "Port",
            FieldId::Username => "Username",
            FieldId::Password => "Password",
            FieldId::Database => "Database",
            FieldId::PoolSize => "Pool size",
            FieldId::Ssl => "SSL",
            FieldId::SqlitePath => "Path",
            FieldId::CreateIfMissing => "Create DB",
        }
    }

    /// True for fields that accept free text input.
    pub fn is_text(&self) -> bool {
        matches!(
            self,
            FieldId::Name
                | FieldId::Url
                | FieldId::Hostname
                | FieldId::Port
                | FieldId::Username
                | FieldId::Password
                | FieldId::Database
                | FieldId::PoolSize
                | FieldId::SqlitePath
        )
    }
}

// ── Input mode ────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FormInputMode {
    Url,
    Config,
}

// ── Form state ────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct AddConnectionForm {
    /// Index into `visible_fields()` of the currently focused field.
    pub focused: usize,
    /// Char offset within the focused text field's buffer.
    pub cursor_pos: usize,
    /// Current input mode for text fields.
    pub text_mode: TextMode,
    pub input_mode: FormInputMode,
    pub engine: Engine,
    // ── Shared ────────────────────────────────────────────────────────────────
    pub name: String,

    // ── URL mode ──────────────────────────────────────────────────────────────
    pub url: String,

    // ── Config mode — Postgres / MySQL ────────────────────────────────────────
    pub hostname: String,
    pub port: String,
    pub username: String,
    pub password: String,
    pub database: String,
    pub pool_size: String,
    pub ssl_enabled: bool,

    // ── Config mode — SQLite ──────────────────────────────────────────────────
    pub sqlite_path: String,
    pub create_if_missing: bool,
}

impl AddConnectionForm {
    pub fn new() -> Self {
        Self {
            focused: 0,
            cursor_pos: 0,
            text_mode: TextMode::Normal,
            input_mode: FormInputMode::Url,
            engine: Engine::Postgres,
            name: String::new(),
            url: String::new(),
            hostname: String::new(),
            port: "5432".to_string(),
            username: String::new(),
            password: String::new(),
            database: String::new(),
            pool_size: "5".to_string(),
            ssl_enabled: false,
            sqlite_path: String::new(),
            create_if_missing: true,
        }
    }

    // ── Field list ────────────────────────────────────────────────────────────

    /// Ordered fields visible for the current engine + input mode.
    pub fn visible_fields(&self) -> Vec<FieldId> {
        let mut fields = vec![FieldId::Name, FieldId::Engine, FieldId::InputMode];

        match (&self.input_mode, &self.engine) {
            (FormInputMode::Url, _) => {
                fields.push(FieldId::Url);
            }
            (FormInputMode::Config, Engine::Postgres | Engine::Mysql) => {
                fields.extend([
                    FieldId::Hostname,
                    FieldId::Port,
                    FieldId::Username,
                    FieldId::Password,
                    FieldId::Database,
                    FieldId::PoolSize,
                    FieldId::Ssl,
                ]);
            }
            (FormInputMode::Config, Engine::Sqlite) => {
                fields.extend([
                    FieldId::SqlitePath,
                    FieldId::CreateIfMissing,
                    FieldId::PoolSize,
                ]);
            }
        }

        fields
    }

    /// The `FieldId` that currently has focus.
    pub fn focused_field(&self) -> FieldId {
        self.visible_fields()
            .into_iter()
            .nth(self.focused)
            .unwrap_or(FieldId::Name)
    }

    // ── Navigation ────────────────────────────────────────────────────────────

    pub fn focus_next(&mut self) {
        let n = self.visible_fields().len();
        self.focused = (self.focused + 1) % n;
        self.reset_text_state();
    }

    pub fn focus_prev(&mut self) {
        let n = self.visible_fields().len();
        self.focused = (self.focused + n - 1) % n;
        self.reset_text_state();
    }

    /// Reset cursor and mode when moving to a new field.
    /// Lands in Normal mode with the cursor on the last character.
    fn reset_text_state(&mut self) {
        self.text_mode = TextMode::Normal;
        // Place on the last char (vim Normal), or 0 for empty / non-text fields.
        let len = self.focused_text_len().unwrap_or(0);
        self.cursor_pos = len.saturating_sub(1);
    }

    // ── Text field access ─────────────────────────────────────────────────────

    /// Mutable reference to the text buffer of the focused field, if it is a
    /// text field. Returns `None` for selectors and toggles.
    pub fn current_text_mut(&mut self) -> Option<&mut String> {
        match self.focused_field() {
            FieldId::Name => Some(&mut self.name),
            FieldId::Url => Some(&mut self.url),
            FieldId::Hostname => Some(&mut self.hostname),
            FieldId::Port => Some(&mut self.port),
            FieldId::Username => Some(&mut self.username),
            FieldId::Password => Some(&mut self.password),
            FieldId::Database => Some(&mut self.database),
            FieldId::PoolSize => Some(&mut self.pool_size),
            FieldId::SqlitePath => Some(&mut self.sqlite_path),
            _ => None,
        }
    }

    /// Read-only text value for any given field (used by the renderer).
    pub fn text_for(&self, field: &FieldId) -> Option<&str> {
        match field {
            FieldId::Name => Some(&self.name),
            FieldId::Url => Some(&self.url),
            FieldId::Hostname => Some(&self.hostname),
            FieldId::Port => Some(&self.port),
            FieldId::Username => Some(&self.username),
            FieldId::Password => Some(&self.password),
            FieldId::Database => Some(&self.database),
            FieldId::PoolSize => Some(&self.pool_size),
            FieldId::SqlitePath => Some(&self.sqlite_path),
            _ => None,
        }
    }

    /// Character count of the focused field's text (used for cursor placement).
    /// Returns `None` if the focused field is not a text field.
    pub fn focused_text_len(&self) -> Option<usize> {
        let field = self.focused_field();
        if field == FieldId::Password {
            return Some(self.password.chars().count());
        }
        self.text_for(&field).map(|s| s.chars().count())
    }

    // ── Cursor movement ─────────────────────────────────────────────────────

    pub fn cursor_left(&mut self) {
        self.cursor_pos = self.cursor_pos.saturating_sub(1);
    }

    pub fn cursor_right(&mut self) {
        let len = self.focused_text_len().unwrap_or(0);
        let max = match self.text_mode {
            TextMode::Insert => len,
            TextMode::Normal => len.saturating_sub(1),
        };
        self.cursor_pos = (self.cursor_pos + 1).min(max);
    }

    pub fn cursor_to_start(&mut self) {
        self.cursor_pos = 0;
    }

    pub fn cursor_to_end(&mut self) {
        let len = self.focused_text_len().unwrap_or(0);
        self.cursor_pos = match self.text_mode {
            TextMode::Insert => len,
            TextMode::Normal => len.saturating_sub(1),
        };
    }

    // ── Mode transitions ────────────────────────────────────────────────────────

    /// `i` — insert before cursor.
    pub fn enter_insert_before(&mut self) {
        self.text_mode = TextMode::Insert;
    }

    /// `a` — insert after cursor.
    pub fn enter_insert_after(&mut self) {
        let len = self.focused_text_len().unwrap_or(0);
        self.cursor_pos = (self.cursor_pos + 1).min(len);
        self.text_mode = TextMode::Insert;
    }

    /// `I` — insert at start.
    pub fn enter_insert_at_start(&mut self) {
        self.cursor_pos = 0;
        self.text_mode = TextMode::Insert;
    }

    /// `A` — insert at end.
    pub fn enter_insert_at_end(&mut self) {
        self.cursor_pos = self.focused_text_len().unwrap_or(0);
        self.text_mode = TextMode::Insert;
    }

    /// `Esc` — return to Normal and clamp cursor to last char.
    pub fn enter_normal(&mut self) {
        self.text_mode = TextMode::Normal;
        let len = self.focused_text_len().unwrap_or(0);
        if len > 0 && self.cursor_pos >= len {
            self.cursor_pos = len - 1;
        }
    }

    // ── Editing ────────────────────────────────────────────────────────────────

    pub fn insert_char(&mut self, c: char) {
        let pos = self.cursor_pos;
        match self.focused_field() {
            FieldId::Name => insert_at(&mut self.name, pos, c),
            FieldId::Url => insert_at(&mut self.url, pos, c),
            FieldId::Hostname => insert_at(&mut self.hostname, pos, c),
            FieldId::Port => insert_at(&mut self.port, pos, c),
            FieldId::Username => insert_at(&mut self.username, pos, c),
            FieldId::Password => insert_at(&mut self.password, pos, c),
            FieldId::Database => insert_at(&mut self.database, pos, c),
            FieldId::PoolSize => insert_at(&mut self.pool_size, pos, c),
            FieldId::SqlitePath => insert_at(&mut self.sqlite_path, pos, c),
            _ => return,
        }
        self.cursor_pos += 1;
    }

    pub fn delete_before_cursor(&mut self) {
        if self.cursor_pos == 0 {
            return;
        }
        let pos = self.cursor_pos - 1;
        match self.focused_field() {
            FieldId::Name => {
                remove_at(&mut self.name, pos);
            }
            FieldId::Url => {
                remove_at(&mut self.url, pos);
            }
            FieldId::Hostname => {
                remove_at(&mut self.hostname, pos);
            }
            FieldId::Port => {
                remove_at(&mut self.port, pos);
            }
            FieldId::Username => {
                remove_at(&mut self.username, pos);
            }
            FieldId::Password => {
                remove_at(&mut self.password, pos);
            }
            FieldId::Database => {
                remove_at(&mut self.database, pos);
            }
            FieldId::PoolSize => {
                remove_at(&mut self.pool_size, pos);
            }
            FieldId::SqlitePath => {
                remove_at(&mut self.sqlite_path, pos);
            }
            _ => return,
        }
        self.cursor_pos -= 1;
    }

    pub fn delete_at_cursor(&mut self) {
        let pos = self.cursor_pos;
        let deleted = match self.focused_field() {
            FieldId::Name => remove_at(&mut self.name, pos),
            FieldId::Url => remove_at(&mut self.url, pos),
            FieldId::Hostname => remove_at(&mut self.hostname, pos),
            FieldId::Port => remove_at(&mut self.port, pos),
            FieldId::Username => remove_at(&mut self.username, pos),
            FieldId::Password => remove_at(&mut self.password, pos),
            FieldId::Database => remove_at(&mut self.database, pos),
            FieldId::PoolSize => remove_at(&mut self.pool_size, pos),
            FieldId::SqlitePath => remove_at(&mut self.sqlite_path, pos),
            _ => return,
        };
        if deleted {
            let new_len = self.focused_text_len().unwrap_or(0);
            if new_len == 0 {
                self.cursor_pos = 0;
            } else if self.cursor_pos >= new_len {
                self.cursor_pos = new_len - 1;
            }
        }
    }

    // ── Selector / toggle cycling ─────────────────────────────────────────────

    /// Cycle the focused selector or toggle one step to the right / forward.
    pub fn cycle_right(&mut self) {
        match self.focused_field() {
            FieldId::Engine => {
                self.engine = match self.engine {
                    Engine::Postgres => Engine::Mysql,
                    Engine::Mysql => Engine::Sqlite,
                    Engine::Sqlite => Engine::Postgres,
                };
                self.sync_port_default();
                self.clamp_focus();
            }
            FieldId::InputMode => {
                self.input_mode = match self.input_mode {
                    FormInputMode::Url => FormInputMode::Config,
                    FormInputMode::Config => FormInputMode::Url,
                };
                self.clamp_focus();
            }
            FieldId::Ssl => self.ssl_enabled = !self.ssl_enabled,
            FieldId::CreateIfMissing => self.create_if_missing = !self.create_if_missing,
            _ => {}
        }
    }

    /// Cycle the focused selector one step to the left / backward.
    pub fn cycle_left(&mut self) {
        match self.focused_field() {
            FieldId::Engine => {
                self.engine = match self.engine {
                    Engine::Postgres => Engine::Sqlite,
                    Engine::Mysql => Engine::Postgres,
                    Engine::Sqlite => Engine::Mysql,
                };
                self.sync_port_default();
                self.clamp_focus();
            }
            // Binary toggles and InputMode are symmetric — left == right.
            _ => self.cycle_right(),
        }
    }

    /// Toggle the focused boolean field (SSL, CreateIfMissing).
    pub fn toggle_focused(&mut self) {
        match self.focused_field() {
            FieldId::Ssl => self.ssl_enabled = !self.ssl_enabled,
            FieldId::CreateIfMissing => self.create_if_missing = !self.create_if_missing,
            FieldId::InputMode => self.cycle_right(),
            _ => {}
        }
    }

    // ── Internal helpers ──────────────────────────────────────────────────────

    /// Update the port default when the engine changes, but only if the port
    /// still holds the previous default value so manual edits are preserved.
    fn sync_port_default(&mut self) {
        if self.port == "5432" || self.port == "3306" {
            self.port = match self.engine {
                Engine::Postgres => "5432",
                Engine::Mysql => "3306",
                Engine::Sqlite => "",
            }
            .to_string();
        }
    }

    /// Ensure the focus index stays within bounds after the field list changes.
    fn clamp_focus(&mut self) {
        let n = self.visible_fields().len();
        if self.focused >= n {
            self.focused = n.saturating_sub(1);
        }
    }

    // ── Validation & build ────────────────────────────────────────────────────

    pub fn validate(&self) -> Result<(), String> {
        if self.name.trim().is_empty() {
            return Err("Name is required".to_string());
        }
        self.build_source().map(|_| ())
    }

    /// Convert the form into a `ConnectionSource` ready for `add_connection`.
    pub fn build_source(&self) -> Result<ConnectionSource, String> {
        match self.input_mode {
            FormInputMode::Url => {
                if self.url.trim().is_empty() {
                    return Err("URL is required".to_string());
                }
                Ok(match self.engine {
                    Engine::Postgres => {
                        ConnectionSource::Url(DatabaseString::Postgres(self.url.clone()))
                    }
                    Engine::Mysql => ConnectionSource::Url(DatabaseString::Mysql(self.url.clone())),
                    Engine::Sqlite => {
                        let abs = crate::connection::normalize_sqlite_path(&self.url)
                            .map_err(|e| e.to_string())?;
                        ConnectionSource::Url(DatabaseString::Sqlite(
                            crate::connection::build_sqlite_url(&abs),
                        ))
                    }
                })
            }

            FormInputMode::Config => {
                let pool = self
                    .pool_size
                    .parse::<i8>()
                    .map_err(|_| "Pool size must be a number".to_string())?;

                match self.engine {
                    Engine::Postgres | Engine::Mysql => {
                        if self.hostname.trim().is_empty() {
                            return Err("Hostname is required".to_string());
                        }
                        if self.username.trim().is_empty() {
                            return Err("Username is required".to_string());
                        }
                        if self.database.trim().is_empty() {
                            return Err("Database is required".to_string());
                        }
                        let port = self
                            .port
                            .parse::<i16>()
                            .map_err(|_| "Port must be a valid number".to_string())?;
                        let ssl = self.ssl_enabled.then_some(SslOptions {
                            verify: SslVerifyMode::Peer,
                            certfile: None,
                        });
                        let conn = match self.engine {
                            Engine::Postgres => DatabaseConnection::Postgres(PostgresConnection {
                                username: self.username.clone(),
                                password: self.password.clone(),
                                hostname: self.hostname.clone(),
                                database: self.database.clone(),
                                stack_trace: false,
                                port,
                                pool_size: pool,
                                ssl,
                            }),
                            Engine::Mysql => DatabaseConnection::Mysql(MysqlConnection {
                                username: self.username.clone(),
                                password: self.password.clone(),
                                hostname: self.hostname.clone(),
                                database: self.database.clone(),
                                stack_trace: false,
                                port,
                                pool_size: pool,
                                ssl,
                            }),
                            _ => unreachable!(),
                        };
                        Ok(ConnectionSource::Config(conn))
                    }

                    Engine::Sqlite => {
                        if self.sqlite_path.trim().is_empty() {
                            return Err("Path is required".to_string());
                        }
                        let abs = crate::connection::normalize_sqlite_path(&self.sqlite_path)
                            .map_err(|e| e.to_string())?;
                        Ok(ConnectionSource::Config(DatabaseConnection::Sqlite(
                            SqliteConnection {
                                path: abs,
                                stack_trace: false,
                                pool_size: pool,
                                create_if_missing: self.create_if_missing,
                            },
                        )))
                    }
                }
            }
        }
    }
}

// ── Module-level helpers ──────────────────────────────────────────────────────

fn char_to_byte(s: &str, char_idx: usize) -> usize {
    s.char_indices()
        .nth(char_idx)
        .map(|(i, _)| i)
        .unwrap_or(s.len())
}

fn insert_at(s: &mut String, char_idx: usize, c: char) {
    let byte = char_to_byte(s, char_idx);
    s.insert(byte, c);
}

/// Returns `true` if a character was removed.
fn remove_at(s: &mut String, char_idx: usize) -> bool {
    if char_idx >= s.chars().count() {
        return false;
    }
    let byte = char_to_byte(s, char_idx);
    s.remove(byte);
    true
}