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

Move keybindings from shrs_line to shrs_core #307

Merged
merged 2 commits into from
Dec 23, 2023
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
12 changes: 11 additions & 1 deletion crates/shrs/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ pub struct ShellConfig {
/// History, see [History]
#[builder(default = "Box::new(DefaultHistory::new())")]
#[builder(setter(custom))]
history: Box<dyn History<HistoryItem = String>>,
pub history: Box<dyn History<HistoryItem = String>>,

/// Keybindings, see [Keybinding]
#[builder(default = "Box::new(DefaultKeybinding::new())")]
#[builder(setter(custom))]
pub keybinding: Box<dyn Keybinding>,
}

impl ShellBuilder {
Expand Down Expand Up @@ -97,6 +102,10 @@ impl ShellBuilder {
self.history = Some(Box::new(history));
self
}
pub fn with_keybinding(mut self, keybinding: impl Keybinding + 'static) -> Self {
self.keybinding = Some(Box::new(keybinding));
self
}
}

impl ShellConfig {
Expand Down Expand Up @@ -158,6 +167,7 @@ impl ShellConfig {
lang: self.lang,
hooks: self.hooks,
signals: Signals::new().unwrap(),
keybinding: self.keybinding,
};
let mut readline = self.readline;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
use std::collections::HashMap;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use shrs_core::shell::{Context, Runtime, Shell};
use thiserror::Error;

pub type BindingFn = dyn FnMut(&Shell, &mut Context, &mut Runtime);
use crate::shell::{Context, Runtime, Shell};

pub type BindingFn = dyn Fn(&Shell, &mut Context, &mut Runtime);

/// Implement this trait to define your own keybinding system
pub trait Keybinding {
/// Return true indicates that event was handled
fn handle_key_event(
&mut self,
&self,
sh: &Shell,
ctx: &mut Context,
rt: &mut Runtime,
Expand All @@ -28,7 +29,7 @@ macro_rules! keybindings {
// TODO temp hacky macro
(|$sh:ident, $ctx:ident, $rt:ident| $($binding:expr => $func:block),* $(,)*) => {{
use $crate::keybinding::{DefaultKeybinding, parse_keybinding, BindingFn};
use $crate::_core::prelude::{Shell, Context, Runtime};
use $crate::prelude::{Shell, Context, Runtime};
DefaultKeybinding::from_iter([
$((
parse_keybinding($binding).unwrap(),
Expand Down Expand Up @@ -122,14 +123,14 @@ impl DefaultKeybinding {

impl Keybinding for DefaultKeybinding {
fn handle_key_event(
&mut self,
&self,
sh: &Shell,
ctx: &mut Context,
rt: &mut Runtime,
key_event: KeyEvent,
) -> bool {
let mut event_handled = false;
for (binding, binding_fn) in self.bindings.iter_mut() {
for (binding, binding_fn) in self.bindings.iter() {
if (key_event.code, key_event.modifiers) == *binding {
binding_fn(sh, ctx, rt);
event_handled = true;
Expand Down
2 changes: 2 additions & 0 deletions crates/shrs_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod env;
pub mod history;
pub mod hooks;
pub mod jobs;
pub mod keybinding;
pub mod lang;
pub mod output_writer;
pub mod prompt;
Expand All @@ -31,6 +32,7 @@ pub mod prelude {
env::Env,
hooks::{Hook, HookFn, Hooks, *},
jobs::{JobId, JobInfo, Jobs},
keybinding::{parse_keybinding, BindingFn, DefaultKeybinding, Keybinding},
lang::Lang,
output_writer::OutputWriter,
prompt::*,
Expand Down
2 changes: 2 additions & 0 deletions crates/shrs_core/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
history::History,
hooks::{AfterCommandCtx, BeforeCommandCtx, ChangeDirCtx, Hooks, JobExitCtx, StartupCtx},
jobs::Jobs,
keybinding::Keybinding,
lang::Lang,
output_writer::OutputWriter,
signal::Signals,
Expand All @@ -48,6 +49,7 @@ pub struct Shell {
pub lang: Box<dyn Lang>,
/// Signals to be handled
pub signals: Signals,
pub keybinding: Box<dyn Keybinding>,
}

/// Shared global shell context
Expand Down
6 changes: 2 additions & 4 deletions crates/shrs_line/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub mod completion;
pub mod cursor;
pub mod highlight;
pub mod hooks;
pub mod keybinding;
pub mod line;
pub mod menu;
pub mod painter;
Expand All @@ -35,21 +34,20 @@ pub use shrs_core as _core;
pub mod prelude {
//! Imports the commonly used structs and types

// Macros
pub use crate::{
buffer_history::{BufferHistory, DefaultBufferHistory},
completion::*,
cursor::CursorStyle,
highlight::{DefaultHighlighter, Highlighter, RuleFn, SyntaxHighlighter, SyntaxTheme},
hooks::*,
keybinding::{parse_keybinding, BindingFn, DefaultKeybinding, Keybinding},
line::{Line, LineBuilder, LineBuilderError, LineCtx, LineMode, Readline},
menu::{DefaultMenu, Menu},
painter::StyledBuf,
prompt::{DefaultPrompt, Prompt, *},
styled,
vi::*,
};
// Macros
pub use crate::{keybindings, styled};
}

#[cfg(test)]
Expand Down
11 changes: 1 addition & 10 deletions crates/shrs_line/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ pub struct Line {
#[builder(setter(custom))]
highlighter: Box<dyn Highlighter>,

/// Keybindings, see [Keybinding]
#[builder(default = "Box::new(DefaultKeybinding::new())")]
#[builder(setter(custom))]
keybinding: Box<dyn Keybinding>,

/// Custom prompt, see [Prompt]
#[builder(default = "Box::new(DefaultPrompt::new())")]
#[builder(setter(custom))]
Expand Down Expand Up @@ -187,10 +182,6 @@ impl LineBuilder {
self.highlighter = Some(Box::new(highlighter));
self
}
pub fn with_keybinding(mut self, keybinding: impl Keybinding + 'static) -> Self {
self.keybinding = Some(Box::new(keybinding));
self
}
pub fn with_prompt(mut self, prompt: impl Prompt + 'static) -> Self {
self.prompt = Some(Box::new(prompt));
self
Expand Down Expand Up @@ -236,7 +227,7 @@ impl Line {
let event = read()?;

if let Event::Key(key_event) = event {
if self.keybinding.handle_key_event(
if line_ctx.sh.keybinding.handle_key_event(
line_ctx.sh,
line_ctx.ctx,
line_ctx.rt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ley keybinding = keybindings! {
"C-l" => Command::new("clear").spawn(),
};

myline.with_keybinding(keybinding);
myshell.with_keybinding(keybinding);
```

The macro allows us to represent key combinations in terms of strings. You can
Expand Down
6 changes: 4 additions & 2 deletions shrs_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::{
process::Command,
};

use shrs::{history::FileBackedHistory, line::_core::shell::set_working_dir, prelude::*};
use shrs::{
history::FileBackedHistory, keybindings, line::_core::shell::set_working_dir, prelude::*,
};
use shrs_cd_stack::{CdStackPlugin, CdStackState};
use shrs_cd_tools::git;
use shrs_command_timer::{CommandTimerPlugin, CommandTimerState};
Expand Down Expand Up @@ -129,7 +131,6 @@ fn main() {
let readline = LineBuilder::default()
.with_completer(completer)
.with_menu(menu)
.with_keybinding(keybinding)
.with_prompt(prompt)
.build()
.expect("Could not construct readline");
Expand Down Expand Up @@ -177,6 +178,7 @@ a rusty POSIX shell | build {}"#,
.with_alias(alias)
.with_readline(readline)
.with_history(history)
.with_keybinding(keybinding)
.with_plugin(OutputCapturePlugin)
.with_plugin(CommandTimerPlugin)
.with_plugin(RunContextPlugin::new())
Expand Down