wip centered

This commit is contained in:
emilis 2023-01-14 23:57:33 +00:00
parent 9dbc2fc471
commit cf0b013204
2 changed files with 30 additions and 16 deletions

View File

@ -102,7 +102,11 @@ impl SigninPage {
#[inline] #[inline]
fn draw(&self) -> String { fn draw(&self) -> String {
let frame = self.frame.unwrap(); let frame = self.frame.unwrap();
frame.write_centered("login kk", 1) format!(
"{header}{hostname}",
header = frame.write_centered("login kk", 1)
hostname = frame.write_centered("instance:", 3),
)
} }
} }
@ -142,7 +146,7 @@ impl Page for SigninPage {
)); ));
write!( write!(
screen, screen,
"{theme}{clear}{frame}{subframe}{hide_cursor}{content}", "{theme}{clear}{frame}{subframe}{show_cursor}{content}",
theme = env.theme.frame(), theme = env.theme.frame(),
clear = clear::All, clear = clear::All,
frame = env.frame.frame_str(env.theme.primary()), frame = env.frame.frame_str(env.theme.primary()),
@ -150,7 +154,7 @@ impl Page for SigninPage {
.frame .frame
.unwrap() .unwrap()
.frame_str(ColorSet::default().to_string()), .frame_str(ColorSet::default().to_string()),
hide_cursor = cursor::Hide, show_cursor = cursor::Show,
content = self.draw(), content = self.draw(),
)?; )?;
screen.flush()?; screen.flush()?;

View File

@ -58,6 +58,27 @@ impl Frame {
.into() .into()
} }
#[inline(always)]
fn write_centered_clear(&self, s: &str) -> String {
let width = self.size().0 as usize;
let len = s.len();
let base_size = ((width - len) / 2) - self.border as usize;
format!(
"{left}{line}{right}",
left = " ".repeat(base_size + ((width - len) % 2)),
line = s,
right = " ".repeat(base_size)
)
}
#[inline(always)]
fn write_frame_line(&self, y: u16, length: u16) -> String {
self.goto_internal(0, y)
+ &FRAME_CHAR_HORIZONTAL
.to_string()
.repeat(length as usize)
}
#[inline(always)] #[inline(always)]
fn write_clear_to_end(&self, s: &str) -> String { fn write_clear_to_end(&self, s: &str) -> String {
let clear_length = self.size().0 as usize let clear_length = self.size().0 as usize
@ -79,25 +100,14 @@ impl Frame {
out_words.concat() out_words.concat()
} }
#[inline(always)]
fn write_frame_line(&self, y: u16, length: u16) -> String {
self.goto_internal(0, y)
+ &FRAME_CHAR_HORIZONTAL
.to_string()
.repeat(length as usize)
}
pub fn write_centered(&self, s: &str, y: u16) -> String { pub fn write_centered(&self, s: &str, y: u16) -> String {
let (width, _) = self.size();
let words = s.split('\n').collect::<Vec<&str>>(); let words = s.split('\n').collect::<Vec<&str>>();
let mut out_words = Vec::with_capacity(words.len()); let mut out_words = Vec::with_capacity(words.len());
for i in 0..words.len() { for i in 0..words.len() {
let word = words[i];
let x = width - (word.len() as u16).min(width);
out_words.push(format!( out_words.push(format!(
"{ret}{str}", "{ret}{str}",
str = word, ret = self.goto(0, y + i as u16),
ret = self.goto(x / 2, y + i as u16), str = self.write_centered_clear(words[i]),
)); ));
} }
out_words.concat() out_words.concat()