cursive/src/views/debug_view.rs

79 lines
2.3 KiB
Rust
Raw Normal View History

use crate::logger;
use crate::theme;
use crate::view::View;
use crate::Printer;
2020-01-06 23:51:38 +00:00
use crate::Vec2;
2019-02-22 20:38:16 +00:00
use unicode_width::UnicodeWidthStr;
2019-02-22 20:38:16 +00:00
/// View used for debugging, showing logs.
pub struct DebugView {
// TODO: wrap log lines if needed, and save the line splits here.
2019-02-22 20:38:16 +00:00
}
impl DebugView {
/// Creates a new DebugView.
pub fn new() -> Self {
DebugView {}
}
}
2019-03-04 18:31:36 +00:00
impl Default for DebugView {
fn default() -> Self {
Self::new()
}
}
2019-02-22 20:38:16 +00:00
impl View for DebugView {
2019-02-28 23:55:02 +00:00
fn draw(&self, printer: &Printer<'_, '_>) {
2019-02-22 20:38:16 +00:00
let logs = logger::LOGS.lock().unwrap();
// Only print the last logs, so skip what doesn't fit
let skipped = logs.len().saturating_sub(printer.size.y);
2019-02-22 21:55:07 +00:00
for (i, record) in logs.iter().skip(skipped).enumerate() {
// TODO: Apply style to message? (Ex: errors in bold?)
// TODO: customizable time format? (24h/AM-PM)
2019-02-22 21:55:07 +00:00
printer.print(
(0, i),
&format!(
"{} | [ ] {}",
record.time.with_timezone(&chrono::Local).format("%T%.3f"),
record.message
),
);
let color = match record.level {
log::Level::Error => theme::BaseColor::Red.dark(),
log::Level::Warn => theme::BaseColor::Yellow.dark(),
log::Level::Info => theme::BaseColor::Black.light(),
log::Level::Debug => theme::BaseColor::Green.dark(),
log::Level::Trace => theme::BaseColor::Blue.dark(),
2019-02-22 21:55:07 +00:00
};
printer.with_color(color.into(), |printer| {
printer.print((16, i), &format!("{:5}", record.level))
});
2019-02-22 20:38:16 +00:00
}
}
fn required_size(&mut self, _constraint: Vec2) -> Vec2 {
// TODO: read the logs, and compute the required size to print it.
let logs = logger::LOGS.lock().unwrap();
2019-02-22 21:55:07 +00:00
let level_width = 8; // Width of "[ERROR] "
let time_width = 16; // Width of "23:59:59.123 | "
// The longest line sets the width
2019-02-22 20:38:16 +00:00
let w = logs
.iter()
2019-02-22 21:55:07 +00:00
.map(|record| record.message.width() + level_width + time_width)
2019-02-22 20:38:16 +00:00
.max()
.unwrap_or(1);
let h = logs.len();
Vec2::new(w, h)
}
fn layout(&mut self, _size: Vec2) {
// Uh?
}
}