Multi-line String Auto Indent
| OS | Status |
|---|---|
| Ubuntu-latest | |
| macOS-latest | |
| Windows-latest |
A Rust utility for automatically normalizing multi-line string indentation while preserving platform-specific line endings.
Overview
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.
Installation
Usage
use ;
let text = r#"
String Auto Indent
Level 1
Level 2
Level 3
"#;
// For cross-platform testing
let line_ending = detect;
// With auto-indent
assert_eq!;
// Without auto-indent
assert_eq!;
Example Output
With auto-indent enabled.
String Auto Indent
Level 1
Level 2
Level 3
With auto-intent disabled.
String Auto Indent
Level 1
Level 2
Level 3
How It Works
- 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.
When to Use
- 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.
License
Licensed under MIT. See LICENSE for details.