/// RFC 02 验收 example
///
/// 验证 Component + Runtime 闭环：
/// - `App::new(root).run()` 启动应用
/// - Component render + event
/// - 按任意键改变名字，按 q 退出

use lv_tui::prelude::*;

struct Hello {
    name: String,
}

impl Component for Hello {
    fn render(&self, cx: &mut RenderCx) {
        cx.line(format!("Hello, {}!", self.name));
        cx.line("");
        cx.line("press any key to change name, q to quit");
    }

    fn event(&mut self, event: &Event, cx: &mut EventCx) {
        if event.is_key(Key::Char('q')) {
            cx.quit();
        } else if matches!(event, Event::Key(_)) {
            self.name = format!("User<{}>", rand::random::<u8>());
            cx.invalidate_paint();
        }
    }
}

fn main() -> lv_tui::Result<()> {
    App::new(Hello {
        name: "lv-tui".into(),
    })
    .run()
}
