scoop-uv 0.11.0

Scoop up your Python envs — pyenv-style workflow powered by uv
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Output utilities

mod json;
mod spinner;

pub use json::*;
pub use spinner::Spinner;

use owo_colors::OwoColorize;

use crate::core::doctor::{CheckResult, CheckStatus};

// ============================================================================
// Size Formatting
// ============================================================================

/// Format bytes to human-readable string
///
/// # Examples
///
/// ```
/// use scoop_uv::output::format_size;
/// assert_eq!(format_size(0), "0 B");
/// assert_eq!(format_size(1024), "1 KB");
/// assert_eq!(format_size(1_048_576), "1 MB");
/// assert_eq!(format_size(1_073_741_824), "1.0 GB");
/// ```
pub fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.1} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.0} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.0} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}

/// Output handler for CLI
pub struct Output {
    /// Verbosity level (0 = normal, 1+ = verbose)
    verbose: u8,
    /// Suppress all output
    quiet: bool,
    /// Disable colors
    no_color: bool,
    /// Output as JSON
    json: bool,
}

impl Output {
    /// Create a new output handler
    pub fn new(verbose: u8, quiet: bool, no_color: bool, json: bool) -> Self {
        // Also check NO_COLOR environment variable
        let no_color = no_color || std::env::var("NO_COLOR").is_ok();

        Self {
            verbose,
            quiet,
            no_color,
            json,
        }
    }

    /// Print a success message
    pub fn success(&self, msg: &str) {
        if self.quiet || self.json {
            return;
        }

        if self.no_color {
            eprintln!("{msg}");
        } else {
            eprintln!("{} {msg}", "".green());
        }
    }

    /// Print an error message
    pub fn error(&self, msg: &str) {
        if self.json {
            return;
        }

        if self.no_color {
            eprintln!("{msg}");
        } else {
            eprintln!("{} {msg}", "".red());
        }
    }

    /// Print an info message
    pub fn info(&self, msg: &str) {
        if self.quiet || self.json {
            return;
        }

        if self.no_color {
            eprintln!("{msg}");
        } else {
            eprintln!("{} {msg}", "".blue());
        }
    }

    /// Print a warning message
    pub fn warn(&self, msg: &str) {
        if self.quiet || self.json {
            return;
        }

        if self.no_color {
            eprintln!("{msg}");
        } else {
            eprintln!("{} {msg}", "".yellow());
        }
    }

    /// Print a debug message (only if verbose)
    pub fn debug(&self, msg: &str) {
        if self.quiet || self.json || self.verbose == 0 {
            return;
        }

        if self.no_color {
            eprintln!("  {msg}");
        } else {
            eprintln!("  {}", msg.dimmed());
        }
    }

    /// Print a line to stdout (for list output)
    pub fn println(&self, msg: &str) {
        if self.quiet {
            return;
        }
        println!("{msg}");
    }

    /// Check if JSON output is enabled
    pub fn is_json(&self) -> bool {
        self.json
    }

    /// Check if quiet mode is enabled
    pub fn is_quiet(&self) -> bool {
        self.quiet
    }

    /// Get verbosity level
    pub fn verbosity(&self) -> u8 {
        self.verbose
    }

    /// Check if colors should be used
    pub fn use_color(&self) -> bool {
        !self.no_color
    }
}

// ============================================================================
// JSON Output Helpers
// ============================================================================

use crate::error::ScoopError;
use serde::Serialize;

impl Output {
    /// Print a JSON success response to stdout
    pub fn json_success<T: Serialize>(&self, command: &'static str, data: T) {
        if !self.json {
            return;
        }
        let response = JsonResponse::success(command, data);
        println!(
            "{}",
            serde_json::to_string_pretty(&response).unwrap_or_default()
        );
    }

    /// Print a JSON error response to stderr
    pub fn json_error(&self, command: &'static str, error: &ScoopError) {
        if !self.json {
            return;
        }
        let mut response = JsonErrorResponse::error(command, error.code(), error.to_string());
        if let Some(suggestion) = error.suggestion() {
            response = response.with_suggestion(suggestion);
        }
        eprintln!(
            "{}",
            serde_json::to_string_pretty(&response).unwrap_or_default()
        );
    }
}

impl Default for Output {
    fn default() -> Self {
        Self::new(0, false, false, false)
    }
}

// ============================================================================
// Doctor Report Output
// ============================================================================

impl Output {
    /// Print doctor report header.
    pub fn doctor_header(&self) {
        if self.quiet || self.json {
            return;
        }
        eprintln!();
        eprintln!("Checking installation...");
        eprintln!();
    }

    /// Print a single check result.
    pub fn doctor_check(&self, result: &CheckResult) {
        if self.json {
            return;
        }

        // Skip OK results in quiet mode
        if self.quiet && result.is_ok() {
            return;
        }

        let (icon, color_fn): (&str, fn(&str) -> String) = match &result.status {
            CheckStatus::Ok => ("", |s| s.green().to_string()),
            CheckStatus::Warning(_) => ("", |s| s.yellow().to_string()),
            CheckStatus::Error(_) => ("", |s| s.red().to_string()),
        };

        // Build message
        let message = match &result.status {
            CheckStatus::Ok => result.name.to_string(),
            CheckStatus::Warning(msg) => format!("{}: {}", result.name, msg),
            CheckStatus::Error(msg) => format!("{}: {}", result.name, msg),
        };

        // Print with or without color
        if self.no_color {
            eprintln!("{} {}", icon, message);
        } else {
            eprintln!("{} {}", color_fn(icon), message);
        }

        // Print details in verbose mode
        if self.verbose > 0 {
            if let Some(details) = &result.details {
                if self.no_color {
                    eprintln!("  {}", details);
                } else {
                    eprintln!("  {}", details.dimmed());
                }
            }
        }

        // Print suggestion for errors/warnings
        if let Some(suggestion) = &result.suggestion {
            if self.no_color {
                eprintln!("{}", suggestion);
            } else {
                eprintln!("  {} {}", "".cyan(), suggestion);
            }
        }
    }

    /// Print doctor report summary.
    pub fn doctor_summary(&self, results: &[CheckResult]) {
        if self.json {
            return;
        }

        let errors = results.iter().filter(|r| r.is_error()).count();
        let warnings = results.iter().filter(|r| r.is_warning()).count();

        eprintln!();
        eprintln!("──────────────────────────────────");

        if errors == 0 && warnings == 0 {
            if self.no_color {
                eprintln!("All checks passed!");
            } else {
                eprintln!("{}", "All checks passed!".green());
            }
        } else {
            let mut parts = Vec::new();
            if errors > 0 {
                parts.push(format!("{} error(s)", errors));
            }
            if warnings > 0 {
                parts.push(format!("{} warning(s)", warnings));
            }

            let summary = format!("Found {}.", parts.join(" and "));
            if self.no_color {
                eprintln!("{}", summary);
            } else {
                eprintln!("{}", summary.yellow());
            }
        }
    }

    /// Print doctor report as JSON.
    pub fn doctor_json(&self, results: &[CheckResult]) {
        if !self.json {
            return;
        }

        let json_results: Vec<serde_json::Value> = results
            .iter()
            .map(|r| {
                let status = match &r.status {
                    CheckStatus::Ok => "ok",
                    CheckStatus::Warning(_) => "warning",
                    CheckStatus::Error(_) => "error",
                };

                let message = match &r.status {
                    CheckStatus::Ok => None,
                    CheckStatus::Warning(msg) => Some(msg.clone()),
                    CheckStatus::Error(msg) => Some(msg.clone()),
                };

                serde_json::json!({
                    "id": r.id,
                    "name": r.name,
                    "status": status,
                    "message": message,
                    "suggestion": r.suggestion,
                    "details": r.details,
                })
            })
            .collect();

        let errors = results.iter().filter(|r| r.is_error()).count();
        let warnings = results.iter().filter(|r| r.is_warning()).count();
        let ok = results.iter().filter(|r| r.is_ok()).count();

        let output = serde_json::json!({
            "version": env!("CARGO_PKG_VERSION"),
            "summary": {
                "total": results.len(),
                "ok": ok,
                "warnings": warnings,
                "errors": errors,
            },
            "checks": json_results,
        });

        println!(
            "{}",
            serde_json::to_string_pretty(&output).unwrap_or_default()
        );
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    mod format_size_tests {
        use super::*;

        #[test]
        fn test_bytes() {
            assert_eq!(format_size(0), "0 B");
            assert_eq!(format_size(512), "512 B");
            assert_eq!(format_size(1023), "1023 B");
        }

        #[test]
        fn test_kilobytes() {
            assert_eq!(format_size(1024), "1 KB");
            assert_eq!(format_size(2048), "2 KB");
            assert_eq!(format_size(10240), "10 KB");
        }

        #[test]
        fn test_megabytes() {
            assert_eq!(format_size(1_048_576), "1 MB");
            assert_eq!(format_size(10_485_760), "10 MB");
        }

        #[test]
        fn test_gigabytes() {
            assert_eq!(format_size(1_073_741_824), "1.0 GB");
            assert_eq!(format_size(2_147_483_648), "2.0 GB");
        }

        #[test]
        fn test_boundary_values() {
            // KB boundary
            assert_eq!(format_size(1023), "1023 B");
            assert_eq!(format_size(1024), "1 KB");
            // MB boundary
            assert_eq!(format_size(1_048_575), "1024 KB");
            assert_eq!(format_size(1_048_576), "1 MB");
            // GB boundary
            assert_eq!(format_size(1_073_741_823), "1024 MB");
            assert_eq!(format_size(1_073_741_824), "1.0 GB");
        }
    }

    mod output_flag_tests {
        use super::*;

        #[test]
        fn is_json_returns_correct_value() {
            let json_output = Output::new(0, false, false, true);
            let normal_output = Output::new(0, false, false, false);

            assert!(json_output.is_json());
            assert!(!normal_output.is_json());
        }

        #[test]
        fn is_quiet_returns_correct_value() {
            let quiet_output = Output::new(0, true, false, false);
            let normal_output = Output::new(0, false, false, false);

            assert!(quiet_output.is_quiet());
            assert!(!normal_output.is_quiet());
        }

        #[test]
        fn default_output_has_expected_flags() {
            let output = Output::default();

            assert!(!output.is_json());
            assert!(!output.is_quiet());
            assert!(output.use_color()); // default should use color
        }

        /// Boundary value: maximum verbosity level
        #[test]
        fn output_handles_max_verbosity() {
            let output = Output::new(u8::MAX, false, false, false);

            // Should not panic, and verbosity should be preserved
            assert_eq!(output.verbosity(), u8::MAX);
        }

        /// Boundary value: all flags enabled simultaneously
        #[test]
        fn output_handles_all_flags_enabled() {
            // quiet=true, no_color=true, json=true - potentially conflicting
            let output = Output::new(0, true, true, true);

            // All flags should be set as specified
            assert!(output.is_quiet());
            assert!(!output.use_color()); // no_color=true means use_color=false
            assert!(output.is_json());
        }

        /// Verbosity levels affect behavior correctly
        #[test]
        fn output_verbosity_levels() {
            let v0 = Output::new(0, false, false, false);
            let v1 = Output::new(1, false, false, false);
            let v2 = Output::new(2, false, false, false);

            assert_eq!(v0.verbosity(), 0);
            assert_eq!(v1.verbosity(), 1);
            assert_eq!(v2.verbosity(), 2);

            // Verify ordering
            assert!(v0.verbosity() < v1.verbosity());
            assert!(v1.verbosity() < v2.verbosity());
        }
    }
}