1 use std::io::{stdin, stdout, Result, Write};
2 use termion::cursor::{Hide, Show};
3 use termion::input::TermRead;
4 use termion::raw::IntoRawMode;
5 use termion::screen::AlternateScreen;
6 use tui_input::backend::termion as backend;
7 use tui_input::{Input, InputResponse};
8 
main() -> Result<()>9 fn main() -> Result<()> {
10     let stdin = stdin();
11     let stdout = stdout().into_raw_mode().unwrap();
12     let mut stdout = AlternateScreen::from(stdout);
13 
14     let value = "Hello ".to_string();
15     let mut input = Input::default().with_value(value);
16     write!(&mut stdout, "{}", Hide)?;
17     backend::write(&mut stdout, input.value(), input.cursor(), (0, 0), 15)?;
18     stdout.flush()?;
19 
20     for evt in stdin.events() {
21         let evt = evt?;
22         if let Some(resp) =
23             backend::to_input_request(&evt).and_then(|req| input.handle(req))
24         {
25             match resp {
26                 InputResponse::Escaped | InputResponse::Submitted => {
27                     break;
28                 }
29 
30                 InputResponse::StateChanged(_) => {
31                     backend::write(
32                         &mut stdout,
33                         input.value(),
34                         input.cursor(),
35                         (0, 0),
36                         15,
37                     )?;
38                     stdout.flush()?;
39                 }
40             }
41         }
42     }
43 
44     write!(stdout, "{}", Show)?;
45     Ok(())
46 }
47