Skip to main content

uv_python_discovery/
reporter.rs

1//! Reporting for Python discovery and managed downloads.
2
3use std::fmt::{self, Write};
4
5use crate::PythonInstallation;
6use indicatif::{MultiProgress, ProgressBar};
7use owo_colors::OwoColorize;
8use uv_command_support::Printer;
9use uv_command_support::progress::{Direction, ProgressReporter};
10use uv_fs::Simplified;
11use uv_python_types::PythonInstallationKey;
12
13#[derive(Debug)]
14pub struct PythonDownloadReporter {
15    reporter: ProgressReporter,
16}
17
18impl PythonDownloadReporter {
19    /// Initialize a [`PythonDownloadReporter`] for a single Python download.
20    pub fn single(printer: Printer) -> Self {
21        Self::new(printer, None)
22    }
23
24    /// Initialize a [`PythonDownloadReporter`] for multiple Python downloads.
25    pub fn new(printer: Printer, length: Option<u64>) -> Self {
26        let multi_progress = MultiProgress::with_draw_target(printer.target());
27        let root = multi_progress.add(ProgressBar::with_draw_target(length, printer.target()));
28        let reporter = ProgressReporter::new(root, multi_progress, printer);
29        Self { reporter }
30    }
31}
32
33impl uv_python_managed::downloads::Reporter for PythonDownloadReporter {
34    fn on_request_start(
35        &self,
36        direction: uv_python_managed::downloads::Direction,
37        name: &PythonInstallationKey,
38        size: Option<u64>,
39    ) -> usize {
40        self.reporter.on_request_start(
41            progress_direction(direction),
42            format!("{name} ({direction})"),
43            size,
44        )
45    }
46
47    fn on_request_progress(&self, id: usize, inc: u64) {
48        self.reporter.on_request_progress(id, inc);
49    }
50
51    fn on_request_complete(&self, direction: uv_python_managed::downloads::Direction, id: usize) {
52        self.reporter
53            .on_request_complete(progress_direction(direction), id);
54    }
55}
56
57/// Map Python's download phases to the shared progress renderer.
58fn progress_direction(direction: uv_python_managed::downloads::Direction) -> Direction {
59    match direction {
60        uv_python_managed::downloads::Direction::Download => Direction::Download,
61        uv_python_managed::downloads::Direction::Extract => Direction::Extract,
62    }
63}
64
65/// Display a message about the interpreter that was selected for the operation.
66pub fn report_interpreter(
67    python: &PythonInstallation,
68    dimmed: bool,
69    printer: Printer,
70) -> fmt::Result {
71    let managed = python.source().is_managed();
72    let implementation = python.implementation();
73    let interpreter = python.interpreter();
74
75    if dimmed {
76        if managed {
77            writeln!(
78                printer.stderr(),
79                "{}",
80                format!(
81                    "Using {} {}{}",
82                    implementation.pretty(),
83                    interpreter.python_version(),
84                    interpreter.variant().display_suffix(),
85                )
86                .dimmed()
87            )?;
88        } else {
89            writeln!(
90                printer.stderr(),
91                "{}",
92                format!(
93                    "Using {} {}{} interpreter at: {}",
94                    implementation.pretty(),
95                    interpreter.python_version(),
96                    interpreter.variant().display_suffix(),
97                    interpreter.sys_executable().user_display()
98                )
99                .dimmed()
100            )?;
101        }
102    } else {
103        if managed {
104            writeln!(
105                printer.stderr(),
106                "Using {} {}{}",
107                implementation.pretty(),
108                interpreter.python_version().cyan(),
109                interpreter.variant().display_suffix().cyan()
110            )?;
111        } else {
112            writeln!(
113                printer.stderr(),
114                "Using {} {}{} interpreter at: {}",
115                implementation.pretty(),
116                interpreter.python_version(),
117                interpreter.variant().display_suffix(),
118                interpreter.sys_executable().user_display().cyan()
119            )?;
120        }
121    }
122
123    Ok(())
124}