Improve uv toolchain list implementation (#4203)

Amends #4163 with review from Jane, thank you!

No behavior changes.
This commit is contained in:
Zanie Blue 2024-06-10 13:49:17 -04:00 committed by GitHub
parent 652c1126d3
commit 06a0fc65d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,5 @@
use std::collections::BTreeSet;
use std::fmt::Write; use std::fmt::Write;
use std::ops::Deref;
use anyhow::Result; use anyhow::Result;
use itertools::Itertools; use itertools::Itertools;
@ -26,18 +26,18 @@ pub(crate) async fn list(
warn_user!("`uv toolchain list` is experimental and may change without warning."); warn_user!("`uv toolchain list` is experimental and may change without warning.");
} }
let downloads = match includes { let download_request = match includes {
ToolchainListIncludes::All => { ToolchainListIncludes::All => Some(PythonDownloadRequest::default()),
let request = PythonDownloadRequest::default(); ToolchainListIncludes::Installed => None,
request.iter_downloads().collect() ToolchainListIncludes::Default => Some(PythonDownloadRequest::from_env()?),
}
ToolchainListIncludes::Installed => Vec::new(),
ToolchainListIncludes::Default => {
let request = PythonDownloadRequest::from_env()?;
request.iter_downloads().collect()
}
}; };
let downloads = download_request
.as_ref()
.map(uv_toolchain::downloads::PythonDownloadRequest::iter_downloads)
.into_iter()
.flatten();
let installed = { let installed = {
InstalledToolchains::from_settings()? InstalledToolchains::from_settings()?
.init()? .init()?
@ -45,23 +45,21 @@ pub(crate) async fn list(
.collect_vec() .collect_vec()
}; };
let mut output = Vec::new(); // Sort and de-duplicate the output.
let mut output = BTreeSet::new();
for toolchain in installed { for toolchain in installed {
output.push(( output.insert((
toolchain.python_version().deref().version.clone(), toolchain.python_version().version().clone(),
toolchain.key().to_owned(), toolchain.key().to_owned(),
)); ));
} }
for download in downloads { for download in downloads {
output.push(( output.insert((
download.python_version().deref().version.clone(), download.python_version().version().clone(),
download.key().to_owned(), download.key().to_owned(),
)); ));
} }
output.sort();
output.dedup();
for (version, key) in output { for (version, key) in output {
writeln!(printer.stdout(), "{:<8} ({key})", version.to_string())?; writeln!(printer.stdout(), "{:<8} ({key})", version.to_string())?;
} }