use crate::layout::{Cells, LengthBound, Pos};
use crate::text::Theme;
use crate::{Result, Widget};
use textwrap::wrap;
use unicode_width::UnicodeWidthStr;
pub struct Label {
text: String,
}
impl Label {
pub fn new(text: &str) -> Self {
let text = text.to_string();
Label { text }
}
pub fn text(&self) -> &str {
&self.text
}
}
impl Widget for Label {
fn width_bounds(&self, _theme: &Theme) -> LengthBound {
let w = self.text.width() as u16;
match w {
0..=8 => LengthBound::new(w..),
9..=20 => LengthBound::new(10..),
_ => LengthBound::new(12..),
}
}
fn height_bounds(&self, _theme: &Theme, width: u16) -> LengthBound {
let rows = wrap(&self.text, usize::from(width)).len() as u16;
LengthBound::new(rows..=rows)
}
fn draw(&self, cells: &mut Cells, offset: Pos) -> Result<()> {
cells.print_text(&self.text, offset)
}
}