diff --git a/docs/src/usage.txt b/docs/src/usage.txt index 5129fbe..b6af6c3 100644 --- a/docs/src/usage.txt +++ b/docs/src/usage.txt @@ -11,7 +11,7 @@ Options: -f, --render Render a specific markdown file -p, --platform Override the operating system, can be specified multiple times in order of preference [possible values: linux, macos, sunos, windows, android, - freebsd, netbsd, openbsd] + freebsd, netbsd, openbsd, common] -L, --language Override the language -u, --update Update the local cache --no-auto-update If auto update is configured, disable it for this run diff --git a/src/cache.rs b/src/cache.rs index a4496ad..d46d29b 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -221,6 +221,7 @@ impl Cache { PlatformType::FreeBsd => "freebsd", PlatformType::NetBsd => "netbsd", PlatformType::OpenBsd => "openbsd", + PlatformType::Common => "common", } } @@ -292,9 +293,7 @@ impl Cache { } } - // Did not find platform specific results, fall back to "common" - Self::find_page_for_platform(&page_filename, &pages_dir, "common", &lang_dirs) - .map(|page| PageLookupResult::with_page(page).with_optional_patch(patch_path)) + None } /// Return the available pages. @@ -311,14 +310,14 @@ impl Cache { .collect(); // Closure that allows the WalkDir instance to traverse platform - // specific and common page directories, but not others. + // relevant page directories, but not others. let should_walk = |entry: &DirEntry| -> bool { let file_type = entry.file_type(); let Some(file_name) = entry.file_name().to_str() else { return false; }; if file_type.is_dir() { - return file_name == "common" || platform_dirs.contains(&file_name); + return platform_dirs.contains(&file_name); } else if file_type.is_file() { return true; } @@ -342,7 +341,7 @@ impl Cache { .map(str::to_string) }; - // Recursively walk through common and (if applicable) platform specific directory + // Recursively walk through platform specific directory let mut pages = WalkDir::new(platforms_dir) .min_depth(1) // Skip root directory .into_iter() @@ -365,7 +364,7 @@ impl Cache { .path() .file_name() .and_then(OsStr::to_str) - .map_or(false, |file_name| file_name.ends_with(".page.md")) + .is_some_and(|file_name| file_name.ends_with(".page.md")) }; let custom_pages = WalkDir::new(custom_pages_dir) diff --git a/src/main.rs b/src/main.rs index 5e6e739..8540705 100644 --- a/src/main.rs +++ b/src/main.rs @@ -282,11 +282,7 @@ fn main() { create_config_and_exit(enable_styles); } - let fallback_platforms: &[PlatformType] = &[PlatformType::current()]; - let platforms = args - .platforms - .as_ref() - .map_or(fallback_platforms, Vec::as_slice); + let platforms = compute_platforms(args.platforms.as_ref()); // If a local file was passed in, render it and exit if let Some(file) = args.render { @@ -332,7 +328,7 @@ fn main() { .map(PathWithSource::path); println!( "{}", - cache.list_pages(custom_pages_dir, platforms).join("\n") + cache.list_pages(custom_pages_dir, &platforms).join("\n") ); process::exit(0); } @@ -358,7 +354,7 @@ fn main() { .custom_pages_dir .as_ref() .map(PathWithSource::path), - platforms, + &platforms, ) { if let Err(ref e) = print_page(&lookup_result, args.raw, enable_styles, args.pager, &config) @@ -384,6 +380,20 @@ fn main() { } } +/// Returns the passed or default platform types and appends `PlatformType::Common` as fallback. +fn compute_platforms(platforms: Option<&Vec>) -> Vec { + match platforms { + Some(p) => { + let mut result = p.clone(); + if !result.contains(&PlatformType::Common) { + result.push(PlatformType::Common); + } + result + } + None => vec![PlatformType::current(), PlatformType::Common], + } +} + #[cfg(test)] mod test { use crate::get_languages; diff --git a/src/types.rs b/src/types.rs index 8a111a6..416d267 100644 --- a/src/types.rs +++ b/src/types.rs @@ -16,6 +16,7 @@ pub enum PlatformType { FreeBsd, NetBsd, OpenBsd, + Common, } impl fmt::Display for PlatformType { @@ -29,6 +30,7 @@ impl fmt::Display for PlatformType { Self::FreeBsd => write!(f, "FreeBSD"), Self::NetBsd => write!(f, "NetBSD"), Self::OpenBsd => write!(f, "OpenBSD"), + Self::Common => write!(f, "Common"), } } } @@ -44,6 +46,7 @@ impl clap::ValueEnum for PlatformType { Self::FreeBsd, Self::NetBsd, Self::OpenBsd, + Self::Common, ] } @@ -57,6 +60,7 @@ impl clap::ValueEnum for PlatformType { Self::FreeBsd => Some(clap::builder::PossibleValue::new("freebsd")), Self::NetBsd => Some(clap::builder::PossibleValue::new("netbsd")), Self::OpenBsd => Some(clap::builder::PossibleValue::new("openbsd")), + Self::Common => Some(clap::builder::PossibleValue::new("common")), } } } diff --git a/tests/lib.rs b/tests/lib.rs index a397285..feb82a0 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -652,6 +652,22 @@ fn test_multiple_platform_command_search_not_found() { .stderr(contains("Page `windows-only` not found in cache.")); } +#[test] +fn test_common_platform_is_used_as_fallback() { + let testenv = TestEnv::new(); + testenv.add_entry("in-common", "this command comes from common"); + + // No platform specified + testenv.command().args(["in-common"]).assert().success(); + + // Platform specified + testenv + .command() + .args(["--platform", "linux", "in-common"]) + .assert() + .success(); +} + #[test] fn test_list_flag_rendering() { let testenv = TestEnv::new().write_custom_pages_config();