|
| 1 | +//! Command palette history: dedupe newest-first with a cap, and cursor recall. |
| 2 | +
|
| 3 | +const HISTORY_LIMIT: usize = 20; |
| 4 | + |
| 5 | +/// Recall outcome: the query to show, with `index` `None` for the live "jj " query past the newest entry. |
| 6 | +pub struct HistoryRecall { |
| 7 | + pub query: String, |
| 8 | + pub index: Option<usize>, |
| 9 | +} |
| 10 | + |
| 11 | +/// Push `command` onto `history` newest-first, deduped, capped at the limit. |
| 12 | +pub fn record(command: &str, history: &[String]) -> Vec<String> { |
| 13 | + let command = command.trim(); |
| 14 | + if command.is_empty() { |
| 15 | + return history.to_vec(); |
| 16 | + } |
| 17 | + let mut values = vec![command.to_owned()]; |
| 18 | + values.extend( |
| 19 | + history |
| 20 | + .iter() |
| 21 | + .filter(|value| value.as_str() != command) |
| 22 | + .cloned(), |
| 23 | + ); |
| 24 | + values.truncate(HISTORY_LIMIT); |
| 25 | + values |
| 26 | +} |
| 27 | + |
| 28 | +/// Walk the cursor one step (`older` toward older entries, else newer); `None` only when history is empty. |
| 29 | +pub fn recall( |
| 30 | + history: &[String], |
| 31 | + history_index: Option<usize>, |
| 32 | + older: bool, |
| 33 | +) -> Option<HistoryRecall> { |
| 34 | + if history.is_empty() { |
| 35 | + return None; |
| 36 | + } |
| 37 | + let index = if older { |
| 38 | + Some( |
| 39 | + history_index |
| 40 | + .map(|index| index.saturating_add(1)) |
| 41 | + .unwrap_or(0) |
| 42 | + .min(history.len() - 1), |
| 43 | + ) |
| 44 | + } else if let Some(index) = history_index |
| 45 | + && index > 0 |
| 46 | + { |
| 47 | + Some(index - 1) |
| 48 | + } else { |
| 49 | + None |
| 50 | + }; |
| 51 | + Some(HistoryRecall { |
| 52 | + query: index |
| 53 | + .map(|index| format!("jj {}", history[index])) |
| 54 | + .unwrap_or_else(|| "jj ".to_owned()), |
| 55 | + index, |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use super::*; |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn record_dedupes_and_keeps_newest_first() { |
| 65 | + let history = record("status", &[]); |
| 66 | + let history = record("log -r @", &history); |
| 67 | + let history = record("status", &history); |
| 68 | + |
| 69 | + assert_eq!(history, vec!["status", "log -r @"]); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn record_caps_at_history_limit() { |
| 74 | + let mut history = Vec::new(); |
| 75 | + for i in 0..(HISTORY_LIMIT + 5) { |
| 76 | + history = record(&format!("cmd {i}"), &history); |
| 77 | + } |
| 78 | + assert_eq!(history.len(), HISTORY_LIMIT); |
| 79 | + assert_eq!(history[0], format!("cmd {}", HISTORY_LIMIT + 4)); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn recall_walks_older_and_newer_entries() { |
| 84 | + let history = vec!["status".to_owned(), "log -r @".to_owned()]; |
| 85 | + |
| 86 | + let first = recall(&history, None, true).expect("first recall"); |
| 87 | + assert_eq!(first.query, "jj status"); |
| 88 | + assert_eq!(first.index, Some(0)); |
| 89 | + |
| 90 | + let second = recall(&history, first.index, true).expect("second recall"); |
| 91 | + assert_eq!(second.query, "jj log -r @"); |
| 92 | + assert_eq!(second.index, Some(1)); |
| 93 | + |
| 94 | + let newer = recall(&history, second.index, false).expect("newer recall"); |
| 95 | + assert_eq!(newer.query, "jj status"); |
| 96 | + assert_eq!(newer.index, Some(0)); |
| 97 | + |
| 98 | + let live_query = recall(&history, newer.index, false).expect("live query recall"); |
| 99 | + assert_eq!(live_query.query, "jj "); |
| 100 | + assert_eq!(live_query.index, None); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn recall_on_empty_history_returns_none() { |
| 105 | + assert!(recall(&[], None, true).is_none()); |
| 106 | + } |
| 107 | +} |
0 commit comments