1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
mod addr;
mod paths;
mod referers;
pub mod summary;
mod status;

use {
    crate::*,
    crossterm::style::{Attribute::*, Color::*},
    minimad::{OwningTemplateExpander, TextTemplate},
    termimad::*,
};

pub struct Printer {
    pub skin: MadSkin,
    pub tables: Tables,
    pub terminal_width: usize,
    pub detail_level: usize,
}

impl Printer {
    pub fn new(detail_level: usize, tables: Tables) -> Self {
        let terminal_width = terminal_size().0 as usize;
        let skin = make_skin();
        Self { skin, tables, terminal_width, detail_level }
    }
    pub fn print(
        &self,
        expander: OwningTemplateExpander,
        template: &str,
    ) {
        let template = TextTemplate::from(template);
        let text = expander.expand(&template);
        let fmt_text = FmtText::from_text(&self.skin, text, Some(self.terminal_width));
        print!("{}", fmt_text);
    }
}


pub fn print_analysis(
    log_base: &LogBase,
    printer: &Printer,
) {
    let log_lines = &log_base.lines;
    for table in printer.tables.clone().into_iter() {
        match table {
            Table::Dates => {
                let histogram = Histogram::of_days(&log_base);
                histogram.print(printer);
            }
            Table::Status => {
                status::print_status_codes(log_lines, printer);
            }
            Table::RemoteAddresses => {
                addr::print_popular_remote_addresses(log_lines, printer);
            }
            Table::Referers => {
                referers::print_popular_referers(log_lines, printer);
            }
            Table::Paths => {
                paths::print_popular_paths(log_lines, printer);
            }
        }
    }
}

fn make_skin() -> MadSkin {
    let mut skin = MadSkin::default();
    skin.set_headers_fg(AnsiValue(178));
    skin.headers[1].compound_style.remove_attr(Underlined);
    skin.italic.remove_attr(Italic);
    skin.bold.set_fg(Yellow);
    skin.italic.set_fg(Magenta);
    skin.scrollbar.thumb.set_fg(AnsiValue(178));
    skin.code_block.align = Alignment::Center;
    skin
}