diff --git a/Cargo.lock b/Cargo.lock index ce289b5..2b9d7d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,7 +131,7 @@ dependencies = [ [[package]] name = "pacpreview" -version = "0.2.0" +version = "0.2.1" dependencies = [ "alpm", "clap", diff --git a/Cargo.toml b/Cargo.toml index aa040d7..9f276fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pacpreview" -version = "0.2.0" +version = "0.2.1" authors = ["Stephen E. Baker "] edition = "2021" diff --git a/src/main.rs b/src/main.rs index 8294d75..cf93354 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +//! Helpful unified package info screen for pacman + mod output; mod types; @@ -5,9 +7,10 @@ use alpm::{Alpm, AlpmList, Dep, Package, SigLevel}; use output::Output; use types::{DepInstalled, Installed}; +/// pacman package information from the syncdb, as well as the local package if it is installed. struct PackageExtra<'alpm> { sync_pkg: Package<'alpm>, - local_pkg: Option> + local_pkg: Option>, } fn main() { @@ -38,26 +41,27 @@ fn main() { } } +/// Look for a package with a given name within the local installed packages and in the all of the +/// repos. fn find_pkg_with_name<'name, 'alpm>( pkg_name: &'name str, alpm: &'alpm Alpm, ) -> Option> { - let installed_pkg = alpm.localdb().pkg(pkg_name).ok(); let db_list = alpm.syncdbs(); for db in db_list { if let Ok(pkg) = db.pkg(pkg_name) { - return Some(PackageExtra { local_pkg: installed_pkg, sync_pkg: pkg }); + return Some(PackageExtra { + local_pkg: installed_pkg, + sync_pkg: pkg, + }); } } None } -fn print_package_details( - out: &mut Output, - alpm: &Alpm, - pkg: &PackageExtra -) -> std::io::Result<()> { +/// Print all of the information regarding a package +fn print_package_details(out: &mut Output, alpm: &Alpm, pkg: &PackageExtra) -> std::io::Result<()> { print_title_line(out, pkg)?; if let Some(desc) = pkg.sync_pkg.desc() { @@ -66,8 +70,8 @@ fn print_package_details( } out.println()?; - if let Some(ip) = pkg.local_pkg { - print_local_pkg_info(out, &pkg.sync_pkg, &ip)?; + if let Some(lp) = pkg.local_pkg { + print_local_pkg_info(out, &pkg.sync_pkg, &lp)?; } print_dep_list(out, alpm, pkg.sync_pkg.optdepends(), "Opt Depends")?; @@ -78,7 +82,7 @@ fn print_dep_list( out: &mut Output, alpm: &Alpm, dep_list: AlpmList, - header: &str + header: &str, ) -> std::io::Result<()> { out.print_section_header(header)?; for dep in dep_list { @@ -92,31 +96,40 @@ fn print_dep_list( } else { DepInstalled::NotSatisfied }; - out.print_dependency(dep.name(), dep.version().map(|v| v.as_ref()), dep.desc().unwrap_or(""), dep_satisfied)?; + out.print_dependency( + dep.name(), + dep.version().map(alpm::Ver::as_str), + dep.desc().unwrap_or(""), + dep_satisfied, + )?; } Ok(()) } fn print_title_line(out: &mut Output, pkg: &PackageExtra) -> std::io::Result<()> { let installed = if let Some(ip) = pkg.local_pkg { - if ip.version() != pkg.sync_pkg.version() { - Installed::Outdated - } else { + if ip.version() == pkg.sync_pkg.version() { Installed::Installed + } else { + Installed::Outdated } } else { Installed::NotInstalled }; let spkg = pkg.sync_pkg; - out.print_title(spkg.db().expect("found in a db").name(), spkg.name(), spkg.version(), installed) + out.print_title( + spkg.db().expect("found in a db").name(), + spkg.name(), + spkg.version(), + installed, + ) } fn print_local_pkg_info( out: &mut Output, sync_pkg: &Package, - local_pkg: &Package + local_pkg: &Package, ) -> std::io::Result<()> { - if local_pkg.version() != sync_pkg.version() { out.print_installed_version(local_pkg.version())?; } diff --git a/src/output.rs b/src/output.rs index 5772331..a05c1ee 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,16 +1,21 @@ -use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; -use std::io::Write; -use textwrap::{termwidth}; +//! Functions for writing package information to the console + +use crate::types::{DepInstalled, Installed}; use std::env; -use crate::types::{Installed, DepInstalled}; +use std::io::Write; +use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; +use textwrap::termwidth; pub struct Output { width: usize, - out: StandardStream + out: StandardStream, } - + fn out_width() -> usize { - if let Some(cols) = env::var("FZF_PREVIEW_COLUMNS").ok().and_then(|c| c.parse::().ok()) { + if let Some(cols) = env::var("FZF_PREVIEW_COLUMNS") + .ok() + .and_then(|c| c.parse::().ok()) + { cols } else { termwidth() @@ -21,24 +26,27 @@ impl Output { pub fn new() -> Self { Self { width: out_width(), - out: StandardStream::stdout(ColorChoice::Auto) + out: StandardStream::stdout(ColorChoice::Auto), } } fn print_installed(&mut self) -> std::io::Result<()> { - self.out.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; + self.out + .set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; write!(&mut self.out, "[installed]")?; self.out.reset() } fn print_outdated(&mut self) -> std::io::Result<()> { - self.out.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)))?; + self.out + .set_color(ColorSpec::new().set_fg(Some(Color::Yellow)))?; write!(&mut self.out, "[~installed]")?; self.out.reset() } fn print_satisifed_by(&mut self, pkg_name: &str) -> std::io::Result<()> { - self.out.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; + self.out + .set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; write!(&mut self.out, "[satisfied by {}]", pkg_name)?; self.out.reset() } @@ -47,13 +55,19 @@ impl Output { writeln!(&mut self.out) } - pub fn print_title(&mut self, database: &str, pkg_name: &str, version: &str, installed: Installed) -> std::io::Result<()> { + pub fn print_title( + &mut self, + database: &str, + pkg_name: &str, + version: &str, + installed: Installed, + ) -> std::io::Result<()> { write!(self.out, "{}/{} {}", database, pkg_name, version)?; write!(self.out, " ")?; match installed { Installed::Installed => self.print_installed(), Installed::Outdated => self.print_outdated(), - Installed::NotInstalled => Ok(()) + Installed::NotInstalled => Ok(()), }?; writeln!(self.out) } @@ -75,7 +89,13 @@ impl Output { writeln!(&mut self.out, "{}:", header) } - pub fn print_dependency(&mut self, pkg_name: &str, version: Option<&str>, desc: &str, satisfied: DepInstalled) -> std::io::Result<()> { + pub fn print_dependency( + &mut self, + pkg_name: &str, + version: Option<&str>, + desc: &str, + satisfied: DepInstalled, + ) -> std::io::Result<()> { write!(&mut self.out, " {}", pkg_name)?; if let Some(ver) = version { write!(&mut self.out, " {}", ver)?; @@ -87,7 +107,7 @@ impl Output { match satisfied { DepInstalled::Installed => self.print_installed(), DepInstalled::SatisfiedBy(x) => self.print_satisifed_by(x), - DepInstalled::NotSatisfied => Ok(()) + DepInstalled::NotSatisfied => Ok(()), }?; self.println() } diff --git a/src/types.rs b/src/types.rs index 5ad72c8..a4a4afb 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,11 +1,13 @@ +#[derive(Clone, Copy)] pub enum Installed { Installed, Outdated, - NotInstalled + NotInstalled, } +#[derive(Clone, Copy)] pub enum DepInstalled<'a> { Installed, SatisfiedBy(&'a str), - NotSatisfied + NotSatisfied, }