kk/src/display/body.rs

105 lines
2.3 KiB
Rust

use std::io::{Stdout, Write};
use termion::{clear, cursor, raw::RawTerminal};
use super::{Environment, Event, Page};
type Result<T> = std::result::Result<T, anyhow::Error>;
#[derive(Debug, Clone)]
pub enum Body {
Echo,
Signin(SigninPage),
}
impl Body {
fn echo(env: Environment, screen: &mut RawTerminal<Stdout>, event: Event) -> Result<()> {
let event = format!("{}", event);
write!(
screen,
"{fr}{}",
env.frame.writeln(&format!("Event: {}", event), 1, 1),
fr = env.primary_frame(),
)?;
screen.flush()?;
Ok(())
}
}
impl Page for Body {
fn receive_event(
&mut self,
env: Environment,
screen: &mut RawTerminal<Stdout>,
event: Event,
) -> Result<()> {
match self {
Body::Signin(b) => b.receive_event(env, screen, event)?,
Body::Echo => Body::echo(env, screen, event)?,
};
Ok(())
}
fn init(&self, env: Environment, screen: &mut RawTerminal<Stdout>) -> Result<()> {
write!(
screen,
"{hide}{clear}{fr}{cursor}",
hide = cursor::Hide,
fr = env.frame.frame_str(&env.theme, env.theme.primary()),
cursor = env.frame.goto(0, 0),
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,
env: Environment,
screen: &mut RawTerminal<Stdout>,
event: Event,
) -> Result<()> {
Ok(())
}
fn init(&self, env: Environment, screen: &mut RawTerminal<Stdout>) -> Result<()> {
write!(
screen,
"{frame}{hide_cursor}",
frame = env.frame.frame_str(&env.theme, env.theme.primary()),
hide_cursor = cursor::Hide,
)?;
screen.flush()?;
Ok(())
}
}