ncurses: compute whole modifier table at runtime

This commit is contained in:
Alexandre Bury 2017-11-20 11:44:23 -08:00
parent b2097bd1c8
commit 0c39f52016

View File

@ -365,26 +365,6 @@ where
}
}
fn parse_modifier(code: i32, key: Key) -> Event {
match code {
0 => Event::Alt(key),
1 => Event::AltShift(key),
2 => Event::Ctrl(key),
3 => Event::CtrlShift(key),
4 => Event::CtrlAlt(key),
_ => {
warn!("Parsing invalid modifier: {} for key {:?}", code, key);
Event::Unknown(split_i32(code))
}
}
}
fn add_modifiers(start: i32, key: Key, map: &mut HashMap<i32, Event>) {
for i in 0..5 {
map.insert(start + i, parse_modifier(i, key));
}
}
fn add_fn<F>(start: i32, with_key: F, map: &mut HashMap<i32, Event>)
where
F: Fn(Key) -> Event,
@ -455,25 +435,45 @@ fn initialize_keymap() -> HashMap<i32, Event> {
add_fn(313, Event::Alt, &mut map);
// Those codes actually vary between ncurses versions...
// Use ncurses::keyname to find the key representing kDC3 (alt-DEL)
let del_offset = (512..1024)
.find(|&code| {
ncurses::keyname(code)
.map(|name| &name == "kDC3")
.unwrap_or(false)
})
.unwrap_or(522);
add_modifiers(del_offset, Key::Del, &mut map);
add_modifiers(del_offset + 6, Key::Down, &mut map);
add_modifiers(del_offset + 11, Key::End, &mut map);
add_modifiers(del_offset + 17, Key::Home, &mut map);
add_modifiers(del_offset + 23, Key::Ins, &mut map);
add_modifiers(del_offset + 28, Key::Left, &mut map);
add_modifiers(del_offset + 33, Key::PageDown, &mut map);
add_modifiers(del_offset + 38, Key::PageUp, &mut map);
add_modifiers(del_offset + 43, Key::Right, &mut map);
add_modifiers(del_offset + 49, Key::Up, &mut map);
let key_names = hashmap!{
"DC" => Key::Del,
"DN" => Key::Down,
"END" => Key::End,
"HOM" => Key::Home,
"IC" => Key::Ins,
"LFT" => Key::Left,
"NXT" => Key::PageDown,
"PRV" => Key::PageUp,
"RIT" => Key::Right,
"UP" => Key::Up,
};
for code in 512..1024 {
let name = match ncurses::keyname(code) {
Some(name) => name,
None => continue,
};
if !name.starts_with("k") {
continue;
}
let (key_name, modifier) = name[1..].split_at(name.len() - 2);
let key = match key_names.get(key_name) {
Some(&key) => key,
None => continue,
};
let event = match modifier {
"3" => Event::Alt(key),
"4" => Event::AltShift(key),
"5" => Event::Ctrl(key),
"6" => Event::CtrlShift(key),
"7" => Event::CtrlAlt(key),
_ => continue,
};
map.insert(code, event);
}
map
}