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 after_update_cursor(&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§
Sourcefn handle_key_press(
&mut self,
key: &Event,
ctx: HandlerContext<'_>,
) -> KeyPressResult
fn handle_key_press( &mut self, key: &Event, ctx: HandlerContext<'_>, ) -> KeyPressResult
Called before handling of every key press.
Sourcefn before_draw_text(&mut self, ctx: HandlerContext<'_>)
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
Sourcefn after_draw_text(&mut self, ctx: HandlerContext<'_>)
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.
Sourcefn after_update_cursor(&mut self, ctx: HandlerContext<'_>)
fn after_update_cursor(&mut self, ctx: HandlerContext<'_>)
Called after the cursor is updated/drawn. Here you can ex. disable cursor blinking or hide it all together
Sourcefn get_input_transform(&mut self, ctx: HandlerContext<'_>) -> InputTransform
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 fn after_draw_text(&mut self, ctx: HandlerContext) {
35 let _ = queue!(stdout(), SetForegroundColor(Color::White));
36 let _ = set_terminal_line(
37 "Welcome to my cool text editor. Here you can write cool stuff! Press ESC to exit.",
38 5,
39 0,
40 true,
41 );
42 let _ = set_terminal_line("Rules:", 5, 1, true);
43 let _ = set_terminal_line("No typing the letter S", 10, 2, true);
44
45 let width = self.get_input_transform(ctx).size.0;
46 let _ = set_terminal_line(&String::from("_").repeat(width as usize), 5, 3, true);
47 }
More examples
examples/fileeditor.rs (line 76)
58 fn after_draw_text(&mut self, ctx: HandlerContext) {
59 let _ = queue!(
60 stdout(),
61 SetForegroundColor(Color::Black),
62 SetBackgroundColor(Color::White)
63 );
64 let left_text = format!("BANANO v{}", env!("CARGO_PKG_VERSION"));
65 let center_text = format!("FILE: '{}'", self.filename);
66 let mut right_text = "NOT MODIFIED";
67
68 if self.original_text != ctx.text_data.text {
69 right_text = "MODIFIED";
70 }
71 if self.is_new {
72 right_text = "NEW FILE"
73 }
74
75 let bottom_text_position = (ctx.terminal_size.1 - 1) as usize;
76 let width = self.get_input_transform(ctx).size.0;
77
78 let _ = set_terminal_line(&left_text, 0, 0, true);
79 let _ = set_terminal_line(
80 ¢er_text,
81 (width as usize - center_text.len()) / 2,
82 0,
83 false,
84 );
85 let _ = set_terminal_line(right_text, width as usize - right_text.len(), 0, false);
86
87 let keybinds = ["^S".to_string(), "^C".to_string()];
88 let descriptions = ["Save File".to_string(), "Exit".to_string()];
89
90 let mut offset = 0;
91 for (keybind, description) in keybinds.iter().zip(descriptions) {
92 let _ = queue!(
93 stdout(),
94 SetForegroundColor(Color::Black),
95 SetBackgroundColor(Color::White)
96 );
97 let _ = set_terminal_line(keybind, offset, bottom_text_position, false);
98 offset += keybind.chars().count() + 1;
99 let _ = queue!(stdout(), ResetColor);
100 let _ = set_terminal_line(&description, offset, bottom_text_position, false);
101 offset += description.chars().count() + 1;
102 }
103 }