use crate::frame::{CellColor, CellStyle};
use crate::region::Region;
#[derive(Debug, Clone)]
pub struct MatchedSpan {
pub text: String,
pub rect: Region,
pub foreground: Option<CellColor>,
pub background: Option<CellColor>,
pub style: CellStyle,
}
impl MatchedSpan {
pub fn has_fg(&self, color: &CellColor) -> bool {
self.foreground.as_ref() == Some(color)
}
pub fn has_bg(&self, color: &CellColor) -> bool {
self.background.as_ref() == Some(color)
}
pub fn is_bold(&self) -> bool {
self.style.bold()
}
pub fn is_inverse(&self) -> bool {
self.style.inverse()
}
pub fn is_highlighted(&self) -> bool {
self.style.bold() || self.style.inverse() || self.background.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_span() -> MatchedSpan {
MatchedSpan {
text: "Tab".to_string(),
rect: Region::new(5, 0, 3, 1),
foreground: Some(CellColor::white()),
background: Some(CellColor::new(0, 0, 128)),
style: CellStyle::from_raw(0x01),
}
}
#[test]
fn has_fg_matches_exact_color() {
let span = sample_span();
assert!(span.has_fg(&CellColor::white()));
assert!(!span.has_fg(&CellColor::black()));
}
#[test]
fn has_bg_matches_exact_color() {
let span = sample_span();
assert!(span.has_bg(&CellColor::new(0, 0, 128)));
assert!(!span.has_bg(&CellColor::white()));
}
#[test]
fn is_highlighted_detects_bold() {
let span = sample_span();
assert!(span.is_highlighted());
assert!(span.is_bold());
}
#[test]
fn is_highlighted_detects_background_color() {
let span = MatchedSpan {
text: "item".to_string(),
rect: Region::new(0, 0, 4, 1),
foreground: None,
background: Some(CellColor::new(50, 50, 50)),
style: CellStyle::default(),
};
assert!(span.is_highlighted());
}
#[test]
fn not_highlighted_when_plain() {
let span = MatchedSpan {
text: "plain".to_string(),
rect: Region::new(0, 0, 5, 1),
foreground: None,
background: None,
style: CellStyle::default(),
};
assert!(!span.is_highlighted());
}
}