tv 0.1.1

Terminal User Interface library
Documentation
use tv::{Attr, Color, Key, Screen};

fn main() -> tv::Result<()> {
    let mut scr = Screen::init()?;
    scr.set_cursor_style(5)?; // blinking bar
    scr.cursor_visible(true)?;

    let mut input = String::new();
    let mut cursor = 0usize;
    let label = "Name: ";

    loop {
        scr.clear()?;

        // Title
        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)?;

        // Label
        scr.set_foreground(Color::rgb(98, 114, 164))?;
        scr.move_print(3, 2, label)?;

        // Input text
        scr.set_foreground(Color::rgb(248, 248, 242))?;
        scr.print(&input)?;

        // Position cursor inside the input
        scr.move_cursor(3, (2 + label.len() + cursor) as u16)?;

        // Hint
        scr.set_foreground(Color::rgb(98, 114, 164))?;
        scr.move_print(5, 2, "Type text, use ←/→ to move, Ctrl+Q to quit")?;

        // Preview
        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(())
}