mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-24 00:54:17 +02:00
show help on the tealdeer command specific to tealdeer only
This commit is contained in:
parent
07825c44ea
commit
8b9f13d5d1
4 changed files with 14 additions and 45 deletions
|
|
@ -1,25 +1,9 @@
|
|||
# tldr
|
||||
|
||||
> A fast tldr client written in Rust.
|
||||
> This is a builtin page that shows information for your installed tealdeer version.
|
||||
> More information: <https://tealdeer-rs.github.io/tealdeer/>.
|
||||
|
||||
- Show the tldr page for a command:
|
||||
|
||||
`tldr {{command}}`
|
||||
|
||||
- Show the tldr page for a subcommand:
|
||||
|
||||
`tldr {{command}} {{subcommand}}`
|
||||
|
||||
- Show the tldr page for a specific platform:
|
||||
|
||||
`tldr --platform {{android|common|freebsd|linux|netbsd|openbsd|osx|sunos|windows}} {{command}}`
|
||||
|
||||
- Show the tldr page in a specific language:
|
||||
|
||||
`tldr --language {{language_code}} {{command}}`
|
||||
|
||||
- Download or update the local page cache:
|
||||
> This page shows tealdeer specific functionality. See tldr tldr for more examples.
|
||||
|
||||
`tldr --update`
|
||||
|
||||
14
src/cache.rs
14
src/cache.rs
|
|
@ -40,7 +40,6 @@ pub struct Cache<'a> {
|
|||
pub struct PageLookupResult {
|
||||
pub page_path: PathBuf,
|
||||
pub patch_path: Option<PathBuf>,
|
||||
pub embedded: Option<&'static str>,
|
||||
}
|
||||
|
||||
impl<'a> Cache<'a> {
|
||||
|
|
@ -266,18 +265,10 @@ impl<'a> Cache<'a> {
|
|||
}
|
||||
|
||||
impl PageLookupResult {
|
||||
pub fn with_embedded(content: &'static str) -> Self {
|
||||
Self {
|
||||
page_path: PathBuf::new(),
|
||||
patch_path: None,
|
||||
embedded: Some(content),
|
||||
}
|
||||
}
|
||||
pub fn with_page(page_path: PathBuf) -> Self {
|
||||
Self {
|
||||
page_path,
|
||||
patch_path: None,
|
||||
embedded: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -292,11 +283,6 @@ impl PageLookupResult {
|
|||
/// This will return an error if either the page file or the patch file
|
||||
/// cannot be opened.
|
||||
pub fn reader(&self) -> Result<BufReader<Box<dyn Read>>> {
|
||||
// Open embedded file
|
||||
if let Some(content) = self.embedded {
|
||||
return Ok(BufReader::new(Box::new(Cursor::new(content.as_bytes()))));
|
||||
}
|
||||
|
||||
// Open page file
|
||||
let page_file = File::open(&self.page_path)
|
||||
.with_context(|| format!("Could not open page file at {}", self.page_path.display()))?;
|
||||
|
|
|
|||
17
src/main.rs
17
src/main.rs
|
|
@ -29,7 +29,7 @@ compile_error!(
|
|||
use std::{
|
||||
env,
|
||||
fs::create_dir_all,
|
||||
io::{self, IsTerminal},
|
||||
io::{self, Cursor, IsTerminal},
|
||||
path::Path,
|
||||
process::{Command, ExitCode},
|
||||
};
|
||||
|
|
@ -68,7 +68,8 @@ const APP_INFO: AppInfo = AppInfo {
|
|||
name: NAME,
|
||||
author: NAME,
|
||||
};
|
||||
const SELF_PAGE: &str = include_str!("../pages/tldr.md");
|
||||
static TEALDEER_PAGE: &str =
|
||||
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/pages/tealdeer.md"));
|
||||
|
||||
/// Clear the cache
|
||||
fn clear_cache(cache: Cache, quietly: bool) -> Result<()> {
|
||||
|
|
@ -259,8 +260,8 @@ fn try_main(args: Cli, enable_styles: bool) -> Result<ExitCode> {
|
|||
|
||||
// If a local file was passed in, render it and exit
|
||||
if let Some(file) = args.render {
|
||||
let path = PageLookupResult::with_page(file);
|
||||
print_page(&path, args.raw, enable_styles, args.pager, &config)?;
|
||||
let reader = PageLookupResult::with_page(file).reader()?;
|
||||
print_page(reader, args.raw, enable_styles, args.pager, &config)?;
|
||||
return Ok(ExitCode::SUCCESS);
|
||||
}
|
||||
|
||||
|
|
@ -408,8 +409,8 @@ fn try_main(args: Cli, enable_styles: bool) -> Result<ExitCode> {
|
|||
);
|
||||
}
|
||||
|
||||
let lookup_result = if command == "tldr" || command == "tealdeer" {
|
||||
PageLookupResult::with_embedded(SELF_PAGE)
|
||||
let reader: Box<dyn io::Read> = if command == "tealdeer" {
|
||||
Box::new(Cursor::new(TEALDEER_PAGE.as_bytes()))
|
||||
} else {
|
||||
let Some(result) = cache.find_page(&command) else {
|
||||
if !args.quiet {
|
||||
|
|
@ -425,10 +426,10 @@ fn try_main(args: Cli, enable_styles: bool) -> Result<ExitCode> {
|
|||
}
|
||||
return Ok(ExitCode::FAILURE);
|
||||
};
|
||||
result
|
||||
Box::new(result.reader()?)
|
||||
};
|
||||
|
||||
print_page(&lookup_result, args.raw, enable_styles, args.pager, &config)?;
|
||||
print_page(reader, args.raw, enable_styles, args.pager, &config)?;
|
||||
}
|
||||
|
||||
Ok(ExitCode::SUCCESS)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
//! Functions for printing pages to the terminal
|
||||
|
||||
use std::io::{self, BufRead, Write};
|
||||
use std::io::{self, BufRead, BufReader, Read, Write};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use yansi::Paint;
|
||||
|
||||
use crate::{
|
||||
cache::PageLookupResult,
|
||||
config::{Config, StyleConfig},
|
||||
formatter::{highlight_lines, PageSnippet},
|
||||
line_iterator::LineIterator,
|
||||
|
|
@ -30,14 +29,13 @@ fn configure_pager(enable_styles: bool) {
|
|||
|
||||
/// Print page by path
|
||||
pub fn print_page(
|
||||
lookup_result: &PageLookupResult,
|
||||
reader: impl Read,
|
||||
enable_markdown: bool,
|
||||
enable_styles: bool,
|
||||
use_pager: bool,
|
||||
config: &Config,
|
||||
) -> Result<()> {
|
||||
// Create reader from file(s)
|
||||
let reader = lookup_result.reader()?;
|
||||
let reader = BufReader::new(reader);
|
||||
|
||||
// Configure pager if applicable
|
||||
if use_pager || config.display.use_pager {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue