some cleanup

This commit is contained in:
Robin Appelman 2024-07-30 23:11:26 +02:00
commit 6530749a01
9 changed files with 70 additions and 60 deletions

View file

@ -52,9 +52,9 @@ fn log_row<'a>(result: &'a LogMatch, app: &'a App, name: &'static str) -> Row<'a
let mut lines = String::new();
for index in match_result.iter() {
let statement = app.log_statements.get(index).expect("invalid match index");
writeln!(&mut message, "{}", statement.message()).unwrap();
writeln!(&mut paths, "{}", statement.path()).unwrap();
writeln!(&mut lines, "{}", statement.line).unwrap();
writeln!(&mut message, "{}", statement.message()).ok();
writeln!(&mut paths, "{}", statement.path()).ok();
writeln!(&mut lines, "{}", statement.line).ok();
}
Row::new([
Text::from(message),

View file

@ -1,11 +1,10 @@
use crate::app::App;
use crate::logline::LogLine;
use crate::ui::style::{TABLE_HEADER_STYLE, TIME_FORMAT};
use crate::logline::{format_time, LogLine};
use crate::ui::style::TABLE_HEADER_STYLE;
use crate::ui::table::ScrollbarTable;
use ratatui::layout::{Alignment, Constraint};
use ratatui::text::Text;
use ratatui::widgets::{Cell, Row};
use time::format_description::well_known::Iso8601;
pub fn raw_logs<'a>(app: &'a App, lines: &[usize]) -> ScrollbarTable<'a> {
let lines = lines.iter().copied().map(|i| &app.lines[i]);
@ -35,6 +34,6 @@ fn log_row(line: &LogLine) -> Row {
Text::from(line.level.as_str()),
Text::from(line.app.as_str()),
Text::from(line.display()),
Text::from(line.time.format(&Iso8601::<TIME_FORMAT>).unwrap()).alignment(Alignment::Right),
Text::from(format_time(line.time)).alignment(Alignment::Right),
])
}

View file

@ -1,21 +1,20 @@
use crate::app::App;
use crate::logline::{FullException, FullLogLine, LogLine, Trace};
use crate::logline::{format_time, FullException, FullLogLine, LogLine, Trace};
use crate::parse_line_full;
use crate::ui::style::{TABLE_HEADER_STYLE, TIME_FORMAT};
use crate::ui::style::TABLE_HEADER_STYLE;
use crate::ui::table::{ScrollbarTable, ScrollbarTableState};
use ratatui::prelude::*;
use ratatui::widgets::{Cell, Paragraph, Row, Wrap};
use std::iter::once;
use time::format_description::well_known::Iso8601;
pub fn single_log(app: &App, line: &LogLine) -> SingleLog {
let raw_line = app.get_line(line.index).unwrap();
let line = parse_line_full(raw_line).unwrap();
let raw_line = app.get_line(line.index);
let line = raw_line.and_then(|raw_line| parse_line_full(raw_line).ok());
SingleLog { line }
}
pub struct SingleLog {
line: FullLogLine,
line: Option<FullLogLine>,
}
impl StatefulWidget for SingleLog {
@ -25,46 +24,51 @@ impl StatefulWidget for SingleLog {
where
Self: Sized,
{
let par = Paragraph::new(format!(
"{}\n\n {} {}\n {}\n\n from {} by {} at {}",
self.line.message,
self.line.method,
self.line.url,
self.line.user_agent,
self.line.remote_address,
self.line.user,
self.line.time.format(&Iso8601::<TIME_FORMAT>).unwrap()
))
.wrap(Wrap::default());
if let Some(line) = self.line {
let par = Paragraph::new(format!(
"{}\n\n {} {}\n {}\n\n from {} by {} at {}",
line.message,
line.method,
line.url,
line.user_agent,
line.remote_address,
line.user,
format_time(line.time),
))
.wrap(Wrap::default());
let layout = Layout::default()
.direction(Direction::Vertical)
.constraints(vec![
Constraint::Min(7),
Constraint::Min(5),
Constraint::Percentage(100),
])
.split(area);
let layout = Layout::default()
.direction(Direction::Vertical)
.constraints(vec![
Constraint::Min(7),
Constraint::Min(5),
Constraint::Percentage(100),
])
.split(area);
par.render(layout[0], buf);
par.render(layout[0], buf);
if let Some(exception) = &self.line.exception {
if self.line.message.contains(&exception.message) {
StatefulWidget::render(
render_exception(exception),
layout[1].union(layout[2]),
buf,
state,
);
} else {
let ex_par = Paragraph::new(format!(
"\n{}:\n {}",
exception.exception, exception.message
))
.wrap(Wrap::default());
ex_par.render(layout[1], buf);
StatefulWidget::render(render_exception(exception), layout[2], buf, state);
if let Some(exception) = &line.exception {
if line.message.contains(&exception.message) {
StatefulWidget::render(
render_exception(exception),
layout[1].union(layout[2]),
buf,
state,
);
} else {
let ex_par = Paragraph::new(format!(
"\n{}:\n {}",
exception.exception, exception.message
))
.wrap(Wrap::default());
ex_par.render(layout[1], buf);
StatefulWidget::render(render_exception(exception), layout[2], buf, state);
}
}
} else {
let par = Paragraph::new("Failed to parse log line").wrap(Wrap::default());
par.render(area, buf);
}
}
}

View file

@ -1,11 +1,5 @@
use ratatui::prelude::Style;
use ratatui::style::palette::tailwind;
use time::format_description::well_known::iso8601::{Config, EncodedConfig, TimePrecision};
pub const TABLE_HEADER_STYLE: Style = Style::new().bg(tailwind::BLACK).fg(tailwind::GREEN.c600);
pub const TABLE_SELECTED_STYLE: Style = Style::new().fg(tailwind::BLACK).bg(tailwind::GREEN.c600);
pub const TIME_FORMAT: EncodedConfig = Config::DEFAULT
.set_time_precision(TimePrecision::Second {
decimal_digits: None,
})
.encode();

View file

@ -57,7 +57,7 @@ impl ScrollbarTableState {
}
pub fn selected(&self) -> usize {
self.table.selected().unwrap()
self.table.selected().unwrap_or_default()
}
pub fn up(&mut self, step: usize) -> usize {