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 75)
57 fn after_draw_text(&mut self, ctx: HandlerContext) {
58 let _ = queue!(
59 stdout(),
60 SetForegroundColor(Color::Black),
61 SetBackgroundColor(Color::White)
62 );
63 let left_text = format!("BANANO v{}", env!("CARGO_PKG_VERSION"));
64 let center_text = format!("FILE: '{}'", self.filename);
65 let mut right_text = "NOT MODIFIED";
66
67 if self.original_text != ctx.text_data.text {
68 right_text = "MODIFIED";
69 }
70 if self.is_new {
71 right_text = "NEW FILE"
72 }
73
74 let bottom_text_position = (ctx.terminal_size.1 - 1) as usize;
75 let width = self.get_input_transform(ctx).size.0;
76
77 let _ = set_terminal_line(&left_text, 0, 0, true);
78 let _ = set_terminal_line(
79 ¢er_text,
80 (width as usize - center_text.len()) / 2,
81 0,
82 false,
83 );
84 let _ = set_terminal_line(right_text, width as usize - right_text.len(), 0, false);
85
86 let keybinds = ["^S".to_string(), "^C".to_string()];
87 let descriptions = ["Save File".to_string(), "Exit".to_string()];
88
89 let mut offset = 0;
90 for (keybind, description) in keybinds.iter().zip(descriptions) {
91 let _ = queue!(
92 stdout(),
93 SetForegroundColor(Color::Black),
94 SetBackgroundColor(Color::White)
95 );
96 let _ = set_terminal_line(keybind, offset, bottom_text_position, false);
97 offset += keybind.chars().count() + 1;
98 let _ = queue!(stdout(), ResetColor);
99 let _ = set_terminal_line(&description, offset, bottom_text_position, false);
100 offset += description.chars().count() + 1;
101 }
102 }