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

Add method to return character from char event. #744

Merged
merged 2 commits into from
Sep 10, 2023
Merged

Conversation

timdubbins
Copy link
Contributor

Firstly, thank you for this library. I've really enjoyed hacking my way through it.

This is a minor proposal that helped me clean up some ugly code. Here's an example:

use cursive::event::{Event, EventResult, EventTrigger};
use cursive::views::TextView;

fn main() {
    let mut siv = cursive::default();
    siv.add_layer(TextView::new("enter uppercased character:"));

    siv.set_on_pre_event_inner(uppercased(), move |event| {
        // use the method here
        let c = event.char().expect("only chars are triggered");
        Some(EventResult::with_cb(move |siv| {
            siv.add_layer(TextView::new(format!("do something useful with: {}", c)))
        }))
    });

    siv.set_on_pre_event(Event::Char('q'), |siv| siv.quit());
    siv.run();
}

fn uppercased() -> EventTrigger {
    EventTrigger::from_fn(|e| matches!(e, Event::Char('A'..='Z')))
}

This Event::chars() method allows us to access the inputted character without creating a mapping, which is a bit tedious when you want to capture a range of values (as we do in the example).

@gyscos
Copy link
Owner

gyscos commented Aug 17, 2023

Thanks for the PR!
I think in this case a match self might be shorter and simpler than a if let.

Also, should it maybe also return the char for AltChar and CtrlChar events?

@gyscos gyscos merged commit ac4ed1c into gyscos:main Sep 10, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants