Render text block padding

This commit is contained in:
mars 2022-10-22 22:57:25 -06:00
parent 8fb89ad70b
commit 8f92067869
2 changed files with 56 additions and 1 deletions

View File

@ -69,7 +69,11 @@ impl Dom {
panic!("Couldn't find text info on node {}", node);
};
for line in layout.render(&style).lines.into_iter() {
let rendered = layout.render(&style);
let block: &Block = self.nodes.get(node).unwrap();
let rendered = rendered.add_padding(&block.padding);
for line in rendered.lines.into_iter() {
println!("{}", line);
}

View File

@ -1,3 +1,5 @@
use strum::EnumString;
use crate::args::*;
use crate::ast::{ParseErrorKind, ParseResult, Value, ValueKind};
use crate::source::WithSource;
@ -8,6 +10,44 @@ pub struct RenderedBlock {
pub width: usize,
pub height: usize,
}
impl RenderedBlock {
pub fn add_padding(&self, padding: &Rect<usize>) -> Self {
let make_spaces = |num: usize| -> String { std::iter::repeat(" ").take(num).collect() };
let new_width = self.width + padding.l + padding.r;
let left_pad = make_spaces(padding.l);
let right_pad = make_spaces(padding.r);
let vertical_pad = make_spaces(new_width);
let mut lines = Vec::new();
lines.extend(std::iter::repeat(vertical_pad.clone()).take(padding.t));
lines.extend(
self.lines
.iter()
.map(|line| format!("{}{}{}", left_pad, line, right_pad)),
);
lines.extend(std::iter::repeat(vertical_pad.clone()).take(padding.b));
Self {
lines,
width: new_width,
height: self.height + padding.t + padding.b,
}
}
}
#[derive(Clone, Debug, EnumString)]
#[strum(serialize_all = "kebab-case", ascii_case_insensitive)]
pub enum Direction {
Left,
Up,
Right,
Down,
}
#[derive(Clone, Debug)]
pub enum AxisSize {
Fixed(usize),
@ -73,6 +113,17 @@ pub struct Rect<T> {
pub b: T,
}
impl<T> Rect<T> {
pub fn map<O>(&self, f: impl Fn(&T) -> O) -> Rect<O> {
Rect::<O> {
l: f(&self.l),
t: f(&self.t),
r: f(&self.r),
b: f(&self.b),
}
}
}
impl<T: Clone + ParseValue> Rect<T> {
pub fn parse_shorthand(parsed: WithSource<&[T]>) -> ParseResult<Self> {
match parsed.len() {