Compare commits

...

2 Commits

2 changed files with 68 additions and 26 deletions

View File

@ -28,7 +28,7 @@ impl Body {
.nextline_after_text(
0,
HorizontalAlignment::Left,
&env.frame,
env.frame.inner_size().0,
);
write!(screen, "{}", env.frame.make(echo_comps))?;
screen.flush()?;
@ -82,25 +82,41 @@ impl Page for Body {
#[derive(Debug, Clone, Default)]
pub struct SigninPage {
hostname: String,
username: String,
cursor: SigninCursorLocation,
frame: Option<Frame>,
}
#[derive(Debug, Clone)]
enum SigninCursorLocation {
Hostname(u16),
Hostname,
Ok,
}
impl Default for SigninCursorLocation {
fn default() -> Self {
Self::Ok
Self::Hostname
}
}
impl SigninPage {
#[inline]
fn next(&mut self) {
self.cursor = match self.cursor {
SigninCursorLocation::Hostname => {
SigninCursorLocation::Ok
}
SigninCursorLocation::Ok => {
SigninCursorLocation::Hostname
}
}
}
#[inline]
fn previous(&mut self) {
// Don't tell them this neat trick
self.next()
}
fn draw(
&self,
highlight: ColorSet,
@ -113,6 +129,11 @@ impl SigninPage {
let (w, _) = frame.inner_size();
let exp_w = w / 3;
let (hostname_theme, ok_theme) = match self.cursor {
SigninCursorLocation::Hostname => (highlight, normal),
SigninCursorLocation::Ok => (normal, highlight),
};
vec![
frame
.prefix_centered_goto(
@ -126,7 +147,7 @@ impl SigninPage {
frame
.prefix_centered_goto(
self.field(
highlight,
hostname_theme,
normal,
exp_w,
"hostname",
@ -135,18 +156,11 @@ impl SigninPage {
HOSTNAME_Y,
)
.0,
frame
.prefix_centered_goto(
self.field(
highlight,
normal,
exp_w,
"username",
&self.username,
),
OK_Y,
)
vec![Component::Theme(ok_theme)],
HorizontalAlignment::Center
.align(Text::Normal("[ok]".into()), w, OK_Y)
.0,
vec![Component::Theme(normal)],
]
.into_iter()
.flatten()
@ -181,6 +195,16 @@ impl SigninPage {
]
.into()
}
#[inline]
fn char(&mut self, c: char) {
match self.cursor {
SigninCursorLocation::Hostname => {
self.hostname.push(c);
}
SigninCursorLocation::Ok => {}
}
}
}
impl Page for SigninPage {
@ -192,10 +216,16 @@ impl Page for SigninPage {
) -> 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::Char(c) => self.char(c),
termion::event::Key::Up => self.next(),
termion::event::Key::Down => self.previous(),
termion::event::Key::Backspace => {
if let SigninCursorLocation::Hostname =
self.cursor
{
self.hostname.pop();
}
}
termion::event::Key::Delete => {}
termion::event::Key::Left => {}
termion::event::Key::Right => {}
@ -204,6 +234,19 @@ impl Page for SigninPage {
},
}
let fr = self.frame.unwrap();
write!(
screen,
"{}{}{}",
env.primary_frame(),
fr.frame_str(env.theme.colors.subwin),
fr.make(self.draw(
env.theme.colors.highlight,
env.theme.colors.subwin
))
)?;
screen.flush()?;
Ok(())
}

View File

@ -88,23 +88,22 @@ impl HorizontalAlignment {
pub fn align(
&self,
s: Text,
frame: &Frame,
body_width: u16,
y: u16,
) -> Components {
let (width, _) = frame.size();
match self {
Self::Left => {
vec![Component::Goto(0, y), Component::String(s)]
}
Self::Right => {
vec![
Component::Goto(s.len() as u16 - width, y),
Component::Goto(s.len() as u16 - body_width, y),
Component::String(s),
]
}
Self::Center => {
vec![
Component::Goto((width - s.len()) / 2, y),
Component::Goto((body_width - s.len()) / 2, y),
Component::String(s),
]
}
@ -173,13 +172,13 @@ impl Components {
self,
y: u16,
align: HorizontalAlignment,
frame: &Frame,
body_width: u16,
) -> Self {
let mut last_y = y;
let xxx = self.0.into_iter().map(|comp| match comp {
Component::String(s) => {
last_y += 1;
align.align(s, frame, last_y).0
align.align(s, body_width, last_y).0
}
Component::Goto(_, y) => {
last_y = y;