Skip to content

Commit

Permalink
Custom tldr pages in tldr-master/pages.custom
Browse files Browse the repository at this point in the history
if the .md file is the same as one that already exists then the custom
    one gets appended to the original one

Added TEALDEER_CUSTOM_PAGES_DIR env variable

If set then tealdeer will search that directory for custom pages
otherwise it will search "pages.custom"
  • Loading branch information
dmaahs2017 committed Sep 22, 2020
1 parent 30b7c5f commit 99277ff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
52 changes: 41 additions & 11 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Cache {
}

/// Search for a page and return the path to it.
pub fn find_page(&self, name: &str) -> Option<PathBuf> {
pub fn find_pages(&self, name: &str) -> Option<Vec<PathBuf>> {
// Build page file name
let page_filename = format!("{}.md", name);

Expand All @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 99277ff

Please sign in to comment.