mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-09 09:49:10 +02:00
Add common platform to CLI (#401)
This commit is contained in:
parent
00c7778125
commit
b9f116d629
5 changed files with 44 additions and 15 deletions
|
|
@ -11,7 +11,7 @@ Options:
|
|||
-f, --render <FILE> Render a specific markdown file
|
||||
-p, --platform <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 <LANGUAGE> Override the language
|
||||
-u, --update Update the local cache
|
||||
--no-auto-update If auto update is configured, disable it for this run
|
||||
|
|
|
|||
13
src/cache.rs
13
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)
|
||||
|
|
|
|||
24
src/main.rs
24
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<PlatformType>>) -> Vec<PlatformType> {
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
16
tests/lib.rs
16
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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue