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
use anyhow::Result;
use crossterm::event::Event;
use tui::{backend::Backend, layout::Rect, Frame};

use crate::{storage::SqliteStorage, tldr::scrape_tldr_github, Process, ProcessOutput};

/// Process to fetch new commands
///
/// This process will provide no UI, it will perform the job on `peek`
pub struct FetchProcess<'a> {
    /// Storage
    storage: &'a SqliteStorage,
    /// Category to fetch
    category: Option<String>,
}

impl<'a> FetchProcess<'a> {
    pub fn new(category: Option<String>, storage: &'a SqliteStorage) -> Self {
        Self { category, storage }
    }
}

impl<'a> Process for FetchProcess<'a> {
    fn min_height(&self) -> usize {
        1
    }

    fn peek(&mut self) -> Result<Option<ProcessOutput>> {
        let mut commands = scrape_tldr_github(self.category.as_deref())?;
        let new = self.storage.insert_commands(&mut commands)?;

        if new == 0 {
            Ok(Some(ProcessOutput::message(
                " -> No new commands to retrieve".to_owned(),
            )))
        } else {
            Ok(Some(ProcessOutput::message(format!(
                " -> Retrieved {new} new commands"
            ))))
        }
    }

    fn render<B: Backend>(&mut self, _frame: &mut Frame<B>, _area: Rect) {
        unreachable!()
    }

    fn process_raw_event(&mut self, _event: Event) -> Result<Option<ProcessOutput>> {
        unreachable!()
    }
}