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 14:57:23 +00:00
|
|
|
"{fr}{}",
|
|
|
|
env.frame.writeln(&format!("Event: {}", event), 1, 1),
|
|
|
|
fr = env.primary_frame(),
|
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 14:57:23 +00:00
|
|
|
"{hide}{clear}{fr}{cursor}",
|
|
|
|
hide = cursor::Hide,
|
|
|
|
fr = env.frame.frame_str(&env.theme, env.theme.primary()),
|
2023-01-14 11:58:53 +00:00
|
|
|
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,
|
2023-01-14 14:57:23 +00:00
|
|
|
"{frame}{hide_cursor}",
|
|
|
|
frame = env.frame.frame_str(&env.theme, env.theme.primary()),
|
2023-01-10 01:30:21 +00:00
|
|
|
hide_cursor = cursor::Hide,
|
|
|
|
)?;
|
|
|
|
screen.flush()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|