Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract LineCtx into its own struct #417

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/shrs_core/src/keybinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use std::collections::HashMap;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use thiserror::Error;

use crate::{shell::{Context, Runtime, Shell}, prelude::LineCtx};
use crate::{shell::{Context, Runtime, Shell}, prelude::LineStateBundle};

pub type BindingFn = dyn Fn(&mut LineCtx);
pub type BindingFn = dyn Fn(&mut LineStateBundle);

/// Implement this trait to define your own keybinding system
pub trait Keybinding {
/// Return true indicates that event was handled
fn handle_key_event(
&self,
line: &mut LineCtx,
line: &mut LineStateBundle,
key_event: KeyEvent,
) -> bool;
fn get_info(&self) -> &HashMap<String, String>;
Expand All @@ -26,13 +26,13 @@ pub type Binding = (KeyCode, KeyModifiers);
#[macro_export]
macro_rules! keybindings {
// TODO temp hacky macro
(|$line:ident| $($binding:expr => ($desc:expr, $func:block)),* $(,)*) => {{
(|$state:ident| $($binding:expr => ($desc:expr, $func:block)),* $(,)*) => {{
use $crate::keybinding::{DefaultKeybinding, parse_keybinding, BindingFn};
use $crate::prelude::{LineCtx};
use $crate::prelude::{LineStateBundle};
DefaultKeybinding::from_iter([
$((
parse_keybinding($binding).unwrap(),
Box::new(|$line: &mut LineCtx| {
Box::new(|$state: &mut LineStateBundle| {
$func;
}) as Box<BindingFn>,
$binding.to_string(),
Expand Down Expand Up @@ -128,7 +128,7 @@ impl DefaultKeybinding {
impl Keybinding for DefaultKeybinding {
fn handle_key_event(
&self,
line: &mut LineCtx,
line: &mut LineStateBundle,
key_event: KeyEvent,
) -> bool {
let mut event_handled = false;
Expand Down
Loading