Skip to content

Commit

Permalink
Add example for custom bindings
Browse files Browse the repository at this point in the history
- Extend Meta-F,Alt+Right feature for hint partial completion
  • Loading branch information
gwenn committed Mar 6, 2021
1 parent bf5bbfd commit 3d692d3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
25 changes: 17 additions & 8 deletions examples/custom_key_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@ impl Highlighter for MyHelper {
}
}

#[derive(Clone)]
struct CompleteHintHandler;
impl ConditionalEventHandler for CompleteHintHandler {
fn handle(&self, evt: &Event, _: RepeatCount, _: bool, ctx: &EventContext) -> Option<Cmd> {
debug_assert_eq!(*evt, Event::from(KeyEvent::ctrl('E')));
if ctx.has_hint() {
Some(Cmd::CompleteHint)
if !ctx.has_hint() {
return None; // default
}
if let Some(k) = evt.get(0) {
#[allow(clippy::if_same_then_else)]
if *k == KeyEvent::ctrl('E') {
Some(Cmd::CompleteHint)
} else if *k == KeyEvent::alt('f') && ctx.line().len() == ctx.pos() {
Some(Cmd::CompleteHint) // TODO give access to hint
} else {
None
}
} else {
None // default
unreachable!()
}
}
}
Expand All @@ -71,10 +81,9 @@ fn main() {
let mut rl = Editor::<MyHelper>::new();
rl.set_helper(Some(MyHelper(HistoryHinter {})));

rl.bind_sequence(
KeyEvent::ctrl('E'),
EventHandler::Conditional(Box::new(CompleteHintHandler)),
);
let ceh = Box::new(CompleteHintHandler);
rl.bind_sequence(KeyEvent::ctrl('E'), EventHandler::Conditional(ceh.clone()));
rl.bind_sequence(KeyEvent::alt('f'), EventHandler::Conditional(ceh));
rl.bind_sequence(
KeyEvent::from('\t'),
EventHandler::Conditional(Box::new(TabEventHandler)),
Expand Down
22 changes: 7 additions & 15 deletions examples/numeric_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ use rustyline::{
struct FilteringEventHandler;
impl ConditionalEventHandler for FilteringEventHandler {
fn handle(&self, evt: &Event, _: RepeatCount, _: bool, _: &EventContext) -> Option<Cmd> {
match evt {
Event::KeySeq(ks) => {
if let Some(KeyEvent(KeyCode::Char(c), m)) = ks.first() {
if m.contains(Modifiers::CTRL)
|| m.contains(Modifiers::ALT)
|| c.is_ascii_digit()
{
None
} else {
Some(Cmd::Noop) // filter out invalid input
}
} else {
None
}
if let Some(KeyEvent(KeyCode::Char(c), m)) = evt.get(0) {
if m.contains(Modifiers::CTRL) || m.contains(Modifiers::ALT) || c.is_ascii_digit() {
None
} else {
Some(Cmd::Noop) // filter out invalid input
}
_ => None,
} else {
None
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ impl Event {
}
self
}

/// Return `i`th key event
pub fn get(&self, i: usize) -> Option<&KeyEvent> {
if let Event::KeySeq(ref ks) = self {
ks.get(i)
} else {
None
}
}
}

impl From<KeyEvent> for Event {
Expand Down

0 comments on commit 3d692d3

Please sign in to comment.