Add custom pages to list output (#285)

Fixes #205.
This commit is contained in:
Olav de Haas 2022-09-24 21:27:35 +02:00 committed by GitHub
commit f4a94112f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 12 deletions

View file

@ -302,7 +302,7 @@ impl Cache {
}
/// Return the available pages.
pub fn list_pages(&self) -> Result<Vec<String>> {
pub fn list_pages(&self, custom_pages_dir: Option<&Path>) -> Result<Vec<String>> {
// Determine platforms directory and platform
let (cache_dir, _) = Self::get_cache_dir()?;
let platforms_dir = cache_dir.join(TLDR_PAGES_DIR).join("pages");
@ -324,23 +324,47 @@ impl Cache {
false
};
let to_stem = |entry: DirEntry| -> Option<String> {
entry
.path()
.file_stem()
.and_then(OsStr::to_str)
.map(str::to_string)
};
// Recursively walk through common and (if applicable) platform specific directory
let mut pages = WalkDir::new(platforms_dir)
.min_depth(1) // Skip root directory
.into_iter()
.filter_entry(|e| should_walk(e)) // Filter out pages for other architectures
.filter_entry(should_walk) // Filter out pages for other architectures
.filter_map(Result::ok) // Convert results to options, filter out errors
.filter_map(|e| {
let path = e.path();
let extension = &path.extension().and_then(OsStr::to_str).unwrap_or("");
if e.file_type().is_file() && extension == &"md" {
path.file_stem()
.and_then(|stem| stem.to_str().map(Into::into))
let extension = e.path().extension().unwrap_or_default();
if e.file_type().is_file() && extension == "md" {
to_stem(e)
} else {
None
}
})
.collect::<Vec<String>>();
if let Some(custom_pages_dir) = custom_pages_dir {
let is_page = |entry: &DirEntry| -> bool {
let extension = entry.path().extension().unwrap_or_default();
entry.file_type().is_file() && extension == "page"
};
let custom_pages = WalkDir::new(custom_pages_dir)
.min_depth(1)
.max_depth(1)
.into_iter()
.filter_entry(is_page)
.filter_map(Result::ok)
.filter_map(to_stem);
pages.extend(custom_pages);
}
pages.sort();
pages.dedup();
Ok(pages)

View file

@ -365,10 +365,12 @@ fn main() {
// List cached commands and exit
if args.list {
// Get list of pages
let pages = cache.list_pages().unwrap_or_else(|e| {
print_error(enable_styles, &e.context("Could not get list of pages"));
process::exit(1);
});
let pages = cache
.list_pages(config.directories.custom_pages_dir.as_deref())
.unwrap_or_else(|e| {
print_error(enable_styles, &e.context("Could not get list of pages"));
process::exit(1);
});
// Print pages
println!("{}", pages.join("\n"));

View file

@ -513,6 +513,12 @@ fn test_pager_flag_enable() {
fn test_list_flag_rendering() {
let testenv = TestEnv::new();
// set custom pages directory
testenv.write_config(format!(
"[directories]\ncustom_pages_dir = '{}'",
testenv.custom_pages_dir.path().to_str().unwrap()
));
testenv
.command()
.args(["--list"])
@ -532,13 +538,17 @@ fn test_list_flag_rendering() {
testenv.add_entry("bar", "");
testenv.add_entry("baz", "");
testenv.add_entry("qux", "");
testenv.add_page_entry("faz", "");
testenv.add_page_entry("bar", "");
testenv.add_page_entry("fiz", "");
testenv.add_patch_entry("buz", "");
testenv
.command()
.args(["--list"])
.assert()
.success()
.stdout("bar\nbaz\nfoo\nqux\n");
.stdout("bar\nbaz\nfaz\nfiz\nfoo\nqux\n");
}
#[test]