From 6719acb4f23dd15d53bce607f7b1e684bd098f31 Mon Sep 17 00:00:00 2001 From: Isak Johansson Date: Tue, 17 Dec 2019 08:56:19 +0100 Subject: [PATCH 1/2] Add -m / --markdown option for raw rendering New option to render as markdown, not with ANSI escape codes. This is useful for when you want to open it up in a pager which doesn't support ANSI escape codes by default. In my use case it allows me to open it inside vim without having to use an addon to convert ANSI escape codes to colours. --- src/main.rs | 22 ++++++++++++++++------ tests/lib.rs | 22 ++++++++++++++++++++-- tests/tar-markdown.expected | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 tests/tar-markdown.expected diff --git a/src/main.rs b/src/main.rs index 8de5ef0..b56ab4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ extern crate env_logger; use std::fs::File; use std::io::BufReader; +use std::io::BufRead; use std::path::{Path, PathBuf}; use std::process; use std::time::Duration; @@ -64,6 +65,7 @@ Options: -u --update Update the local cache -c --clear-cache Clear the local cache -p --pager Use a pager to page output + -m --markdown Display the raw markdown instead of rendering it -q --quiet Suppress informational messages --config-path Show config file path --seed-config Create a basic config @@ -100,10 +102,11 @@ struct Args { flag_quiet: bool, flag_config_path: bool, flag_seed_config: bool, + flag_markdown: bool, } /// Print page by path -fn print_page(path: &Path, enable_styles: bool) -> Result<(), String> { +fn print_page(path: &Path, enable_markdown: bool, enable_styles: bool) -> Result<(), String> { // Open file let file = File::open(path).map_err(|msg| format!("Could not open file: {}", msg))?; let reader = BufReader::new(file); @@ -121,9 +124,16 @@ fn print_page(path: &Path, enable_styles: bool) -> Result<(), String> { } }; - // Create tokenizer and print output - let mut tokenizer = Tokenizer::new(reader); - print_lines(&mut tokenizer, &config); + if enable_markdown { + // Print the raw markdown of the file. + for line in reader.lines() { + println!("{}", line.unwrap()); + } + } else { + // Create tokenizer and print output + let mut tokenizer = Tokenizer::new(reader); + print_lines(&mut tokenizer, &config); + }; Ok(()) } @@ -324,7 +334,7 @@ fn main() { // Render local file and exit if let Some(ref file) = args.flag_render { let path = PathBuf::from(file); - if let Err(msg) = print_page(&path, enable_styles) { + if let Err(msg) = print_page(&path, args.flag_markdown, enable_styles) { eprintln!("{}", msg); process::exit(1); } else { @@ -360,7 +370,7 @@ fn main() { // Search for command in cache if let Some(path) = cache.find_page(&command) { - if let Err(msg) = print_page(&path, enable_styles) { + if let Err(msg) = print_page(&path, args.flag_markdown, enable_styles) { eprintln!("{}", msg); process::exit(1); } else { diff --git a/tests/lib.rs b/tests/lib.rs index bb1e2d3..74f4a1e 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -193,6 +193,26 @@ fn test_show_config_path() { ))); } +#[test] +fn test_markdown_rendering() { + let testenv = TestEnv::new(); + + testenv + .command() + .args(&["--update"]) + .assert() + .success() + .stdout(contains("Successfully updated cache.")); + + let expected = include_str!("tar-markdown.expected"); + testenv + .command() + .args(&["-m", "tar"]) + .assert() + .success() + .stdout(similar(expected)); +} + fn _test_correct_rendering(input_file: &str, filename: &str) { let testenv = TestEnv::new(); @@ -202,9 +222,7 @@ fn _test_correct_rendering(input_file: &str, filename: &str) { let mut file = File::create(&file_path).unwrap(); file.write_all(input_file.as_bytes()).unwrap(); - // Load expected output let expected = include_str!("inkscape-default.expected"); - testenv .command() .args(&["-f", &file_path.to_str().unwrap()]) diff --git a/tests/tar-markdown.expected b/tests/tar-markdown.expected new file mode 100644 index 0000000..d141430 --- /dev/null +++ b/tests/tar-markdown.expected @@ -0,0 +1,33 @@ +# tar + +> Archiving utility. +> Often combined with a compression method, such as gzip or bzip. +> More information: . + +- Create an archive from files: + +`tar cf {{target.tar}} {{file1}} {{file2}} {{file3}}` + +- Create a gzipped archive: + +`tar czf {{target.tar.gz}} {{file1}} {{file2}} {{file3}}` + +- Extract a (compressed) archive into the current directory: + +`tar xf {{source.tar[.gz|.bz2|.xz]}}` + +- Extract an archive into a target directory: + +`tar xf {{source.tar}} -C {{directory}}` + +- Create a compressed archive, using archive suffix to determine the compression program: + +`tar caf {{target.tar.xz}} {{file1}} {{file2}} {{file3}}` + +- List the contents of a tar file: + +`tar tvf {{source.tar}}` + +- Extract files matching a pattern: + +`tar xf {{source.tar}} --wildcards {{"*.html"}}` From 95fab99e8226be7b0e4d313033b79a212d09d771 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Wed, 22 Jan 2020 22:52:44 +0100 Subject: [PATCH 2/2] Add -m command to README and completion files --- README.md | 1 + bash_tealdeer | 2 +- fish_tealdeer | 2 ++ zsh_tealdeer | 5 +++-- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 329fedd..3bfc42f 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ These are the clients I tried but failed to compile or run: -u --update Update the local cache -c --clear-cache Clear the local cache -p --pager Use a pager to page output + -m --markdown Display the raw markdown instead of rendering it -q --quiet Suppress informational messages --config-path Show config file path --seed-config Create a basic config diff --git a/bash_tealdeer b/bash_tealdeer index 5b7e55a..ba28806 100644 --- a/bash_tealdeer +++ b/bash_tealdeer @@ -6,7 +6,7 @@ _tealdeer() _init_completion || return case $prev in - -h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|--config-path|--seed-config|-q|--quiet) + -h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|-p|--pager|-m|--markdown|--config-path|--seed-config|-q|--quiet) return ;; -f|--render) diff --git a/fish_tealdeer b/fish_tealdeer index b9db05b..880b015 100644 --- a/fish_tealdeer +++ b/fish_tealdeer @@ -10,6 +10,8 @@ complete -c tldr -s f -l render -d 'Render a specific markdown file.' -r complete -c tldr -s o -l os -d 'Override the operating system.' -xa 'linux osx sunos windows other' complete -c tldr -s u -l update -d 'Update the local cache.' -f complete -c tldr -s c -l clear-cache -d 'Clear the local cache.' -f +complete -c tldr -s p -l pager -d 'Use a pager to page output.' -f +complete -c tldr -s m -l markdown -d 'Display the raw markdown instead of rendering it.' -f complete -c tldr -s q -l quiet -d 'Suppress informational messages.' -f complete -c tldr -l config-path -d 'Show config file path.' -f complete -c tldr -l seed-config -d 'Create a basic config.' -f diff --git a/zsh_tealdeer b/zsh_tealdeer index 93528a0..53f2609 100644 --- a/zsh_tealdeer +++ b/zsh_tealdeer @@ -8,7 +8,7 @@ _tealdeer() { local I="-h --help -v --version" integer ret=1 local -a args - + args+=( "($I -l --list)"{-l,--list}"[List all commands in the cache]" "($I -f --render)"{-f,--render}"[Render a specific markdown file]:file:_files" @@ -21,6 +21,7 @@ _tealdeer() { "($I -u --update)"{-u,--update}"[Update the local cache]" "($I -c --clear-cache)"{-c,--clear-cache}"[Clear the local cache]" "($I -p --pager)"{-p,--pager}"[Use a pager to page output]" + "($I -m --markdown)"{-m,--markdown}"[Display the raw markdown instead of rendering it]" "($I -q --quiet)"{-q,--quiet}"[Suppress informational messages]" "($I)--config-path[Show config file path]" "($I)--seed-config[Create a basic config]" @@ -29,7 +30,7 @@ _tealdeer() { '*:file:_files' '1: :_applications' ) - + _arguments $args[@] && ret=0 return ret }