Struct CoolInput

Source
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>

Source

pub fn new(handler: H, tab_width: usize) -> Self

Examples found in repository?
examples/simple-custom-input.rs (line 26)
25fn main() -> Result<(), std::io::Error> {
26    let mut my_input = CoolInput::new(MyHandler, 0);
27    my_input.listen()?;
28    Ok(())
29}
More examples
Hide additional examples
examples/basic.rs (line 4)
3fn main() -> Result<(), std::io::Error> {
4    let mut my_input = CoolInput::new(DefaultInputHandler, 0);
5    my_input.listen()?;
6    Ok(())
7}
examples/full-custom-input.rs (line 56)
55fn main() -> Result<(), std::io::Error> {
56    let mut cool_input = CoolInput::new(CoolCustomInput, 0);
57    cool_input.listen()?;
58    Ok(())
59}
examples/single-line-input.rs (line 40)
39fn main() -> Result<(), std::io::Error> {
40    let mut my_input = CoolInput::new(MyHandler, 0);
41    my_input.listen()?;
42    println!("your input was: '{}'", my_input.text_data.text);
43    Ok(())
44}
examples/fileeditor.rs (line 125)
120    pub fn prompt(prompt: &str) -> Result<bool, std::io::Error> {
121        let handler = ConfirmationInputHandler {
122            prompt: prompt.to_string(),
123            value: false,
124        };
125        let mut input = CoolInput::new(handler, 0);
126        input.listen()?;
127        Ok(input.custom_input.value)
128    }
129}
130impl CustomInputHandler for ConfirmationInputHandler {
131    fn get_input_transform(&mut self, ctx: HandlerContext) -> InputTransform {
132        let prompt_offset = self.prompt.chars().count() as u16;
133        InputTransform {
134            size: (ctx.terminal_size.0 - prompt_offset, ctx.terminal_size.1),
135            offset: (prompt_offset, 0),
136        }
137    }
138    fn after_update_cursor(&mut self, _: HandlerContext) {
139        let _ = queue!(stdout(), cursor::Hide);
140    }
141    fn after_draw_text(&mut self, _: HandlerContext) {
142        let _ = set_terminal_line(&self.prompt, 0, 0, false);
143    }
144    fn handle_key_press(&mut self, key: &Event, _: HandlerContext) -> KeyPressResult {
145        if let Event::Key(key_event) = key {
146            if key_event.kind == KeyEventKind::Press {
147                // Make CTRL + C stop
148                if let KeyCode::Char(c) = key_event.code {
149                    if c == 'c' && key_event.modifiers.contains(KeyModifiers::CONTROL) {
150                        return KeyPressResult::Stop;
151                    } else if c == 'y' || c == 'n' {
152                        self.value = c == 'y';
153                        return KeyPressResult::Stop;
154                    }
155                }
156            }
157        }
158        KeyPressResult::Handled
159    }
160}
161
162fn main() -> Result<(), std::io::Error> {
163    let args: Vec<_> = env::args().collect();
164    if args.len() != 2 {
165        println!("please specify a filename!");
166        return Ok(());
167    }
168    let filename = &args[1];
169    let mut text = String::new();
170    let mut is_new = true;
171    if path_exists(filename) {
172        text = fs::read_to_string(filename).expect("Unable to read file contents.");
173        is_new = false;
174    }
175    let mut cool_input = CoolInput::new(
176        FileEditorInput::open_filename(filename.to_string(), text.to_owned(), is_new),
177        0,
178    );
179    cool_input.text_data.text = text;
180    cool_input.listen()?;
181    if cool_input.custom_input.original_text != cool_input.text_data.text {
182        let save = ConfirmationInputHandler::prompt("Save file? [y/n]").unwrap();
183        if save {
184            save_file(filename, &cool_input.text_data.text);
185        }
186    }
187    Ok(())
188}
Source

pub fn get_terminal_size(&mut self) -> Result<(u16, u16), Error>

Get the size of the terminal running the program

Source

pub fn get_input_transform(&mut self) -> Result<InputTransform, Error>

Source

pub fn render(&mut self) -> Result<(), Error>

Render all text and update cursor

Source

pub fn handle_key_press(&mut self, key: Event) -> Result<(), Error>

Handle a key event

Source

pub fn listen_quiet(&mut self) -> Result<(), Error>

Start listening for key presses without preparing the terminal

Source

pub fn pre_listen(&mut self) -> Result<(), Error>

Prepare the terminal for input

Source

pub fn post_listen(&mut self) -> Result<(), Error>

Restore the terminal after input is finished.

Source

pub fn listen(&mut self) -> Result<(), Error>

Prepare terminal and start to listen for key presses until finished.

Examples found in repository?
examples/simple-custom-input.rs (line 27)
25fn main() -> Result<(), std::io::Error> {
26    let mut my_input = CoolInput::new(MyHandler, 0);
27    my_input.listen()?;
28    Ok(())
29}
More examples
Hide additional examples
examples/basic.rs (line 5)
3fn main() -> Result<(), std::io::Error> {
4    let mut my_input = CoolInput::new(DefaultInputHandler, 0);
5    my_input.listen()?;
6    Ok(())
7}
examples/full-custom-input.rs (line 57)
55fn main() -> Result<(), std::io::Error> {
56    let mut cool_input = CoolInput::new(CoolCustomInput, 0);
57    cool_input.listen()?;
58    Ok(())
59}
examples/single-line-input.rs (line 41)
39fn main() -> Result<(), std::io::Error> {
40    let mut my_input = CoolInput::new(MyHandler, 0);
41    my_input.listen()?;
42    println!("your input was: '{}'", my_input.text_data.text);
43    Ok(())
44}
examples/fileeditor.rs (line 126)
120    pub fn prompt(prompt: &str) -> Result<bool, std::io::Error> {
121        let handler = ConfirmationInputHandler {
122            prompt: prompt.to_string(),
123            value: false,
124        };
125        let mut input = CoolInput::new(handler, 0);
126        input.listen()?;
127        Ok(input.custom_input.value)
128    }
129}
130impl CustomInputHandler for ConfirmationInputHandler {
131    fn get_input_transform(&mut self, ctx: HandlerContext) -> InputTransform {
132        let prompt_offset = self.prompt.chars().count() as u16;
133        InputTransform {
134            size: (ctx.terminal_size.0 - prompt_offset, ctx.terminal_size.1),
135            offset: (prompt_offset, 0),
136        }
137    }
138    fn after_update_cursor(&mut self, _: HandlerContext) {
139        let _ = queue!(stdout(), cursor::Hide);
140    }
141    fn after_draw_text(&mut self, _: HandlerContext) {
142        let _ = set_terminal_line(&self.prompt, 0, 0, false);
143    }
144    fn handle_key_press(&mut self, key: &Event, _: HandlerContext) -> KeyPressResult {
145        if let Event::Key(key_event) = key {
146            if key_event.kind == KeyEventKind::Press {
147                // Make CTRL + C stop
148                if let KeyCode::Char(c) = key_event.code {
149                    if c == 'c' && key_event.modifiers.contains(KeyModifiers::CONTROL) {
150                        return KeyPressResult::Stop;
151                    } else if c == 'y' || c == 'n' {
152                        self.value = c == 'y';
153                        return KeyPressResult::Stop;
154                    }
155                }
156            }
157        }
158        KeyPressResult::Handled
159    }
160}
161
162fn main() -> Result<(), std::io::Error> {
163    let args: Vec<_> = env::args().collect();
164    if args.len() != 2 {
165        println!("please specify a filename!");
166        return Ok(());
167    }
168    let filename = &args[1];
169    let mut text = String::new();
170    let mut is_new = true;
171    if path_exists(filename) {
172        text = fs::read_to_string(filename).expect("Unable to read file contents.");
173        is_new = false;
174    }
175    let mut cool_input = CoolInput::new(
176        FileEditorInput::open_filename(filename.to_string(), text.to_owned(), is_new),
177        0,
178    );
179    cool_input.text_data.text = text;
180    cool_input.listen()?;
181    if cool_input.custom_input.original_text != cool_input.text_data.text {
182        let save = ConfirmationInputHandler::prompt("Save file? [y/n]").unwrap();
183        if save {
184            save_file(filename, &cool_input.text_data.text);
185        }
186    }
187    Ok(())
188}

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.