From 06a0fc65d41f7b789ff297fc96f03865a96044fd Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 10 Jun 2024 13:49:17 -0400 Subject: [PATCH] Improve `uv toolchain list` implementation (#4203) Amends #4163 with review from Jane, thank you! No behavior changes. --- crates/uv/src/commands/toolchain/list.rs | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/crates/uv/src/commands/toolchain/list.rs b/crates/uv/src/commands/toolchain/list.rs index 711c5523f..9961b81bf 100644 --- a/crates/uv/src/commands/toolchain/list.rs +++ b/crates/uv/src/commands/toolchain/list.rs @@ -1,5 +1,5 @@ +use std::collections::BTreeSet; use std::fmt::Write; -use std::ops::Deref; use anyhow::Result; use itertools::Itertools; @@ -26,18 +26,18 @@ pub(crate) async fn list( warn_user!("`uv toolchain list` is experimental and may change without warning."); } - let downloads = match includes { - ToolchainListIncludes::All => { - let request = PythonDownloadRequest::default(); - request.iter_downloads().collect() - } - ToolchainListIncludes::Installed => Vec::new(), - ToolchainListIncludes::Default => { - let request = PythonDownloadRequest::from_env()?; - request.iter_downloads().collect() - } + let download_request = match includes { + ToolchainListIncludes::All => Some(PythonDownloadRequest::default()), + ToolchainListIncludes::Installed => None, + ToolchainListIncludes::Default => Some(PythonDownloadRequest::from_env()?), }; + let downloads = download_request + .as_ref() + .map(uv_toolchain::downloads::PythonDownloadRequest::iter_downloads) + .into_iter() + .flatten(); + let installed = { InstalledToolchains::from_settings()? .init()? @@ -45,23 +45,21 @@ pub(crate) async fn list( .collect_vec() }; - let mut output = Vec::new(); + // Sort and de-duplicate the output. + let mut output = BTreeSet::new(); for toolchain in installed { - output.push(( - toolchain.python_version().deref().version.clone(), + output.insert(( + toolchain.python_version().version().clone(), toolchain.key().to_owned(), )); } for download in downloads { - output.push(( - download.python_version().deref().version.clone(), + output.insert(( + download.python_version().version().clone(), download.key().to_owned(), )); } - output.sort(); - output.dedup(); - for (version, key) in output { writeln!(printer.stdout(), "{:<8} ({key})", version.to_string())?; }