diff --git a/src/cache.rs b/src/cache.rs index bc097e85..a4cb6f8d 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -143,7 +143,7 @@ impl Cache { } /// Search for a page and return the path to it. - pub fn find_page(&self, name: &str) -> Option { + pub fn find_pages(&self, name: &str) -> Option> { // Build page file name let page_filename = format!("{}.md", name); @@ -156,27 +156,57 @@ impl Cache { } }; + // Determine platform let platform = self.get_platform_dir(); // Search for the page in the platform specific directory - if let Some(pf) = platform { - let path = platforms_dir.join(&pf).join(&page_filename); - if path.exists() && path.is_file() { - return Some(path); + let path = if let Some(pf) = platform { + let p = platforms_dir.join(&pf).join(&page_filename); + if p.exists() && p.is_file() { + Some(p) } - } + else { + None + } + } else { + None + }; // If platform is not supported or if platform specific page does not exist, // look up the page in the "common" directory. - let path = platforms_dir.join("common").join(&page_filename); + let path = if let None = path { + let p = platforms_dir.join("common").join(&page_filename); + if p.exists() && p.is_file() { + Some(p) + } else { + None + } + } else { + None + }; - // Return it if it exists, otherwise give up and return `None` - if path.exists() && path.is_file() { - Some(path) + // Search the custom directory for matching files + let custom_env = if let Ok(value) = env::var("TEALDEER_CUSTOM_PAGES_DIR") { + value + } else { + String::from("../pages.custom") + }; + + let custom_path = platforms_dir.join(custom_env).join(&page_filename); + let custom_path = if custom_path.exists() && custom_path.is_file() { + Some(custom_path) } else { None - } + }; + + // Return it if it exists, otherwise give up and return `None` + return match (path, custom_path) { + (Some(p), Some(cp)) => Some(vec![p,cp]), + (Some(p), None) => Some(vec![p]), + (None, Some(cp)) => Some(vec![cp]), + (None, None) => None, + }; } /// Return the available pages. diff --git a/src/main.rs b/src/main.rs index 532c6781..9df665b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -418,13 +418,14 @@ fn main() { } // Search for command in cache - if let Some(path) = cache.find_page(&command) { - if let Err(msg) = print_page(&path, args.flag_markdown, &config) { - eprintln!("{}", msg); - process::exit(1); - } else { - process::exit(0); + if let Some(paths) = cache.find_pages(&command) { + for path in paths { + if let Err(msg) = print_page(&path, args.flag_markdown, &config) { + eprintln!("{}", msg); + process::exit(1); + } } + process::exit(0); } else { if !args.flag_quiet { println!("Page {} not found in cache", &command);