sql-cli 1.72.0

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
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
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
// Pure text manipulation functions with no TUI dependencies
// These functions take text and cursor position, return results

/// Result of a text operation that modifies the text
#[derive(Debug, Clone)]
pub struct TextOperationResult {
    /// The new text after the operation
    pub new_text: String,
    /// The new cursor position after the operation
    pub new_cursor_position: usize,
    /// Text that was deleted/killed (for kill ring)
    pub killed_text: Option<String>,
    /// Description of what happened
    pub description: String,
}

/// Result of a cursor movement operation
#[derive(Debug, Clone)]
pub struct CursorMovementResult {
    /// The new cursor position
    pub new_position: usize,
    /// The word or token that was jumped over
    pub jumped_text: Option<String>,
}

// ========== Pure Text Manipulation Functions ==========

/// Kill text from cursor to end of line (Ctrl+K behavior)
#[must_use]
pub fn kill_line(text: &str, cursor_position: usize) -> TextOperationResult {
    let text_len = text.len();

    if cursor_position >= text_len {
        // Cursor at end, nothing to kill
        return TextOperationResult {
            new_text: text.to_string(),
            new_cursor_position: cursor_position,
            killed_text: None,
            description: "Nothing to kill".to_string(),
        };
    }

    // Find the end of the current line
    let line_end = text[cursor_position..]
        .find('\n')
        .map_or(text_len, |pos| cursor_position + pos);

    let killed = text[cursor_position..line_end].to_string();
    let mut new_text = String::with_capacity(text_len);
    new_text.push_str(&text[..cursor_position]);

    // If we're killing up to a newline, keep the newline
    if line_end < text_len && text.chars().nth(line_end) == Some('\n') {
        new_text.push('\n');
        new_text.push_str(&text[line_end + 1..]);
    } else {
        new_text.push_str(&text[line_end..]);
    }

    let killed_len = killed.len();
    TextOperationResult {
        new_text,
        new_cursor_position: cursor_position,
        killed_text: Some(killed),
        description: format!("Killed {killed_len} characters"),
    }
}

/// Kill text from beginning of line to cursor (Ctrl+U behavior)
#[must_use]
pub fn kill_line_backward(text: &str, cursor_position: usize) -> TextOperationResult {
    if cursor_position == 0 {
        // Cursor at start, nothing to kill
        return TextOperationResult {
            new_text: text.to_string(),
            new_cursor_position: 0,
            killed_text: None,
            description: "Nothing to kill".to_string(),
        };
    }

    // Find the start of the current line
    let line_start = text[..cursor_position].rfind('\n').map_or(0, |pos| pos + 1);

    let killed = text[line_start..cursor_position].to_string();
    let mut new_text = String::with_capacity(text.len());
    new_text.push_str(&text[..line_start]);
    new_text.push_str(&text[cursor_position..]);

    let killed_len = killed.len();
    TextOperationResult {
        new_text,
        new_cursor_position: line_start,
        killed_text: Some(killed),
        description: format!("Killed {killed_len} characters backward"),
    }
}

/// Delete word backward from cursor (Ctrl+W behavior)
#[must_use]
pub fn delete_word_backward(text: &str, cursor_position: usize) -> TextOperationResult {
    if cursor_position == 0 {
        return TextOperationResult {
            new_text: text.to_string(),
            new_cursor_position: 0,
            killed_text: None,
            description: "At beginning of text".to_string(),
        };
    }

    // Skip any trailing whitespace
    let mut pos = cursor_position;
    while pos > 0 && text.chars().nth(pos - 1).is_some_and(char::is_whitespace) {
        pos -= 1;
    }

    // Find the start of the word
    let word_start = if pos == 0 {
        0
    } else {
        let mut start = pos;
        while start > 0 && !text.chars().nth(start - 1).is_some_and(char::is_whitespace) {
            start -= 1;
        }
        start
    };

    let killed = text[word_start..cursor_position].to_string();
    let mut new_text = String::with_capacity(text.len());
    new_text.push_str(&text[..word_start]);
    new_text.push_str(&text[cursor_position..]);

    let killed_trimmed = killed.trim().to_string();
    TextOperationResult {
        new_text,
        new_cursor_position: word_start,
        killed_text: Some(killed),
        description: format!("Deleted word: '{killed_trimmed}'"),
    }
}

/// Delete word forward from cursor (Alt+D behavior)
#[must_use]
pub fn delete_word_forward(text: &str, cursor_position: usize) -> TextOperationResult {
    let text_len = text.len();
    if cursor_position >= text_len {
        return TextOperationResult {
            new_text: text.to_string(),
            new_cursor_position: cursor_position,
            killed_text: None,
            description: "At end of text".to_string(),
        };
    }

    // Skip any leading whitespace
    let mut pos = cursor_position;
    while pos < text_len && text.chars().nth(pos).is_some_and(char::is_whitespace) {
        pos += 1;
    }

    // Find the end of the word
    let word_end = if pos >= text_len {
        text_len
    } else {
        let mut end = pos;
        while end < text_len && !text.chars().nth(end).is_some_and(char::is_whitespace) {
            end += 1;
        }
        end
    };

    let killed = text[cursor_position..word_end].to_string();
    let mut new_text = String::with_capacity(text.len());
    new_text.push_str(&text[..cursor_position]);
    new_text.push_str(&text[word_end..]);

    let killed_trimmed = killed.trim().to_string();
    TextOperationResult {
        new_text,
        new_cursor_position: cursor_position,
        killed_text: Some(killed),
        description: format!("Deleted word: '{killed_trimmed}'"),
    }
}

// ========== Pure Cursor Movement Functions ==========

/// Move cursor backward one word (Ctrl+Left or Alt+B)
#[must_use]
pub fn move_word_backward(text: &str, cursor_position: usize) -> CursorMovementResult {
    if cursor_position == 0 {
        return CursorMovementResult {
            new_position: 0,
            jumped_text: None,
        };
    }

    // Skip any trailing whitespace
    let mut pos = cursor_position;
    while pos > 0 && text.chars().nth(pos - 1).is_some_and(char::is_whitespace) {
        pos -= 1;
    }

    // Find the start of the word
    let word_start = if pos == 0 {
        0
    } else {
        let mut start = pos;
        while start > 0 && !text.chars().nth(start - 1).is_some_and(char::is_whitespace) {
            start -= 1;
        }
        start
    };

    let jumped = if word_start < cursor_position {
        Some(text[word_start..cursor_position].to_string())
    } else {
        None
    };

    CursorMovementResult {
        new_position: word_start,
        jumped_text: jumped,
    }
}

/// Move cursor forward one word (Ctrl+Right or Alt+F)
#[must_use]
pub fn move_word_forward(text: &str, cursor_position: usize) -> CursorMovementResult {
    let text_len = text.len();
    if cursor_position >= text_len {
        return CursorMovementResult {
            new_position: cursor_position,
            jumped_text: None,
        };
    }

    // Skip current word
    let mut pos = cursor_position;
    while pos < text_len && !text.chars().nth(pos).is_some_and(char::is_whitespace) {
        pos += 1;
    }

    // Skip whitespace
    while pos < text_len && text.chars().nth(pos).is_some_and(char::is_whitespace) {
        pos += 1;
    }

    let jumped = if pos > cursor_position {
        Some(text[cursor_position..pos].to_string())
    } else {
        None
    };

    CursorMovementResult {
        new_position: pos,
        jumped_text: jumped,
    }
}

/// Jump to previous SQL token (more sophisticated than word)
#[must_use]
pub fn jump_to_prev_token(text: &str, cursor_position: usize) -> CursorMovementResult {
    if cursor_position == 0 {
        return CursorMovementResult {
            new_position: 0,
            jumped_text: None,
        };
    }

    // SQL tokens include: keywords, identifiers, operators, literals
    // For now, implement similar to word but can be enhanced for SQL
    let mut pos = cursor_position;

    // Skip any trailing whitespace or operators
    while pos > 0 {
        let ch = text.chars().nth(pos - 1);
        if let Some(c) = ch {
            if c.is_whitespace() || "(),;=<>!+-*/".contains(c) {
                pos -= 1;
            } else {
                break;
            }
        } else {
            break;
        }
    }

    // Find the start of the token
    let token_start = if pos == 0 {
        0
    } else {
        let mut start = pos;
        while start > 0 {
            let ch = text.chars().nth(start - 1);
            if let Some(c) = ch {
                if !c.is_whitespace() && !"(),;=<>!+-*/".contains(c) {
                    start -= 1;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        start
    };

    let jumped = if token_start < cursor_position {
        Some(text[token_start..cursor_position].to_string())
    } else {
        None
    };

    CursorMovementResult {
        new_position: token_start,
        jumped_text: jumped,
    }
}

/// Jump to next SQL token
#[must_use]
pub fn jump_to_next_token(text: &str, cursor_position: usize) -> CursorMovementResult {
    let text_len = text.len();
    if cursor_position >= text_len {
        return CursorMovementResult {
            new_position: cursor_position,
            jumped_text: None,
        };
    }

    let mut pos = cursor_position;

    // Skip current token
    while pos < text_len {
        let ch = text.chars().nth(pos);
        if let Some(c) = ch {
            if !c.is_whitespace() && !"(),;=<>!+-*/".contains(c) {
                pos += 1;
            } else {
                break;
            }
        } else {
            break;
        }
    }

    // Skip whitespace and operators to next token
    while pos < text_len {
        let ch = text.chars().nth(pos);
        if let Some(c) = ch {
            if c.is_whitespace() || "(),;=<>!+-*/".contains(c) {
                pos += 1;
            } else {
                break;
            }
        } else {
            break;
        }
    }

    let jumped = if pos > cursor_position {
        Some(text[cursor_position..pos].to_string())
    } else {
        None
    };

    CursorMovementResult {
        new_position: pos,
        jumped_text: jumped,
    }
}

// ========== Helper Functions ==========

/// Clear all text (simple helper)
#[must_use]
pub fn clear_text() -> TextOperationResult {
    TextOperationResult {
        new_text: String::new(),
        new_cursor_position: 0,
        killed_text: None,
        description: "Cleared all text".to_string(),
    }
}

/// Insert character at cursor position
#[must_use]
pub fn insert_char(text: &str, cursor_position: usize, ch: char) -> TextOperationResult {
    let mut new_text = String::with_capacity(text.len() + 1);
    new_text.push_str(&text[..cursor_position.min(text.len())]);
    new_text.push(ch);
    if cursor_position < text.len() {
        new_text.push_str(&text[cursor_position..]);
    }

    TextOperationResult {
        new_text,
        new_cursor_position: cursor_position + 1,
        killed_text: None,
        description: format!("Inserted '{ch}'"),
    }
}

/// Delete character at cursor position (Delete key)
#[must_use]
pub fn delete_char(text: &str, cursor_position: usize) -> TextOperationResult {
    if cursor_position >= text.len() {
        return TextOperationResult {
            new_text: text.to_string(),
            new_cursor_position: cursor_position,
            killed_text: None,
            description: "Nothing to delete".to_string(),
        };
    }

    let deleted = text.chars().nth(cursor_position).unwrap();
    let mut new_text = String::with_capacity(text.len() - 1);
    new_text.push_str(&text[..cursor_position]);
    new_text.push_str(&text[cursor_position + 1..]);

    TextOperationResult {
        new_text,
        new_cursor_position: cursor_position,
        killed_text: Some(deleted.to_string()),
        description: format!("Deleted '{deleted}'"),
    }
}

/// Delete character before cursor (Backspace)
#[must_use]
pub fn backspace(text: &str, cursor_position: usize) -> TextOperationResult {
    if cursor_position == 0 {
        return TextOperationResult {
            new_text: text.to_string(),
            new_cursor_position: 0,
            killed_text: None,
            description: "At beginning".to_string(),
        };
    }

    let deleted = text.chars().nth(cursor_position - 1).unwrap();
    let mut new_text = String::with_capacity(text.len() - 1);
    new_text.push_str(&text[..cursor_position - 1]);
    new_text.push_str(&text[cursor_position..]);

    TextOperationResult {
        new_text,
        new_cursor_position: cursor_position - 1,
        killed_text: Some(deleted.to_string()),
        description: format!("Deleted '{deleted}'"),
    }
}

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

    #[test]
    fn test_kill_line() {
        let text = "SELECT * FROM table WHERE id = 1";
        let result = kill_line(text, 7);
        assert_eq!(result.new_text, "SELECT ");
        assert_eq!(
            result.killed_text,
            Some("* FROM table WHERE id = 1".to_string())
        );
        assert_eq!(result.new_cursor_position, 7);
    }

    #[test]
    fn test_kill_line_backward() {
        let text = "SELECT * FROM table";
        let result = kill_line_backward(text, 7);
        assert_eq!(result.new_text, "* FROM table");
        assert_eq!(result.killed_text, Some("SELECT ".to_string()));
        assert_eq!(result.new_cursor_position, 0);
    }

    #[test]
    fn test_delete_word_backward() {
        let text = "SELECT * FROM table";
        let result = delete_word_backward(text, 13); // After "FROM"
        assert_eq!(result.new_text, "SELECT *  table");
        assert_eq!(result.killed_text, Some("FROM".to_string()));
        assert_eq!(result.new_cursor_position, 9);
    }

    #[test]
    fn test_move_word_forward() {
        let text = "SELECT * FROM table";
        let result = move_word_forward(text, 0);
        assert_eq!(result.new_position, 7); // After "SELECT "

        let result2 = move_word_forward(text, 7);
        assert_eq!(result2.new_position, 9); // After "* "
    }

    #[test]
    fn test_move_word_backward() {
        let text = "SELECT * FROM table";
        let result = move_word_backward(text, 13); // From end of "FROM"
        assert_eq!(result.new_position, 9); // Start of "FROM"

        let result2 = move_word_backward(text, 9);
        assert_eq!(result2.new_position, 7); // Start of "*"
    }

    #[test]
    fn test_jump_to_next_token() {
        let text = "SELECT id, name FROM users WHERE id = 1";
        let result = jump_to_next_token(text, 0);
        assert_eq!(result.new_position, 7); // After "SELECT "

        let result2 = jump_to_next_token(text, 7);
        assert_eq!(result2.new_position, 11); // After "id, " (skips comma and space)
    }

    #[test]
    fn test_insert_and_delete() {
        let text = "SELECT";
        let result = insert_char(text, 6, ' ');
        assert_eq!(result.new_text, "SELECT ");
        assert_eq!(result.new_cursor_position, 7);

        let result2 = delete_char(&result.new_text, 6);
        assert_eq!(result2.new_text, "SELECT");

        let result3 = backspace(&result.new_text, 7);
        assert_eq!(result3.new_text, "SELECT");
        assert_eq!(result3.new_cursor_position, 6);
    }
}

// ========== SQL-Specific Text Functions ==========

/// Extract partial word at cursor for SQL completion
/// Handles quoted identifiers and SQL-specific parsing
#[must_use]
pub fn extract_partial_word_at_cursor(query: &str, cursor_pos: usize) -> Option<String> {
    if cursor_pos == 0 || cursor_pos > query.len() {
        return None;
    }

    let chars: Vec<char> = query.chars().collect();
    let mut start = cursor_pos;
    let end = cursor_pos;

    // Check if we might be in a quoted identifier
    let mut in_quote = false;

    // Find start of word (go backward)
    while start > 0 {
        let prev_char = chars[start - 1];
        if prev_char == '"' {
            // Found a quote, include it and stop
            start -= 1;
            in_quote = true;
            break;
        } else if prev_char.is_alphanumeric() || prev_char == '_' || (prev_char == ' ' && in_quote)
        {
            start -= 1;
        } else {
            break;
        }
    }

    // If we found a quote but are in a quoted identifier,
    // we need to continue backwards to include the identifier content
    if in_quote && start > 0 {
        // We've already moved past the quote, now get the content before it
        // Actually, we want to include everything from the quote forward
        // The logic above is correct - we stop at the quote
    }

    // Convert back to byte positions
    let start_byte = chars[..start].iter().map(|c| c.len_utf8()).sum();
    let end_byte = chars[..end].iter().map(|c| c.len_utf8()).sum();

    if start_byte < end_byte {
        Some(query[start_byte..end_byte].to_string())
    } else {
        None
    }
}

/// Result of applying a completion to text
#[derive(Debug, Clone)]
pub struct CompletionResult {
    /// The new text with completion applied
    pub new_text: String,
    /// The new cursor position after completion
    pub new_cursor_position: usize,
    /// Description of what was completed
    pub description: String,
}

/// Apply a completion suggestion to text at cursor position
/// Handles quoted identifiers and smart cursor positioning
#[must_use]
pub fn apply_completion_to_text(
    query: &str,
    cursor_pos: usize,
    partial_word: &str,
    suggestion: &str,
) -> CompletionResult {
    let before_partial = &query[..cursor_pos - partial_word.len()];
    let after_cursor = &query[cursor_pos..];

    // Handle quoted identifiers - avoid double quotes
    let suggestion_to_use = if partial_word.starts_with('"') && suggestion.starts_with('"') {
        // The partial already includes the opening quote, so use suggestion without its quote
        if suggestion.len() > 1 {
            suggestion[1..].to_string()
        } else {
            suggestion.to_string()
        }
    } else {
        suggestion.to_string()
    };

    let new_query = format!("{before_partial}{suggestion_to_use}{after_cursor}");

    // Smart cursor positioning based on function signature
    let new_cursor_pos = if suggestion_to_use.ends_with("('')") {
        // Function with parameters - position cursor between the quotes
        // e.g., Contains('|') where | is cursor
        before_partial.len() + suggestion_to_use.len() - 2
    } else if suggestion_to_use.ends_with("()") {
        // Parameterless function - position cursor after closing parenthesis
        // e.g., Length()| where | is cursor
        before_partial.len() + suggestion_to_use.len()
    } else {
        // Regular completion (not a function) - position at end
        before_partial.len() + suggestion_to_use.len()
    };

    // Better description based on completion type
    let description = if suggestion_to_use.ends_with("('')") {
        format!("Completed '{suggestion}' with cursor positioned for parameter input")
    } else if suggestion_to_use.ends_with("()") {
        format!("Completed parameterless function '{suggestion}'")
    } else {
        format!("Completed '{suggestion}'")
    };

    CompletionResult {
        new_text: new_query,
        new_cursor_position: new_cursor_pos,
        description,
    }
}