intelli_shell/process/
fetch.rs

1use anyhow::Result;
2use crossterm::event::Event;
3use ratatui::{backend::Backend, layout::Rect, Frame};
4
5use crate::{storage::SqliteStorage, tldr::scrape_tldr_github, Process, ProcessOutput};
6
7/// Process to fetch new commands
8///
9/// This process will provide no UI, it will perform the job on `peek`
10pub struct FetchProcess<'a> {
11    /// Storage
12    storage: &'a SqliteStorage,
13    /// Category to fetch
14    category: Option<String>,
15}
16
17impl<'a> FetchProcess<'a> {
18    pub fn new(category: Option<String>, storage: &'a SqliteStorage) -> Self {
19        Self { category, storage }
20    }
21}
22
23impl<'a> Process for FetchProcess<'a> {
24    fn min_height(&self) -> usize {
25        1
26    }
27
28    fn peek(&mut self) -> Result<Option<ProcessOutput>> {
29        let mut commands = scrape_tldr_github(self.category.as_deref())?;
30        let new = self.storage.insert_commands(&mut commands)?;
31
32        if new == 0 {
33            Ok(Some(ProcessOutput::message(
34                " -> No new commands to retrieve".to_owned(),
35            )))
36        } else {
37            Ok(Some(ProcessOutput::message(format!(
38                " -> Retrieved {new} new commands"
39            ))))
40        }
41    }
42
43    fn render<B: Backend>(&mut self, _frame: &mut Frame<B>, _area: Rect) {
44        unreachable!()
45    }
46
47    fn process_raw_event(&mut self, _event: Event) -> Result<Option<ProcessOutput>> {
48        unreachable!()
49    }
50}