OS | Status |
---|---|
Ubuntu-latest | |
macOS-latest | |
Windows-latest |
A Rust utility for automatically normalizing multi-line string indentation while preserving platform-specific line endings.
When working with multi-line strings inside indented code blocks, unwanted leading spaces may be introduced. This can affect readability, logging output, and formatted text generation.
string-auto-indent
provides an automated way to normalize multi-line strings without modifying the first line's indentation.
cargo add string-auto-indent
use string_auto_indent::{auto_indent, LineEnding};
let excessively_indented_text = r#"
Best Practices for Text Indentation
-----------------------------------
1. Importance of Proper Indentation
a. Enhances readability by clearly defining structure.
b. Prevents misinterpretation of hierarchical content.
c. Improves maintainability in collaborative environments.
2. Common Indentation Guidelines
a. Use consistent spacing (e.g., 2 or 4 spaces per level).
b. Avoid mixing spaces and tabs to ensure uniform formatting.
c. Align nested elements to maintain structural clarity.
1b. Maintain relative indentation depth across all nested elements.
2b. Ensure indentation reflects logical hierarchy.
"#;
// Expected output after applying `auto_indent`
let normalized_indentation = r#"
Best Practices for Text Indentation
-----------------------------------
1. Importance of Proper Indentation
a. Enhances readability by clearly defining structure.
b. Prevents misinterpretation of hierarchical content.
c. Improves maintainability in collaborative environments.
2. Common Indentation Guidelines
a. Use consistent spacing (e.g., 2 or 4 spaces per level).
b. Avoid mixing spaces and tabs to ensure uniform formatting.
c. Align nested elements to maintain structural clarity.
1b. Maintain relative indentation depth across all nested elements.
2b. Ensure indentation reflects logical hierarchy.
"#;
// Verify that `auto_indent` correctly normalizes indentation
assert_eq!(
auto_indent(excessively_indented_text),
normalized_indentation,
"The auto_indent function should normalize leading whitespace."
);
- Detects the platform’s line endings (
\n
,\r\n
,\r
) and normalizes input for processing. - Preserves the first line exactly as written.
- Finds the least-indented non-empty line (excluding the first) and adjusts all others accordingly.
- Ensures blank lines remain but contain no extra spaces.
- Restores platform-specific line endings when outputting the result.
- Formatting log messages or CLI output while ensuring alignment.
- Cleaning up documentation strings or multi-line literals in indented Rust code.
- Processing structured text while ensuring consistent indentation.
- Declaring multi-line variables in code where the indentation should match the codebase for readability, but the actual string content should not retain unnecessary leading spaces.
- Ensuring consistent formatting in generated strings for use in templates, serialization, or output rendering.
Licensed under MIT. See LICENSE
for details.