Skip to content

Commit

Permalink
pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
adamperkowski committed Dec 12, 2024
1 parent 3b81012 commit f597969
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 38 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
32 changes: 5 additions & 27 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use colored::Colorize;

use nvrs::*;

mod args;
Expand All @@ -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(())
Expand Down Expand Up @@ -128,7 +129,7 @@ async fn nuke(core: Core, nuke_names: Option<Vec<String>>, 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));
}
Expand Down Expand Up @@ -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();
}
}
};
Expand All @@ -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<String> = 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())
Expand Down
10 changes: 7 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
let self_ref = self.to_owned();
Expand Down
44 changes: 44 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,6 +91,47 @@ pub enum Error {
/// custom Result type for nvrs
pub type Result<T> = std::result::Result<T, Error>;

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<String> = 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...?";
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
///
Expand All @@ -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`
Expand Down
8 changes: 7 additions & 1 deletion src/tui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/tui/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct App {
}

impl App {
pub async fn new() -> Result<Self> {
pub async fn new() -> error::Result<Self> {
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?;
Expand Down

0 comments on commit f597969

Please sign in to comment.