Skip to content

Commit

Permalink
feat(list): more compact layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Jul 6, 2024
1 parent 37a5834 commit 0e057b8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
55 changes: 45 additions & 10 deletions src/cli/list.rs
Original file line number Diff line number Diff line change
@@ -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<Item = PathBuf>) -> bool {
pub fn handle(package_paths: impl Iterator<Item = PathBuf>) -> Result<bool> {
let mut found_package = false;

for package_path in package_paths {
Expand All @@ -12,18 +17,48 @@ pub fn handle(package_paths: impl Iterator<Item = PathBuf>) -> 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::<Vec<_>>();

for (idx, line) in wrapped
.as_slice()
.chunks(terminal_width - longest_pad - 2)
.map(|s| s.iter().collect::<String>())
.enumerate()
{
if idx != 0 {
write!(lock, "{}", " ".repeat(longest_pad + 2))?;
}
writeln!(lock, "{line}")?;
}
} else {
writeln!(lock, "{content}")?;
}
}
}
}

found_package
Ok(found_package)
}
4 changes: 2 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand All @@ -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()?;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/default/list.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
args = []

stdout = """
dev
echo dev
dev echo dev
"""
stderr = ""
3 changes: 1 addition & 2 deletions tests/list/basic.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
args = ["list"]

stdout = """
dev
echo dev
dev echo dev
"""
stderr = ""
3 changes: 1 addition & 2 deletions tests/list/with_info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ args = ["list"]

stdout = """
@nrr/testing@0.1.0
dev
echo dev
dev echo dev
"""
stderr = ""

0 comments on commit 0e057b8

Please sign in to comment.