crax 0.1.8

An interesting CLI for frontend programmer
Documentation
use dyn_fmt::AsStrFormatExt;
use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize};
use std::io::Error;

use crate::log::Log;

#[derive(Deserialize, Debug, Serialize, Clone)]
pub struct Open {
    pub full: Option<String>,
    pub url: String,
    pub rep: Option<String>,
    pub play: Option<String>,
    pub search: Option<String>,
    pub strategy: Option<String>,
}

pub struct OpenOption {
    pub rep: bool,
    pub play: bool,
}

impl Open {
    pub fn open(&self, option: &OpenOption) -> Result<(), Error> {
        if option.rep && self.rep.is_some() {
            webbrowser::open(self.rep.as_ref().unwrap())?;
            return Ok(());
        }

        if option.play && self.play.is_some() {
            webbrowser::open(self.play.as_ref().unwrap())?;
            return Ok(());
        }

        webbrowser::open(&self.url)?;

        Ok(())
    }

    pub fn get_printed_name(&self, name: &str) -> String {
        match &self.full {
            Some(full) => "{}({})".format(&[name, full]),
            None => "{}".format(&[name]),
        }
    }

    pub fn contains(&self, name: &str, str: &str) -> bool {
        self.get_printed_name(name).contains(str)
    }

    pub fn view(&self, name: &str, detail: bool) {
        let Open { url, rep, play, search, .. } = self;

        let s: String = if detail {
            "🔎 {} url: {} rep: {} play: {} search: {}".format(&[
                &self.get_printed_name(name).yellow().to_string(),
                url,
                &rep.to_owned().unwrap_or("-/-".to_string()),
                &play.to_owned().unwrap_or("-/-".to_string()),
                &search.to_owned().unwrap_or("-/-".to_string()),
            ])
        } else {
            "🔎 {}: {}".format([&self.get_printed_name(name).yellow().to_string(), &url.clone()])
        };

        Log::Info(&s).println()
    }
}