From 0e057b8b446c770c4283bee38801d237428e2dd9 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Sat, 6 Jul 2024 10:46:49 +0800 Subject: [PATCH] feat(list): more compact layout --- src/cli/list.rs | 55 ++++++++++++++++++++++++++++++++------- src/cli/mod.rs | 4 +-- tests/default/list.toml | 3 +-- tests/list/basic.toml | 3 +-- tests/list/with_info.toml | 3 +-- 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/cli/list.rs b/src/cli/list.rs index 864e5f4..cdd3548 100644 --- a/src/cli/list.rs +++ b/src/cli/list.rs @@ -1,9 +1,14 @@ +use color_eyre::eyre::Result; use owo_colors::{OwoColorize as _, Stream}; +use terminal_size::terminal_size; use crate::package_json::PackageJson; -use std::path::PathBuf; +use std::{ + io::{stdout, Write as _}, + path::PathBuf, +}; -pub fn handle(package_paths: impl Iterator) -> bool { +pub fn handle(package_paths: impl Iterator) -> Result { let mut found_package = false; for package_path in package_paths { @@ -12,18 +17,48 @@ pub fn handle(package_paths: impl Iterator) -> bool { println!(); } - print!("{}", package.make_prefix(None, Stream::Stdout)); + let mut lock = stdout().lock(); + + write!(lock, "{}", package.make_prefix(None, Stream::Stdout))?; found_package = true; - for (script_name, script_content) in &package.scripts { - println!( - "{}", - script_name.if_supports_color(Stream::Stdout, |text| text.cyan()) - ); - println!(" {script_content}"); + let longest_pad = package + .scripts + .iter() + .map(|s| s.0.len()) + .max() + .unwrap_or_default(); + + let terminal_width = terminal_size().map(|size| size.0 .0 as usize); + + for (name, content) in &package.scripts { + write!( + lock, + "{} ", + format!("{name:>longest_pad$}") + .if_supports_color(Stream::Stdout, |text| text.cyan()) + )?; + + if let Some(terminal_width) = terminal_width { + let wrapped = content.chars().collect::>(); + + for (idx, line) in wrapped + .as_slice() + .chunks(terminal_width - longest_pad - 2) + .map(|s| s.iter().collect::()) + .enumerate() + { + if idx != 0 { + write!(lock, "{}", " ".repeat(longest_pad + 2))?; + } + writeln!(lock, "{line}")?; + } + } else { + writeln!(lock, "{content}")?; + } } } } - found_package + Ok(found_package) } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index b769280..4524d4d 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -110,7 +110,7 @@ impl Cli { } Some(Subcommands::List) => { - let found_package = list::handle(package_paths); + let found_package = list::handle(package_paths)?; if !found_package { eprintln!( "{} No packages found!", @@ -131,7 +131,7 @@ impl Cli { }, )?; } else { - let found_package = list::handle(package_paths); + let found_package = list::handle(package_paths)?; if !found_package { Cli::command().print_help()?; } diff --git a/tests/default/list.toml b/tests/default/list.toml index 0cb1406..7525fec 100644 --- a/tests/default/list.toml +++ b/tests/default/list.toml @@ -1,7 +1,6 @@ args = [] stdout = """ -dev - echo dev +dev echo dev """ stderr = "" diff --git a/tests/list/basic.toml b/tests/list/basic.toml index 765194c..ad6598e 100644 --- a/tests/list/basic.toml +++ b/tests/list/basic.toml @@ -1,7 +1,6 @@ args = ["list"] stdout = """ -dev - echo dev +dev echo dev """ stderr = "" diff --git a/tests/list/with_info.toml b/tests/list/with_info.toml index 78befea..892e05d 100644 --- a/tests/list/with_info.toml +++ b/tests/list/with_info.toml @@ -2,7 +2,6 @@ args = ["list"] stdout = """ @nrr/testing@0.1.0 -dev - echo dev +dev echo dev """ stderr = ""