use ratatui::{
prelude::Line,
style::{Color, Modifier, Style},
text::{Span, Text},
};
use std::{
io::{self, Write},
sync::{Arc, Mutex},
};
use tracing::warn;
#[derive(Debug, Default, Clone)]
pub struct TuiLog<'a> {
pub inner: Arc<Mutex<Text<'a>>>,
}
impl<'a> TuiLog<'a> {
pub fn resize(&self, h: usize) {
let mut inner = self.inner.lock().unwrap();
let len = inner.lines.len().saturating_sub(h);
inner.lines.drain(..len);
}
}
impl<'a> Write for TuiLog<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
use cansi::v3::categorise_text;
let line =
core::str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let mut spans = Vec::new();
let mut lines = Vec::new();
for out in categorise_text(line) {
let mut style = Style::default();
style.fg = match out.fg {
Some(cansi::Color::Black) => Some(Color::Black),
Some(cansi::Color::Red) => Some(Color::Red),
Some(cansi::Color::Green) => Some(Color::Green),
Some(cansi::Color::Yellow) => Some(Color::Yellow),
Some(cansi::Color::Blue) => Some(Color::Blue),
Some(cansi::Color::Magenta) => Some(Color::Magenta),
Some(cansi::Color::Cyan) => Some(Color::Cyan),
Some(cansi::Color::White) => Some(Color::White),
Some(c) => {
warn!("Unknown color {:#?}", c);
style.fg
},
None => style.fg,
};
match out.intensity {
Some(cansi::Intensity::Normal) | None => {},
Some(cansi::Intensity::Bold) => style.add_modifier = Modifier::BOLD,
Some(cansi::Intensity::Faint) => style.add_modifier = Modifier::DIM,
}
for t in out.text.split_inclusive('\n') {
if !t.is_empty() {
spans.push(Span::styled(t.to_owned(), style));
}
if t.ends_with('\n') {
lines.push(Line::from(core::mem::take(&mut spans)));
}
}
}
if !spans.is_empty() {
lines.push(Line::from(spans));
}
self.inner.lock().unwrap().lines.append(&mut lines);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}