Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
clippy fixes, a few comments, formatting, and minor refactoring
  • Loading branch information
TheCycoONE committed Feb 7, 2022
1 parent c6678d7 commit a563475
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pacpreview"
version = "0.2.0"
version = "0.2.1"
authors = ["Stephen E. Baker <[email protected]>"]
edition = "2021"

Expand Down
49 changes: 31 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//! Helpful unified package info screen for pacman
mod output;
mod types;

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<Package<'alpm>>
local_pkg: Option<Package<'alpm>>,
}

fn main() {
Expand Down Expand Up @@ -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<PackageExtra<'alpm>> {

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() {
Expand All @@ -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")?;
Expand All @@ -78,7 +82,7 @@ fn print_dep_list(
out: &mut Output,
alpm: &Alpm,
dep_list: AlpmList<Dep>,
header: &str
header: &str,
) -> std::io::Result<()> {
out.print_section_header(header)?;
for dep in dep_list {
Expand All @@ -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())?;
}
Expand Down
50 changes: 35 additions & 15 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -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::<usize>().ok()) {
if let Some(cols) = env::var("FZF_PREVIEW_COLUMNS")
.ok()
.and_then(|c| c.parse::<usize>().ok())
{
cols
} else {
termwidth()
Expand All @@ -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()
}
Expand All @@ -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)
}
Expand All @@ -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)?;
Expand All @@ -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()
}
Expand Down
6 changes: 4 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit a563475

Please sign in to comment.