Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(python list): added --format param to uv python list #10448

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ pub enum VersionFormat {
Json,
}

#[derive(Debug, Default, Clone, Copy, clap::ValueEnum)]
pub enum PythonListFormat {
/// Display the version as plain text.
#[default]
Text,
/// Display the version as JSON.
Json,
}

#[derive(Debug, Default, Clone, clap::ValueEnum)]
pub enum ListFormat {
/// Display the list of packages in a human-readable table.
Expand Down Expand Up @@ -4288,6 +4297,10 @@ pub struct PythonListArgs {
/// By default, these display as `<download available>`.
#[arg(long)]
pub show_urls: bool,

/// Select the output format between: `text` (default) or `json`.
#[arg(long, value_enum, default_value_t = PythonListFormat::default())]
pub format: PythonListFormat,
}

#[derive(Args)]
Expand Down
134 changes: 101 additions & 33 deletions crates/uv/src/commands/python/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::Serialize;
use std::collections::BTreeSet;
use std::fmt::Write;
use uv_cli::PythonListFormat;

use anyhow::Result;
use itertools::Either;
Expand All @@ -24,6 +26,20 @@ enum Kind {
System,
}

#[derive(Default, Debug, Serialize)]
struct PrintData {
key: String,
version: String,
path: Option<String>,
symlink: Option<String>,
url: Option<String>,
os: String,
variant: String,
implementation: String,
arch: String,
libc: String,
}

/// List available Python installations.
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
pub(crate) async fn list(
Expand All @@ -32,6 +48,7 @@ pub(crate) async fn list(
all_platforms: bool,
all_arches: bool,
show_urls: bool,
format: PythonListFormat,
python_preference: PythonPreference,
python_downloads: PythonDownloads,
cache: &Cache,
Expand Down Expand Up @@ -167,40 +184,91 @@ pub(crate) async fn list(
include.push((key, uri));
}

// Compute the width of the first column.
let width = include
.iter()
.fold(0usize, |acc, (key, _)| acc.max(key.to_string().len()));
match format {
PythonListFormat::Json => {
let data: Vec<PrintData> = include
.iter()
.map(|(key, uri)| match uri {
Either::Left(path) => {
let is_symlink = fs_err::symlink_metadata(path).unwrap().is_symlink();
if is_symlink {
Comment on lines +192 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaict these 3 paths only differ by one field each. I'd like to see this refactored to just compute the 2 special fields and then make one PrintData at the end.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right! I'll make that adjustment. Thanks for the pointer.

return PrintData {
key: key.to_string(),
version: key.version().to_string(),
path: Some(path.user_display().to_string()),
symlink: Some(path.read_link().unwrap().user_display().to_string()),
arch: key.arch().to_string(),
implementation: key.implementation().to_string(),
os: key.os().to_string(),
variant: key.variant().to_string(),
libc: key.libc().to_string(),
..Default::default()
};
}
return PrintData {
key: key.to_string(),
version: key.version().to_string(),
path: Some(path.user_display().to_string()),
arch: key.arch().to_string(),
implementation: key.implementation().to_string(),
os: key.os().to_string(),
variant: key.variant().to_string(),
libc: key.libc().to_string(),
..Default::default()
};
}
Either::Right(url) => PrintData {
key: key.to_string(),
version: key.version().to_string(),
url: Some((*url).to_string()),
arch: key.arch().to_string(),
implementation: key.implementation().to_string(),
os: key.os().to_string(),
variant: key.variant().to_string(),
libc: key.libc().to_string(),
..Default::default()
},
})
.collect();
writeln!(printer.stdout(), "{}", serde_json::to_string(&data)?)?;
}
PythonListFormat::Text => {
// Compute the width of the first column.
let width = include
.iter()
.fold(0usize, |acc, (key, _)| acc.max(key.to_string().len()));

for (key, uri) in include {
let key = key.to_string();
match uri {
Either::Left(path) => {
let is_symlink = fs_err::symlink_metadata(path)?.is_symlink();
if is_symlink {
writeln!(
printer.stdout(),
"{key:width$} {} -> {}",
path.user_display().cyan(),
path.read_link()?.user_display().cyan()
)?;
} else {
writeln!(
printer.stdout(),
"{key:width$} {}",
path.user_display().cyan()
)?;
}
}
Either::Right(url) => {
if show_urls {
writeln!(printer.stdout(), "{key:width$} {}", url.dimmed())?;
} else {
writeln!(
printer.stdout(),
"{key:width$} {}",
"<download available>".dimmed()
)?;
for (key, uri) in include {
let key = key.to_string();
match uri {
Either::Left(path) => {
let is_symlink = fs_err::symlink_metadata(path)?.is_symlink();
if is_symlink {
writeln!(
printer.stdout(),
"{key:width$} {} -> {}",
path.user_display().cyan(),
path.read_link()?.user_display().cyan()
)?;
} else {
writeln!(
printer.stdout(),
"{key:width$} {}",
path.user_display().cyan()
)?;
}
}
Either::Right(url) => {
if show_urls {
writeln!(printer.stdout(), "{key:width$} {}", url.dimmed())?;
} else {
writeln!(
printer.stdout(),
"{key:width$} {}",
"<download available>".dimmed()
)?;
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
args.all_platforms,
args.all_arches,
args.show_urls,
args.format,
globals.python_preference,
globals.python_downloads,
&cache,
Expand Down
7 changes: 5 additions & 2 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use uv_cli::{
AddArgs, ColorChoice, ExternalCommand, GlobalArgs, InitArgs, ListFormat, LockArgs, Maybe,
PipCheckArgs, PipCompileArgs, PipFreezeArgs, PipInstallArgs, PipListArgs, PipShowArgs,
PipSyncArgs, PipTreeArgs, PipUninstallArgs, PythonFindArgs, PythonInstallArgs, PythonListArgs,
PythonPinArgs, PythonUninstallArgs, RemoveArgs, RunArgs, SyncArgs, ToolDirArgs,
ToolInstallArgs, ToolListArgs, ToolRunArgs, ToolUninstallArgs, TreeArgs, VenvArgs,
PythonListFormat, PythonPinArgs, PythonUninstallArgs, RemoveArgs, RunArgs, SyncArgs,
ToolDirArgs, ToolInstallArgs, ToolListArgs, ToolRunArgs, ToolUninstallArgs, TreeArgs, VenvArgs,
};
use uv_client::Connectivity;
use uv_configuration::{
Expand Down Expand Up @@ -737,6 +737,7 @@ pub(crate) struct PythonListSettings {
pub(crate) all_arches: bool,
pub(crate) all_versions: bool,
pub(crate) show_urls: bool,
pub(crate) format: PythonListFormat,
}

impl PythonListSettings {
Expand All @@ -750,6 +751,7 @@ impl PythonListSettings {
only_installed,
only_downloads,
show_urls,
format,
} = args;

let kinds = if only_installed {
Expand All @@ -766,6 +768,7 @@ impl PythonListSettings {
all_arches,
all_versions,
show_urls,
format,
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -4571,6 +4571,16 @@ uv python list [OPTIONS]

<p>See <code>--project</code> to only change the project root directory.</p>

</dd><dt><code>--format</code> <i>format</i></dt><dd><p>Select the output format between: <code>text</code> (default) or <code>json</code></p>

<p>[default: text]</p>
<p>Possible values:</p>

<ul>
<li><code>text</code>: Display the version as plain text</li>

<li><code>json</code>: Display the version as JSON</li>
</ul>
</dd><dt><code>--help</code>, <code>-h</code></dt><dd><p>Display the concise help for this command</p>

</dd><dt><code>--native-tls</code></dt><dd><p>Whether to load TLS certificates from the platform&#8217;s native certificate store.</p>
Expand Down
Loading