use std::io::{Stdout, Write}; use termion::{clear, cursor, raw::RawTerminal}; use super::{ frame::Frame, theme::ColorSet, Environment, Event, Page, }; type Result = std::result::Result; #[derive(Debug, Clone)] pub enum Body { Echo, Signin(SigninPage), } impl Body { fn echo( env: Environment, screen: &mut RawTerminal, event: Event, ) -> Result<()> { let event = format!("{}", event); write!( screen, "{theme}{content}", theme = env.theme.primary(), content = env.frame.writeln(&format!("Event: {}", event), 0, 0), )?; screen.flush()?; Ok(()) } fn echo_init( &self, env: Environment, screen: &mut RawTerminal, ) -> Result<()> { write!( screen, "{hide}{clear}{fr}{cursor}", clear = clear::All, hide = cursor::Hide, fr = env.frame.frame_str(env.theme.primary()), cursor = env.frame.goto(0, 0), )?; screen.flush()?; Ok(()) } } impl Page for Body { fn receive_event( &mut self, env: Environment, screen: &mut RawTerminal, event: Event, ) -> Result<()> { match self { Body::Signin(b) => b.receive_event(env, screen, event)?, Body::Echo => Body::echo(env, screen, event)?, }; Ok(()) } fn init( &mut self, env: Environment, screen: &mut RawTerminal, ) -> Result<()> { match self { Body::Echo => self.echo_init(env, screen), Body::Signin(signin) => signin.init(env, screen), } } } #[derive(Debug, Clone, Default)] pub struct SigninPage { hostname: String, username: String, cursor: SigninCursorLocation, cursor_position: u16, frame: Option, } #[derive(Debug, Clone)] enum SigninCursorLocation { Hostname, Username, Next, } impl Default for SigninCursorLocation { fn default() -> Self { Self::Hostname } } impl SigninPage { #[inline] fn draw(&self) -> String { let frame = self.frame.unwrap(); frame.write_centered("login kk", 1) } } impl Page for SigninPage { fn receive_event( &mut self, env: Environment, screen: &mut RawTerminal, event: Event, ) -> Result<()> { match event { Event::Key(key) => match key { termion::event::Key::Char(_) => {} termion::event::Key::Up => {} termion::event::Key::Down => {} termion::event::Key::Backspace => {} termion::event::Key::Delete => {} termion::event::Key::Left => {} termion::event::Key::Right => {} // _ => {} }, } Ok(()) } fn init( &mut self, env: Environment, screen: &mut RawTerminal, ) -> Result<()> { self.frame = Some(env.frame.sub( env.theme.colors.subwin, super::frame::FrameSize::ByPercent(80, 80), 2, )); write!( screen, "{theme}{clear}{frame}{subframe}{hide_cursor}{content}", theme = env.theme.frame(), clear = clear::All, frame = env.frame.frame_str(env.theme.primary()), subframe = self .frame .unwrap() .frame_str(ColorSet::default().to_string()), hide_cursor = cursor::Hide, content = self.draw(), )?; screen.flush()?; Ok(()) } }