perch/src/keys.rs

77 lines
2.9 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
})
};
}
mod edit; pub use self::edit::*;
mod save; pub use self::save::*;
mod quit; pub use self::quit::*;
impl Handle<TuiIn> for Taggart {
fn handle (&mut self, input: &TuiIn) -> Perhaps<bool> {
let min = self.offset;
let max = self.offset + self.display.h().saturating_sub(1);
let event = &*input.event();
match &self.mode {
Some(Mode::Edit { .. }) => self.mode_edit(event),
Some(Mode::Save { value }) => self.mode_save(event, *value),
Some(Mode::Quit { value }) => self.mode_quit(event, *value),
None => match event {
press!(Char('q')) => { self.quit_begin(&input) },
press!(Char('w')) => { self.save_begin() },
press!(Enter) => { self.edit_begin() },
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!(Char(' ')) => { open(self.entries[self.cursor].path.as_ref())?; }
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); }
_ => {}
}
_ => todo!("{:?}", self.mode)
}
self.clamp(min, max);
Ok(None)
}
}
impl Taggart {
fn clamp (&mut self, min: usize, max: usize) {
if self.cursor < min {
self.offset = self.cursor;
}
if self.cursor > max {
self.offset += self.cursor - max;
}
if self.offset > self.entries.len().saturating_sub(self.display.h()) {
self.offset = self.entries.len().saturating_sub(self.display.h())
}
if self.cursor >= self.entries.len() {
self.cursor = self.entries.len().saturating_sub(1)
}
if self.column + 1 > self.columns.0.len() {
self.column = self.columns.0.len().saturating_sub(1)
}
}
}