use crate::widget::logformatter::LogFormatter;
use crate::widget::standard_formatter::LogStandardFormatter;
use parking_lot::Mutex;
use std::sync::Arc;
use ratatui::{
buffer::Buffer,
layout::Rect,
style::Style,
widgets::{Block, Widget},
};
use crate::{CircularBuffer, TuiLoggerLevelOutput, TuiWidgetState, TUI_LOGGER};
use super::inner::TuiWidgetInnerState;
pub struct TuiLoggerWidget<'b> {
block: Option<Block<'b>>,
logformatter: Option<Box<dyn LogFormatter>>,
style: Style,
style_error: Option<Style>,
style_warn: Option<Style>,
style_debug: Option<Style>,
style_trace: Option<Style>,
style_info: Option<Style>,
format_separator: char,
format_timestamp: Option<String>,
format_output_level: Option<TuiLoggerLevelOutput>,
format_output_target: bool,
format_output_file: bool,
format_output_line: bool,
state: Arc<Mutex<TuiWidgetInnerState>>,
}
impl<'b> Default for TuiLoggerWidget<'b> {
fn default() -> TuiLoggerWidget<'b> {
TuiLoggerWidget {
block: None,
logformatter: None,
style: Default::default(),
style_error: None,
style_warn: None,
style_debug: None,
style_trace: None,
style_info: None,
format_separator: ':',
format_timestamp: Some("%H:%M:%S".to_string()),
format_output_level: Some(TuiLoggerLevelOutput::Long),
format_output_target: true,
format_output_file: true,
format_output_line: true,
state: Arc::new(Mutex::new(TuiWidgetInnerState::new())),
}
}
}
impl<'b> TuiLoggerWidget<'b> {
pub fn block(mut self, block: Block<'b>) -> Self {
self.block = Some(block);
self
}
pub fn opt_formatter(mut self, formatter: Option<Box<dyn LogFormatter>>) -> Self {
self.logformatter = formatter;
self
}
pub fn formatter(mut self, formatter: Box<dyn LogFormatter>) -> Self {
self.logformatter = Some(formatter);
self
}
pub fn opt_style(mut self, style: Option<Style>) -> Self {
if let Some(s) = style {
self.style = s;
}
self
}
pub fn opt_style_error(mut self, style: Option<Style>) -> Self {
if style.is_some() {
self.style_error = style;
}
self
}
pub fn opt_style_warn(mut self, style: Option<Style>) -> Self {
if style.is_some() {
self.style_warn = style;
}
self
}
pub fn opt_style_info(mut self, style: Option<Style>) -> Self {
if style.is_some() {
self.style_info = style;
}
self
}
pub fn opt_style_trace(mut self, style: Option<Style>) -> Self {
if style.is_some() {
self.style_trace = style;
}
self
}
pub fn opt_style_debug(mut self, style: Option<Style>) -> Self {
if style.is_some() {
self.style_debug = style;
}
self
}
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn style_error(mut self, style: Style) -> Self {
self.style_error = Some(style);
self
}
pub fn style_warn(mut self, style: Style) -> Self {
self.style_warn = Some(style);
self
}
pub fn style_info(mut self, style: Style) -> Self {
self.style_info = Some(style);
self
}
pub fn style_trace(mut self, style: Style) -> Self {
self.style_trace = Some(style);
self
}
pub fn style_debug(mut self, style: Style) -> Self {
self.style_debug = Some(style);
self
}
pub fn opt_output_separator(mut self, opt_sep: Option<char>) -> Self {
if let Some(ch) = opt_sep {
self.format_separator = ch;
}
self
}
pub fn output_separator(mut self, sep: char) -> Self {
self.format_separator = sep;
self
}
pub fn opt_output_timestamp(mut self, opt_fmt: Option<Option<String>>) -> Self {
if let Some(fmt) = opt_fmt {
self.format_timestamp = fmt;
}
self
}
pub fn output_timestamp(mut self, fmt: Option<String>) -> Self {
self.format_timestamp = fmt;
self
}
pub fn opt_output_level(mut self, opt_fmt: Option<Option<TuiLoggerLevelOutput>>) -> Self {
if let Some(fmt) = opt_fmt {
self.format_output_level = fmt;
}
self
}
pub fn output_level(mut self, level: Option<TuiLoggerLevelOutput>) -> Self {
self.format_output_level = level;
self
}
pub fn opt_output_target(mut self, opt_enabled: Option<bool>) -> Self {
if let Some(enabled) = opt_enabled {
self.format_output_target = enabled;
}
self
}
pub fn output_target(mut self, enabled: bool) -> Self {
self.format_output_target = enabled;
self
}
pub fn opt_output_file(mut self, opt_enabled: Option<bool>) -> Self {
if let Some(enabled) = opt_enabled {
self.format_output_file = enabled;
}
self
}
pub fn output_file(mut self, enabled: bool) -> Self {
self.format_output_file = enabled;
self
}
pub fn opt_output_line(mut self, opt_enabled: Option<bool>) -> Self {
if let Some(enabled) = opt_enabled {
self.format_output_line = enabled;
}
self
}
pub fn output_line(mut self, enabled: bool) -> Self {
self.format_output_line = enabled;
self
}
pub fn inner_state(mut self, state: Arc<Mutex<TuiWidgetInnerState>>) -> Self {
self.state = state;
self
}
pub fn state(mut self, state: &TuiWidgetState) -> Self {
self.state = state.inner.clone();
self
}
}
impl<'b> Widget for TuiLoggerWidget<'b> {
fn render(mut self, area: Rect, buf: &mut Buffer) {
let formatter = match self.logformatter.take() {
Some(fmt) => fmt,
None => {
let fmt = LogStandardFormatter {
style: self.style,
style_error: self.style_error,
style_warn: self.style_warn,
style_debug: self.style_debug,
style_trace: self.style_trace,
style_info: self.style_info,
format_separator: self.format_separator,
format_timestamp: self.format_timestamp,
format_output_level: self.format_output_level,
format_output_target: self.format_output_target,
format_output_file: self.format_output_file,
format_output_line: self.format_output_line,
};
Box::new(fmt)
}
};
buf.set_style(area, self.style);
let list_area = match self.block.take() {
Some(b) => {
let inner_area = b.inner(area);
b.render(area, buf);
inner_area
}
None => area,
};
if list_area.width < formatter.min_width() || list_area.height < 1 {
return;
}
let mut state = self.state.lock();
let la_height = list_area.height as usize;
let la_left = list_area.left();
let la_top = list_area.top();
let la_width = list_area.width as usize;
let mut lines = CircularBuffer::new(la_height);
{
state.opt_timestamp_next_page = None;
let opt_timestamp_bottom = state.opt_timestamp_bottom;
let mut opt_timestamp_prev_page = None;
let mut tui_lock = TUI_LOGGER.inner.lock();
let mut circular = CircularBuffer::new(10); for evt in tui_lock.events.rev_iter() {
if let Some(level) = state.config.get(&evt.target) {
if level < evt.level {
continue;
}
} else if let Some(level) = state.config.default_display_level {
if level < evt.level {
continue;
}
}
if state.focus_selected {
if let Some(target) = state.opt_selected_target.as_ref() {
if target != &evt.target {
continue;
}
}
}
if let Some(timestamp) = opt_timestamp_bottom.as_ref() {
if *timestamp < evt.timestamp {
circular.push(evt.timestamp);
continue;
}
}
if !circular.is_empty() {
state.opt_timestamp_next_page = circular.take().first().cloned();
}
let mut evt_lines = formatter.format(la_width, evt);
while let Some(line) = evt_lines.pop() {
lines.push(line);
}
if lines.len() >= la_height {
break;
}
if opt_timestamp_prev_page.is_none() && lines.len() >= la_height / 2 {
opt_timestamp_prev_page = Some(evt.timestamp);
}
}
state.opt_timestamp_prev_page = opt_timestamp_prev_page.or(state.opt_timestamp_bottom);
}
let offset: u16 = if state.opt_timestamp_bottom.is_none() {
0
} else {
let lines_cnt = lines.len();
std::cmp::max(0, la_height - lines_cnt) as u16
};
for (i, line) in lines.iter().rev().take(la_height).enumerate() {
line.render(
Rect {
x: la_left,
y: la_top + i as u16 + offset,
width: list_area.width,
height: 1,
},
buf,
)
}
}
}