kk/src/display/compose.rs

65 lines
1.4 KiB
Rust
Raw Normal View History

use std::ops::Deref;
use super::{frame::Frame, theme::ColorSet};
#[derive(Clone, Debug)]
pub enum Component {
String(String),
Goto(u16, u16),
Theme(ColorSet),
Clear,
}
impl Component {
pub fn prepare_for(&self, frame: &Frame) -> String {
match self {
Component::String(s) => s.to_owned(),
Component::Goto(x, y) => frame.goto(*x, *y),
Component::Theme(c) => c.to_string(),
Component::Clear => termion::clear::All.to_string(),
}
}
}
#[derive(Clone, Debug)]
pub struct Components(Vec<Component>);
impl Deref for Components {
type Target = Vec<Component>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Vec<Component>> for Components {
fn from(value: Vec<Component>) -> Self {
Self(value)
}
}
impl From<Component> for Components {
fn from(value: Component) -> Self {
Self(vec![value])
}
}
impl Components {
pub fn str_len(&self) -> u16 {
let mut len_cnt: u16 = 0;
for elem in &self.0 {
if let Component::String(s) = elem {
len_cnt += s.len() as u16;
}
}
len_cnt
}
pub fn concat_for(self, frame: &Frame) -> String {
self.0
.into_iter()
.map(|component| component.prepare_for(frame))
.collect::<String>()
}
}