2015-05-15 19:16:58 +00:00
|
|
|
//! User-input events and their effects.
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
use std::fmt;
|
2015-05-15 00:41:17 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
use ncurses;
|
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
use ::Cursive;
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Callback is a function that can be triggered by an event.
|
|
|
|
/// It has a mutable access to the cursive root.
|
2015-05-24 00:07:22 +00:00
|
|
|
pub type Callback = Box<Fn(&mut Cursive)>;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
|
|
|
/// Answer to an event notification.
|
|
|
|
/// The event can be consumed or ignored.
|
|
|
|
pub enum EventResult {
|
|
|
|
/// The event was ignored. The parent can keep handling it.
|
|
|
|
Ignored,
|
|
|
|
/// The event was consumed. An optionnal callback to run is attached.
|
2015-05-24 00:07:22 +00:00
|
|
|
Consumed(Option<Rc<Callback>>),
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
2015-05-28 01:04:33 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq,Eq,Clone,Copy,Hash)]
|
|
|
|
pub enum Key {
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Both Enter and numpad Enter
|
2015-05-28 01:04:33 +00:00
|
|
|
Enter,
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Tabulation key
|
2015-05-28 05:21:23 +00:00
|
|
|
Tab,
|
2015-05-28 23:19:16 +00:00
|
|
|
ShiftTab,
|
2015-05-28 21:44:10 +00:00
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
Up,
|
|
|
|
Down,
|
2015-05-28 01:04:33 +00:00
|
|
|
PageUp,
|
|
|
|
PageDown,
|
|
|
|
Backspace,
|
|
|
|
Home,
|
|
|
|
End,
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Delete key
|
2015-05-28 01:04:33 +00:00
|
|
|
Del,
|
2015-05-29 18:31:20 +00:00
|
|
|
ShiftDel,
|
|
|
|
CtrlDel,
|
2015-05-28 19:22:39 +00:00
|
|
|
/// Insert key
|
|
|
|
Ins,
|
2015-05-28 20:49:13 +00:00
|
|
|
/// Escape key. Often buffered by the terminal,
|
|
|
|
/// may appear with a delay or along with the next key.
|
|
|
|
Esc,
|
2015-05-28 21:44:10 +00:00
|
|
|
// Left arrow while shift is pressed
|
2015-05-28 23:19:16 +00:00
|
|
|
CtrlPageUp,
|
|
|
|
CtrlPageDown,
|
2015-05-28 21:44:10 +00:00
|
|
|
ShiftLeft,
|
|
|
|
ShiftRight,
|
2015-05-29 18:31:20 +00:00
|
|
|
CtrlShiftLeft,
|
|
|
|
CtrlShiftRight,
|
2015-05-28 21:44:10 +00:00
|
|
|
CtrlLeft,
|
|
|
|
CtrlRight,
|
|
|
|
CtrlUp,
|
|
|
|
CtrlDown,
|
2015-05-28 21:57:21 +00:00
|
|
|
F(u8),
|
2015-05-29 18:31:20 +00:00
|
|
|
CtrlF(u8),
|
|
|
|
ShiftF(u8),
|
|
|
|
CtrlShiftF(u8),
|
2015-05-28 21:44:10 +00:00
|
|
|
CtrlChar(char),
|
2015-05-28 01:04:33 +00:00
|
|
|
Unknown(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Key {
|
|
|
|
pub fn from_ncurses(ch: i32) -> Self {
|
|
|
|
match ch {
|
2015-05-29 18:31:20 +00:00
|
|
|
// Tab is '\t'
|
2015-05-28 05:21:23 +00:00
|
|
|
9 => Key::Tab,
|
2015-05-29 18:31:20 +00:00
|
|
|
// Treat '\n' and the numpad Enter the same
|
2015-05-28 19:22:39 +00:00
|
|
|
10 | ncurses::KEY_ENTER => Key::Enter,
|
2015-05-29 18:31:20 +00:00
|
|
|
// This is the escape key when pressed by itself.
|
|
|
|
// When used for control sequences, it should have been caught earlier.
|
2015-05-28 20:49:13 +00:00
|
|
|
27 => Key::Esc,
|
2015-05-29 18:31:20 +00:00
|
|
|
// `Backspace` sends 127, but Ctrl-H sends `Backspace`
|
2015-05-28 21:44:10 +00:00
|
|
|
127 | ncurses::KEY_BACKSPACE => Key::Backspace,
|
2015-05-28 23:19:16 +00:00
|
|
|
// Those keys don't seem to be documented...
|
|
|
|
515 => Key::CtrlDel,
|
2015-05-28 21:44:10 +00:00
|
|
|
521 => Key::CtrlDown,
|
|
|
|
541 => Key::CtrlLeft,
|
2015-05-29 18:31:20 +00:00
|
|
|
542 => Key::CtrlShiftLeft,
|
2015-05-28 23:19:16 +00:00
|
|
|
546 => Key::CtrlPageDown,
|
|
|
|
551 => Key::CtrlPageUp,
|
2015-05-28 21:44:10 +00:00
|
|
|
556 => Key::CtrlRight,
|
2015-05-29 18:31:20 +00:00
|
|
|
557 => Key::CtrlShiftRight,
|
2015-05-28 21:44:10 +00:00
|
|
|
562 => Key::CtrlUp,
|
2015-05-30 03:44:53 +00:00
|
|
|
ncurses::KEY_DC => Key::Del,
|
|
|
|
ncurses::KEY_IC => Key::Ins,
|
2015-05-28 23:19:16 +00:00
|
|
|
ncurses::KEY_BTAB => Key::ShiftTab,
|
2015-05-28 21:44:10 +00:00
|
|
|
ncurses::KEY_SLEFT => Key::ShiftLeft,
|
|
|
|
ncurses::KEY_SRIGHT => Key::ShiftRight,
|
|
|
|
ncurses::KEY_LEFT => Key::Left,
|
|
|
|
ncurses::KEY_RIGHT => Key::Right,
|
|
|
|
ncurses::KEY_UP => Key::Up,
|
|
|
|
ncurses::KEY_DOWN => Key::Down,
|
2015-05-28 01:04:33 +00:00
|
|
|
ncurses::KEY_PPAGE => Key::PageUp,
|
|
|
|
ncurses::KEY_NPAGE => Key::PageDown,
|
|
|
|
ncurses::KEY_HOME => Key::Home,
|
|
|
|
ncurses::KEY_END => Key::End,
|
2015-05-29 18:31:20 +00:00
|
|
|
ncurses::KEY_SDC => Key::ShiftDel,
|
|
|
|
// All Fn keys use the same enum with associated number
|
2015-05-28 21:57:21 +00:00
|
|
|
f @ ncurses::KEY_F1 ... ncurses::KEY_F15 => Key::F((f - ncurses::KEY_F0) as u8),
|
2015-05-29 18:31:20 +00:00
|
|
|
f @ 281 ... 291 => Key::ShiftF((f - 281 + 5) as u8),
|
|
|
|
f @ 293 ... 303 => Key::CtrlF((f - 293 + 5) as u8),
|
|
|
|
f @ 305 ... 315 => Key::CtrlShiftF((f - 305 + 5) as u8),
|
|
|
|
// Shift and Ctrl F{1-4} need escape sequences...
|
|
|
|
|
|
|
|
// TODO: shift and ctrl Fn keys
|
2015-05-28 23:19:16 +00:00
|
|
|
// Avoids 8-10 (H,I,J), they are used by other commands.
|
2015-05-28 21:44:10 +00:00
|
|
|
c @ 1 ... 7 | c @ 11 ... 25 => Key::CtrlChar(('a' as u8 + (c-1) as u8) as char),
|
2015-05-28 01:04:33 +00:00
|
|
|
_ => Key::Unknown(ch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Key {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Key::Unknown(ch) => write!(f, "Unknown: {}", ch),
|
2015-05-28 21:44:10 +00:00
|
|
|
Key::CtrlChar(ch) => write!(f, "Ctrl-{}", ch),
|
2015-05-28 21:57:21 +00:00
|
|
|
Key::F(n) => write!(f, "F{}", n),
|
2015-05-29 18:31:20 +00:00
|
|
|
Key::ShiftF(n) => write!(f, "Shift-F{}", n),
|
|
|
|
Key::CtrlF(n) => write!(f, "Ctrl-F{}", n),
|
|
|
|
Key::CtrlShiftF(n) => write!(f, "Ctrl-Shift-F{}", n),
|
2015-05-28 01:04:33 +00:00
|
|
|
key => write!(f, "{}", match key {
|
2015-05-28 21:44:10 +00:00
|
|
|
Key::Left => "Left",
|
|
|
|
Key::Right => "Right",
|
|
|
|
Key::Down => "Down",
|
|
|
|
Key::Up => "Up",
|
2015-05-29 18:31:20 +00:00
|
|
|
Key::CtrlShiftLeft => "Ctrl-Shift-Left",
|
|
|
|
Key::CtrlShiftRight => "Ctrl-Shift-Right",
|
2015-05-28 21:44:10 +00:00
|
|
|
Key::ShiftLeft => "Shift-Left",
|
|
|
|
Key::ShiftRight => "Shift-Right",
|
2015-05-29 18:31:20 +00:00
|
|
|
Key::ShiftDel => "Shift-Del",
|
2015-05-28 23:19:16 +00:00
|
|
|
Key::CtrlDel => "Ctrl-Del",
|
2015-05-28 21:44:10 +00:00
|
|
|
Key::CtrlLeft => "Ctrl-Left",
|
|
|
|
Key::CtrlRight => "Ctrl-Right",
|
|
|
|
Key::CtrlUp => "Ctrl-Up",
|
|
|
|
Key::CtrlDown => "Ctrl-Down",
|
2015-05-28 23:19:16 +00:00
|
|
|
Key::CtrlPageUp => "Ctrl-PageUp",
|
|
|
|
Key::CtrlPageDown => "Ctrl-PageDown",
|
2015-05-28 01:04:33 +00:00
|
|
|
Key::PageUp => "PageUp",
|
|
|
|
Key::PageDown => "PageDown",
|
|
|
|
Key::Home => "Home",
|
|
|
|
Key::End => "End",
|
|
|
|
Key::Backspace => "Backspace",
|
|
|
|
Key::Del => "Del",
|
2015-05-28 05:21:23 +00:00
|
|
|
Key::Enter => "Enter",
|
2015-05-28 23:19:16 +00:00
|
|
|
Key::ShiftTab => "Shift-Tab",
|
2015-05-28 05:21:23 +00:00
|
|
|
Key::Tab => "Tab",
|
2015-05-28 19:22:39 +00:00
|
|
|
Key::Ins => "Ins",
|
2015-05-28 20:49:13 +00:00
|
|
|
Key::Esc => "Esc",
|
2015-05-29 18:31:20 +00:00
|
|
|
_ => "Missing key label",
|
2015-05-28 01:04:33 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq,Eq,Clone,Copy,Hash)]
|
|
|
|
pub enum Event {
|
|
|
|
CharEvent(char),
|
|
|
|
KeyEvent(Key),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ToEvent {
|
|
|
|
fn to_event(self) -> Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToEvent for char {
|
|
|
|
fn to_event(self) -> Event {
|
|
|
|
Event::CharEvent(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToEvent for Key {
|
|
|
|
fn to_event(self) -> Event {
|
|
|
|
Event::KeyEvent(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToEvent for Event {
|
|
|
|
fn to_event(self) -> Event {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|