fresh-editor 0.1.56

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! Input history for prompt navigation
//!
//! This module provides a history mechanism for prompts, similar to bash/readline.
//! Users can navigate through previously entered values using up/down arrow keys.
//!
//! ## Design Goals
//!
//! 1. **Intuitive navigation**: Behaves like bash/readline history
//!    - Up arrow moves to previous (older) items
//!    - Down arrow moves to next (newer) items
//!    - Pressing down past the last item returns to current input
//!
//! 2. **Non-destructive editing**: Editing historical items doesn't modify stored history
//!    - History items are immutable once stored
//!    - Edits only affect the current prompt input
//!
//! 3. **Persistence-ready**: Designed for future file-based persistence
//!    - Simple structure that can be serialized (Vec<String>)
//!    - Placeholder methods for save/load operations
//!    - Separate histories for different prompt types (search vs replace)
//!
//! ## Usage Example
//!
//! ```
//! use fresh::input_history::InputHistory;
//!
//! let mut history = InputHistory::new();
//!
//! // Add items to history
//! history.push("first search".to_string());
//! history.push("second search".to_string());
//!
//! // Navigate backwards (up arrow)
//! let prev = history.navigate_prev("current input");
//! assert_eq!(prev, Some("second search".to_string()));
//!
//! // Navigate backwards again
//! let prev2 = history.navigate_prev("current input");
//! assert_eq!(prev2, Some("first search".to_string()));
//!
//! // Navigate forwards (down arrow)
//! let next = history.navigate_next();
//! assert_eq!(next, Some("second search".to_string()));
//!
//! // Navigate past the end returns to original input
//! let next2 = history.navigate_next();
//! assert_eq!(next2, Some("current input".to_string()));
//! ```

/// Input history for prompt navigation (like bash/readline)
///
/// This struct maintains a history of previously entered values
/// and allows navigating through them with up/down arrows.
///
/// ## Navigation Behavior
///
/// - History items are stored in a Vec (oldest to newest)
/// - `position = None` means "at current input" (not navigating)
/// - `position = Some(i)` means "viewing history item i"
/// - When you first press up, current input is saved to `temp_input`
/// - When you navigate past the end (down from last item), `temp_input` is restored
///
/// ## Future Persistence
///
/// To add persistence later:
/// - Implement `serde::Serialize` and `serde::Deserialize`
/// - Add methods: `save_to_file()`, `load_from_file()`
/// - Store in config directory, separate files per history type
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct InputHistory {
    /// History items (oldest to newest)
    items: Vec<String>,
    /// Maximum number of items to keep
    max_size: usize,
    /// Current navigation position
    /// - None = at current input (not navigating)
    /// - Some(index) = viewing history item at index
    position: Option<usize>,
    /// Temporary storage for current input when navigating away
    temp_input: Option<String>,
}

impl InputHistory {
    /// Default maximum history size
    pub const DEFAULT_MAX_SIZE: usize = 100;

    /// Create a new history with default capacity (100 items)
    pub fn new() -> Self {
        Self::with_capacity(Self::DEFAULT_MAX_SIZE)
    }

    /// Create a new history with specified capacity
    ///
    /// # Arguments
    /// * `max_size` - Maximum number of history items to keep (must be > 0)
    ///
    /// # Panics
    /// Panics if `max_size` is 0
    pub fn with_capacity(max_size: usize) -> Self {
        assert!(max_size > 0, "History max_size must be greater than 0");
        Self {
            items: Vec::new(),
            max_size,
            position: None,
            temp_input: None,
        }
    }

    /// Add an item to history (most recent)
    ///
    /// This method:
    /// - Skips empty strings
    /// - Skips exact duplicates of the most recent item
    /// - Enforces max_size by removing oldest items
    /// - Resets navigation state
    ///
    /// # Example
    /// ```
    /// # use fresh::input_history::InputHistory;
    /// let mut history = InputHistory::new();
    /// history.push("first".to_string());
    /// history.push("second".to_string());
    /// history.push("second".to_string()); // Skipped (duplicate)
    /// assert_eq!(history.len(), 2);
    /// ```
    pub fn push(&mut self, item: String) {
        // Skip empty strings
        if item.is_empty() {
            return;
        }

        // Skip duplicates of the most recent item
        if self.items.last().map(|s| s.as_str()) == Some(item.as_str()) {
            return;
        }

        // Add the item
        self.items.push(item);

        // Enforce max size by removing oldest items
        while self.items.len() > self.max_size {
            self.items.remove(0);
        }

        // Reset navigation state
        self.reset_navigation();
    }

    /// Navigate to previous item in history (up arrow)
    ///
    /// On first call, saves `current_input` to temporary storage and returns
    /// the most recent history item. On subsequent calls, moves backwards
    /// through history.
    ///
    /// # Arguments
    /// * `current_input` - The current prompt input (saved on first navigation)
    ///
    /// # Returns
    /// * `Some(String)` - The previous history item
    /// * `None` - No more items (already at oldest)
    ///
    /// # Example
    /// ```
    /// # use fresh::input_history::InputHistory;
    /// let mut history = InputHistory::new();
    /// history.push("first".to_string());
    /// history.push("second".to_string());
    ///
    /// let prev = history.navigate_prev("typing...");
    /// assert_eq!(prev, Some("second".to_string()));
    ///
    /// let prev2 = history.navigate_prev("typing...");
    /// assert_eq!(prev2, Some("first".to_string()));
    ///
    /// let prev3 = history.navigate_prev("typing...");
    /// assert_eq!(prev3, None); // Already at oldest
    /// ```
    pub fn navigate_prev(&mut self, current_input: &str) -> Option<String> {
        if self.items.is_empty() {
            return None;
        }

        match self.position {
            None => {
                // First navigation: save current input and go to last item
                self.temp_input = Some(current_input.to_string());
                self.position = Some(self.items.len() - 1);
                Some(self.items[self.items.len() - 1].clone())
            }
            Some(pos) if pos > 0 => {
                // Navigate to previous item
                self.position = Some(pos - 1);
                Some(self.items[pos - 1].clone())
            }
            Some(_) => {
                // Already at oldest item
                None
            }
        }
    }

    /// Navigate to next item in history (down arrow)
    ///
    /// Moves forward through history (towards more recent items).
    /// When navigating past the most recent item, returns the original
    /// input that was saved when navigation started.
    ///
    /// # Returns
    /// * `Some(String)` - The next history item, or original input if past end
    /// * `None` - Not currently navigating
    ///
    /// # Example
    /// ```
    /// # use fresh::input_history::InputHistory;
    /// let mut history = InputHistory::new();
    /// history.push("first".to_string());
    /// history.push("second".to_string());
    ///
    /// // Start navigating backwards
    /// history.navigate_prev("typing...");
    /// history.navigate_prev("typing...");
    ///
    /// // Navigate forwards
    /// let next = history.navigate_next();
    /// assert_eq!(next, Some("second".to_string()));
    ///
    /// // Navigate past the end returns to original input
    /// let next2 = history.navigate_next();
    /// assert_eq!(next2, Some("typing...".to_string()));
    /// ```
    pub fn navigate_next(&mut self) -> Option<String> {
        match self.position {
            None => {
                // Not navigating
                None
            }
            Some(pos) if pos < self.items.len() - 1 => {
                // Navigate to next item
                self.position = Some(pos + 1);
                Some(self.items[pos + 1].clone())
            }
            Some(_) => {
                // At most recent item, return to original input
                let original = self.temp_input.clone();
                self.reset_navigation();
                original
            }
        }
    }

    /// Reset navigation state
    ///
    /// Call this when:
    /// - User confirms the prompt (Enter)
    /// - User cancels the prompt (Escape)
    /// - User starts typing (optional, depends on desired behavior)
    ///
    /// This clears the temporary input storage and resets the position.
    pub fn reset_navigation(&mut self) {
        self.position = None;
        self.temp_input = None;
    }

    /// Get the most recent item without navigating
    ///
    /// Useful for pre-filling prompts with the last search term.
    ///
    /// # Example
    /// ```
    /// # use fresh::input_history::InputHistory;
    /// let mut history = InputHistory::new();
    /// history.push("last search".to_string());
    /// assert_eq!(history.last(), Some("last search"));
    /// ```
    pub fn last(&self) -> Option<&str> {
        self.items.last().map(|s| s.as_str())
    }

    /// Initialize navigation at the last history item
    ///
    /// Call this when pre-filling a prompt with the last history item.
    /// This sets up the navigation state so that pressing Up will go to
    /// the second-to-last item, not the last item again.
    ///
    /// # Example
    /// ```
    /// # use fresh::input_history::InputHistory;
    /// let mut history = InputHistory::new();
    /// history.push("first".to_string());
    /// history.push("second".to_string());
    /// history.push("third".to_string());
    ///
    /// // Pre-fill prompt with "third"
    /// history.init_at_last();
    ///
    /// // Now Up goes to "second", not "third"
    /// let prev = history.navigate_prev("third");
    /// assert_eq!(prev, Some("second".to_string()));
    /// ```
    pub fn init_at_last(&mut self) {
        if !self.items.is_empty() {
            self.position = Some(self.items.len() - 1);
            self.temp_input = None;
        }
    }

    /// Check if history is empty
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Get number of items in history
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Clear all history
    ///
    /// Removes all items and resets navigation state.
    pub fn clear(&mut self) {
        self.items.clear();
        self.reset_navigation();
    }

    /// Get a reference to the history items (oldest to newest)
    ///
    /// Useful for session persistence.
    pub fn items(&self) -> &[String] {
        &self.items
    }

    /// Create a history from existing items
    ///
    /// Useful for session restoration.
    pub fn from_items(items: Vec<String>) -> Self {
        let mut history = Self::new();
        // Add items respecting deduplication rules
        for item in items {
            history.push(item);
        }
        history
    }

    // ========================================================================
    // Persistence methods
    // ========================================================================

    /// Save history to a file
    pub fn save_to_file(&self, path: &std::path::Path) -> std::io::Result<()> {
        // Only save items, not navigation state
        let json = serde_json::to_string_pretty(&self.items)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;

        // Create parent directory if it doesn't exist
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        std::fs::write(path, json)?;
        Ok(())
    }

    /// Load history from a file
    pub fn load_from_file(path: &std::path::Path) -> std::io::Result<Self> {
        if !path.exists() {
            return Ok(Self::new());
        }

        let json = std::fs::read_to_string(path)?;
        let items: Vec<String> = serde_json::from_str(&json)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;

        let mut history = Self::new();
        history.items = items;

        // Trim to max_size if file had more items
        if history.items.len() > history.max_size {
            let excess = history.items.len() - history.max_size;
            history.items.drain(0..excess);
        }

        Ok(history)
    }
}

/// Get the data directory for Fresh editor state
/// Returns $XDG_DATA_HOME/fresh or ~/.local/share/fresh on Linux
/// Returns ~/Library/Application Support/fresh on macOS
pub fn get_data_dir() -> std::io::Result<std::path::PathBuf> {
    let data_dir = dirs::data_dir().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "Could not determine data directory",
        )
    })?;
    Ok(data_dir.join("fresh"))
}

/// Get the path for search history file
pub fn get_search_history_path() -> std::io::Result<std::path::PathBuf> {
    Ok(get_data_dir()?.join("search_history.json"))
}

/// Get the path for replace history file
pub fn get_replace_history_path() -> std::io::Result<std::path::PathBuf> {
    Ok(get_data_dir()?.join("replace_history.json"))
}

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

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

    #[test]
    fn test_new_history_is_empty() {
        let history = InputHistory::new();
        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
        assert_eq!(history.last(), None);
    }

    #[test]
    fn test_push_adds_items() {
        let mut history = InputHistory::new();
        history.push("first".to_string());
        history.push("second".to_string());
        history.push("third".to_string());

        assert_eq!(history.len(), 3);
        assert_eq!(history.last(), Some("third"));
    }

    #[test]
    fn test_push_skips_empty_strings() {
        let mut history = InputHistory::new();
        history.push("first".to_string());
        history.push("".to_string());
        history.push("second".to_string());

        assert_eq!(history.len(), 2);
    }

    #[test]
    fn test_push_skips_consecutive_duplicates() {
        let mut history = InputHistory::new();
        history.push("first".to_string());
        history.push("second".to_string());
        history.push("second".to_string());
        history.push("second".to_string());
        history.push("third".to_string());

        assert_eq!(history.len(), 3);
        assert_eq!(history.items, vec!["first", "second", "third"]);
    }

    #[test]
    fn test_push_allows_non_consecutive_duplicates() {
        let mut history = InputHistory::new();
        history.push("search".to_string());
        history.push("other".to_string());
        history.push("search".to_string()); // Should be added

        assert_eq!(history.len(), 3);
        assert_eq!(history.items, vec!["search", "other", "search"]);
    }

    #[test]
    fn test_navigate_prev_empty_history() {
        let mut history = InputHistory::new();
        let result = history.navigate_prev("current");
        assert_eq!(result, None);
    }

    #[test]
    fn test_navigate_prev_basic() {
        let mut history = InputHistory::new();
        history.push("first".to_string());
        history.push("second".to_string());
        history.push("third".to_string());

        // First up: go to most recent
        let prev = history.navigate_prev("typing...");
        assert_eq!(prev, Some("third".to_string()));

        // Second up: go to previous
        let prev = history.navigate_prev("typing...");
        assert_eq!(prev, Some("second".to_string()));

        // Third up: go to oldest
        let prev = history.navigate_prev("typing...");
        assert_eq!(prev, Some("first".to_string()));

        // Fourth up: no more items
        let prev = history.navigate_prev("typing...");
        assert_eq!(prev, None);
    }

    #[test]
    fn test_navigate_next_without_prev() {
        let mut history = InputHistory::new();
        history.push("item".to_string());

        // navigate_next without navigate_prev should return None
        let result = history.navigate_next();
        assert_eq!(result, None);
    }

    #[test]
    fn test_navigate_next_returns_to_original() {
        let mut history = InputHistory::new();
        history.push("first".to_string());
        history.push("second".to_string());

        // Navigate backwards
        history.navigate_prev("typing...");
        history.navigate_prev("typing...");

        // Navigate forwards
        let next = history.navigate_next();
        assert_eq!(next, Some("second".to_string()));

        // Navigate past the end should return original input
        let next = history.navigate_next();
        assert_eq!(next, Some("typing...".to_string()));

        // After returning to original, we're no longer navigating
        let next = history.navigate_next();
        assert_eq!(next, None);
    }

    #[test]
    fn test_reset_navigation() {
        let mut history = InputHistory::new();
        history.push("item".to_string());

        // Start navigating
        history.navigate_prev("current");
        assert!(history.position.is_some());
        assert!(history.temp_input.is_some());

        // Reset
        history.reset_navigation();
        assert!(history.position.is_none());
        assert!(history.temp_input.is_none());
    }

    #[test]
    fn test_max_size_enforcement() {
        let mut history = InputHistory::with_capacity(3);

        history.push("first".to_string());
        history.push("second".to_string());
        history.push("third".to_string());
        assert_eq!(history.len(), 3);

        // Adding fourth item should remove first
        history.push("fourth".to_string());
        assert_eq!(history.len(), 3);
        assert_eq!(history.items, vec!["second", "third", "fourth"]);

        // Adding fifth item should remove second
        history.push("fifth".to_string());
        assert_eq!(history.len(), 3);
        assert_eq!(history.items, vec!["third", "fourth", "fifth"]);
    }

    #[test]
    fn test_clear() {
        let mut history = InputHistory::new();
        history.push("first".to_string());
        history.push("second".to_string());
        history.navigate_prev("current");

        history.clear();

        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
        assert!(history.position.is_none());
        assert!(history.temp_input.is_none());
    }

    #[test]
    fn test_up_down_up_down_sequence() {
        let mut history = InputHistory::new();
        history.push("first".to_string());
        history.push("second".to_string());
        history.push("third".to_string());

        // Up, up, down, up sequence
        assert_eq!(history.navigate_prev("current"), Some("third".to_string()));
        assert_eq!(history.navigate_prev("current"), Some("second".to_string()));
        assert_eq!(history.navigate_next(), Some("third".to_string()));
        assert_eq!(history.navigate_prev("current"), Some("second".to_string()));
    }

    #[test]
    fn test_full_navigation_cycle() {
        let mut history = InputHistory::new();
        history.push("alpha".to_string());
        history.push("beta".to_string());
        history.push("gamma".to_string());

        let original = "my search query";

        // Go all the way back
        assert_eq!(history.navigate_prev(original), Some("gamma".to_string()));
        assert_eq!(history.navigate_prev(original), Some("beta".to_string()));
        assert_eq!(history.navigate_prev(original), Some("alpha".to_string()));
        assert_eq!(history.navigate_prev(original), None); // At oldest

        // Go all the way forward
        assert_eq!(history.navigate_next(), Some("beta".to_string()));
        assert_eq!(history.navigate_next(), Some("gamma".to_string()));
        assert_eq!(history.navigate_next(), Some(original.to_string())); // Back to original
        assert_eq!(history.navigate_next(), None); // Not navigating anymore
    }

    #[test]
    #[should_panic(expected = "History max_size must be greater than 0")]
    fn test_zero_capacity_panics() {
        InputHistory::with_capacity(0);
    }

    #[test]
    fn test_single_item_history() {
        let mut history = InputHistory::with_capacity(1);

        history.push("first".to_string());
        history.push("second".to_string());
        history.push("third".to_string());

        // Should only keep the most recent item
        assert_eq!(history.len(), 1);
        assert_eq!(history.last(), Some("third"));
    }
}