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