use crossterm::style::Color;
use unicode_segmentation::UnicodeSegmentation;
use crate::graphics::FontStyle;
use crate::tdom::node::NodeId;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InlineRun {
pub text: String,
pub style: FontStyle,
pub fg: Option<Color>,
pub bg: Option<Color>,
pub force_break: bool,
pub node_id: Option<NodeId>,
}
impl InlineRun {
pub fn new(
text: impl Into<String>,
style: FontStyle,
fg: Option<Color>,
bg: Option<Color>,
) -> Self {
Self {
text: text.into(),
style,
fg,
bg,
force_break: false,
node_id: None,
}
}
pub fn owned(
text: impl Into<String>,
style: FontStyle,
fg: Option<Color>,
bg: Option<Color>,
node_id: Option<NodeId>,
) -> Self {
Self {
text: text.into(),
style,
fg,
bg,
force_break: false,
node_id,
}
}
pub fn forced_break(style: FontStyle, fg: Option<Color>, bg: Option<Color>) -> Self {
Self {
text: "\n".to_string(),
style,
fg,
bg,
force_break: true,
node_id: None,
}
}
pub fn width(&self) -> usize {
use crate::graphics::grapheme_width;
self.text.graphemes(true).map(grapheme_width).sum()
}
pub fn width_at(&self, font_size_cells: usize) -> usize {
use crate::graphics::grapheme_width;
let font_size = font_size_cells.max(1);
self.text
.graphemes(true)
.map(|g| grapheme_width(g).max(font_size))
.sum()
}
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
}