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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
pub mod app;
pub mod styles;
pub mod ui;
pub mod data;

use app::App;
use crossterm::{
    event::{self, DisableMouseCapture, Event, KeyCode, KeyModifiers, MouseEventKind},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use log_analyzer::{
    models::settings::Settings,
    services::log_service::{LogAnalyzer, LogService},
    stores::{
        analysis_store::InMemmoryAnalysisStore, log_store::InMemmoryLogStore,
        processing_store::InMemmoryProcessingStore,
    },
};

use std::{
    error::Error,
    fs, io,
    sync::Arc,
    time::{Duration, Instant},
};
use tui::{
    backend::{Backend, CrosstermBackend},
    Frame, Terminal, style::Color,
};
use ui::{
    ui_error_message::draw_error_popup, ui_filter_popup::draw_filter_popup,
    ui_loading_popup::draw_loading_popup, ui_log_analyzer::draw_log_analyzer_view,
    ui_navigation_popup::draw_navigation_popup, ui_source_popup::draw_source_popup,
};


pub async fn async_main(settings_path: Option<String>) -> Result<(), Box<dyn Error>> {
    // setup terminal
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // Create
    let log_store = Arc::new(InMemmoryLogStore::new());
    let processing_store = Arc::new(InMemmoryProcessingStore::new());
    let analysis_store = Arc::new(InMemmoryAnalysisStore::new());

    let log_service = LogService::new(log_store, processing_store, analysis_store);
    let mut color = Color::LightBlue;

    if let Some(settings) = settings_path {
        if let Ok(file) = fs::read_to_string(settings) {
            if let Ok(settings) = Settings::from_json(&file) {
                if let Some(formats) = settings.formats {
                    for format in formats {
                        log_service.add_format(&format.alias, &format.regex)?;
                    }
                }
                if let Some(filters) = settings.filters {
                    for filter in filters {
                        log_service.add_filter(filter);
                    }
                }
                if let Some((r, g, b)) = settings.primary_color {
                    color = Color::Rgb(r, g, b)
                }
            }
        }
    }

    // create app and run it
    let tick_rate = Duration::from_millis(100);
    let app = App::new(Box::new(log_service), color).await;
    let res = run_app(&mut terminal, app, tick_rate).await;

    // restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    if let Err(err) = res {
        println!("{:?}", err);
    }
    Ok(())
}


async fn run_app<B: Backend>(
    terminal: &mut Terminal<B>,
    mut app: App,
    tick_rate: Duration,
) -> io::Result<()> {
    let mut last_tick = Instant::now();

    loop {
        terminal.draw(|f| ui(f, &mut app))?;

        let timeout = tick_rate
            .checked_sub(last_tick.elapsed())
            .unwrap_or_else(|| Duration::from_secs(0));
        if crossterm::event::poll(timeout)? {
            let event = event::read()?;

            match event {
                Event::Key(key) => {
                    match key.modifiers {
                        // Quit
                        KeyModifiers::CONTROL => match key.code {
                            KeyCode::Char('c') => return Ok(()),
                            _ => async_std::task::block_on(app.handle_input(key)),
                        },
                        // Navigate
                        KeyModifiers::SHIFT => match key.code {
                            KeyCode::Char(_) => async_std::task::block_on(app.handle_input(key)),
                            KeyCode::Up | KeyCode::BackTab => app.navigate(KeyCode::Up),
                            KeyCode::Down | KeyCode::Tab => app.navigate(KeyCode::Down),
                            KeyCode::Left => app.navigate(KeyCode::Left),
                            KeyCode::Right => app.navigate(KeyCode::Right),
                            _ => {}
                        },
                        // Handle in widget
                        _ => match key.code {
                            KeyCode::Tab => app.navigate(KeyCode::Down),
                            _ => app.handle_input(key).await,
                        },
                    }
                }
                Event::Mouse(mouse) => match mouse.kind {
                    MouseEventKind::ScrollUp => {}
                    MouseEventKind::ScrollDown => {}
                    MouseEventKind::Down(button) => match button {
                        crossterm::event::MouseButton::Left => {}
                        crossterm::event::MouseButton::Right => {}
                        _ => {}
                    },
                    _ => {}
                },
                _ => {}
            }
        }
        if last_tick.elapsed() >= tick_rate {
            app.on_tick().await;
            last_tick = Instant::now();
        }
    }
}

fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
    draw_log_analyzer_view(f, app);

    if app.show_source_popup {
        draw_source_popup(f, app)
    } else if app.show_filter_popup {
        draw_filter_popup(f, app)
    } else if app.show_navigation_popup {
        draw_navigation_popup(f, app)
    }

    if app.show_error_message {
        draw_error_popup(f, app)
    }

    if app.processing.is_processing {
        draw_loading_popup(f, app)
    }
}