From bbce70fca2145220a7625f2e748000a93151d4cc Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 20 Sep 2023 21:24:00 +0100 Subject: [PATCH] moss/cli/info: Pretty print information now Signed-off-by: Ikey Doherty --- moss/src/cli/info.rs | 61 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/moss/src/cli/info.rs b/moss/src/cli/info.rs index aca824bf..8c994b3e 100644 --- a/moss/src/cli/info.rs +++ b/moss/src/cli/info.rs @@ -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") @@ -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}")]