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

Update custom key bindings example #638

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions examples/custom_key_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::borrow::Cow::{self, Borrowed, Owned};
use rustyline::highlight::Highlighter;
use rustyline::hint::HistoryHinter;
use rustyline::{
Cmd, ConditionalEventHandler, Editor, Event, EventContext, EventHandler, KeyEvent, RepeatCount,
Result,
Cmd, ConditionalEventHandler, Editor, Event, EventContext, EventHandler, KeyCode, KeyEvent,
Modifiers, RepeatCount, Result,
};
use rustyline_derive::{Completer, Helper, Hinter, Validator};

Expand Down Expand Up @@ -69,15 +69,21 @@ impl ConditionalEventHandler for CompleteHintHandler {
struct TabEventHandler;
impl ConditionalEventHandler for TabEventHandler {
fn handle(&self, evt: &Event, n: RepeatCount, _: bool, ctx: &EventContext) -> Option<Cmd> {
debug_assert_eq!(*evt, Event::from(KeyEvent::from('\t')));
debug_assert_eq!(*evt, Event::from(KeyEvent(KeyCode::Tab, Modifiers::NONE)));
if ctx.line()[..ctx.pos()]
.chars()
.rev()
.next()
.filter(|c| c.is_whitespace())
.is_some()
{
Some(Cmd::SelfInsert(n, '\t'))
if cfg!(target_os = "windows") {
// Inserting a tab is broken in windows with rustyline
// use 4 spaces as a workaround for now
Some(Cmd::Insert(n, " ".into()))
} else {
Some(Cmd::Insert(n, "\t".into()))
}
} else {
None // default complete
}
Expand All @@ -92,7 +98,7 @@ fn main() -> Result<()> {
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'),
KeyEvent(KeyCode::Tab, Modifiers::NONE),
EventHandler::Conditional(Box::new(TabEventHandler)),
);
rl.bind_sequence(
Expand Down