sql-cli 1.71.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
//! `SearchManager` - Encapsulates all search logic for the TUI
//!
//! This module provides a clean separation between search logic and UI rendering.
//! It handles case sensitivity, coordinate mapping, and iteration through matches.

use tracing::warn;

/// Represents a single search match
#[derive(Debug, Clone, PartialEq)]
pub struct SearchMatch {
    /// Row index in the data (0-based)
    pub row: usize,
    /// Column index in the data (0-based)
    pub column: usize,
    /// The actual value that matched
    pub value: String,
    /// The highlighted portion of the match
    pub highlight_range: (usize, usize),
}

/// Configuration for search behavior
#[derive(Debug, Clone)]
pub struct SearchConfig {
    /// Whether search is case sensitive
    pub case_sensitive: bool,
    /// Whether to use regex matching
    pub use_regex: bool,
    /// Whether to search only visible columns
    pub visible_columns_only: bool,
    /// Whether to wrap around when reaching the end
    pub wrap_around: bool,
}

impl Default for SearchConfig {
    fn default() -> Self {
        Self {
            case_sensitive: false,
            use_regex: false,
            visible_columns_only: false,
            wrap_around: true,
        }
    }
}

/// Manages search state and provides iteration through matches
pub struct SearchManager {
    /// Current search pattern
    pattern: String,
    /// All matches found
    matches: Vec<SearchMatch>,
    /// Current match index
    current_index: usize,
    /// Search configuration
    config: SearchConfig,
    /// Cached regex (if using regex mode)
    regex: Option<regex::Regex>,
}

impl Default for SearchManager {
    fn default() -> Self {
        Self::new()
    }
}

impl SearchManager {
    /// Create a new `SearchManager` with default config
    #[must_use]
    pub fn new() -> Self {
        Self {
            pattern: String::new(),
            matches: Vec::new(),
            current_index: 0,
            config: SearchConfig::default(),
            regex: None,
        }
    }

    /// Create with custom configuration
    #[must_use]
    pub fn with_config(config: SearchConfig) -> Self {
        Self {
            pattern: String::new(),
            matches: Vec::new(),
            current_index: 0,
            config,
            regex: None,
        }
    }

    /// Update search configuration
    pub fn set_config(&mut self, config: SearchConfig) {
        // Clear regex cache if switching modes
        if !config.use_regex {
            self.regex = None;
        }
        self.config = config;
    }

    /// Set case sensitivity
    pub fn set_case_sensitive(&mut self, case_sensitive: bool) {
        self.config.case_sensitive = case_sensitive;
        // Re-compile regex if needed
        if self.config.use_regex && !self.pattern.is_empty() {
            self.compile_regex();
        }
    }

    /// Perform a search on the given data
    /// Returns the number of matches found
    pub fn search(
        &mut self,
        pattern: &str,
        data: &[Vec<String>],
        visible_columns: Option<&[usize]>,
    ) -> usize {
        self.pattern = pattern.to_string();
        self.matches.clear();
        self.current_index = 0;

        if pattern.is_empty() {
            return 0;
        }

        // Compile regex if needed
        if self.config.use_regex {
            self.compile_regex();
            if self.regex.is_none() {
                return 0; // Invalid regex
            }
        }

        // Determine which columns to search
        let columns_to_search: Vec<usize> = if self.config.visible_columns_only {
            visible_columns.map(<[usize]>::to_vec).unwrap_or_else(|| {
                // If no visible columns specified, search all
                if data.is_empty() {
                    vec![]
                } else {
                    (0..data[0].len()).collect()
                }
            })
        } else {
            // Search all columns
            if data.is_empty() {
                vec![]
            } else {
                (0..data[0].len()).collect()
            }
        };

        // Search through data
        for (row_idx, row) in data.iter().enumerate() {
            for &col_idx in &columns_to_search {
                if col_idx >= row.len() {
                    continue;
                }

                let cell_value = &row[col_idx];
                if let Some(range) = self.matches_pattern(cell_value, pattern) {
                    self.matches.push(SearchMatch {
                        row: row_idx,
                        column: col_idx,
                        value: cell_value.clone(),
                        highlight_range: range,
                    });
                }
            }
        }

        self.matches.len()
    }

    /// Check if a value matches the pattern and return highlight range
    fn matches_pattern(&self, value: &str, pattern: &str) -> Option<(usize, usize)> {
        if self.config.use_regex {
            // Use regex matching
            if let Some(ref regex) = self.regex {
                if let Some(m) = regex.find(value) {
                    return Some((m.start(), m.end()));
                }
            }
        } else {
            // Use substring matching
            let search_value = if self.config.case_sensitive {
                value.to_string()
            } else {
                value.to_lowercase()
            };

            let search_pattern = if self.config.case_sensitive {
                pattern.to_string()
            } else {
                pattern.to_lowercase()
            };

            if let Some(pos) = search_value.find(&search_pattern) {
                return Some((pos, pos + pattern.len()));
            }
        }
        None
    }

    /// Compile regex pattern
    fn compile_regex(&mut self) {
        let pattern = if self.config.case_sensitive {
            self.pattern.clone()
        } else {
            format!("(?i){}", self.pattern)
        };

        match regex::Regex::new(&pattern) {
            Ok(regex) => self.regex = Some(regex),
            Err(e) => {
                warn!("Invalid regex pattern: {}", e);
                self.regex = None;
            }
        }
    }

    /// Get the current match (if any)
    #[must_use]
    pub fn current_match(&self) -> Option<&SearchMatch> {
        if self.matches.is_empty() {
            None
        } else {
            self.matches.get(self.current_index)
        }
    }

    /// Move to the next match
    pub fn next_match(&mut self) -> Option<&SearchMatch> {
        if self.matches.is_empty() {
            return None;
        }

        if self.current_index + 1 < self.matches.len() {
            self.current_index += 1;
        } else if self.config.wrap_around {
            self.current_index = 0;
        }

        self.current_match()
    }

    /// Move to the previous match
    pub fn previous_match(&mut self) -> Option<&SearchMatch> {
        if self.matches.is_empty() {
            return None;
        }

        if self.current_index > 0 {
            self.current_index -= 1;
        } else if self.config.wrap_around {
            self.current_index = self.matches.len() - 1;
        }

        self.current_match()
    }

    /// Jump to a specific match index
    pub fn jump_to_match(&mut self, index: usize) -> Option<&SearchMatch> {
        if index < self.matches.len() {
            self.current_index = index;
            self.current_match()
        } else {
            None
        }
    }

    /// Get the first match (useful for initial navigation)
    #[must_use]
    pub fn first_match(&self) -> Option<&SearchMatch> {
        self.matches.first()
    }

    /// Get all matches
    #[must_use]
    pub fn all_matches(&self) -> &[SearchMatch] {
        &self.matches
    }

    /// Get the total number of matches
    #[must_use]
    pub fn match_count(&self) -> usize {
        self.matches.len()
    }

    /// Get current match index (1-based for display)
    #[must_use]
    pub fn current_match_number(&self) -> usize {
        if self.matches.is_empty() {
            0
        } else {
            self.current_index + 1
        }
    }

    /// Clear all search state
    pub fn clear(&mut self) {
        self.pattern.clear();
        self.matches.clear();
        self.current_index = 0;
        self.regex = None;
    }

    /// Check if there's an active search
    #[must_use]
    pub fn has_active_search(&self) -> bool {
        !self.pattern.is_empty()
    }

    /// Get the current search pattern
    #[must_use]
    pub fn pattern(&self) -> &str {
        &self.pattern
    }

    /// Calculate scroll offset needed to show a match in viewport
    #[must_use]
    pub fn calculate_scroll_offset(
        &self,
        match_pos: &SearchMatch,
        viewport_height: usize,
        current_offset: usize,
    ) -> usize {
        let row = match_pos.row;

        // If match is above current view, scroll up to it
        if row < current_offset {
            row
        }
        // If match is below current view, center it
        else if row >= current_offset + viewport_height {
            row.saturating_sub(viewport_height / 2)
        }
        // Match is already visible
        else {
            current_offset
        }
    }

    /// Find the next match from a given position
    #[must_use]
    pub fn find_next_from(&self, current_row: usize, current_col: usize) -> Option<&SearchMatch> {
        // Find matches after current position
        for match_item in &self.matches {
            if match_item.row > current_row
                || (match_item.row == current_row && match_item.column > current_col)
            {
                return Some(match_item);
            }
        }

        // Wrap around if enabled
        if self.config.wrap_around && !self.matches.is_empty() {
            return self.matches.first();
        }

        None
    }

    /// Find the previous match from a given position
    #[must_use]
    pub fn find_previous_from(
        &self,
        current_row: usize,
        current_col: usize,
    ) -> Option<&SearchMatch> {
        // Find matches before current position (in reverse)
        for match_item in self.matches.iter().rev() {
            if match_item.row < current_row
                || (match_item.row == current_row && match_item.column < current_col)
            {
                return Some(match_item);
            }
        }

        // Wrap around if enabled
        if self.config.wrap_around && !self.matches.is_empty() {
            return self.matches.last();
        }

        None
    }
}

/// Iterator for search matches
pub struct SearchIterator<'a> {
    manager: &'a SearchManager,
    index: usize,
}

impl<'a> Iterator for SearchIterator<'a> {
    type Item = &'a SearchMatch;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.manager.matches.len() {
            let result = &self.manager.matches[self.index];
            self.index += 1;
            Some(result)
        } else {
            None
        }
    }
}

impl SearchManager {
    /// Get an iterator over all matches
    #[must_use]
    pub fn iter(&self) -> SearchIterator {
        SearchIterator {
            manager: self,
            index: 0,
        }
    }
}

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

    #[test]
    fn test_case_insensitive_search() {
        let mut manager = SearchManager::new();
        manager.set_case_sensitive(false);

        let data = vec![
            vec!["Unconfirmed".to_string(), "data1".to_string()],
            vec!["unconfirmed".to_string(), "data2".to_string()],
            vec!["UNCONFIRMED".to_string(), "data3".to_string()],
            vec!["confirmed".to_string(), "data4".to_string()],
        ];

        let count = manager.search("unconfirmed", &data, None);
        assert_eq!(count, 3);

        // All case variations should match
        let matches: Vec<_> = manager.iter().collect();
        assert_eq!(matches.len(), 3);
        assert_eq!(matches[0].row, 0);
        assert_eq!(matches[1].row, 1);
        assert_eq!(matches[2].row, 2);
    }

    #[test]
    fn test_case_sensitive_search() {
        let mut manager = SearchManager::new();
        manager.set_case_sensitive(true);

        let data = vec![
            vec!["Unconfirmed".to_string(), "data1".to_string()],
            vec!["unconfirmed".to_string(), "data2".to_string()],
            vec!["UNCONFIRMED".to_string(), "data3".to_string()],
        ];

        let count = manager.search("Unconfirmed", &data, None);
        assert_eq!(count, 1);

        let first_match = manager.first_match().unwrap();
        assert_eq!(first_match.row, 0);
        assert_eq!(first_match.value, "Unconfirmed");
    }

    #[test]
    fn test_navigation() {
        let mut manager = SearchManager::new();

        let data = vec![
            vec!["apple".to_string(), "banana".to_string()],
            vec!["apple pie".to_string(), "cherry".to_string()],
            vec!["orange".to_string(), "apple juice".to_string()],
        ];

        manager.search("apple", &data, None);
        assert_eq!(manager.match_count(), 3);

        // Test next navigation
        let first = manager.current_match().unwrap();
        assert_eq!((first.row, first.column), (0, 0));

        let second = manager.next_match().unwrap();
        assert_eq!((second.row, second.column), (1, 0));

        let third = manager.next_match().unwrap();
        assert_eq!((third.row, third.column), (2, 1));

        // Test wrap around
        let wrapped = manager.next_match().unwrap();
        assert_eq!((wrapped.row, wrapped.column), (0, 0));

        // Test previous navigation
        let prev = manager.previous_match().unwrap();
        assert_eq!((prev.row, prev.column), (2, 1));
    }

    #[test]
    fn test_visible_columns_filter() {
        let mut config = SearchConfig::default();
        config.visible_columns_only = true;
        let mut manager = SearchManager::with_config(config);

        let data = vec![
            vec![
                "apple".to_string(),
                "hidden".to_string(),
                "banana".to_string(),
            ],
            vec![
                "orange".to_string(),
                "apple".to_string(),
                "cherry".to_string(),
            ],
        ];

        // Search only in columns 0 and 2 (column 1 is hidden)
        let visible = vec![0, 2];
        let count = manager.search("apple", &data, Some(&visible));

        // Should only find apple in column 0 of row 0, not in column 1 of row 1
        assert_eq!(count, 1);
        let match_item = manager.first_match().unwrap();
        assert_eq!(match_item.row, 0);
        assert_eq!(match_item.column, 0);
    }

    #[test]
    fn test_scroll_offset_calculation() {
        let manager = SearchManager::new();

        let match_item = SearchMatch {
            row: 50,
            column: 0,
            value: String::new(),
            highlight_range: (0, 0),
        };

        // Match below viewport - should center
        let offset = manager.calculate_scroll_offset(&match_item, 20, 10);
        assert_eq!(offset, 40); // 50 - 20/2

        // Match above viewport - should scroll to it
        let offset = manager.calculate_scroll_offset(&match_item, 20, 60);
        assert_eq!(offset, 50);

        // Match already visible - keep current offset
        let offset = manager.calculate_scroll_offset(&match_item, 20, 45);
        assert_eq!(offset, 45);
    }

    #[test]
    fn test_find_from_position() {
        let mut manager = SearchManager::new();

        let data = vec![
            vec!["a".to_string(), "b".to_string(), "match".to_string()],
            vec!["match".to_string(), "c".to_string(), "d".to_string()],
            vec!["e".to_string(), "match".to_string(), "f".to_string()],
        ];

        manager.search("match", &data, None);

        // Find next from position (0, 1)
        let next = manager.find_next_from(0, 1).unwrap();
        assert_eq!((next.row, next.column), (0, 2));

        // Find next from position (1, 0)
        let next = manager.find_next_from(1, 0).unwrap();
        assert_eq!((next.row, next.column), (2, 1));

        // Find previous from position (2, 0)
        let prev = manager.find_previous_from(2, 0).unwrap();
        assert_eq!((prev.row, prev.column), (1, 0));
    }
}