diff --git a/src/cli.rs b/src/cli.rs index d461a3e..3df16bd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, diff --git a/src/formatter.rs b/src/formatter.rs index 1b504dd..9df57cb 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -68,6 +68,7 @@ pub fn highlight_lines( process_snippet: &mut F, keep_empty_lines: bool, show_title: bool, + compact: bool, ) -> Result<(), E> where L: Iterator, @@ -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)?; } diff --git a/src/main.rs b/src/main.rs index 1d1b5fc..871c693 100644 --- a/src/main.rs +++ b/src/main.rs @@ -259,7 +259,14 @@ fn try_main(args: Cli, enable_styles: bool) -> Result { // 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 { 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) diff --git a/src/output.rs b/src/output.rs index 927d20e..8f6b1b3 100644 --- a/src/output.rs +++ b/src/output.rs @@ -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), } diff --git a/tests/lib.rs b/tests/lib.rs index cb987db..253677c 100644 --- a/tests/lib.rs +++ b/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( diff --git a/tests/rendered/inkscape-compact-no-color.expected b/tests/rendered/inkscape-compact-no-color.expected new file mode 100644 index 0000000..ec21516 --- /dev/null +++ b/tests/rendered/inkscape-compact-no-color.expected @@ -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 +