use std::io::{Stdout, Write}; use termion::{clear, cursor, raw::RawTerminal}; use super::{ frame::{self, FrameDef}, theme::Theme, Event, Page, }; type Result = std::result::Result; #[derive(Debug, Clone)] pub enum Body { Echo, Signin(SigninPage), } impl Body { fn echo(theme: Theme, screen: &mut RawTerminal, event: Event) -> Result<()> { let event = format!("{}", event); write!( screen, "{theme}{clear}{start}Event: {}", event, theme = theme.display_string(), clear = clear::All, start = cursor::Goto(1, 1) )?; screen.flush()?; Ok(()) } } impl Page for Body { fn receive_event( &mut self, theme: Theme, screen: &mut RawTerminal, event: Event, ) -> Result<()> { match self { Body::Signin(b) => b.receive_event(theme, screen, event)?, Body::Echo => Body::echo(theme, screen, event)?, }; Ok(()) } fn init(&self, theme: Theme, screen: &mut RawTerminal) -> Result<()> { write!( screen, "{theme}{cursor}{clear}{fr}", fr = frame::draw_frame(theme, FrameDef::ByPercent(90, 90)), theme = theme.display_string(), cursor = cursor::Goto(1, 1), clear = clear::All )?; screen.flush()?; Ok(()) } } #[derive(Debug, Clone, Default)] struct SigninPage { hostname: String, username: String, cursor: SigninCursorLocation, } #[derive(Debug, Clone)] enum SigninCursorLocation { Hostname, Username, Next, } impl Default for SigninCursorLocation { fn default() -> Self { Self::Hostname } } impl SigninPage { fn frame_string() -> String { format!("") } } impl Page for SigninPage { fn receive_event( &mut self, theme: Theme, screen: &mut RawTerminal, event: Event, ) -> Result<()> { Ok(()) } fn init(&self, theme: Theme, screen: &mut RawTerminal) -> Result<()> { let fr = frame::draw_frame(theme, FrameDef::ByPercent(40, 40)); write!( screen, "{theme}{clear}{hide_cursor}{frame}", frame = fr, theme = theme.display_string(), clear = clear::All, hide_cursor = cursor::Hide, )?; screen.flush()?; Ok(()) } }