cool_rust_input

Trait CustomInputHandler

Source
pub trait CustomInputHandler {
    // Provided methods
    fn handle_key_press(
        &mut self,
        key: &Event,
        ctx: HandlerContext<'_>,
    ) -> KeyPressResult { ... }
    fn before_draw_text(&mut self, ctx: HandlerContext<'_>) { ... }
    fn after_draw_text(&mut self, ctx: HandlerContext<'_>) { ... }
    fn get_input_transform(&mut self, ctx: HandlerContext<'_>) -> InputTransform { ... }
}
Expand description

Trait that allows custom implementations / behaviour of an input

Provided Methods§

Source

fn handle_key_press( &mut self, key: &Event, ctx: HandlerContext<'_>, ) -> KeyPressResult

Called before handling of every key press.

Source

fn before_draw_text(&mut self, ctx: HandlerContext<'_>)

Called before the user’s text input is drawn. Here you can ex. change color of the inputted text

Source

fn after_draw_text(&mut self, ctx: HandlerContext<'_>)

Called after the user’s text is drawn. Here you can ex. draw other text like information or a title of the document.

Source

fn get_input_transform(&mut self, ctx: HandlerContext<'_>) -> InputTransform

Called by the parent input to get the input area’s size and offset (in a InputTransform).

Examples found in repository?
examples/full-custom-input.rs (line 45)
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn after_draw_text(&mut self, ctx: HandlerContext) {
        let _ = queue!(stdout(), SetForegroundColor(Color::White));
        let _ = set_terminal_line(
            "Welcome to my cool text editor. Here you can write cool stuff! Press ESC to exit.",
            5,
            0,
            true,
        );
        let _ = set_terminal_line("Rules:", 5, 1, true);
        let _ = set_terminal_line("No typing the letter S", 10, 2, true);

        let width = self.get_input_transform(ctx).size.0;
        let _ = set_terminal_line(&String::from("_").repeat(width as usize), 5, 3, true);
    }
More examples
Hide additional examples
examples/fileeditor.rs (line 73)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    fn after_draw_text(&mut self, ctx: HandlerContext) {
        let _ = queue!(
            stdout(),
            SetForegroundColor(Color::Black),
            SetBackgroundColor(Color::White)
        );
        let left_text = format!("BANANO v{}", env!("CARGO_PKG_VERSION"));
        let center_text = format!("FILE: '{}'", self.filename);
        let mut right_text = "NOT MODIFIED";
        let bottom_text = "^S Save File  ^C Exit";

        if self.original_text != ctx.text_data.text {
            right_text = "MODIFIED";
        }
        if self.is_new {
            right_text = "NEW FILE"
        }

        let bottom_text_position = (ctx.terminal_size.1 - 1) as usize;
        let width = self.get_input_transform(ctx).size.0;

        let _ = set_terminal_line(&left_text, 0, 0, true);
        let _ = set_terminal_line(
            &center_text,
            (width as usize - center_text.len()) / 2,
            0,
            false,
        );
        let _ = set_terminal_line(right_text, width as usize - right_text.len(), 0, false);

        let _ = queue!(stdout(), ResetColor);
        let _ = set_terminal_line(bottom_text, 0, bottom_text_position, true);
    }

Implementors§