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

pub fn get_terminal_size(&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 cursor_within_screen(&mut self) -> Result<bool, Error>

Source

pub fn handle_event(&mut self, event: Event) -> Result<(), Error>

Handle an 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 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> 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.