use std::time::{Duration, Instant};
use hjkl_keymap::{Chord, KeyEvent};
use crate::app::keymap::HjklMode;
pub struct Entry {
pub key: String,
pub desc: String,
}
pub fn format_key(ev: KeyEvent, leader: char) -> String {
Chord(vec![ev]).to_notation(leader)
}
pub fn entries_for(
km: &hjkl_keymap::Keymap<crate::keymap_actions::AppAction, HjklMode>,
mode: HjklMode,
prefix: &[KeyEvent],
leader: char,
) -> Vec<Entry> {
let chord = Chord(prefix.to_vec());
let mut entries: Vec<Entry> = km
.children_all(mode, &chord)
.into_iter()
.map(|(ev, binding)| {
let key = format_key(ev, leader);
let desc = match binding {
Some(b) => b.desc.clone(),
None => "\u{2026}".to_string(), };
Entry { key, desc }
})
.collect();
entries.sort_by(|a, b| a.key.cmp(&b.key));
entries
}
pub fn should_show(
pending_at: Option<Instant>,
delay: Duration,
enabled: bool,
now: Instant,
) -> bool {
if !enabled {
return false;
}
match pending_at {
Some(at) => now.duration_since(at) >= delay,
None => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn should_show_returns_false_when_disabled() {
let at = Instant::now() - Duration::from_secs(2);
assert!(!should_show(
Some(at),
Duration::from_millis(500),
false,
Instant::now()
));
}
#[test]
fn should_show_returns_false_when_no_prefix() {
assert!(!should_show(
None,
Duration::from_millis(500),
true,
Instant::now()
));
}
#[test]
fn should_show_returns_false_before_delay() {
let at = Instant::now();
assert!(!should_show(Some(at), Duration::from_millis(500), true, at));
}
#[test]
fn should_show_returns_true_after_delay() {
let at = Instant::now() - Duration::from_secs(2);
assert!(should_show(
Some(at),
Duration::from_millis(500),
true,
Instant::now()
));
}
}