string-auto-indent 0.1.0-alpha

Normalizes multi-line string indentation while preserving platform-specific line endings.
Documentation

Multi-line String Auto Indent

made-with-rust crates.io Documentation MIT licensed

OS Status
Ubuntu-latest Ubuntu Tests
macOS-latest macOS Tests
Windows-latest Windows Tests

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

cargo install string-auto-indent

Usage

use string_auto_indent::{auto_indent, LineEnding};

let text = r#"
    String Auto Indent

    Level 1
        Level 2
            Level 3
"#;

// For cross-platform testing
let line_ending = LineEnding::detect(text);

// With auto-indent
assert_eq!(
    auto_indent(text),
    // For cross-platform testing: Restore platform-specific line endings
    line_ending.restore("String Auto Indent\n\nLevel 1\n    Level 2\n        Level 3\n")
);

// Without auto-indent
assert_eq!(
    text,
    // For cross-platform testing: Restore platform-specific line endings
    line_ending.restore("\n    String Auto Indent\n\n    Level 1\n        Level 2\n            Level 3\n"),
);

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

  1. Detects the platform’s line endings (\n, \r\n, \r) and normalizes input for processing.
  2. Preserves the first line exactly as written.
  3. Finds the least-indented non-empty line (excluding the first) and adjusts all others accordingly.
  4. Ensures blank lines remain but contain no extra spaces.
  5. 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.