intelli_shell/process/
tldr_fetch.rs

1use std::io::{BufRead, BufReader};
2
3use color_eyre::eyre::{Context, Result};
4
5use super::{Process, ProcessOutput};
6use crate::{cli::TldrFetchProcess, config::Config, format_error, format_msg, service::IntelliShellService};
7
8impl Process for TldrFetchProcess {
9    async fn execute(self, config: Config, service: IntelliShellService) -> Result<ProcessOutput> {
10        let mut commands = self.commands;
11        if let Some(filter_commands) = self.filter_commands {
12            let content = filter_commands
13                .into_reader()
14                .wrap_err("Couldn't read filter commands file")?;
15            let reader = BufReader::new(content);
16            for line in reader.lines() {
17                let line = line.wrap_err("Failed to read line from filter commands")?;
18                let trimmed = line.trim();
19                if !trimmed.is_empty() && !trimmed.starts_with("#") && !trimmed.starts_with("//") {
20                    commands.push(trimmed.to_string());
21                }
22            }
23        }
24        match service.fetch_tldr_commands(self.category, commands).await {
25            Ok((0, 0)) => Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "No commands were found"))),
26            Ok((0, updated)) => Ok(ProcessOutput::success().stderr(format_msg!(
27                config.theme,
28                "No new commands imported, {updated} already existed"
29            ))),
30            Ok((imported, 0)) => {
31                Ok(ProcessOutput::success().stderr(format_msg!(config.theme, "Imported {imported} new commands")))
32            }
33            Ok((imported, updated)) => Ok(ProcessOutput::success().stderr(format_msg!(
34                config.theme,
35                "Imported {imported} new commands {}",
36                config.theme.secondary.apply(format!("({updated} already existed)"))
37            ))),
38            Err(report) => Err(report),
39        }
40    }
41}