Skip to content

Commit 7dc70f2

Browse files
authored
Merge pull request #111 from RossSmyth/openMan
Add basic logic for opening program manpages
2 parents 3bdb4b8 + 6460dc2 commit 7dc70f2

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/main.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{
1212

1313
use cache::{Cache, CacheEntry};
1414
use clap::crate_version;
15-
use clap::Parser;
15+
use clap::{Args, Parser, Subcommand};
1616
use log::{debug, error, trace};
1717

1818
fn pick(picker: &str, derivations: &[String]) -> Option<String> {
@@ -264,16 +264,19 @@ fn main() -> ExitCode {
264264
}
265265
}
266266

267-
if args.cmd.is_empty() {
267+
if args.cmd.is_empty() && args.subcmds.is_none() {
268268
return if args.empty_cache {
269269
ExitCode::SUCCESS
270270
} else {
271271
ExitCode::FAILURE
272272
};
273273
}
274274

275-
let command = &args.cmd[0];
276-
let trail = &args.cmd[1..];
275+
let (command, trail) = if let Some(SubCmds::Man(ManArgs { ref cmd })) = args.subcmds {
276+
(&cmd[0], &cmd[1..])
277+
} else {
278+
(&args.cmd[0], &args.cmd[1..])
279+
};
277280

278281
if args.delete_entry {
279282
if let Some(ref mut cache) = cache {
@@ -358,6 +361,21 @@ fn main() -> ExitCode {
358361
&args.nixpkgs_flake,
359362
);
360363
println!("{path}");
364+
} else if args.subcmds.is_some() {
365+
// Open manpage via
366+
// nix shell nixpkgs#drvName --command man commandName
367+
let err = run_command_or_open_shell(
368+
use_channel,
369+
&entry.derivation.replace(".out", "^*"),
370+
"man",
371+
&[command.to_string()],
372+
&args.nixpkgs_flake,
373+
)
374+
.exec();
375+
376+
// This code will only run if an error occurs launching
377+
eprintln!("{err:?}");
378+
return ExitCode::FAILURE;
361379
} else {
362380
let mut run_cmd = run_command_from_cache(
363381
&mut cache,
@@ -383,6 +401,7 @@ fn main() -> ExitCode {
383401
/// Runs programs without installing them
384402
#[derive(Parser)]
385403
#[clap(version = crate_version!(), trailing_var_arg = true)]
404+
#[command(subcommand_negates_reqs = true)]
386405
struct Opt {
387406
/// Generate the man page, then exit
388407
#[clap(long, hide = true)]
@@ -438,4 +457,23 @@ struct Opt {
438457
/// Command to run
439458
#[clap(required_unless_present_any = ["empty_cache", "mangen"], name = "cmd")]
440459
cmd: Vec<String>,
460+
461+
#[clap(subcommand)]
462+
subcmds: Option<SubCmds>,
463+
}
464+
465+
#[derive(Subcommand)]
466+
#[clap(disable_help_subcommand = true)]
467+
enum SubCmds {
468+
/// Show the manpage if it exists instead of running the executable
469+
///
470+
/// Currently only supports Section 1 pages for programs.
471+
Man(ManArgs),
472+
}
473+
474+
#[derive(Args)]
475+
struct ManArgs {
476+
/// Command to show manpage for
477+
#[clap(required = true, name = "cmd")]
478+
cmd: Vec<String>,
441479
}

0 commit comments

Comments
 (0)