pub fn set_terminal_line(
text: &str,
x: usize,
y: usize,
overwrite: bool,
) -> Result<(), Error>Expand description
Helper function to draw text to the screen by a coordinate
Examples found in repository?
More examples
examples/full-custom-input.rs (lines 36-41)
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 }examples/fileeditor.rs (line 78)
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 }
104 fn get_input_transform(&mut self, ctx: HandlerContext) -> InputTransform {
105 let size = (ctx.terminal_size.0, ctx.terminal_size.1 - 3);
106 let offset = (0, 2);
107 InputTransform { size, offset }
108 }
109}
110
111pub fn path_exists(path: &str) -> bool {
112 fs::metadata(path).is_ok()
113}
114
115/// A simple Y/N prompt input handler. Automatically stops on first keypress, no enter required.
116pub struct ConfirmationInputHandler {
117 pub prompt: String,
118 pub value: bool,
119}
120impl ConfirmationInputHandler {
121 pub fn prompt(prompt: &str) -> Result<bool, std::io::Error> {
122 let handler = ConfirmationInputHandler {
123 prompt: prompt.to_string(),
124 value: false,
125 };
126 let mut input = CoolInput::new(handler, 0);
127 input.listen()?;
128 Ok(input.custom_input.value)
129 }
130}
131impl CustomInputHandler for ConfirmationInputHandler {
132 fn get_input_transform(&mut self, ctx: HandlerContext) -> InputTransform {
133 let prompt_offset = self.prompt.chars().count() as u16;
134 InputTransform {
135 size: (ctx.terminal_size.0 - prompt_offset, ctx.terminal_size.1),
136 offset: (prompt_offset, 0),
137 }
138 }
139 fn after_update_cursor(&mut self, _: HandlerContext) {
140 let _ = queue!(stdout(), cursor::Hide);
141 }
142 fn after_draw_text(&mut self, _: HandlerContext) {
143 let _ = set_terminal_line(&self.prompt, 0, 0, false);
144 }