introduce the indent option to the configuration file to control indentation

This commit is contained in:
Pavel Timofeev 2026-03-12 12:33:15 -03:00
commit 335ca73173
6 changed files with 29 additions and 40 deletions

View file

@ -86,10 +86,6 @@ 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,

View file

@ -171,6 +171,8 @@ struct RawDisplayConfig {
pub use_pager: bool,
#[serde(default)]
pub show_title: bool,
#[serde(default)]
pub indent: usize,
}
impl From<&RawDisplayConfig> for DisplayConfig {
@ -179,6 +181,7 @@ impl From<&RawDisplayConfig> for DisplayConfig {
compact: raw_display_config.compact,
use_pager: raw_display_config.use_pager,
show_title: raw_display_config.show_title,
indent: raw_display_config.indent,
}
}
}
@ -312,6 +315,7 @@ impl Default for RawConfig {
raw_config.style.example_code.foreground = Some(RawColor::Cyan);
raw_config.style.example_variable.foreground = Some(RawColor::Cyan);
raw_config.style.example_variable.underline = true;
raw_config.display.indent = 6;
raw_config
}
@ -331,6 +335,7 @@ pub struct DisplayConfig {
pub compact: bool,
pub use_pager: bool,
pub show_title: bool,
pub indent: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]

View file

@ -68,7 +68,7 @@ pub fn highlight_lines<L, F, E>(
process_snippet: &mut F,
keep_empty_lines: bool,
show_title: bool,
compact: bool,
indent: usize,
) -> Result<(), E>
where
L: Iterator<Item = LineType>,
@ -97,9 +97,8 @@ where
LineType::Description(text) => process_snippet(PageSnippet::Description(&text))?,
LineType::ExampleText(text) => process_snippet(PageSnippet::Text(&text))?,
LineType::ExampleCode(text) => {
if !compact {
process_snippet(PageSnippet::NormalCode(" "))?;
}
let spaces = " ".repeat(indent);
process_snippet(PageSnippet::NormalCode(&spaces))?;
highlight_code(&command, &text, process_snippet)?;
process_snippet(PageSnippet::Linebreak)?;
}

View file

@ -259,14 +259,7 @@ 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,
args.compact,
&config,
)?;
print_page(&path, args.raw, enable_styles, args.pager, &config)?;
return Ok(ExitCode::SUCCESS);
}
@ -430,14 +423,7 @@ fn try_main(args: Cli, enable_styles: bool) -> Result<ExitCode> {
return Ok(ExitCode::FAILURE);
};
print_page(
&lookup_result,
args.raw,
enable_styles,
args.pager,
args.compact,
&config,
)?;
print_page(&lookup_result, args.raw, enable_styles, args.pager, &config)?;
}
Ok(ExitCode::SUCCESS)

View file

@ -34,7 +34,6 @@ pub fn print_page(
enable_markdown: bool,
enable_styles: bool,
use_pager: bool,
compact: bool,
config: &Config,
) -> Result<()> {
// Create reader from file(s)
@ -61,8 +60,7 @@ pub fn print_page(
if snip.is_empty() {
Ok(())
} else {
print_snippet(&mut handle, snip, &config.style, compact)
.context("Failed to print snippet")
print_snippet(&mut handle, snip, &config.style).context("Failed to print snippet")
}
};
@ -72,7 +70,7 @@ pub fn print_page(
&mut process_snippet,
!config.display.compact,
config.display.show_title,
compact,
config.display.indent,
)
.context("Could not write to stdout")?;
}
@ -87,18 +85,15 @@ 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, "{}{}", indent, s.paint(style.description)),
Text(s) => writeln!(writer, "{}{}", indent, s.paint(style.example_text)),
Description(s) => writeln!(writer, " {}", s.paint(style.description)),
Text(s) => writeln!(writer, " {}", s.paint(style.example_text)),
Title(s) => writeln!(writer, " {}", s.paint(style.command_name)),
Linebreak => writeln!(writer),
}

View file

@ -811,14 +811,21 @@ fn test_rendering_color_never() {
);
}
/// An end-to-end integration test for the `--compact` flag (no indentation on description/text lines).
/// An end-to-end integration test for the indent config option
#[test]
fn test_rendering_compact() {
_test_correct_rendering(
"inkscape-v2",
include_str!("rendered/inkscape-compact-no-color.expected"),
&["--color", "never", "--compact"],
);
fn test_rendering_with_indentation() {
let testenv = TestEnv::new().install_default_cache();
let expected_custom_indentation = include_str!("rendered/inkscape-compact-no-color.expected");
// Configure to set indent to 2 spaces
testenv.append_to_config("display.indent = 2\n");
testenv
.command()
.args(["--color", "never", "inkscape-v2"])
.assert()
.success()
.stdout(diff(expected_custom_indentation));
}
#[test]
@ -852,6 +859,7 @@ fn test_correct_rendering_with_config() {
fn test_show_title_config() {
// Test that default behavior without show_title shows no title
let testenv = TestEnv::new().install_default_cache();
testenv.append_to_config("display.indent = 6\n");
let expected_no_title = include_str!("rendered/inkscape-default.expected");
testenv