From f5979696338439c54540f5be95b7d6d49746f1a6 Mon Sep 17 00:00:00 2001 From: Adam Perkowski Date: Thu, 12 Dec 2024 23:33:25 +0100 Subject: [PATCH] pretty --- Cargo.toml | 5 +++-- src/cli/main.rs | 32 +++++--------------------------- src/config.rs | 10 +++++++--- src/error.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 10 ++++++---- src/tui/main.rs | 8 +++++++- src/tui/state.rs | 2 +- 7 files changed, 73 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6ed2a4c..388b590 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,9 @@ include = [ ] [features] -nvrs_cli = ["clap", "colored", "futures"] -nvrs_tui = ["ratatui", "crossterm", "anyhow", "tachyonfx", "futures"] +nvrs_cli = ["clap", "pretty", "futures"] +nvrs_tui = ["ratatui", "crossterm", "anyhow", "tachyonfx", "pretty", "futures"] +pretty = ["colored"] default = ["aur", "github", "gitlab", "regex"] aur = [] github = [] diff --git a/src/cli/main.rs b/src/cli/main.rs index 2ca4a91..ce350d8 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -1,4 +1,5 @@ use colored::Colorize; + use nvrs::*; mod args; @@ -19,10 +20,10 @@ async fn main() -> error::Result<()> { match res { Ok(_) => (), - Err(e) => pretty_error(&e), + Err(e) => e.pretty(), } } - Err(e) => pretty_error(&e), + Err(e) => e.pretty(), } Ok(()) @@ -128,7 +129,7 @@ async fn nuke(core: Core, nuke_names: Option>, no_fail: bool) -> err if config_content.packages.contains_key(&package_name) { config_content.packages.remove(&package_name); } else if no_fail { - pretty_error(&error::Error::PkgNotInConfig(package_name.clone())); + error::Error::PkgNotInConfig(package_name.clone()).pretty(); } else { return Err(error::Error::PkgNotInConfig(package_name)); } @@ -202,7 +203,7 @@ async fn sync(core: Core, no_fail: bool) -> error::Result<()> { if !no_fail { return Err(e); } else { - pretty_error(&e); + e.pretty(); } } }; @@ -211,29 +212,6 @@ async fn sync(core: Core, no_fail: bool) -> error::Result<()> { verfiles::save(&newver, false, &config.__config__).await } -fn pretty_error(err: &error::Error) { - let mut lines: Vec = err - .to_string() - .lines() - .map(|line| line.to_string()) - .collect(); - let first = lines.remove(0); - let first_split = first.split_once(':').unwrap_or(("", &first)); - if first_split.0.is_empty() { - println!("{} {}", "!".red().bold().on_black(), first_split.1.red()); - } else { - println!( - "{} {}:{}", - "!".red().bold().on_black(), - first_split.0, - first_split.1.red() - ); - } - for line in lines { - println!("{} {}", "!".red().on_black(), line) - } -} - #[tokio::test] async fn core_initializing() { assert!(init().await.is_ok()) diff --git a/src/config.rs b/src/config.rs index 563488c..a003e9d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -138,11 +138,15 @@ impl Package { /// global function to get various API-specific agrs for a package /// /// # example - /// ```rust,ignore - /// // package has `source = "github"` * `github = "adamperkowski/nvrs"` specified + /// ```rust + /// use nvrs::config::Package; + /// + /// let package = Package::new("github".to_string(), "adamperkowski/nvrs".to_string(), + /// false, "v".to_string()).unwrap(); + /// /// let args = package.get_api(); /// - /// assert_eq!(package, ("github", vec!["adamperkowski/nvrs"])) + /// assert_eq!(args, ("github".to_string(), vec!["adamperkowski/nvrs".to_string()])) /// ``` pub fn get_api(&self) -> (String, Vec) { let self_ref = self.to_owned(); diff --git a/src/error.rs b/src/error.rs index 28b2d85..0d55df0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,9 @@ use thiserror::Error as ThisError; +#[cfg(feature = "pretty")] +use colored::Colorize; + const RATE_LIMIT: &str = "we might be getting rate-limited here"; const CONFIG_PATHS: &str = "config file locations: ~/.config/nvrs.toml @@ -88,6 +91,47 @@ pub enum Error { /// custom Result type for nvrs pub type Result = std::result::Result; +impl Error { + /// display a pretty formatted error message + /// # example usage + /// ```rust + /// use nvrs::error; + /// + /// let config_error = error::Error::NoConfig; + /// let source_error = error::Error::SourceNotFound("github".to_string()); + /// + ///println!("config error:\n"); + /// config_err.pretty(); + ///println!("\n\nsource error:\n"); + /// source_err.pretty(); + /// ``` + /// the above example will result in: + /// [image](https://imgur.com/a/4SZeFXn) + #[cfg(feature = "pretty")] + pub fn pretty(&self) { + let mut lines: Vec = self + .to_string() + .lines() + .map(|line| line.to_string()) + .collect(); + let first = lines.remove(0); + let first_split = first.split_once(':').unwrap_or(("", &first)); + if first_split.0.is_empty() { + println!("{} {}", "!".red().bold().on_black(), first_split.1.red()); + } else { + println!( + "{} {}:{}", + "!".red().bold().on_black(), + first_split.0, + first_split.1.red() + ); + } + for line in lines { + println!("{} {}", "!".red().on_black(), line) + } + } +} + #[test] fn test_error() { let message = "nvrs died. now why could that be...?"; diff --git a/src/lib.rs b/src/lib.rs index d83c4ba..a677231 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub mod verfiles; /// # tokio_test::block_on(async { /// use nvrs::*; /// -/// let config = config::load(None).await.unwrap(); +/// let config = config::load(&None).await.unwrap(); /// let verfiles = verfiles::load(&config.0.__config__).await.unwrap(); /// let keyfile = keyfile::load(&config.0.__config__).await.unwrap(); /// @@ -48,14 +48,16 @@ pub struct Core { /// an asynchronous function that package's source and gets the latest release /// # example usage -/// ```rust,ignore +/// ```rust /// # tokio_test::block_on(async { -/// use nvrs::run_source; +/// use nvrs::{run_source, config}; /// /// let package_name = "nvrs".to_string(); +/// let package = config::Package::new("github".to_string(), "adamperkowski/nvrs".to_string(), false, "v".to_string()).unwrap(); +/// /// let client = reqwest::Client::new(); /// -/// run_source((package_name, package), client).await; +/// run_source((package_name, package), client, None).await; /// # }) /// ``` /// see [crate::config::Package] for `package` diff --git a/src/tui/main.rs b/src/tui/main.rs index c1d9c4d..702f90e 100644 --- a/src/tui/main.rs +++ b/src/tui/main.rs @@ -3,7 +3,13 @@ mod state; #[tokio::main] async fn main() -> Result<()> { - let mut app = state::App::new().await?; + let mut app = state::App::new() + .await + .map_err(|e| { + e.pretty(); + std::process::exit(1) + }) + .unwrap(); let mut terminal = ratatui::init(); while app.is_running { diff --git a/src/tui/state.rs b/src/tui/state.rs index 9880899..5832403 100644 --- a/src/tui/state.rs +++ b/src/tui/state.rs @@ -37,7 +37,7 @@ pub struct App { } impl App { - pub async fn new() -> Result { + pub async fn new() -> error::Result { let config = config::load(&None).await?; // TODO: custom config path let verfiles = verfiles::load(&config.0.__config__).await?; let keyfile = keyfile::load(&config.0.__config__).await?;