pub struct CoolInput<H: CustomInputHandler> {
pub text_data: TextInputData,
pub scroll_x: usize,
pub scroll_y: usize,
pub listening: bool,
pub custom_input: H,
}
Expand description
The main input type. Uses a custom input handler (a struct which implements CustomInputHandler)
Fields§
§text_data: TextInputData
§scroll_x: usize
§scroll_y: usize
§listening: bool
§custom_input: H
Implementations§
Source§impl<H: CustomInputHandler> CoolInput<H>
impl<H: CustomInputHandler> CoolInput<H>
Sourcepub fn new(handler: H, tab_width: usize) -> Self
pub fn new(handler: H, tab_width: usize) -> Self
Examples found in repository?
More examples
examples/fileeditor.rs (line 126)
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 }
145 fn handle_key_press(&mut self, key: &Event, _: HandlerContext) -> KeyPressResult {
146 if let Event::Key(key_event) = key {
147 if key_event.kind == KeyEventKind::Press {
148 // Make CTRL + C stop
149 if let KeyCode::Char(c) = key_event.code {
150 if c == 'c' && key_event.modifiers.contains(KeyModifiers::CONTROL) {
151 return KeyPressResult::Stop;
152 } else if c == 'y' || c == 'n' {
153 self.value = c == 'y';
154 return KeyPressResult::Stop;
155 }
156 }
157 }
158 }
159 KeyPressResult::Handled
160 }
161}
162
163fn main() -> Result<(), std::io::Error> {
164 let args: Vec<_> = env::args().collect();
165 if args.len() != 2 {
166 println!("please specify a filename!");
167 return Ok(());
168 }
169 let filename = &args[1];
170 let mut text = String::new();
171 let mut is_new = true;
172 if path_exists(filename) {
173 text = fs::read_to_string(filename).expect("Unable to read file contents.");
174 is_new = false;
175 }
176 let mut cool_input = CoolInput::new(
177 FileEditorInput::open_filename(filename.to_string(), text.to_owned(), is_new),
178 0,
179 );
180 cool_input.text_data.text = text;
181 cool_input.listen()?;
182 if cool_input.custom_input.original_text != cool_input.text_data.text {
183 let save = ConfirmationInputHandler::prompt("Save file? [y/n]").unwrap();
184 if save {
185 save_file(filename, &cool_input.text_data.text);
186 }
187 }
188 Ok(())
189}
Sourcepub fn get_terminal_size(&self) -> Result<(u16, u16), Error>
pub fn get_terminal_size(&self) -> Result<(u16, u16), Error>
Get the size of the terminal running the program
pub fn get_input_transform(&mut self) -> Result<InputTransform, Error>
pub fn cursor_within_screen(&mut self) -> Result<bool, Error>
Sourcepub fn listen_quiet(&mut self) -> Result<(), Error>
pub fn listen_quiet(&mut self) -> Result<(), Error>
Start listening for key presses without preparing the terminal
Sourcepub fn pre_listen(&mut self) -> Result<(), Error>
pub fn pre_listen(&mut self) -> Result<(), Error>
Prepare the terminal for input
Sourcepub fn post_listen(&mut self) -> Result<(), Error>
pub fn post_listen(&mut self) -> Result<(), Error>
Restore the terminal after input is finished.
Sourcepub fn listen(&mut self) -> Result<(), Error>
pub fn listen(&mut self) -> Result<(), Error>
Prepare terminal and start to listen for key presses until finished.
Examples found in repository?
More examples
examples/fileeditor.rs (line 127)
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 }
145 fn handle_key_press(&mut self, key: &Event, _: HandlerContext) -> KeyPressResult {
146 if let Event::Key(key_event) = key {
147 if key_event.kind == KeyEventKind::Press {
148 // Make CTRL + C stop
149 if let KeyCode::Char(c) = key_event.code {
150 if c == 'c' && key_event.modifiers.contains(KeyModifiers::CONTROL) {
151 return KeyPressResult::Stop;
152 } else if c == 'y' || c == 'n' {
153 self.value = c == 'y';
154 return KeyPressResult::Stop;
155 }
156 }
157 }
158 }
159 KeyPressResult::Handled
160 }
161}
162
163fn main() -> Result<(), std::io::Error> {
164 let args: Vec<_> = env::args().collect();
165 if args.len() != 2 {
166 println!("please specify a filename!");
167 return Ok(());
168 }
169 let filename = &args[1];
170 let mut text = String::new();
171 let mut is_new = true;
172 if path_exists(filename) {
173 text = fs::read_to_string(filename).expect("Unable to read file contents.");
174 is_new = false;
175 }
176 let mut cool_input = CoolInput::new(
177 FileEditorInput::open_filename(filename.to_string(), text.to_owned(), is_new),
178 0,
179 );
180 cool_input.text_data.text = text;
181 cool_input.listen()?;
182 if cool_input.custom_input.original_text != cool_input.text_data.text {
183 let save = ConfirmationInputHandler::prompt("Save file? [y/n]").unwrap();
184 if save {
185 save_file(filename, &cool_input.text_data.text);
186 }
187 }
188 Ok(())
189}
Auto Trait Implementations§
impl<H> Freeze for CoolInput<H>where
H: Freeze,
impl<H> RefUnwindSafe for CoolInput<H>where
H: RefUnwindSafe,
impl<H> Send for CoolInput<H>where
H: Send,
impl<H> Sync for CoolInput<H>where
H: Sync,
impl<H> Unpin for CoolInput<H>where
H: Unpin,
impl<H> UnwindSafe for CoolInput<H>where
H: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more