Add Insert and Delete keys

This commit is contained in:
Dániel Buga 2022-04-20 00:21:58 +02:00 committed by Dongdong Zhou
parent 4a53cddbf2
commit 5d8978161a
1 changed files with 15 additions and 13 deletions

View File

@ -683,7 +683,7 @@ macro_rules! modval {
} }
// Generates ANSI sequences to move the cursor by one position. // Generates ANSI sequences to move the cursor by one position.
macro_rules! move_cursor_one { macro_rules! term_sequence {
([], $evt:ident, $pre:literal, $post:literal) => { ([], $evt:ident, $pre:literal, $post:literal) => {
if $evt.mods.is_empty() { if $evt.mods.is_empty() {
return Some(concat!($pre, $post)); return Some(concat!($pre, $post));
@ -691,21 +691,21 @@ macro_rules! move_cursor_one {
}; };
([all], $evt:ident, $pre:literal, $post:literal) => { ([all], $evt:ident, $pre:literal, $post:literal) => {
{ {
move_cursor_one!([], $evt, $pre, $post); term_sequence!([], $evt, $pre, $post);
move_cursor_one!([shift, alt, ctrl], $evt, $pre, $post); term_sequence!([shift, alt, ctrl], $evt, $pre, $post);
move_cursor_one!([alt | shift, ctrl | shift, alt | ctrl], $evt, $pre, $post); term_sequence!([alt | shift, ctrl | shift, alt | ctrl], $evt, $pre, $post);
move_cursor_one!([alt | ctrl | shift], $evt, $pre, $post); term_sequence!([alt | ctrl | shift], $evt, $pre, $post);
return None; return None;
} }
}; };
([$($mod:ident)|+], $evt:ident, $pre:literal, $post:literal) => { ([$($mod:ident)|+], $evt:ident, $pre:literal, $post:literal) => {
if $evt.mods == modifiers!($($mod)|+) { if $evt.mods == modifiers!($($mod)|+) {
return Some(concat!($pre, "1;", modval!($($mod)|+), $post)); return Some(concat!($pre, ";", modval!($($mod)|+), $post));
} }
}; };
([$($($mod:ident)|+),+], $evt:ident, $pre:literal, $post:literal) => { ([$($($mod:ident)|+),+], $evt:ident, $pre:literal, $post:literal) => {
$( $(
move_cursor_one!([$($mod)|+], $evt, $pre, $post); term_sequence!([$($mod)|+], $evt, $pre, $post);
)+ )+
}; };
} }
@ -767,12 +767,14 @@ macro_rules! move_cursor_one {
Key::Escape => Some("\x1b"), Key::Escape => Some("\x1b"),
// The following either expands to `\x1b[X` or `\x1b[1;NX` where N is a modifier value // The following either expands to `\x1b[X` or `\x1b[1;NX` where N is a modifier value
Key::ArrowUp => move_cursor_one!([all], key, "\x1b[", "A"), Key::ArrowUp => term_sequence!([all], key, "\x1b[1", "A"),
Key::ArrowDown => move_cursor_one!([all], key, "\x1b[", "B"), Key::ArrowDown => term_sequence!([all], key, "\x1b[1", "B"),
Key::ArrowRight => move_cursor_one!([all], key, "\x1b[", "C"), Key::ArrowRight => term_sequence!([all], key, "\x1b[1", "C"),
Key::ArrowLeft => move_cursor_one!([all], key, "\x1b[", "D"), Key::ArrowLeft => term_sequence!([all], key, "\x1b[1", "D"),
Key::Home => move_cursor_one!([all], key, "\x1b[", "H"), Key::Home => term_sequence!([all], key, "\x1b[1", "H"),
Key::End => move_cursor_one!([all], key, "\x1b[", "F"), Key::End => term_sequence!([all], key, "\x1b[1", "F"),
Key::Insert => term_sequence!([all], key, "\x1b[2", "~"),
Key::Delete => term_sequence!([all], key, "\x1b[3", "~"),
_ => None, _ => None,
} }
} }