43 lines
1.4 KiB
Rust
43 lines
1.4 KiB
Rust
|
|
use color::Rgba8;
|
||
|
|
use ratatui::layout::Rect;
|
||
|
|
use ratatui::prelude::{Color, Line, Span, Style};
|
||
|
|
use kiln_core::log::LogLine;
|
||
|
|
|
||
|
|
/// Converts a core [`Rgba8`] color into a ratatui truecolor [`Color`].
|
||
|
|
///
|
||
|
|
/// The alpha channel is dropped — terminals have no per-cell alpha. Truecolor
|
||
|
|
/// requires terminal support; terminals limited to 256 colors approximate it.
|
||
|
|
pub fn rgba8_to_color(c: Rgba8) -> Color {
|
||
|
|
Color::Rgb(c.r, c.g, c.b)
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Converts a core [`LogLine`] into a styled ratatui [`Line`] for display.
|
||
|
|
///
|
||
|
|
/// Each [`LogSpan`](kiln_core::log::LogSpan) becomes a ratatui [`Span`]; a span's
|
||
|
|
/// foreground/background color is applied only when present, so `None` colors
|
||
|
|
/// inherit the surrounding (default) style.
|
||
|
|
pub fn logline_to_line(line: &LogLine) -> Line<'static> {
|
||
|
|
let spans = line
|
||
|
|
.spans
|
||
|
|
.iter()
|
||
|
|
.map(|s| {
|
||
|
|
let mut style = Style::default();
|
||
|
|
if let Some(fg) = s.fg {
|
||
|
|
style = style.fg(rgba8_to_color(fg));
|
||
|
|
}
|
||
|
|
if let Some(bg) = s.bg {
|
||
|
|
style = style.bg(rgba8_to_color(bg));
|
||
|
|
}
|
||
|
|
Span::styled(s.text.clone(), style)
|
||
|
|
})
|
||
|
|
.collect::<Vec<_>>();
|
||
|
|
Line::from(spans)
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Returns true if two `Rect`s share at least one cell.
|
||
|
|
pub fn rects_overlap(a: Rect, b: Rect) -> bool {
|
||
|
|
a.x < b.x + b.width
|
||
|
|
&& b.x < a.x + a.width
|
||
|
|
&& a.y < b.y + b.height
|
||
|
|
&& b.y < a.y + a.height
|
||
|
|
}
|