use crate::ported::params::getiparam;
use crate::ported::zle::compcore::{get_compstate_str, set_compstate_str};
pub fn _menu() -> i32 {
if getiparam("_matcher_num") > 1 {
return 1;
}
let old_list = get_compstate_str("old_list").unwrap_or_default();
if !old_list.is_empty() {
set_compstate_str("old_list", "keep");
let old_insert: i64 = get_compstate_str("old_insert")
.as_deref()
.and_then(|s| s.parse().ok())
.unwrap_or(0);
set_compstate_str("insert", &(old_insert + 1).to_string());
} else {
set_compstate_str("insert", "1");
}
1
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ported::params::setiparam;
fn reset_compstate_keys() {
set_compstate_str("old_list", "");
set_compstate_str("insert", "");
set_compstate_str("old_insert", "");
}
#[test]
fn defers_when_matcher_num_gt_one() {
let _g = crate::test_util::global_state_lock();
reset_compstate_keys();
setiparam("_matcher_num", 5);
set_compstate_str("old_list", "preserved");
let r = _menu();
assert_eq!(r, 1);
assert_eq!(get_compstate_str("old_list").as_deref(), Some("preserved"));
reset_compstate_keys();
setiparam("_matcher_num", 0);
}
#[test]
fn no_old_list_sets_insert_to_one() {
let _g = crate::test_util::global_state_lock();
reset_compstate_keys();
setiparam("_matcher_num", 1);
let r = _menu();
assert_eq!(r, 1);
assert_eq!(get_compstate_str("insert").as_deref(), Some("1"));
reset_compstate_keys();
}
#[test]
fn with_old_list_keeps_and_advances_insert() {
let _g = crate::test_util::global_state_lock();
reset_compstate_keys();
setiparam("_matcher_num", 1);
set_compstate_str("old_list", "yes");
set_compstate_str("old_insert", "7");
let r = _menu();
assert_eq!(r, 1);
assert_eq!(get_compstate_str("old_list").as_deref(), Some("keep"));
assert_eq!(
get_compstate_str("insert").as_deref(),
Some("8"),
"sh:16 insert = old_insert + 1"
);
reset_compstate_keys();
}
#[test]
fn old_insert_zero_yields_insert_one() {
let _g = crate::test_util::global_state_lock();
reset_compstate_keys();
setiparam("_matcher_num", 1);
set_compstate_str("old_list", "yes");
set_compstate_str("old_insert", "0");
let r = _menu();
assert_eq!(r, 1);
assert_eq!(get_compstate_str("insert").as_deref(), Some("1"));
reset_compstate_keys();
}
#[test]
fn old_insert_missing_yields_insert_one() {
let _g = crate::test_util::global_state_lock();
reset_compstate_keys();
setiparam("_matcher_num", 1);
set_compstate_str("old_list", "yes");
let r = _menu();
assert_eq!(r, 1);
assert_eq!(get_compstate_str("insert").as_deref(), Some("1"));
reset_compstate_keys();
}
}