use tv::{Attr, Color, Key, Screen};
fn main() -> tv::Result<()> {
let mut scr = Screen::init()?;
scr.set_cursor_style(5)?; scr.cursor_visible(true)?;
let mut input = String::new();
let mut cursor = 0usize;
let label = "Name: ";
loop {
scr.clear()?;
scr.set_foreground(Color::rgb(139, 233, 253))?;
scr.add_attribute(Attr::BOLD)?;
scr.move_print(1, 2, "Text Input Example")?;
scr.remove_attribute(Attr::BOLD)?;
scr.set_foreground(Color::rgb(98, 114, 164))?;
scr.move_print(3, 2, label)?;
scr.set_foreground(Color::rgb(248, 248, 242))?;
scr.print(&input)?;
scr.move_cursor(3, (2 + label.len() + cursor) as u16)?;
scr.set_foreground(Color::rgb(98, 114, 164))?;
scr.move_print(5, 2, "Type text, use ←/→ to move, Ctrl+Q to quit")?;
if !input.is_empty() {
scr.set_foreground(Color::rgb(80, 250, 123))?;
scr.move_print(7, 2, &format!("Hello, {}!", input))?;
}
let key = scr.get_char()?;
match key {
Key::Ctrl('q') => break,
Key::Char(ch) => {
input.insert(cursor, ch);
cursor += 1;
}
Key::Backspace => {
if cursor > 0 {
cursor -= 1;
input.remove(cursor);
}
}
Key::Delete => {
if cursor < input.len() {
input.remove(cursor);
}
}
Key::Left => {
if cursor > 0 {
cursor -= 1;
}
}
Key::Right => {
if cursor < input.len() {
cursor += 1;
}
}
Key::Home => cursor = 0,
Key::End => cursor = input.len(),
_ => {}
}
}
scr.set_cursor_style(0)?;
scr.endwin()?;
Ok(())
}