perch/src/keys.rs

67 lines
2.6 KiB
Rust

use crate::*;
use opener::open;
macro_rules! press {
(Shift-$($key:tt)+) => {
Event::Key(KeyEvent {
code: KeyCode::$($key)+,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::SHIFT,
state: KeyEventState::NONE
})
};
($($key:tt)+) => {
Event::Key(KeyEvent {
code: KeyCode::$($key)+,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
state: KeyEventState::NONE
})
};
}
impl Handle<TuiIn> for Taggart {
fn handle (&mut self, input: &TuiIn) -> Perhaps<bool> {
let x_min = self.offset;
let x_max = self.offset + self.size.h().saturating_sub(1);
let event = &*input.event();
match &self.editing {
None => match event {
press!(Up) => { self.cursor = self.cursor.saturating_sub(1); },
press!(Down) => { self.cursor = self.cursor + 1; },
press!(PageUp) => { self.cursor = self.cursor.saturating_sub(PAGE_SIZE); },
press!(PageDown) => { self.cursor += PAGE_SIZE; },
press!(Left) => { self.column = self.column.saturating_sub(1); },
press!(Right) => { self.column = self.column + 1; },
press!(Enter) => { self.edit_begin() },
press!(Char(' ')) => { open(&self.paths[self.cursor].path)?; }
press!(Char(']')) => { self.columns.0[self.column].width += 1; }
press!(Char('[')) => { self.columns.0[self.column].width =
self.columns.0[self.column].width.saturating_sub(1).max(5); }
_ => {}
},
Some(_value) => match event {
press!(Char(c)) => self.edit_insert(*c),
press!(Shift-Char(c)) => self.edit_insert(c.to_uppercase().next().unwrap()),
press!(Backspace) => self.edit_backspace(),
press!(Delete) => self.edit_delete(),
press!(Enter) => self.edit_cancel(),
press!(Esc) => self.edit_confirm(),
_ => {}
},
}
if self.cursor < x_min {
self.offset = self.cursor;
}
if self.cursor > x_max {
self.offset += self.cursor - x_max;
}
if self.cursor >= self.paths.len() {
self.cursor = self.paths.len().saturating_sub(1)
}
if self.column + 1 > self.columns.0.len() {
self.column = self.columns.0.len().saturating_sub(1)
}
Ok(None)
}
}