Skip to content

Commit

Permalink
moss/cli/info: Pretty print information now
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <ikey@serpentos.com>
  • Loading branch information
ikeycode committed Sep 20, 2023
1 parent aa68cb2 commit bbce70f
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions moss/src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ use std::path::PathBuf;

use clap::{arg, ArgMatches, Command};
use futures::StreamExt;
use itertools::Itertools;
use moss::{
client::{self, Client},
package::{Flags, Name},
Package,
};
use thiserror::Error;
use tui::Stylize;

const COLUMN_WIDTH: usize = 20;

pub fn command() -> Command {
Command::new("info")
Expand Down Expand Up @@ -42,14 +47,66 @@ pub async fn handle(args: &ArgMatches) -> Result<(), Error> {
return Err(Error::NotFound(pkg));
}
for candidate in resolved {
// TODO: Pretty print
println!("{:?}", candidate.meta);
print_package(&candidate);
}
}

Ok(())
}

/// Print the title for each metadata section
fn print_titled(title: &'static str) {
let display_width = COLUMN_WIDTH - title.len();
print!("{}{:width$} ", title.bold(), " ", width = display_width);
}

/// HAX: Printing a paragraph by line breaks.
/// TODO: Split into proper paragraphs - limited to num columns in tty
fn print_paragraph(p: &str) {
for (index, line) in p.lines().enumerate() {
match index {
0 => println!("{}", line),
_ => println!("{:width$} {}", " ", line.dim(), width = COLUMN_WIDTH),
}
}
}

/// Pretty print a package
fn print_package(pkg: &Package) {
print_titled("Name");
println!("{}", pkg.meta.name);
print_titled("Version");
println!("{}", pkg.meta.version_identifier);
print_titled("Summary");
println!("{}", pkg.meta.summary);
print_titled("Description");
print_paragraph(&pkg.meta.description);
print_titled("Homepage");
println!("{}", pkg.meta.homepage);
if !pkg.meta.dependencies.is_empty() {
print_titled("Dependencies");
let deps = pkg
.meta
.dependencies
.iter()
.map(|d| d.to_string())
.sorted()
.join("\n");
print_paragraph(&deps);
}
if !pkg.meta.providers.is_empty() {
print_titled("Providers");
let provs = pkg
.meta
.providers
.iter()
.map(|p| p.to_string())
.sorted()
.join("\n");
print_paragraph(&provs);
}
}

#[derive(Debug, Error)]
pub enum Error {
#[error("client error: {0}")]
Expand Down

0 comments on commit bbce70f

Please sign in to comment.