Skip to content

Commit

Permalink
Polish printer.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 15, 2023
1 parent 8f0f008 commit dbfcc53
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 153 deletions.
108 changes: 54 additions & 54 deletions crates/cli/src/commands/tool/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,65 +34,65 @@ pub async fn tool_info(args: ArgsRef<ToolInfoArgs>) {

// INVENTORY

printer.start_section("Inventory");

printer.entry("Store", color::path(tool.get_inventory_dir()));

printer.entry("Executable", color::path(tool.get_exe_path()?));

if let Some(dir) = tool.get_globals_bin_dir() {
printer.entry("Globals directory", color::path(dir));
}

if let Some(prefix) = tool.get_globals_prefix() {
printer.entry("Globals prefix", color::property(prefix));
}

printer.entry_list(
"Binaries",
tool.get_bin_locations()?.into_iter().map(|bin| {
format!(
"{} {}",
color::path(bin.path),
if bin.primary {
color::muted_light("(primary)")
} else {
"".into()
}
)
}),
color::failure("None"),
);

printer.entry_list(
"Shims",
tool.get_shim_locations()?.into_iter().map(|shim| {
format!(
"{} {}",
color::path(shim.path),
if shim.primary {
color::muted_light("(primary)")
} else {
"".into()
}
)
}),
color::failure("None"),
);

printer.end_section();
printer.named_section("Inventory", |p| {
p.entry("Store", color::path(tool.get_inventory_dir()));

p.entry("Executable", color::path(tool.get_exe_path()?));

if let Some(dir) = tool.get_globals_bin_dir() {
p.entry("Globals directory", color::path(dir));
}

if let Some(prefix) = tool.get_globals_prefix() {
p.entry("Globals prefix", color::property(prefix));
}

p.entry_list(
"Binaries",
tool.get_bin_locations()?.into_iter().map(|bin| {
format!(
"{} {}",
color::path(bin.path),
if bin.primary {
color::muted_light("(primary)")
} else {
"".into()
}
)
}),
Some(color::failure("None")),
);

p.entry_list(
"Shims",
tool.get_shim_locations()?.into_iter().map(|shim| {
format!(
"{} {}",
color::path(shim.path),
if shim.primary {
color::muted_light("(primary)")
} else {
"".into()
}
)
}),
Some(color::failure("None")),
);

Ok(())
})?;

// PLUGIN

printer.start_section("Plugin");
printer.named_section("Plugin", |p| {
if let Some(version) = &tool.metadata.plugin_version {
p.entry("Version", color::hash(version));
}

if let Some(version) = &tool.metadata.plugin_version {
printer.entry("Version", color::hash(version));
}
p.locator(tool.locator.as_ref().unwrap());

printer.locator(tool.locator.as_ref().unwrap());

printer.end_section();
Ok(())
})?;

printer.flush();
}
139 changes: 62 additions & 77 deletions crates/cli/src/commands/tool/list.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::error::ProtoCliError;
use crate::helpers::load_configured_tools_with_filters;
use crate::printer::Printer;
use chrono::{DateTime, NaiveDateTime};
use clap::Args;
use miette::IntoDiagnostic;
use proto_core::Id;
use starbase::system;
use starbase_styles::color::{self, OwoStyle};
use starbase_styles::color;
use starbase_utils::json;
use std::collections::{HashMap, HashSet};
use std::io::{BufWriter, Write};
use tracing::info;

#[derive(Args, Clone, Debug)]
Expand Down Expand Up @@ -50,91 +50,76 @@ pub async fn tool_list(args: ArgsRef<ListToolsArgs>) {
return Ok(());
}

let stdout = std::io::stdout();
let mut buffer = BufWriter::new(stdout.lock());
let mut printer = Printer::new();

for tool in tools {
writeln!(
buffer,
"{} {} {}",
OwoStyle::new().bold().style(color::id(&tool.id)),
color::muted("-"),
color::muted_light(&tool.metadata.name),
)
.unwrap();

writeln!(buffer, " Store: {}", color::path(tool.get_inventory_dir())).unwrap();

if !tool.manifest.aliases.is_empty() {
writeln!(buffer, " Aliases:").unwrap();

for (alias, version) in &tool.manifest.aliases {
writeln!(
buffer,
" {} {} {}",
color::hash(version.to_string()),
color::muted("="),
color::label(alias),
)
.unwrap();
}
}

if !tool.manifest.installed_versions.is_empty() {
writeln!(buffer, " Versions:").unwrap();
printer.line();
printer.header(&tool.id, &tool.metadata.name);

printer.section(|p| {
p.entry("Store", color::path(tool.get_inventory_dir()));

p.entry_map(
"Aliases",
tool.manifest
.aliases
.iter()
.map(|(k, v)| (color::hash(v.to_string()), color::label(k)))
.collect::<Vec<_>>(),
None,
);

let mut versions = tool.manifest.installed_versions.iter().collect::<Vec<_>>();
versions.sort();

for version in versions {
let mut comments = vec![];
let mut is_default = false;

if let Some(meta) = &tool.manifest.versions.get(version) {
if let Some(at) = create_datetime(meta.installed_at) {
comments.push(format!("installed {}", at.format("%x")));
}
p.entry_map(
"Versions",
versions
.iter()
.map(|version| {
let mut comments = vec![];
let mut is_default = false;

if let Some(meta) = &tool.manifest.versions.get(version) {
if let Some(at) = create_datetime(meta.installed_at) {
comments.push(format!("installed {}", at.format("%x")));
}

if let Some(last_used) = &meta.last_used_at {
if let Some(at) = create_datetime(*last_used) {
comments.push(format!("last used {}", at.format("%x")));
}
}
}

if let Some(last_used) = &meta.last_used_at {
if let Some(at) = create_datetime(*last_used) {
comments.push(format!("last used {}", at.format("%x")));
if tool
.manifest
.default_version
.as_ref()
.is_some_and(|dv| *dv == version.to_unresolved_spec())
{
comments.push("default version".into());
is_default = true;
}
}
}

if tool
.manifest
.default_version
.as_ref()
.is_some_and(|dv| dv == &version.to_unresolved_spec())
{
comments.push("default version".into());
is_default = true;
}

if comments.is_empty() {
writeln!(buffer, " {}", color::hash(version.to_string())).unwrap();
} else {
writeln!(
buffer,
" {} {} {}",
if is_default {
color::symbol(version.to_string())
} else {
color::hash(version.to_string())
},
color::muted("-"),
color::muted_light(comments.join(", "))
)
.unwrap();
}
}
}

writeln!(buffer).unwrap();

(
if is_default {
color::invalid(version.to_string())
} else {
color::hash(version.to_string())
},
color::muted_light(comments.join(", ")),
)
})
.collect::<Vec<_>>(),
None,
);

Ok(())
})?;
}

buffer.flush().unwrap();
printer.flush();
}

fn create_datetime(millis: u128) -> Option<NaiveDateTime> {
Expand Down
7 changes: 4 additions & 3 deletions crates/cli/src/commands/tool/list_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ pub async fn tool_list_plugins(args: ArgsRef<ListToolPluginsArgs>) {
},
);

printer.depth += 1;
printer.locator(item.locator);
printer.depth -= 1;
printer.section(|p| {
p.locator(item.locator);
Ok(())
})?;
}

printer.flush();
Expand Down
Loading

0 comments on commit dbfcc53

Please sign in to comment.