Skip to content

Commit

Permalink
Use which-retrieved path directly when spawning pager (#5198)
Browse files Browse the repository at this point in the history
## Summary

Closes #5124.

## Test Plan

Ran `cargo run -- help pip compile` on my Windows machine, which failed
before but succeeds after this change.
  • Loading branch information
charliermarsh authored Jul 18, 2024
1 parent dfe2faa commit 54bca18
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions crates/uv/src/commands/help.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::OsStr;
use std::{fmt::Display, fmt::Write};

use anstream::{stream::IsTerminal, ColorChoice};
Expand Down Expand Up @@ -68,13 +69,18 @@ pub(crate) fn help(query: &[String], printer: Printer, no_pager: bool) -> Result

let is_terminal = std::io::stdout().is_terminal();
let should_page = !no_pager && !is_root && is_terminal;
if should_page && which("less").is_ok() {
// When using less, we use the command name as the file name and can support colors
let prompt = format!("help: uv {}", query.join(" "));
spawn_pager("less", &["-R", "-P", &prompt], &help_ansi)?;
} else if should_page && which("more").is_ok() {
// When using more, we skip the ANSI color codes
spawn_pager("more", &[], &help)?;

if should_page {
if let Ok(less) = which("less") {
// When using less, we use the command name as the file name and can support colors
let prompt = format!("help: uv {}", query.join(" "));
spawn_pager(less, &["-R", "-P", &prompt], &help_ansi)?;
} else if let Ok(more) = which("more") {
// When using more, we skip the ANSI color codes
spawn_pager(more, &[], &help)?;
} else {
writeln!(printer.stdout(), "{help_ansi}")?;
}
} else {
writeln!(printer.stdout(), "{help_ansi}")?;
}
Expand All @@ -98,7 +104,7 @@ fn find_command<'a>(
}

/// Spawn a paging command to display contents.
fn spawn_pager(command: &str, args: &[&str], contents: impl Display) -> Result<()> {
fn spawn_pager(command: impl AsRef<OsStr>, args: &[&str], contents: impl Display) -> Result<()> {
use std::io::Write;

let mut child = std::process::Command::new(command)
Expand Down

0 comments on commit 54bca18

Please sign in to comment.