Skip to content

Commit

Permalink
Issue oppiliappan#47 Remapping Keys
Browse files Browse the repository at this point in the history
Should now be able to remap most keys including the ":" that puts the
user in command mode.
  • Loading branch information
Tylryan committed Dec 7, 2022
1 parent a611b09 commit a352ecd
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 107 deletions.
157 changes: 73 additions & 84 deletions src/app/impl_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl View for App {
}

fn on_event(&mut self, e: Event) -> EventResult {
let up = CONFIGURATION.keybindings.up;
match self.file_event_recv.try_recv() {
Ok(DebouncedEvent::Write(_)) => {
let read_from_file = |file: PathBuf| -> Vec<Box<dyn HabitWrapper>> {
Expand All @@ -82,97 +81,87 @@ impl View for App {
}

// TODO: Using a match statment, it won't be able to match on a dynamic variable
// This will have to be changed to an if statement
match e {
Event::Key(Key::Right) | Event::Key(Key::Tab) | Event::Char('l') => {
self.set_focus(Absolute::Right);
return EventResult::Consumed(None);
}
Event::Key(Key::Left) | Event::Shift(Key::Tab) | Event::Char('h') => {
self.set_focus(Absolute::Left);
return EventResult::Consumed(None);
}
Event::Key(Key::Up) | Event::Char('k') => {
self.set_focus(Absolute::Up);
return EventResult::Consumed(None);
}
Event::Key(Key::Down) | Event::Char('j') => {
self.set_focus(Absolute::Down);
return EventResult::Consumed(None);
}
let move_up = e == Event::Key(Key::Up) || e == Event::Char(CONFIGURATION.move_up());
let move_down = e == Event::Key(Key::Down) || e == Event::Char(CONFIGURATION.move_down());
let move_left = e == Event::Key(Key::Left) || e == Event::Shift(Key::Tab) || e == Event::Char(CONFIGURATION.move_left());
let move_right = e == Event::Key(Key::Right) || e == Event::Key(Key::Tab) || e == Event::Char(CONFIGURATION.move_right());

Event::Char('K') => {
self.move_cursor(Absolute::Up);
return EventResult::Consumed(None);
}
Event::Char('H') => {
self.move_cursor(Absolute::Left);
return EventResult::Consumed(None);
}
Event::Char('J') => {
self.move_cursor(Absolute::Down);
return EventResult::Consumed(None);
}
Event::Char('L') => {
self.move_cursor(Absolute::Right);
return EventResult::Consumed(None);
}
let move_prev_day = e == Event::Char(CONFIGURATION.move_prev_day());
let move_next_day = e == Event::Char(CONFIGURATION.move_next_day());
let move_prev_week = e == Event::Char(CONFIGURATION.move_prev_week());
let move_next_week = e == Event::Char(CONFIGURATION.move_next_week());
let move_prev_month = e == Event::Char(CONFIGURATION.move_prev_month());
let move_next_month = e == Event::Char(CONFIGURATION.move_next_month());

Event::Char('v') => {
if self.habits.is_empty() {
return EventResult::Consumed(None);
}
if self.habits[self.focus].inner_data_ref().view_mode() == ViewMode::Week {
self.set_mode(ViewMode::Day)
} else {
self.set_mode(ViewMode::Week)
}
return EventResult::Consumed(None);
}
Event::Char('V') => {
for habit in self.habits.iter_mut() {
habit.inner_data_mut_ref().set_view_mode(ViewMode::Week);
}
return EventResult::Consumed(None);
}
Event::Key(Key::Esc) => {
for habit in self.habits.iter_mut() {
habit.inner_data_mut_ref().set_view_mode(ViewMode::Day);
}
self.reset_cursor();
return EventResult::Consumed(None);
}
let weekly_stats = e == Event::Char(CONFIGURATION.show_weekly_stats());
let monthly_stats = e == Event::Char(CONFIGURATION.show_monthly_stats());
let clear_message = e == Event::CtrlChar(CONFIGURATION.clear_msg());
let escape = e == Event::Key(Key::Esc);

/* We want sifting to be an app level function,
* that later trickles down into each habit
* */
Event::Char(']') => {
self.sift_forward();
if move_up {
self.set_focus(Absolute::Up);
return EventResult::Consumed(None);
} else if move_down {
self.set_focus(Absolute::Down);
return EventResult::Consumed(None);
} else if move_left {
self.set_focus(Absolute::Left);
return EventResult::Consumed(None);
} else if move_right {
self.set_focus(Absolute::Right);
return EventResult::Consumed(None);
} else if move_prev_day {
self.move_cursor(Absolute::Left);
return EventResult::Consumed(None);
} else if move_next_day {
self.move_cursor(Absolute::Right);
return EventResult::Consumed(None);
} else if move_prev_week {
self.move_cursor(Absolute::Up);
return EventResult::Consumed(None);
} else if move_next_week {
self.move_cursor(Absolute::Down);
return EventResult::Consumed(None);
} else if move_prev_month {
self.sift_backward();
return EventResult::Consumed(None);
} else if move_next_month {
self.sift_forward();
return EventResult::Consumed(None);
} else if clear_message {
self.message.clear();
self.message.set_kind(MessageKind::Info);
return EventResult::Consumed(None);
} else if weekly_stats {
if self.habits.is_empty() {
return EventResult::Consumed(None);
}
Event::Char('[') => {
self.sift_backward();
return EventResult::Consumed(None);
}
Event::Char('}') => {
self.reset_cursor();
return EventResult::Consumed(None);
}
Event::CtrlChar('l') => {
self.message.clear();
self.message.set_kind(MessageKind::Info);
return EventResult::Consumed(None);
if self.habits[self.focus].inner_data_ref().view_mode() == ViewMode::Week {
self.set_mode(ViewMode::Day)
} else {
self.set_mode(ViewMode::Week)
}
return EventResult::Consumed(None);

/* Every keybind that is not caught by App trickles
* down to the focused habit.
* */
_ => {
if self.habits.is_empty() {
return EventResult::Ignored;
}
self.habits[self.focus].on_event(e)
} else if monthly_stats {
for habit in self.habits.iter_mut() {
habit.inner_data_mut_ref().set_view_mode(ViewMode::Week);
}
return EventResult::Consumed(None);
} else if escape {
for habit in self.habits.iter_mut() {
habit.inner_data_mut_ref().set_view_mode(ViewMode::Day);
}
self.reset_cursor();
return EventResult::Consumed(None);
}
else {
if self.habits.is_empty() { return EventResult::Ignored; }
else { self.habits[self.focus].on_event(e) }
}
// Event::Char('}') => {
// self.reset_cursor();
// return EventResult::Consumed(None);
// }
}
}
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use cursive::theme::{BaseColor, Color, ColorStyle};
use cursive::view::Resizable;
use cursive::views::{EditView, LinearLayout, OnEventView, TextView};
use cursive::Cursive;

use crate::app::App;
use crate::utils::{GRID_WIDTH, VIEW_WIDTH};
use crate::CONFIGURATION;

static COMMANDS: &'static [&'static str] = &[
"add",
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn main() {
LinearLayout::vertical().child(NamedView::new("Main", app)),
);
s.add_layer(layout);
s.add_global_callback(':', |s| open_command_window(s));
s.add_global_callback(CONFIGURATION.cmd_mode(), |s| open_command_window(s));

s.set_theme(theme::theme_gen());
s.run();
Expand Down
97 changes: 76 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cursive::theme::{BaseColor, Color};
// use cursive::event::Event as CursiveEvent;
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -76,12 +77,12 @@ fn dark_red() -> String { "dark white".into() }
impl Default for Colors {
fn default() -> Self {
Colors {
reached: cyan(),
todo: magenta(),
reached: cyan() ,
todo: magenta() ,
inactive: light_black(),
future: magenta(),
future: magenta() ,
cursor: light_black(),
today: dark_red(),
today: dark_red() ,
stats_bar_bg: light_black(),
stats_bar_fg: light_black(),
}
Expand All @@ -91,27 +92,67 @@ impl Default for Colors {
#[derive(Serialize, Deserialize)]
pub struct KeyBindings {
#[serde(default = "up")]
pub up: char ,
pub up: char ,
#[serde(default = "down")]
pub down: char ,
pub down: char ,
#[serde(default = "left")]
pub left: char ,
pub left: char ,
#[serde(default = "right")]
pub right: char ,
pub right: char ,
#[serde(default = "prev_day")]
pub prev_day: char ,
#[serde(default = "next_day")]
pub next_day: char ,
#[serde(default = "prev_week")]
pub prev_week: char ,
#[serde(default = "next_week")]
pub next_week: char ,
#[serde(default = "prev_month")]
pub prev_month: char ,
#[serde(default = "next_month")]
pub next_month: char ,
#[serde(default = "weekly_stats")]
pub weekly_stats: char ,
#[serde(default = "monthly_stats")]
pub monthly_stats: char ,
#[serde(default = "clear_message")]
pub clear_message: char ,
#[serde(default = "command_mode")]
pub command_mode: char ,
}

fn up() -> char { 'k' }
fn down() -> char { 'j' }
fn left() -> char { 'h' }
fn right() -> char { 'l' }
fn up() -> char { 'k' }
fn down() -> char { 'j' }
fn left() -> char { 'h' }
fn right() -> char { 'l' }
fn prev_day() -> char { 'H' }
fn next_day() -> char { 'L' }
fn prev_week() -> char { 'K' }
fn next_week() -> char { 'J' }
fn prev_month() -> char { '[' }
fn next_month() -> char { ']' }
fn weekly_stats() -> char { 'v' }
fn monthly_stats() -> char { 'V' }
fn clear_message() -> char { 'l' }
fn command_mode() -> char { ':' }

impl Default for KeyBindings {
fn default() -> Self {
KeyBindings {
up: up() ,
down: down() ,
left: left() ,
right: right(),
up: up() ,
down: down() ,
left: left() ,
right: right() ,
prev_day: prev_day() ,
next_day: next_day() ,
prev_week: prev_week() ,
next_week: next_week() ,
prev_month: prev_month() ,
next_month: next_month() ,
weekly_stats: weekly_stats() ,
monthly_stats: monthly_stats() ,
clear_message: clear_message() ,
command_mode: command_mode() ,
}
}
}
Expand Down Expand Up @@ -155,17 +196,31 @@ impl AppConfig {
return Color::parse(&self.colors.cursor).unwrap_or(Color::Light(BaseColor::Black));
}
pub fn today_color(&self) -> Color {
return Color::parse(&self.colors.today).unwrap_or(Color::Dark(BaseColor::Red));
return Color::parse(&self.colors.today).unwrap_or(Color::Dark(BaseColor::Blue));
}
pub fn stats_bar_bg_color(&self) -> Color {
return Color::parse(&self.colors.stats_bar_bg).unwrap_or(Color::Light(BaseColor::Black));
}
pub fn stats_bar_fg_color(&self) -> Color {
return Color::parse(&self.colors.stats_bar_fg).unwrap_or(Color::Dark(BaseColor::White));
}
pub fn move_up(self) -> char {
return self.keybindings.up;
return Color::parse(&self.colors.stats_bar_fg).unwrap_or(Color::Dark(BaseColor::Cyan));
}

pub fn move_up(&self) -> char { return self.keybindings.up; }
pub fn move_down(&self) -> char { return self.keybindings.down; }
pub fn move_left(&self) -> char { return self.keybindings.left; }
pub fn move_right(&self) -> char { return self.keybindings.right; }
pub fn move_prev_day(&self) -> char { return self.keybindings.prev_day; }
pub fn move_next_day(&self) -> char { return self.keybindings.next_day; }
pub fn move_prev_week(&self) -> char { return self.keybindings.prev_week; }
pub fn move_next_week(&self) -> char { return self.keybindings.next_week; }
pub fn move_prev_month(&self) -> char { return self.keybindings.prev_month; }
pub fn move_next_month(&self) -> char { return self.keybindings.next_month; }
pub fn show_weekly_stats(&self) -> char { return self.keybindings.weekly_stats; }
pub fn show_monthly_stats(&self) -> char { return self.keybindings.monthly_stats; }
pub fn clear_msg(&self) -> char { return self.keybindings.clear_message; }
pub fn cmd_mode(&self) -> char { return self.keybindings.command_mode; }


}

pub fn load_configuration_file() -> AppConfig {
Expand Down

0 comments on commit a352ecd

Please sign in to comment.