Skip to content

Commit

Permalink
Minor internal refactors list (astral-sh#2224)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Mar 6, 2024
1 parent 190a161 commit 30bc16a
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions crates/uv/src/commands/pip_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ pub(crate) fn pip_list(
// Filter if `--editable` is specified; always sort by name.
let results = site_packages
.iter()
.filter(|f| (!f.is_editable() && !editable) || (f.is_editable() && !exclude_editable))
.filter(|f| !exclude.contains(f.name()))
.filter(|dist| {
(!dist.is_editable() && !editable) || (dist.is_editable() && !exclude_editable)
})
.filter(|dist| !exclude.contains(dist.name()))
.sorted_unstable_by(|a, b| a.name().cmp(b.name()).then(a.version().cmp(b.version())))
.collect_vec();
if results.is_empty() {
Expand All @@ -80,14 +82,14 @@ pub(crate) fn pip_list(
rows: results
.iter()
.copied()
.map(|f| f.name().to_string())
.map(|dist| dist.name().to_string())
.collect_vec(),
},
Column {
header: String::from("Version"),
rows: results
.iter()
.map(|f| f.version().to_string())
.map(|dist| dist.version().to_string())
.collect_vec(),
},
];
Expand All @@ -98,28 +100,23 @@ pub(crate) fn pip_list(
header: String::from("Editable project location"),
rows: results
.iter()
.map(|f| f.as_editable())
.map(|e| {
if let Some(url) = e {
url.to_file_path()
.unwrap()
.into_os_string()
.into_string()
.unwrap()
} else {
String::new()
}
.map(|dist| dist.as_editable())
.map(|url| {
url.map(|url| {
url.to_file_path().unwrap().simplified_display().to_string()
})
.unwrap_or_default()
})
.collect_vec(),
});
}

for elems in Multizip(columns.iter().map(Column::fmt_padded).collect_vec()) {
for elems in MultiZip(columns.iter().map(Column::fmt).collect_vec()) {
println!("{}", elems.join(" "));
}
}
ListFormat::Json => {
let rows = results.iter().copied().map(Row::from).collect_vec();
let rows = results.iter().copied().map(Entry::from).collect_vec();
let output = serde_json::to_string(&rows)?;
println!("{output}");
}
Expand All @@ -146,15 +143,16 @@ pub(crate) fn pip_list(
Ok(ExitStatus::Success)
}

/// An entry in a JSON list of installed packages.
#[derive(Debug, Serialize)]
struct Row {
struct Entry {
name: String,
version: String,
#[serde(skip_serializing_if = "Option::is_none")]
editable_project_location: Option<String>,
}

impl From<&InstalledDist> for Row {
impl From<&InstalledDist> for Entry {
fn from(dist: &InstalledDist) -> Self {
Self {
name: dist.name().to_string(),
Expand Down Expand Up @@ -184,7 +182,7 @@ impl<'a> Column {
}

/// Return an iterator of the column, with the header and rows formatted to the maximum width.
fn fmt_padded(&'a self) -> impl Iterator<Item = String> + 'a {
fn fmt(&'a self) -> impl Iterator<Item = String> + 'a {
let max_width = self.max_width();
let header = vec![
format!("{0:width$}", self.header, width = max_width),
Expand All @@ -200,9 +198,9 @@ impl<'a> Column {
/// Zip an unknown number of iterators.
/// Combination of [`itertools::multizip`] and [`itertools::izip`].
#[derive(Debug)]
struct Multizip<T>(Vec<T>);
struct MultiZip<T>(Vec<T>);

impl<T> Iterator for Multizip<T>
impl<T> Iterator for MultiZip<T>
where
T: Iterator,
{
Expand Down

0 comments on commit 30bc16a

Please sign in to comment.