Skip to content

Commit

Permalink
Add --no-pager option in help command
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza committed Jul 12, 2024
1 parent 23c6cd7 commit 738c53c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
4 changes: 4 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ pub enum Commands {

#[derive(Args, Debug)]
pub struct HelpArgs {
/// Disable pager when printing help
#[arg(long)]
pub no_pager: bool,

pub command: Option<Vec<String>>,
}

Expand Down
6 changes: 3 additions & 3 deletions crates/uv/src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::ExitStatus;
use crate::printer::Printer;
use uv_cli::Cli;

pub(crate) fn help(query: &[String], printer: Printer) -> Result<ExitStatus> {
pub(crate) fn help(query: &[String], printer: Printer, no_pager: bool) -> Result<ExitStatus> {
let mut uv = Cli::command();

// It is very important to build the command before beginning inspection or subcommands
Expand Down Expand Up @@ -67,11 +67,11 @@ pub(crate) fn help(query: &[String], printer: Printer) -> Result<ExitStatus> {
};

let is_terminal = std::io::stdout().is_terminal();
if !is_root && is_terminal && which("less").is_ok() {
if !no_pager && !is_root && is_terminal && 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 !is_root && is_terminal && which("more").is_ok() {
} else if !no_pager && !is_root && is_terminal && which("more").is_ok() {
// When using more, we skip the ANSI color codes
spawn_pager("more", &[], &help)?;
} else {
Expand Down
8 changes: 5 additions & 3 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
let cache = Cache::from_settings(cache_settings.no_cache, cache_settings.cache_dir)?;

match *cli.command {
Commands::Help(args) => {
commands::help(args.command.unwrap_or_default().as_slice(), printer)
}
Commands::Help(args) => commands::help(
args.command.unwrap_or_default().as_slice(),
printer,
args.no_pager,
),
Commands::Pip(PipNamespace {
command: PipCommand::Compile(args),
}) => {
Expand Down
60 changes: 60 additions & 0 deletions crates/uv/tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,66 @@ fn help_with_version() {
----- stdout -----
uv [VERSION] ([COMMIT] DATE)
----- stderr -----
"###);
}

#[test]
fn test_with_no_pager() {
let context = TestContext::new_with_versions(&[]);

uv_snapshot!(context.filters(), context.help().arg("--no-pager"), @r###"
success: true
exit_code: 0
----- stdout -----
An extremely fast Python package manager.
Usage: uv [OPTIONS] <COMMAND>
Commands:
pip Resolve and install Python packages
tool Run and manage executable Python packages
python Manage Python installations
venv Create a virtual environment
cache Manage the cache
version Display uv's version
help Display documentation for a command
Options:
-q, --quiet
Do not print any output
-v, --verbose...
Use verbose output
--color <COLOR_CHOICE>
Control colors in output [default: auto] [possible values: auto, always, never]
--native-tls
Whether to load TLS certificates from the platform's native certificate store [env:
UV_NATIVE_TLS=]
--offline
Disable network access, relying only on locally cached data and locally available files
--python-preference <PYTHON_PREFERENCE>
Whether to prefer using Python from uv or on the system [possible values: only-managed,
installed, managed, system, only-system]
--python-fetch <PYTHON_FETCH>
Whether to automatically download Python when required [possible values: automatic,
manual]
--isolated
Avoid discovering a `pyproject.toml` or `uv.toml` file in the current directory or any
parent directories
-n, --no-cache
Avoid reading from or writing to the cache [env: UV_NO_CACHE=]
--cache-dir [CACHE_DIR]
Path to the cache directory [env: UV_CACHE_DIR=]
--config-file <CONFIG_FILE>
The path to a `uv.toml` file to use for configuration [env: UV_CONFIG_FILE=]
-h, --help
Print help
-V, --version
Print version
Use `uv help <command>` for more information on a specific command.
----- stderr -----
"###);
}

0 comments on commit 738c53c

Please sign in to comment.