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

Use which-retrieved path directly when spawning pager #5198

Merged
merged 1 commit into from
Jul 18, 2024
Merged
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
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
Loading