211 lines
5.2 KiB
Rust
211 lines
5.2 KiB
Rust
use std::io::{Stdout, Write};
|
|
|
|
use termion::{clear, cursor, raw::RawTerminal};
|
|
|
|
use super::{
|
|
compose::{Component, Components},
|
|
frame::Frame,
|
|
theme::ColorSet,
|
|
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,
|
|
"{theme}{content}",
|
|
theme = env.theme.primary(),
|
|
content = env
|
|
.frame
|
|
.writeln(&format!("Event: {}", event), 0, 0)
|
|
.concat_for(&env.frame),
|
|
)?;
|
|
screen.flush()?;
|
|
Ok(())
|
|
}
|
|
|
|
fn echo_init(
|
|
&self,
|
|
env: Environment,
|
|
screen: &mut RawTerminal<Stdout>,
|
|
) -> Result<()> {
|
|
write!(
|
|
screen,
|
|
"{hide}{clear}{fr}{cursor}",
|
|
clear = clear::All,
|
|
hide = cursor::Hide,
|
|
fr = env.frame.frame_str(env.theme.colors.primary),
|
|
cursor = env.frame.goto(0, 0),
|
|
)?;
|
|
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(
|
|
&mut self,
|
|
env: Environment,
|
|
screen: &mut RawTerminal<Stdout>,
|
|
) -> 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,
|
|
frame: Option<Frame>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
enum SigninCursorLocation {
|
|
Hostname(u16),
|
|
Ok,
|
|
}
|
|
|
|
impl Default for SigninCursorLocation {
|
|
fn default() -> Self {
|
|
Self::Ok
|
|
}
|
|
}
|
|
|
|
impl SigninPage {
|
|
#[inline]
|
|
fn draw(
|
|
&self,
|
|
highlight: ColorSet,
|
|
normal: ColorSet,
|
|
) -> Components {
|
|
const HEADER_Y: u16 = 1;
|
|
const HOSTNAME_Y: u16 = 3;
|
|
const OK_Y: u16 = 5;
|
|
let frame = self.frame.unwrap();
|
|
let fr_width = frame.size().0;
|
|
let total_width = fr_width / 3;
|
|
|
|
todo!()
|
|
// frame
|
|
// .write_centered("login kk", HEADER_Y)
|
|
// .into_iter()
|
|
// .chain(
|
|
// frame
|
|
// .write_centered(
|
|
// &self.field_str(
|
|
// highlight,
|
|
// total_width,
|
|
// "hostname",
|
|
// "",
|
|
// ),
|
|
// HOSTNAME_Y,
|
|
// )
|
|
// .into_iter(),
|
|
// )
|
|
// .chain(vec![Component::Theme(normal)].into_iter())
|
|
// .chain(frame.write_centered("[ok]", OK_Y).into_iter())
|
|
// .collect::<Vec<Component>>()
|
|
// .into()
|
|
}
|
|
|
|
#[inline]
|
|
fn field_str(
|
|
&self,
|
|
color: ColorSet,
|
|
width: u16,
|
|
field: &str,
|
|
field_content: &str,
|
|
) -> String {
|
|
let unpadded = format!("{field}: {field_content}",);
|
|
let len = field.len() + field_content.len() + 2;
|
|
unpadded + &"_".repeat(width as usize - len)
|
|
}
|
|
}
|
|
|
|
impl Page for SigninPage {
|
|
fn receive_event(
|
|
&mut self,
|
|
env: Environment,
|
|
screen: &mut RawTerminal<Stdout>,
|
|
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<Stdout>,
|
|
) -> Result<()> {
|
|
self.frame = Some(env.frame.sub(
|
|
env.theme.colors.subframe,
|
|
super::frame::FrameSize::ByPercent(50, 50),
|
|
2,
|
|
));
|
|
write!(
|
|
screen,
|
|
"{theme}{clear}{frame}{subframe}{content}{hide_cursor}",
|
|
theme = env.theme.frame(),
|
|
clear = clear::All,
|
|
frame = env.frame.frame_str(env.theme.colors.primary),
|
|
subframe = self
|
|
.frame
|
|
.unwrap()
|
|
.frame_str(env.theme.colors.subwin),
|
|
hide_cursor = cursor::Hide,
|
|
content = "",
|
|
// content = self.draw(
|
|
// env.theme.colors.highlight,
|
|
// env.theme.colors.subwin
|
|
// ),
|
|
)?;
|
|
screen.flush()?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|