jiq 2.21.1

Interactive JSON query tool with real-time output
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
use ratatui::style::{Modifier, Style};
use tui_textarea::TextArea;

use super::matcher::HistoryMatcher;
use super::storage;

/// Maximum number of history items to display in the popup.
pub const MAX_VISIBLE_HISTORY: usize = 15;

/// Creates a TextArea configured for history search input.
fn create_search_textarea() -> TextArea<'static> {
    let mut textarea = TextArea::default();
    textarea.set_cursor_line_style(Style::default());
    textarea.set_cursor_style(Style::default().add_modifier(Modifier::REVERSED));
    textarea
}

/// Manages the state of the history popup.
///
/// Design note on `persist_to_disk`:
/// This flag allows tests to use in-memory-only history without writing to the real
/// history file. While trait-based dependency injection would be more "proper", it would
/// add significant complexity for a single-user CLI tool with minimal benefit.
/// This pragmatic approach keeps the codebase simple while ensuring test isolation.
pub struct HistoryState {
    entries: Vec<String>,
    filtered_indices: Vec<usize>,
    search_textarea: TextArea<'static>,
    selected_index: usize,
    visible: bool,
    matcher: HistoryMatcher,
    /// Controls whether history is persisted to disk (false in tests)
    persist_to_disk: bool,
    cycling_index: Option<usize>,
}

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

impl HistoryState {
    /// Creates a new HistoryState and loads history from disk.
    pub fn new() -> Self {
        let entries = storage::load_history();
        let filtered_indices = (0..entries.len()).collect();

        Self {
            entries,
            filtered_indices,
            search_textarea: create_search_textarea(),
            selected_index: 0,
            visible: false,
            matcher: HistoryMatcher::new(),
            persist_to_disk: true,
            cycling_index: None,
        }
    }

    /// Creates an empty HistoryState without loading from disk.
    /// Useful for testing - does not persist to disk.
    #[cfg(test)]
    pub fn empty() -> Self {
        Self {
            entries: Vec::new(),
            filtered_indices: Vec::new(),
            search_textarea: create_search_textarea(),
            selected_index: 0,
            visible: false,
            matcher: HistoryMatcher::new(),
            persist_to_disk: false,
            cycling_index: None,
        }
    }

    /// Adds an entry to in-memory history only (does NOT persist to disk).
    /// Used for testing to avoid polluting real history file.
    #[cfg(test)]
    pub fn add_entry_in_memory(&mut self, query: &str) {
        if query.trim().is_empty() {
            return;
        }

        self.entries.retain(|e| e != query);
        self.entries.insert(0, query.to_string());
        self.filtered_indices = (0..self.entries.len()).collect();
    }

    /// Opens the history popup with an optional initial search query.
    pub fn open(&mut self, initial_query: Option<&str>) {
        self.visible = true;
        // Clear existing text and set initial query
        self.search_textarea.select_all();
        self.search_textarea.cut();
        if let Some(q) = initial_query {
            self.search_textarea.insert_str(q);
        }
        self.update_filter();
        self.selected_index = 0;
    }

    /// Closes the history popup and resets state.
    pub fn close(&mut self) {
        self.visible = false;
        self.search_textarea.select_all();
        self.search_textarea.cut();
        self.selected_index = 0;
        self.filtered_indices = (0..self.entries.len()).collect();
    }

    /// Returns whether the history popup is visible.
    pub fn is_visible(&self) -> bool {
        self.visible
    }

    /// Returns the current search query (used for testing).
    #[cfg(test)]
    pub fn search_query(&self) -> &str {
        self.search_textarea
            .lines()
            .first()
            .map(|s| s.as_str())
            .unwrap_or("")
    }

    /// Returns a mutable reference to the search TextArea for input handling.
    pub fn search_textarea_mut(&mut self) -> &mut TextArea<'static> {
        &mut self.search_textarea
    }

    /// Called after TextArea input to update the filter.
    pub fn on_search_input_changed(&mut self) {
        self.update_filter();
        self.selected_index = 0;
    }

    /// Selects the next item in the filtered list.
    pub fn select_next(&mut self) {
        if !self.filtered_indices.is_empty() {
            self.selected_index = (self.selected_index + 1) % self.filtered_indices.len();
        }
    }

    /// Selects the previous item in the filtered list.
    pub fn select_previous(&mut self) {
        if !self.filtered_indices.is_empty() {
            self.selected_index = if self.selected_index == 0 {
                self.filtered_indices.len() - 1
            } else {
                self.selected_index - 1
            };
        }
    }

    /// Returns the currently selected entry, if any.
    pub fn selected_entry(&self) -> Option<&str> {
        self.filtered_indices
            .get(self.selected_index)
            .and_then(|&idx| self.entries.get(idx))
            .map(String::as_str)
    }

    /// Returns the index of the currently selected item.
    pub fn selected_index(&self) -> usize {
        self.selected_index
    }

    /// Returns the total number of entries (unfiltered).
    pub fn total_count(&self) -> usize {
        self.entries.len()
    }

    /// Returns the number of filtered entries.
    pub fn filtered_count(&self) -> usize {
        self.filtered_indices.len()
    }

    /// Returns an iterator over the visible (filtered) entries with their display indices.
    /// Limited to MAX_VISIBLE_HISTORY items.
    /// Returns entries in reverse order (most recent at bottom, closest to input).
    ///
    /// Note: This allocates a small Vec (~15 items) to enable reversing.
    /// For MAX_VISIBLE_HISTORY=15, this is ~240 bytes - acceptable for render frequency.
    pub fn visible_entries(&self) -> impl Iterator<Item = (usize, &str)> {
        let entries: Vec<(usize, &str)> = self
            .filtered_indices
            .iter()
            .take(MAX_VISIBLE_HISTORY)
            .enumerate()
            .filter_map(|(original_idx, &entry_idx)| {
                self.entries
                    .get(entry_idx)
                    .map(|e| (original_idx, e.as_str()))
            })
            .collect();

        // Reverse the display order but keep original indices for selection highlighting
        entries.into_iter().rev()
    }

    /// Adds a query to the history (saves to disk if persist_to_disk is true).
    ///
    /// If disk save fails, continues with in-memory update and logs error to stderr.
    /// This allows the app to degrade gracefully - history works for current session
    /// even if persistence fails.
    pub fn add_entry(&mut self, query: &str) {
        if query.trim().is_empty() {
            return;
        }

        // Only persist to disk if enabled (disabled for tests)
        if self.persist_to_disk
            && let Err(e) = storage::add_entry(query)
        {
            eprintln!("Warning: Failed to save query history to disk: {}", e);
            eprintln!("History will work for this session only.");
            // Continue with in-memory update despite save failure
        }

        self.entries.retain(|e| e != query);
        self.entries.insert(0, query.to_string());

        self.filtered_indices = (0..self.entries.len()).collect();
    }

    /// Updates the filtered indices based on the current search query.
    fn update_filter(&mut self) {
        let query = self
            .search_textarea
            .lines()
            .first()
            .map(|s| s.as_str())
            .unwrap_or("");
        self.filtered_indices = self.matcher.filter(query, &self.entries);
    }

    /// Cycle to the previous history entry (older).
    /// Returns the entry if available.
    pub fn cycle_previous(&mut self) -> Option<String> {
        if self.entries.is_empty() {
            return None;
        }

        let next_idx = match self.cycling_index {
            None => 0,
            Some(idx) if idx + 1 < self.entries.len() => idx + 1,
            Some(idx) => idx, // At end, stay there
        };

        self.cycling_index = Some(next_idx);
        self.entries.get(next_idx).cloned()
    }

    /// Cycle to the next history entry (newer).
    /// Returns the entry if available, or None if at most recent.
    pub fn cycle_next(&mut self) -> Option<String> {
        match self.cycling_index {
            None => None,
            Some(0) => {
                // At most recent, reset cycling
                self.cycling_index = None;
                None
            }
            Some(idx) => {
                let next_idx = idx - 1;
                self.cycling_index = Some(next_idx);
                self.entries.get(next_idx).cloned()
            }
        }
    }

    /// Reset cycling state (called when user types).
    pub fn reset_cycling(&mut self) {
        self.cycling_index = None;
    }
}

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

    fn create_test_state(entries: Vec<&str>) -> HistoryState {
        HistoryState {
            entries: entries.into_iter().map(String::from).collect(),
            filtered_indices: vec![0, 1, 2],
            search_textarea: create_search_textarea(),
            selected_index: 0,
            visible: false,
            matcher: HistoryMatcher::new(),
            persist_to_disk: false,
            cycling_index: None,
        }
    }

    #[test]
    fn test_open_sets_visible() {
        let mut state = create_test_state(vec![".foo", ".bar", ".baz"]);
        state.open(None);
        assert!(state.is_visible());
    }

    #[test]
    fn test_open_with_initial_query() {
        let mut state = create_test_state(vec![".foo", ".bar", ".baz"]);
        state.open(Some(".foo"));
        assert_eq!(state.search_query(), ".foo");
    }

    #[test]
    fn test_close_resets_state() {
        let mut state = create_test_state(vec![".foo", ".bar", ".baz"]);
        state.open(Some("test"));
        state.select_next();
        state.close();

        assert!(!state.is_visible());
        assert!(state.search_query().is_empty());
        assert_eq!(state.selected_index(), 0);
    }

    #[test]
    fn test_navigation_wraps() {
        let mut state = create_test_state(vec![".foo", ".bar", ".baz"]);
        state.filtered_indices = vec![0, 1, 2];

        state.select_previous();
        assert_eq!(state.selected_index(), 2);

        state.select_next();
        assert_eq!(state.selected_index(), 0);
    }

    #[test]
    fn test_selected_entry() {
        let mut state = create_test_state(vec![".foo", ".bar", ".baz"]);
        state.filtered_indices = vec![0, 1, 2];

        assert_eq!(state.selected_entry(), Some(".foo"));

        state.select_next();
        assert_eq!(state.selected_entry(), Some(".bar"));
    }

    #[test]
    fn test_textarea_search_input() {
        let mut state = create_test_state(vec![".foo", ".bar", ".baz"]);

        // Insert text via TextArea
        state.search_textarea_mut().insert_str("fo");
        assert_eq!(state.search_query(), "fo");

        // Clear via select_all + cut
        state.search_textarea_mut().select_all();
        state.search_textarea_mut().cut();
        assert_eq!(state.search_query(), "");
    }

    #[test]
    fn test_visible_entries_limited() {
        let entries: Vec<&str> = (0..20).map(|_| ".test").collect();
        let mut state = create_test_state(entries);
        state.filtered_indices = (0..20).collect();

        let visible: Vec<_> = state.visible_entries().collect();
        assert_eq!(visible.len(), MAX_VISIBLE_HISTORY);
    }

    #[test]
    fn test_empty_navigation() {
        let mut state = create_test_state(vec![]);
        state.filtered_indices = vec![];

        state.select_next();
        state.select_previous();
        assert_eq!(state.selected_index(), 0);
    }

    #[test]
    fn test_single_entry_navigation() {
        let mut state = create_test_state(vec![".only"]);
        state.filtered_indices = vec![0];

        // Should wrap to same entry
        state.select_next();
        assert_eq!(state.selected_index(), 0);
        assert_eq!(state.selected_entry(), Some(".only"));

        state.select_previous();
        assert_eq!(state.selected_index(), 0);
        assert_eq!(state.selected_entry(), Some(".only"));
    }

    #[test]
    fn test_filter_updates_reset_selection() {
        let mut state = create_test_state(vec![".apple", ".banana", ".apricot"]);
        state.filtered_indices = vec![0, 1, 2];
        state.selected_index = 2;

        // Input change resets selection to 0
        state.search_textarea_mut().insert_char('a');
        state.on_search_input_changed();
        assert_eq!(state.selected_index(), 0);
    }

    #[test]
    fn test_selected_entry_with_out_of_bounds_index() {
        let mut state = create_test_state(vec![".foo", ".bar"]);
        state.filtered_indices = vec![0, 1];
        state.selected_index = 5; // Out of bounds

        // Should return None gracefully
        assert_eq!(state.selected_entry(), None);
    }

    #[test]
    fn test_cycling_at_boundaries() {
        let mut state = create_test_state(vec![".first", ".second", ".third"]);

        // Cycle to end
        let e1 = state.cycle_previous();
        let e2 = state.cycle_previous();
        let e3 = state.cycle_previous();
        assert_eq!(e1, Some(".first".to_string()));
        assert_eq!(e2, Some(".second".to_string()));
        assert_eq!(e3, Some(".third".to_string()));

        // Spam Ctrl+P at end - should stay at .third
        let e4 = state.cycle_previous();
        let e5 = state.cycle_previous();
        assert_eq!(e4, Some(".third".to_string()));
        assert_eq!(e5, Some(".third".to_string()));
    }

    #[test]
    fn test_cycling_forward_to_none() {
        let mut state = create_test_state(vec![".first", ".second"]);

        // Cycle back
        state.cycle_previous();
        state.cycle_previous();

        // Cycle forward
        let e1 = state.cycle_next();
        assert_eq!(e1, Some(".first".to_string()));

        // Cycle forward to most recent
        let e2 = state.cycle_next();
        assert_eq!(e2, None); // At most recent, should reset
    }

    #[test]
    fn test_reset_cycling() {
        let mut state = create_test_state(vec![".first", ".second"]);

        state.cycle_previous();
        state.cycle_previous();
        assert_eq!(state.cycling_index, Some(1));

        state.reset_cycling();
        assert_eq!(state.cycling_index, None);

        // Next cycle should start fresh
        let entry = state.cycle_previous();
        assert_eq!(entry, Some(".first".to_string()));
    }
}