Skip to content
9 changes: 9 additions & 0 deletions pkgs/profpatsch/nman/nman.1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ of
and searches all its outputs for a man page named
.Ar ATTR
which may be in any section.
For dotted attribute paths like
.Ql pkgs.profpatsch.nman ,
only the last component
.Po Ql nman Pc
is used as the man page name to search for.
If multiple matches are found, the one that is alphanumerically
lower is preferred:
For example,
Expand All @@ -47,6 +52,7 @@ Like above, but
.Nm
will only look for the man page in the given section
.Ar SECTION .
For dotted attributes, the last component is used as the page name to search for.
Note that
.Ar SECTION
must be a number or
Expand Down Expand Up @@ -108,6 +114,9 @@ nman mandoc 7 man
# open lowdown(1) from local default.nix
nman --local lowdown

# open nman(1) from dotted attribute path (searches for "nman", not "pkgs.profpatsch.nman")
nman --local pkgs.profpatsch.nman

# open man page from a custom package set
nman -l myCustomPackage
.Ed
Expand Down
29 changes: 27 additions & 2 deletions pkgs/profpatsch/nman/nman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ fn main() {
std::env::args().partition(|s| s.starts_with("-"));

let mut cli_res: CliResult = match args.len() {
2 => Action(CliAction::Man(&args[1], None, &args[1])),
2 => Action(CliAction::Man(&args[1], None, extract_page_name_from_attr(&args[1]))),
3 => match parse_man_section(&args[2]) {
Ok(s) => Action(CliAction::Man(&args[1], Some(s), &args[1])),
Ok(s) => Action(CliAction::Man(&args[1], Some(s), extract_page_name_from_attr(&args[1]))),
Err(_) => Action(CliAction::Man(&args[1], None, &args[2])),
},
4 => match parse_man_section(&args[2]) {
Expand Down Expand Up @@ -616,6 +616,13 @@ fn match_man_page_file(name: &str, section: &str, page: &str) -> bool {
}
}

/// Extract the page name from a dotted attribute path.
/// For example, "pkgs.profpatsch.nman" becomes "nman".
/// If there are no dots, returns the original string.
fn extract_page_name_from_attr(attr: &str) -> &str {
attr.split('.').last().unwrap_or(attr)
}

/// Check if a string describes a man section,
/// i. e. is a number or "3p" (Perl Developer's
/// manual). Used to distinguish between man pages
Expand Down Expand Up @@ -706,6 +713,24 @@ mod tests {
assert!(parse_man_section("").is_err());
}

#[test]
fn test_extract_page_name_from_attr() {
// Simple case without dots
assert_eq!(extract_page_name_from_attr("nman"), "nman");
assert_eq!(extract_page_name_from_attr("hello"), "hello");

// Dotted attribute paths
assert_eq!(extract_page_name_from_attr("pkgs.profpatsch.nman"), "nman");
assert_eq!(extract_page_name_from_attr("a.b.c.d"), "d");
assert_eq!(extract_page_name_from_attr("nixpkgs.hello"), "hello");

// Edge cases
assert_eq!(extract_page_name_from_attr(""), "");
assert_eq!(extract_page_name_from_attr("."), "");
assert_eq!(extract_page_name_from_attr("package."), "");
assert_eq!(extract_page_name_from_attr(".package"), "package");
}

#[test]
fn test_output_preference() {
// lower =^= preferred
Expand Down