mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-22 16:14:18 +02:00
add a new flag (-C/--compact) to display output without indentation
This commit is contained in:
parent
6f91c3a765
commit
d72c1dfd22
6 changed files with 75 additions and 6 deletions
|
|
@ -86,6 +86,10 @@ pub(crate) struct Cli {
|
|||
#[arg(short = 'r', long = "raw", requires = "command_or_file")]
|
||||
pub raw: bool,
|
||||
|
||||
/// Display output without indentation
|
||||
#[arg(short = 'C', long = "compact", requires = "command_or_file")]
|
||||
pub compact: bool,
|
||||
|
||||
/// Suppress informational messages
|
||||
#[arg(short = 'q', long = "quiet")]
|
||||
pub quiet: bool,
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@ pub fn highlight_lines<L, F, E>(
|
|||
process_snippet: &mut F,
|
||||
keep_empty_lines: bool,
|
||||
show_title: bool,
|
||||
compact: bool,
|
||||
) -> Result<(), E>
|
||||
where
|
||||
L: Iterator<Item = LineType>,
|
||||
|
|
@ -96,7 +97,9 @@ where
|
|||
LineType::Description(text) => process_snippet(PageSnippet::Description(&text))?,
|
||||
LineType::ExampleText(text) => process_snippet(PageSnippet::Text(&text))?,
|
||||
LineType::ExampleCode(text) => {
|
||||
process_snippet(PageSnippet::NormalCode(" "))?;
|
||||
if !compact {
|
||||
process_snippet(PageSnippet::NormalCode(" "))?;
|
||||
}
|
||||
highlight_code(&command, &text, process_snippet)?;
|
||||
process_snippet(PageSnippet::Linebreak)?;
|
||||
}
|
||||
|
|
|
|||
18
src/main.rs
18
src/main.rs
|
|
@ -259,7 +259,14 @@ 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)?;
|
||||
print_page(
|
||||
&path,
|
||||
args.raw,
|
||||
enable_styles,
|
||||
args.pager,
|
||||
args.compact,
|
||||
&config,
|
||||
)?;
|
||||
return Ok(ExitCode::SUCCESS);
|
||||
}
|
||||
|
||||
|
|
@ -423,7 +430,14 @@ fn try_main(args: Cli, enable_styles: bool) -> Result<ExitCode> {
|
|||
return Ok(ExitCode::FAILURE);
|
||||
};
|
||||
|
||||
print_page(&lookup_result, args.raw, enable_styles, args.pager, &config)?;
|
||||
print_page(
|
||||
&lookup_result,
|
||||
args.raw,
|
||||
enable_styles,
|
||||
args.pager,
|
||||
args.compact,
|
||||
&config,
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(ExitCode::SUCCESS)
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ pub fn print_page(
|
|||
enable_markdown: bool,
|
||||
enable_styles: bool,
|
||||
use_pager: bool,
|
||||
compact: bool,
|
||||
config: &Config,
|
||||
) -> Result<()> {
|
||||
// Create reader from file(s)
|
||||
|
|
@ -60,7 +61,8 @@ pub fn print_page(
|
|||
if snip.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
print_snippet(&mut handle, snip, &config.style).context("Failed to print snippet")
|
||||
print_snippet(&mut handle, snip, &config.style, compact)
|
||||
.context("Failed to print snippet")
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -70,6 +72,7 @@ pub fn print_page(
|
|||
&mut process_snippet,
|
||||
!config.display.compact,
|
||||
config.display.show_title,
|
||||
compact,
|
||||
)
|
||||
.context("Could not write to stdout")?;
|
||||
}
|
||||
|
|
@ -84,15 +87,18 @@ fn print_snippet(
|
|||
writer: &mut impl Write,
|
||||
snip: PageSnippet<&str>,
|
||||
style: &StyleConfig,
|
||||
compact: bool,
|
||||
) -> io::Result<()> {
|
||||
use PageSnippet::*;
|
||||
|
||||
let indent = if compact { "" } else { " " };
|
||||
|
||||
match snip {
|
||||
CommandName(s) => write!(writer, "{}", s.paint(style.command_name)),
|
||||
Variable(s) => write!(writer, "{}", s.paint(style.example_variable)),
|
||||
NormalCode(s) => write!(writer, "{}", s.paint(style.example_code)),
|
||||
Description(s) => writeln!(writer, " {}", s.paint(style.description)),
|
||||
Text(s) => writeln!(writer, " {}", s.paint(style.example_text)),
|
||||
Description(s) => writeln!(writer, "{}{}", indent, s.paint(style.description)),
|
||||
Text(s) => writeln!(writer, "{}{}", indent, s.paint(style.example_text)),
|
||||
Title(s) => writeln!(writer, " {}", s.paint(style.command_name)),
|
||||
Linebreak => writeln!(writer),
|
||||
}
|
||||
|
|
|
|||
10
tests/lib.rs
10
tests/lib.rs
|
|
@ -811,6 +811,16 @@ fn test_rendering_color_never() {
|
|||
);
|
||||
}
|
||||
|
||||
/// An end-to-end integration test for the `--compact` flag (no indentation on description/text lines).
|
||||
#[test]
|
||||
fn test_rendering_compact() {
|
||||
_test_correct_rendering(
|
||||
"inkscape-v2",
|
||||
include_str!("rendered/inkscape-compact-no-color.expected"),
|
||||
&["--color", "never", "--compact"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rendering_i18n() {
|
||||
_test_correct_rendering(
|
||||
|
|
|
|||
32
tests/rendered/inkscape-compact-no-color.expected
Normal file
32
tests/rendered/inkscape-compact-no-color.expected
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
|
||||
An SVG (Scalable Vector Graphics) editing program.
|
||||
Use -z to not open the GUI and only process files in the console.
|
||||
|
||||
Open an SVG file in the Inkscape GUI:
|
||||
|
||||
inkscape filename.svg
|
||||
|
||||
Export an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):
|
||||
|
||||
inkscape filename.svg -e filename.png
|
||||
|
||||
Export an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):
|
||||
|
||||
inkscape filename.svg -e filename.png -w 600 -h 400
|
||||
|
||||
Export a single object, given its ID, into a bitmap:
|
||||
|
||||
inkscape filename.svg -i id -e object.png
|
||||
|
||||
Export an SVG document to PDF, converting all texts to paths:
|
||||
|
||||
inkscape filename.svg | inkscape | inkscape --export-pdf=inkscape.pdf | inkscape | inkscape --export-text-to-path
|
||||
|
||||
Duplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:
|
||||
|
||||
inkscape filename.svg --select=path123 --verb=EditDuplicate --verb=ObjectRotate90 --verb=FileSave --verb=FileQuit
|
||||
|
||||
Some invalid command just to test the correct highlighting of the command name:
|
||||
|
||||
inkscape --use-inkscape=v3.0 file
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue