|
|
|
@ -3,10 +3,12 @@ use termion::{clear, cursor}; |
|
|
|
|
const ESTIMATED_FRAME_BIT_SIZE: usize = 11; |
|
|
|
|
use std::io::Write; |
|
|
|
|
|
|
|
|
|
use crate::display::debug::debug; |
|
|
|
|
|
|
|
|
|
use super::theme::Theme; |
|
|
|
|
|
|
|
|
|
const FRAME_CHAR_HORIZONTAL: char = 'h'; |
|
|
|
|
const FRAME_CHAR_VERTICAL: char = 'v'; |
|
|
|
|
const FRAME_CHAR_HORIZONTAL: char = ' '; |
|
|
|
|
const FRAME_CHAR_VERTICAL: char = ' '; |
|
|
|
|
|
|
|
|
|
pub enum FrameSize { |
|
|
|
|
ByAbsolute(u16, u16), |
|
|
|
@ -18,72 +20,100 @@ pub struct Frame { |
|
|
|
|
// Both are (w, h)
|
|
|
|
|
start: (u16, u16), |
|
|
|
|
end: (u16, u16), |
|
|
|
|
width: u16, |
|
|
|
|
theme: Theme, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Frame { |
|
|
|
|
#[inline] |
|
|
|
|
#[inline(always)] |
|
|
|
|
pub fn goto(&self, x: u16, y: u16) -> String { |
|
|
|
|
let cg = cursor::Goto( |
|
|
|
|
(self.start.0 + x).min(self.end.0 - 1), |
|
|
|
|
(self.start.1 + y).min(self.end.1 - 1), |
|
|
|
|
(self.width + self.start.0 + x).min(self.end.0 - self.width), |
|
|
|
|
(self.width + self.start.1 + y).min(self.end.1 - self.width), |
|
|
|
|
); |
|
|
|
|
cg.into() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline(always)] |
|
|
|
|
fn goto_internal(&self, x: u16, y: u16) -> String { |
|
|
|
|
cursor::Goto( |
|
|
|
|
(self.start.0 + x).min(self.end.0), |
|
|
|
|
(self.start.1 + y).min(self.end.1), |
|
|
|
|
) |
|
|
|
|
.into() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn writeln(&self, s: &str, x: u16, y: u16) -> String { |
|
|
|
|
let words = s.split('\n').collect::<Vec<&str>>(); |
|
|
|
|
let mut out_words = Vec::with_capacity(words.len()); |
|
|
|
|
for i in 0..words.len() { |
|
|
|
|
out_words.push(format!( |
|
|
|
|
"{str}{ret}", |
|
|
|
|
"{ret}{str}", |
|
|
|
|
str = words[i], |
|
|
|
|
ret = self.goto(1, y + i as u16 + 1) |
|
|
|
|
ret = self.goto(x, y + i as u16), |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
out_words.concat() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
|
|
#[inline(always)] |
|
|
|
|
pub fn size(&self) -> (u16, u16) { |
|
|
|
|
(self.end.0 - self.start.0, self.end.1 - self.start.1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn from_terminal_size() -> Result<Self, anyhow::Error> { |
|
|
|
|
#[inline(always)] |
|
|
|
|
pub fn from_terminal_size(theme: Theme, width: u16) -> Result<Self, anyhow::Error> { |
|
|
|
|
let term = termion::terminal_size()?; |
|
|
|
|
// Swap term dimensions to use x/y
|
|
|
|
|
let term = (term.1, term.0); |
|
|
|
|
Ok(Self::from_bottom_right(term, term)) |
|
|
|
|
Ok(Self::from_bottom_right(term, term, width, theme)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn from_bottom_right((pos_h, pos_w): (u16, u16), (term_h, term_w): (u16, u16)) -> Self { |
|
|
|
|
#[inline(always)] |
|
|
|
|
fn from_bottom_right( |
|
|
|
|
(pos_h, pos_w): (u16, u16), |
|
|
|
|
(term_h, term_w): (u16, u16), |
|
|
|
|
width: u16, |
|
|
|
|
theme: Theme, |
|
|
|
|
) -> Self { |
|
|
|
|
Self { |
|
|
|
|
start: (1.max(term_w - pos_w), 1.max(term_h - pos_h)), |
|
|
|
|
end: (pos_w, pos_h), |
|
|
|
|
width, |
|
|
|
|
theme, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn frame_str(&self, theme: &Theme, body_theme: String) -> String { |
|
|
|
|
self.goto(0, 0); |
|
|
|
|
pub fn frame_str(&self, body_theme: String) -> String { |
|
|
|
|
if self.width == 0 { |
|
|
|
|
return body_theme + &clear::All.to_string(); |
|
|
|
|
} |
|
|
|
|
self.goto_internal(0, 0); |
|
|
|
|
let (w_len, h_len) = self.size(); |
|
|
|
|
let width_str = FRAME_CHAR_HORIZONTAL.to_string().repeat(w_len as usize + 1); |
|
|
|
|
let mut frame = Vec::with_capacity(h_len as usize + 4); |
|
|
|
|
let frame_theme = theme.frame(); |
|
|
|
|
let frame_theme = self.theme.frame(); |
|
|
|
|
let body_clear = body_theme.clone() + &clear::CurrentLine.to_string(); |
|
|
|
|
frame.push(frame_theme.clone()); |
|
|
|
|
|
|
|
|
|
let make_line = |
|
|
|
|
|y: u16| format!("{left}{}", width_str, left = cursor::Goto(self.start.0, y)); |
|
|
|
|
frame.push(make_line(self.start.1)); |
|
|
|
|
for y in self.start.1 + 1..self.end.1 { |
|
|
|
|
for y in 0..self.width { |
|
|
|
|
frame.push(make_line(self.start.1 + y)); |
|
|
|
|
} |
|
|
|
|
for y in self.width + self.start.1..self.end.1 - self.width { |
|
|
|
|
frame.push(format!( |
|
|
|
|
"{left}{body_clear}{frame_theme}{char}{right}{char}", |
|
|
|
|
body_clear = &body_clear, |
|
|
|
|
frame_theme = &frame_theme, |
|
|
|
|
left = cursor::Goto(self.start.0, y), |
|
|
|
|
right = cursor::Goto(self.start.0 + self.end.0, y), |
|
|
|
|
char = FRAME_CHAR_VERTICAL, |
|
|
|
|
right = cursor::Goto(self.start.0 + self.end.0 - self.width, y), |
|
|
|
|
char = FRAME_CHAR_VERTICAL.to_string().repeat(self.width as usize), |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
for y in self.end.1 - self.width - 1..self.end.1 { |
|
|
|
|
frame.push(make_line(self.start.1 + y)); |
|
|
|
|
} |
|
|
|
|
frame.push(make_line(self.end.1)); |
|
|
|
|
frame.push(self.goto(1, 1)); |
|
|
|
|
frame.push(body_theme); |
|
|
|
@ -92,7 +122,8 @@ impl Frame { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl FrameSize { |
|
|
|
|
fn abs_size(&self) -> Frame { |
|
|
|
|
#[inline(always)] |
|
|
|
|
fn abs_size(&self, theme: Theme, width: u16) -> Frame { |
|
|
|
|
let (term_height, term_width) = |
|
|
|
|
termion::terminal_size().expect("could not get terminal size"); |
|
|
|
|
let pos = match self { |
|
|
|
@ -108,6 +139,6 @@ impl FrameSize { |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
Frame::from_bottom_right(pos, (term_height, term_width)) |
|
|
|
|
Frame::from_bottom_right(pos, (term_height, term_width), width, theme) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|